4655ba5431756a3d69b136cc15a514f5a9e81e7c
[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.Hashtable;
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   Hashtable 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     public void paint(Graphics g)
158     {
159       Dimension d = getSize();
160       if (isGcol)
161       {
162         if (isColourByLabel)
163         {
164           g.setColor(Color.white);
165           g.fillRect(d.width / 2, 0, d.width / 2, d.height);
166           g.setColor(Color.black);
167           Font f = new Font("Verdana", Font.PLAIN, 10);
168           g.setFont(f);
169           g.drawString(MessageManager.getString("label.label"), 0, 0);
170         }
171         else
172         {
173           g.setColor(maxCol);
174           g.fillRect(d.width / 2, 0, d.width / 2, d.height);
175
176         }
177       }
178     }
179
180   }
181
182   boolean amendFeatures(final SequenceI[] sequences,
183           final SequenceFeature[] features, boolean newFeatures,
184           final AlignmentPanel ap)
185   {
186     Panel bigPanel = new Panel(new BorderLayout());
187     final TextField name = new TextField(16);
188     final TextField source = new TextField(16);
189     final TextArea description = new TextArea(3, 35);
190     final TextField start = new TextField(8);
191     final TextField end = new TextField(8);
192     final Choice overlaps;
193     Button deleteButton = new Button("Delete");
194     deleteFeature = false;
195
196     colourPanel = new FeatureColourPanel();
197     colourPanel.setSize(110, 15);
198     final FeatureRenderer fr = this;
199
200     Panel panel = new Panel(new GridLayout(3, 1));
201
202     featureIndex = 0; // feature to be amended.
203     Panel tmp;
204
205     // /////////////////////////////////////
206     // /MULTIPLE FEATURES AT SELECTED RESIDUE
207     if (!newFeatures && features.length > 1)
208     {
209       panel = new Panel(new GridLayout(4, 1));
210       tmp = new Panel();
211       tmp.add(new Label("Select Feature: "));
212       overlaps = new Choice();
213       for (int i = 0; i < features.length; i++)
214       {
215         String item = features[i].getType() + "/" + features[i].getBegin()
216                 + "-" + features[i].getEnd();
217
218         if (features[i].getFeatureGroup() != null)
219         {
220           item += " (" + features[i].getFeatureGroup() + ")";
221         }
222
223         overlaps.addItem(item);
224       }
225
226       tmp.add(overlaps);
227
228       overlaps.addItemListener(new java.awt.event.ItemListener()
229       {
230         public void itemStateChanged(java.awt.event.ItemEvent e)
231         {
232           int index = overlaps.getSelectedIndex();
233           if (index != -1)
234           {
235             featureIndex = index;
236             name.setText(features[index].getType());
237             description.setText(features[index].getDescription());
238             source.setText(features[index].getFeatureGroup());
239             start.setText(features[index].getBegin() + "");
240             end.setText(features[index].getEnd() + "");
241
242             SearchResults highlight = new SearchResults();
243             highlight.addResult(sequences[0], features[index].getBegin(),
244                     features[index].getEnd());
245
246             ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
247
248           }
249           Object col = getFeatureStyle(name.getText());
250           if (col == null)
251           {
252             col = new jalview.schemes.UserColourScheme()
253                     .createColourFromName(name.getText());
254           }
255
256           colourPanel.updateColor(col);
257         }
258       });
259
260       panel.add(tmp);
261     }
262     // ////////
263     // ////////////////////////////////////
264
265     tmp = new Panel();
266     panel.add(tmp);
267     tmp.add(new Label("Name: ", Label.RIGHT));
268     tmp.add(name);
269
270     tmp = new Panel();
271     panel.add(tmp);
272     tmp.add(new Label("Group: ", Label.RIGHT));
273     tmp.add(source);
274
275     tmp = new Panel();
276     panel.add(tmp);
277     tmp.add(new Label("Colour: ", Label.RIGHT));
278     tmp.add(colourPanel);
279
280     bigPanel.add(panel, BorderLayout.NORTH);
281
282     panel = new Panel();
283     panel.add(new Label("Description: ", Label.RIGHT));
284     panel.add(new ScrollPane().add(description));
285
286     if (!newFeatures)
287     {
288       bigPanel.add(panel, BorderLayout.SOUTH);
289
290       panel = new Panel();
291       panel.add(new Label(" Start:", Label.RIGHT));
292       panel.add(start);
293       panel.add(new Label("  End:", Label.RIGHT));
294       panel.add(end);
295       bigPanel.add(panel, BorderLayout.CENTER);
296     }
297     else
298     {
299       bigPanel.add(panel, BorderLayout.CENTER);
300     }
301
302     if (lastFeatureAdded == null)
303     {
304       if (features[0].type != null)
305       {
306         lastFeatureAdded = features[0].type;
307       }
308       else
309       {
310         lastFeatureAdded = "feature_1";
311       }
312     }
313
314     if (lastFeatureGroupAdded == null)
315     {
316       if (features[0].featureGroup != null)
317       {
318         lastFeatureGroupAdded = features[0].featureGroup;
319       }
320       else
321       {
322         lastFeatureAdded = "Jalview";
323       }
324     }
325
326     String title = newFeatures ? MessageManager
327             .getString("label.create_new_sequence_features")
328             : MessageManager.formatMessage("label.amend_delete_features",
329                     new String[] { sequences[0].getName() });
330
331     final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
332             240);
333
334     dialog.setMainPanel(bigPanel);
335
336     if (newFeatures)
337     {
338       name.setText(lastFeatureAdded);
339       source.setText(lastFeatureGroupAdded);
340     }
341     else
342     {
343       dialog.ok.setLabel(MessageManager.getString("label.amend"));
344       dialog.buttonPanel.add(deleteButton, 1);
345       deleteButton.addActionListener(new ActionListener()
346       {
347         public void actionPerformed(ActionEvent evt)
348         {
349           deleteFeature = true;
350           dialog.setVisible(false);
351         }
352       });
353       name.setText(features[0].getType());
354       source.setText(features[0].getFeatureGroup());
355     }
356
357     start.setText(features[0].getBegin() + "");
358     end.setText(features[0].getEnd() + "");
359     description.setText(features[0].getDescription());
360     Color col = getColour(name.getText());
361     if (col == null)
362     {
363       col = new jalview.schemes.UserColourScheme()
364               .createColourFromName(name.getText());
365     }
366     Object fcol = getFeatureStyle(name.getText());
367     // simply display the feature color in a box
368     colourPanel.updateColor(fcol);
369     dialog.setResizable(true);
370     // TODO: render the graduated color in the box.
371     colourPanel.addMouseListener(new java.awt.event.MouseAdapter()
372     {
373       public void mousePressed(java.awt.event.MouseEvent evt)
374       {
375         if (!colourPanel.isGcol)
376         {
377           new UserDefinedColours(fr, ap.alignFrame);
378         }
379         else
380         {
381           FeatureColourChooser fcc = new FeatureColourChooser(
382                   ap.alignFrame, name.getText());
383           dialog.transferFocus();
384         }
385       }
386     });
387     dialog.setVisible(true);
388
389     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
390
391     if (dialog.accept)
392     {
393       // This ensures that the last sequence
394       // is refreshed and new features are rendered
395       lastSeq = null;
396       lastFeatureAdded = name.getText().trim();
397       lastFeatureGroupAdded = source.getText().trim();
398       lastDescriptionAdded = description.getText().replace('\n', ' ');
399     }
400
401     if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
402     {
403       lastFeatureGroupAdded = null;
404     }
405
406     if (!newFeatures)
407     {
408
409       SequenceFeature sf = features[featureIndex];
410       if (dialog.accept)
411       {
412         sf.type = lastFeatureAdded;
413         sf.featureGroup = lastFeatureGroupAdded;
414         sf.description = lastDescriptionAdded;
415         if (!colourPanel.isGcol)
416         {
417           // update colour - otherwise its already done.
418           setColour(sf.type, colourPanel.getBackground());
419         }
420         try
421         {
422           sf.begin = Integer.parseInt(start.getText());
423           sf.end = Integer.parseInt(end.getText());
424         } catch (NumberFormatException ex)
425         {
426         }
427
428         ffile.parseDescriptionHTML(sf, false);
429         setVisible(lastFeatureAdded); // if user edited name then make sure new
430                                       // type is visible
431       }
432       if (deleteFeature)
433       {
434         sequences[0].deleteFeature(sf);
435       }
436
437     }
438     else
439     {
440       if (dialog.accept && name.getText().length() > 0)
441       {
442         for (int i = 0; i < sequences.length; i++)
443         {
444           features[i].type = lastFeatureAdded;
445           features[i].featureGroup = lastFeatureGroupAdded;
446           features[i].description = lastDescriptionAdded;
447           sequences[i].addSequenceFeature(features[i]);
448           ffile.parseDescriptionHTML(features[i], false);
449         }
450
451         Color newColour = colourPanel.getBackground();
452         // setColour(lastFeatureAdded, fcol);
453
454         if (lastFeatureGroupAdded != null)
455         {
456           setGroupVisibility(lastFeatureGroupAdded, true);
457         }
458         setColour(lastFeatureAdded, newColour); // was fcol
459         setVisible(lastFeatureAdded);
460         findAllFeatures(false); // different to original applet behaviour ?
461         // findAllFeatures();
462       }
463       else
464       {
465         // no update to the alignment
466         return false;
467       }
468     }
469     // refresh the alignment and the feature settings dialog
470     if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
471     {
472       ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
473     }
474     // findAllFeatures();
475
476     ap.paintAlignment(true);
477
478     return true;
479   }
480 }