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