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