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