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