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