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