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