JAL-2068 minimised jalview.gui imports, test for removeAnnotation
[jalview.git] / src / jalview / gui / 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.gui;
22
23 import jalview.datamodel.SearchResults;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.GraduatedColor;
27 import jalview.util.MessageManager;
28
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Font;
33 import java.awt.GridLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38 import java.awt.event.MouseAdapter;
39 import java.awt.event.MouseEvent;
40 import java.util.Arrays;
41 import java.util.Comparator;
42
43 import javax.swing.JColorChooser;
44 import javax.swing.JComboBox;
45 import javax.swing.JLabel;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JSpinner;
50 import javax.swing.JTextArea;
51 import javax.swing.JTextField;
52 import javax.swing.SwingConstants;
53
54 /**
55  * DOCUMENT ME!
56  * 
57  * @author $author$
58  * @version $Revision$
59  */
60 public class FeatureRenderer extends
61         jalview.renderer.seqfeatures.FeatureRenderer implements
62         jalview.api.FeatureRenderer
63 {
64   Color resBoxColour;
65
66   AlignmentPanel ap;
67
68   /**
69    * Creates a new FeatureRenderer object.
70    * 
71    * @param av
72    *          DOCUMENT ME!
73    */
74   public FeatureRenderer(AlignmentPanel ap)
75   {
76     super(ap.av);
77     this.ap = ap;
78     if (ap != null && ap.getSeqPanel() != null
79             && ap.getSeqPanel().seqCanvas != null
80             && ap.getSeqPanel().seqCanvas.fr != null)
81     {
82       transferSettings(ap.getSeqPanel().seqCanvas.fr);
83     }
84   }
85
86   // // /////////////
87   // // Feature Editing Dialog
88   // // Will be refactored in next release.
89
90   static String lastFeatureAdded;
91
92   static String lastFeatureGroupAdded;
93
94   static String lastDescriptionAdded;
95
96   Object oldcol, fcol;
97
98   int featureIndex = 0;
99
100   boolean amendFeatures(final SequenceI[] sequences,
101           final SequenceFeature[] features, boolean newFeatures,
102           final AlignmentPanel ap)
103   {
104
105     featureIndex = 0;
106
107     final JPanel bigPanel = new JPanel(new BorderLayout());
108     final JComboBox overlaps;
109     final JTextField name = new JTextField(25);
110     final JTextField source = new JTextField(25);
111     final JTextArea description = new JTextArea(3, 25);
112     final JSpinner start = new JSpinner();
113     final JSpinner end = new JSpinner();
114     start.setPreferredSize(new Dimension(80, 20));
115     end.setPreferredSize(new Dimension(80, 20));
116     final FeatureRenderer me = this;
117     final JLabel colour = new JLabel();
118     colour.setOpaque(true);
119     // colour.setBorder(BorderFactory.createEtchedBorder());
120     colour.setMaximumSize(new Dimension(30, 16));
121     colour.addMouseListener(new MouseAdapter()
122     {
123       FeatureColourChooser fcc = null;
124
125       @Override
126       public void mousePressed(MouseEvent evt)
127       {
128         if (fcol instanceof Color)
129         {
130           Color col = JColorChooser.showDialog(Desktop.desktop,
131                   MessageManager.getString("label.select_feature_colour"),
132                   ((Color) fcol));
133           if (col != null)
134           {
135             fcol = col;
136             updateColourButton(bigPanel, colour, col);
137           }
138         }
139         else
140         {
141
142           if (fcc == null)
143           {
144             final String type = features[featureIndex].getType();
145             fcc = new FeatureColourChooser(me, type);
146             fcc.setRequestFocusEnabled(true);
147             fcc.requestFocus();
148
149             fcc.addActionListener(new ActionListener()
150             {
151
152               @Override
153               public void actionPerformed(ActionEvent e)
154               {
155                 fcol = fcc.getLastColour();
156                 fcc = null;
157                 setColour(type, fcol);
158                 updateColourButton(bigPanel, colour, fcol);
159               }
160             });
161
162           }
163         }
164       }
165     });
166     JPanel tmp = new JPanel();
167     JPanel panel = new JPanel(new GridLayout(3, 1));
168
169     // /////////////////////////////////////
170     // /MULTIPLE FEATURES AT SELECTED RESIDUE
171     if (!newFeatures && features.length > 1)
172     {
173       panel = new JPanel(new GridLayout(4, 1));
174       tmp = new JPanel();
175       tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
176       overlaps = new JComboBox();
177       for (int i = 0; i < features.length; i++)
178       {
179         overlaps.addItem(features[i].getType() + "/"
180                 + features[i].getBegin() + "-" + features[i].getEnd()
181                 + " (" + features[i].getFeatureGroup() + ")");
182       }
183
184       tmp.add(overlaps);
185
186       overlaps.addItemListener(new ItemListener()
187       {
188         @Override
189         public void itemStateChanged(ItemEvent e)
190         {
191           int index = overlaps.getSelectedIndex();
192           if (index != -1)
193           {
194             featureIndex = index;
195             name.setText(features[index].getType());
196             description.setText(features[index].getDescription());
197             source.setText(features[index].getFeatureGroup());
198             start.setValue(new Integer(features[index].getBegin()));
199             end.setValue(new Integer(features[index].getEnd()));
200
201             SearchResults highlight = new SearchResults();
202             highlight.addResult(sequences[0], features[index].getBegin(),
203                     features[index].getEnd());
204
205             ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
206
207           }
208           Object col = getFeatureStyle(name.getText());
209           if (col == null)
210           {
211             col = new jalview.schemes.UserColourScheme()
212                     .createColourFromName(name.getText());
213           }
214           oldcol = fcol = col;
215           updateColourButton(bigPanel, colour, col);
216         }
217       });
218
219       panel.add(tmp);
220     }
221     // ////////
222     // ////////////////////////////////////
223
224     tmp = new JPanel();
225     panel.add(tmp);
226     tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
227     tmp.add(name);
228
229     tmp = new JPanel();
230     panel.add(tmp);
231     tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
232             JLabel.RIGHT));
233     tmp.add(source);
234
235     tmp = new JPanel();
236     panel.add(tmp);
237     tmp.add(new JLabel(MessageManager.getString("label.colour"),
238             JLabel.RIGHT));
239     tmp.add(colour);
240     colour.setPreferredSize(new Dimension(150, 15));
241     colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
242     colour.setForeground(Color.black);
243     colour.setHorizontalAlignment(SwingConstants.CENTER);
244     colour.setVerticalAlignment(SwingConstants.CENTER);
245     colour.setHorizontalTextPosition(SwingConstants.CENTER);
246     colour.setVerticalTextPosition(SwingConstants.CENTER);
247     bigPanel.add(panel, BorderLayout.NORTH);
248
249     panel = new JPanel();
250     panel.add(new JLabel(MessageManager.getString("label.description"),
251             JLabel.RIGHT));
252     description.setFont(JvSwingUtils.getTextAreaFont());
253     description.setLineWrap(true);
254     panel.add(new JScrollPane(description));
255
256     if (!newFeatures)
257     {
258       bigPanel.add(panel, BorderLayout.SOUTH);
259
260       panel = new JPanel();
261       panel.add(new JLabel(MessageManager.getString("label.start"),
262               JLabel.RIGHT));
263       panel.add(start);
264       panel.add(new JLabel(MessageManager.getString("label.end"),
265               JLabel.RIGHT));
266       panel.add(end);
267       bigPanel.add(panel, BorderLayout.CENTER);
268     }
269     else
270     {
271       bigPanel.add(panel, BorderLayout.CENTER);
272     }
273
274     if (lastFeatureAdded == null)
275     {
276       if (features[0].type != null)
277       {
278         lastFeatureAdded = features[0].type;
279       }
280       else
281       {
282         lastFeatureAdded = "feature_1";
283       }
284     }
285
286     if (lastFeatureGroupAdded == null)
287     {
288       if (features[0].featureGroup != null)
289       {
290         lastFeatureGroupAdded = features[0].featureGroup;
291       }
292       else
293       {
294         lastFeatureGroupAdded = "Jalview";
295       }
296     }
297
298     if (newFeatures)
299     {
300       name.setText(lastFeatureAdded);
301       source.setText(lastFeatureGroupAdded);
302     }
303     else
304     {
305       name.setText(features[0].getType());
306       source.setText(features[0].getFeatureGroup());
307     }
308
309     start.setValue(new Integer(features[0].getBegin()));
310     end.setValue(new Integer(features[0].getEnd()));
311     description.setText(features[0].getDescription());
312     updateColourButton(bigPanel, colour,
313             (oldcol = fcol = getFeatureStyle(name.getText())));
314     Object[] options;
315     if (!newFeatures)
316     {
317       options = new Object[] { "Amend", "Delete", "Cancel" };
318     }
319     else
320     {
321       options = new Object[] { "OK", "Cancel" };
322     }
323
324     String title = newFeatures ? MessageManager
325             .getString("label.create_new_sequence_features")
326             : MessageManager.formatMessage("label.amend_delete_features",
327                     new String[] { sequences[0].getName() });
328
329     int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
330             bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
331             JOptionPane.QUESTION_MESSAGE, null, options,
332             MessageManager.getString("action.ok"));
333
334     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
335
336     if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
337     {
338       // This ensures that the last sequence
339       // is refreshed and new features are rendered
340       lastSeq = null;
341       lastFeatureAdded = name.getText().trim();
342       lastFeatureGroupAdded = source.getText().trim();
343       lastDescriptionAdded = description.getText().replaceAll("\n", " ");
344       // TODO: determine if the null feature group is valid
345       if (lastFeatureGroupAdded.length() < 1)
346       {
347         lastFeatureGroupAdded = null;
348       }
349     }
350
351     if (!newFeatures)
352     {
353       SequenceFeature sf = features[featureIndex];
354
355       if (reply == JOptionPane.NO_OPTION)
356       {
357         sequences[0].getDatasetSequence().deleteFeature(sf);
358       }
359       else if (reply == JOptionPane.YES_OPTION)
360       {
361         sf.type = lastFeatureAdded;
362         sf.featureGroup = lastFeatureGroupAdded;
363         sf.description = lastDescriptionAdded;
364
365         setColour(sf.type, fcol);
366         getFeaturesDisplayed().setVisible(sf.type);
367
368         try
369         {
370           sf.begin = ((Integer) start.getValue()).intValue();
371           sf.end = ((Integer) end.getValue()).intValue();
372         } catch (NumberFormatException ex)
373         {
374         }
375
376         ffile.parseDescriptionHTML(sf, false);
377       }
378     }
379     else
380     // NEW FEATURES ADDED
381     {
382       if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
383       {
384         for (int i = 0; i < sequences.length; i++)
385         {
386           features[i].type = lastFeatureAdded;
387           // fix for JAL-1538 - always set feature group here
388           features[i].featureGroup = lastFeatureGroupAdded;
389           features[i].description = lastDescriptionAdded;
390           sequences[i].addSequenceFeature(features[i]);
391           ffile.parseDescriptionHTML(features[i], false);
392         }
393
394         if (lastFeatureGroupAdded != null)
395         {
396           setGroupVisibility(lastFeatureGroupAdded, true);
397         }
398         setColour(lastFeatureAdded, fcol);
399         setVisible(lastFeatureAdded);
400
401         findAllFeatures(false);
402
403         ap.paintAlignment(true);
404
405         return true;
406       }
407       else
408       {
409         return false;
410       }
411     }
412
413     ap.paintAlignment(true);
414
415     return true;
416   }
417
418   /**
419    * update the amend feature button dependent on the given style
420    * 
421    * @param bigPanel
422    * @param col
423    * @param col2
424    */
425   protected void updateColourButton(JPanel bigPanel, JLabel colour,
426           Object col2)
427   {
428     colour.removeAll();
429     colour.setIcon(null);
430     colour.setToolTipText(null);
431     colour.setText("");
432
433     if (col2 instanceof Color)
434     {
435       colour.setBackground((Color) col2);
436     }
437     else
438     {
439       colour.setBackground(bigPanel.getBackground());
440       colour.setForeground(Color.black);
441       FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
442       // colour.setForeground(colour.getBackground());
443     }
444   }
445
446   /**
447    * Orders features in render precedence (last in order is last to render, so
448    * displayed on top of other features)
449    * 
450    * @param order
451    */
452   public void orderFeatures(Comparator<String> order)
453   {
454     Arrays.sort(renderOrder, order);
455   }
456 }