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