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