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