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