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