JAL-653 JAL-1968 FeaturesFile now handles Jalview or GFF2 or GFF3
[jalview.git] / src / jalview / appletgui / FeatureRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.datamodel.SearchResults;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.AnnotationColourGradient;
27 import jalview.schemes.GraduatedColor;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.AlignmentViewport;
30
31 import java.awt.BorderLayout;
32 import java.awt.Button;
33 import java.awt.Choice;
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Font;
37 import java.awt.Graphics;
38 import java.awt.GridLayout;
39 import java.awt.Label;
40 import java.awt.Panel;
41 import java.awt.ScrollPane;
42 import java.awt.TextArea;
43 import java.awt.TextField;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.util.Map;
47
48 /**
49  * DOCUMENT ME!
50  * 
51  * @author $author$
52  * @version $Revision$
53  */
54 public class FeatureRenderer extends
55         jalview.renderer.seqfeatures.FeatureRenderer
56 {
57
58   // Holds web links for feature groups and feature types
59   // in the form label|link
60   Map<String, String> featureLinks = null;
61
62   /**
63    * Creates a new FeatureRenderer object.
64    * 
65    * @param av
66    *          DOCUMENT ME!
67    */
68   public FeatureRenderer(AlignmentViewport av)
69   {
70     super();
71     this.av = av;
72
73     setTransparencyAvailable(!System.getProperty("java.version")
74             .startsWith("1.1"));
75   }
76
77   static String lastFeatureAdded;
78
79   static String lastFeatureGroupAdded;
80
81   static String lastDescriptionAdded;
82
83   int featureIndex = 0;
84
85   boolean deleteFeature = false;
86
87   FeatureColourPanel colourPanel;
88
89   class FeatureColourPanel extends Panel
90   {
91     String label = "";
92
93     private Color maxCol;
94
95     private boolean isColourByLabel, isGcol;
96
97     /**
98      * render a feature style in the amend feature dialog box
99      */
100     public void updateColor(Object newcol)
101     {
102
103       Color bg, col = null;
104       GraduatedColor gcol = null;
105       String vlabel = "";
106       if (newcol instanceof Color)
107       {
108         isGcol = false;
109         col = (Color) newcol;
110         gcol = null;
111       }
112       else if (newcol instanceof GraduatedColor)
113       {
114         isGcol = true;
115         gcol = (GraduatedColor) newcol;
116         col = null;
117       }
118       else
119       {
120         throw new Error(
121                 MessageManager
122                         .getString("error.invalid_colour_for_mycheckbox"));
123       }
124       if (col != null)
125       {
126         setBackground(bg = col);
127       }
128       else
129       {
130         if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
131         {
132           vlabel += " "
133                   + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
134                           : "(<)");
135         }
136         if (isColourByLabel = gcol.isColourByLabel())
137         {
138           setBackground(bg = Color.white);
139           vlabel += " (by Label)";
140         }
141         else
142         {
143           setBackground(bg = gcol.getMinColor());
144           maxCol = gcol.getMaxColor();
145         }
146       }
147       label = vlabel;
148       setBackground(bg);
149       repaint();
150     }
151
152     FeatureColourPanel()
153     {
154       super(null);
155     }
156
157     @Override
158     public void paint(Graphics g)
159     {
160       Dimension d = getSize();
161       if (isGcol)
162       {
163         if (isColourByLabel)
164         {
165           g.setColor(Color.white);
166           g.fillRect(d.width / 2, 0, d.width / 2, d.height);
167           g.setColor(Color.black);
168           Font f = new Font("Verdana", Font.PLAIN, 10);
169           g.setFont(f);
170           g.drawString(MessageManager.getString("label.label"), 0, 0);
171         }
172         else
173         {
174           g.setColor(maxCol);
175           g.fillRect(d.width / 2, 0, d.width / 2, d.height);
176
177         }
178       }
179     }
180
181   }
182
183   boolean amendFeatures(final SequenceI[] sequences,
184           final SequenceFeature[] features, boolean newFeatures,
185           final AlignmentPanel ap)
186   {
187     Panel bigPanel = new Panel(new BorderLayout());
188     final TextField name = new TextField(16);
189     final TextField source = new TextField(16);
190     final TextArea description = new TextArea(3, 35);
191     final TextField start = new TextField(8);
192     final TextField end = new TextField(8);
193     final Choice overlaps;
194     Button deleteButton = new Button("Delete");
195     deleteFeature = false;
196
197     colourPanel = new FeatureColourPanel();
198     colourPanel.setSize(110, 15);
199     final FeatureRenderer fr = this;
200
201     Panel panel = new Panel(new GridLayout(3, 1));
202
203     featureIndex = 0; // feature to be amended.
204     Panel tmp;
205
206     // /////////////////////////////////////
207     // /MULTIPLE FEATURES AT SELECTED RESIDUE
208     if (!newFeatures && features.length > 1)
209     {
210       panel = new Panel(new GridLayout(4, 1));
211       tmp = new Panel();
212       tmp.add(new Label("Select Feature: "));
213       overlaps = new Choice();
214       for (int i = 0; i < features.length; i++)
215       {
216         String item = features[i].getType() + "/" + features[i].getBegin()
217                 + "-" + features[i].getEnd();
218
219         if (features[i].getFeatureGroup() != null)
220         {
221           item += " (" + features[i].getFeatureGroup() + ")";
222         }
223
224         overlaps.addItem(item);
225       }
226
227       tmp.add(overlaps);
228
229       overlaps.addItemListener(new java.awt.event.ItemListener()
230       {
231         @Override
232         public void itemStateChanged(java.awt.event.ItemEvent e)
233         {
234           int index = overlaps.getSelectedIndex();
235           if (index != -1)
236           {
237             featureIndex = index;
238             name.setText(features[index].getType());
239             description.setText(features[index].getDescription());
240             source.setText(features[index].getFeatureGroup());
241             start.setText(features[index].getBegin() + "");
242             end.setText(features[index].getEnd() + "");
243
244             SearchResults highlight = new SearchResults();
245             highlight.addResult(sequences[0], features[index].getBegin(),
246                     features[index].getEnd());
247
248             ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
249
250           }
251           Object col = getFeatureStyle(name.getText());
252           if (col == null)
253           {
254             col = new jalview.schemes.UserColourScheme()
255                     .createColourFromName(name.getText());
256           }
257
258           colourPanel.updateColor(col);
259         }
260       });
261
262       panel.add(tmp);
263     }
264     // ////////
265     // ////////////////////////////////////
266
267     tmp = new Panel();
268     panel.add(tmp);
269     tmp.add(new Label("Name: ", Label.RIGHT));
270     tmp.add(name);
271
272     tmp = new Panel();
273     panel.add(tmp);
274     tmp.add(new Label("Group: ", Label.RIGHT));
275     tmp.add(source);
276
277     tmp = new Panel();
278     panel.add(tmp);
279     tmp.add(new Label("Colour: ", Label.RIGHT));
280     tmp.add(colourPanel);
281
282     bigPanel.add(panel, BorderLayout.NORTH);
283
284     panel = new Panel();
285     panel.add(new Label("Description: ", Label.RIGHT));
286     panel.add(new ScrollPane().add(description));
287
288     if (!newFeatures)
289     {
290       bigPanel.add(panel, BorderLayout.SOUTH);
291
292       panel = new Panel();
293       panel.add(new Label(" Start:", Label.RIGHT));
294       panel.add(start);
295       panel.add(new Label("  End:", Label.RIGHT));
296       panel.add(end);
297       bigPanel.add(panel, BorderLayout.CENTER);
298     }
299     else
300     {
301       bigPanel.add(panel, BorderLayout.CENTER);
302     }
303
304     if (lastFeatureAdded == null)
305     {
306       if (features[0].type != null)
307       {
308         lastFeatureAdded = features[0].type;
309       }
310       else
311       {
312         lastFeatureAdded = "feature_1";
313       }
314     }
315
316     if (lastFeatureGroupAdded == null)
317     {
318       if (features[0].featureGroup != null)
319       {
320         lastFeatureGroupAdded = features[0].featureGroup;
321       }
322       else
323       {
324         lastFeatureAdded = "Jalview";
325       }
326     }
327
328     String title = newFeatures ? MessageManager
329             .getString("label.create_new_sequence_features")
330             : MessageManager.formatMessage("label.amend_delete_features",
331                     new String[] { sequences[0].getName() });
332
333     final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
334             240);
335
336     dialog.setMainPanel(bigPanel);
337
338     if (newFeatures)
339     {
340       name.setText(lastFeatureAdded);
341       source.setText(lastFeatureGroupAdded);
342     }
343     else
344     {
345       dialog.ok.setLabel(MessageManager.getString("label.amend"));
346       dialog.buttonPanel.add(deleteButton, 1);
347       deleteButton.addActionListener(new ActionListener()
348       {
349         @Override
350         public void actionPerformed(ActionEvent evt)
351         {
352           deleteFeature = true;
353           dialog.setVisible(false);
354         }
355       });
356       name.setText(features[0].getType());
357       source.setText(features[0].getFeatureGroup());
358     }
359
360     start.setText(features[0].getBegin() + "");
361     end.setText(features[0].getEnd() + "");
362     description.setText(features[0].getDescription());
363     Color col = getColour(name.getText());
364     if (col == null)
365     {
366       col = new jalview.schemes.UserColourScheme()
367               .createColourFromName(name.getText());
368     }
369     Object fcol = getFeatureStyle(name.getText());
370     // simply display the feature color in a box
371     colourPanel.updateColor(fcol);
372     dialog.setResizable(true);
373     // TODO: render the graduated color in the box.
374     colourPanel.addMouseListener(new java.awt.event.MouseAdapter()
375     {
376       @Override
377       public void mousePressed(java.awt.event.MouseEvent evt)
378       {
379         if (!colourPanel.isGcol)
380         {
381           new UserDefinedColours(fr, ap.alignFrame);
382         }
383         else
384         {
385           FeatureColourChooser fcc = new FeatureColourChooser(
386                   ap.alignFrame, name.getText());
387           dialog.transferFocus();
388         }
389       }
390     });
391     dialog.setVisible(true);
392
393     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
394
395     if (dialog.accept)
396     {
397       // This ensures that the last sequence
398       // is refreshed and new features are rendered
399       lastSeq = null;
400       lastFeatureAdded = name.getText().trim();
401       lastFeatureGroupAdded = source.getText().trim();
402       lastDescriptionAdded = description.getText().replace('\n', ' ');
403     }
404
405     if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
406     {
407       lastFeatureGroupAdded = null;
408     }
409
410     if (!newFeatures)
411     {
412
413       SequenceFeature sf = features[featureIndex];
414       if (dialog.accept)
415       {
416         sf.type = lastFeatureAdded;
417         sf.featureGroup = lastFeatureGroupAdded;
418         sf.description = lastDescriptionAdded;
419         if (!colourPanel.isGcol)
420         {
421           // update colour - otherwise its already done.
422           setColour(sf.type, colourPanel.getBackground());
423         }
424         try
425         {
426           sf.begin = Integer.parseInt(start.getText());
427           sf.end = Integer.parseInt(end.getText());
428         } catch (NumberFormatException ex)
429         {
430         }
431
432         ffile.parseDescriptionHTML(sf, false);
433         setVisible(lastFeatureAdded); // if user edited name then make sure new
434                                       // type is visible
435       }
436       if (deleteFeature)
437       {
438         sequences[0].deleteFeature(sf);
439       }
440
441     }
442     else
443     {
444       if (dialog.accept && name.getText().length() > 0)
445       {
446         for (int i = 0; i < sequences.length; i++)
447         {
448           features[i].type = lastFeatureAdded;
449           features[i].featureGroup = lastFeatureGroupAdded;
450           features[i].description = lastDescriptionAdded;
451           sequences[i].addSequenceFeature(features[i]);
452           ffile.parseDescriptionHTML(features[i], false);
453         }
454
455         Color newColour = colourPanel.getBackground();
456         // setColour(lastFeatureAdded, fcol);
457
458         if (lastFeatureGroupAdded != null)
459         {
460           setGroupVisibility(lastFeatureGroupAdded, true);
461         }
462         setColour(lastFeatureAdded, newColour); // was fcol
463         setVisible(lastFeatureAdded);
464         findAllFeatures(false); // different to original applet behaviour ?
465         // findAllFeatures();
466       }
467       else
468       {
469         // no update to the alignment
470         return false;
471       }
472     }
473     // refresh the alignment and the feature settings dialog
474     if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
475     {
476       ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
477     }
478     // findAllFeatures();
479
480     ap.paintAlignment(true);
481
482     return true;
483   }
484 }