JAL-2094 new classes ColorI, Colour added
[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.Colour;
28 import jalview.schemes.FeatureColour;
29 import jalview.schemes.UserColourScheme;
30 import jalview.util.ColorUtils;
31 import jalview.util.MessageManager;
32
33 import java.awt.BorderLayout;
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Font;
37 import java.awt.GridLayout;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.ItemEvent;
41 import java.awt.event.ItemListener;
42 import java.awt.event.MouseAdapter;
43 import java.awt.event.MouseEvent;
44 import java.util.Arrays;
45 import java.util.Comparator;
46
47 import javax.swing.JColorChooser;
48 import javax.swing.JComboBox;
49 import javax.swing.JLabel;
50 import javax.swing.JOptionPane;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollPane;
53 import javax.swing.JSpinner;
54 import javax.swing.JTextArea;
55 import javax.swing.JTextField;
56 import javax.swing.SwingConstants;
57
58 /**
59  * DOCUMENT ME!
60  * 
61  * @author $author$
62  * @version $Revision$
63  */
64 public class FeatureRenderer extends
65         jalview.renderer.seqfeatures.FeatureRenderer implements
66         jalview.api.FeatureRenderer
67 {
68   Color resBoxColour;
69
70   AlignmentPanel ap;
71
72   /**
73    * Creates a new FeatureRenderer object.
74    * 
75    * @param av
76    *          DOCUMENT ME!
77    */
78   public FeatureRenderer(AlignmentPanel ap)
79   {
80     super();
81     this.ap = ap;
82     this.av = ap.av;
83     if (ap != null && ap.getSeqPanel() != null
84             && ap.getSeqPanel().seqCanvas != null
85             && ap.getSeqPanel().seqCanvas.fr != null)
86     {
87       transferSettings(ap.getSeqPanel().seqCanvas.fr);
88     }
89   }
90
91   // // /////////////
92   // // Feature Editing Dialog
93   // // Will be refactored in next release.
94
95   static String lastFeatureAdded;
96
97   static String lastFeatureGroupAdded;
98
99   static String lastDescriptionAdded;
100
101   FeatureColourI oldcol, fcol;
102
103   int featureIndex = 0;
104
105   boolean amendFeatures(final SequenceI[] sequences,
106           final SequenceFeature[] features, boolean newFeatures,
107           final AlignmentPanel ap)
108   {
109
110     featureIndex = 0;
111
112     final JPanel bigPanel = new JPanel(new BorderLayout());
113     final JComboBox overlaps;
114     final JTextField name = new JTextField(25);
115     final JTextField source = new JTextField(25);
116     final JTextArea description = new JTextArea(3, 25);
117     final JSpinner start = new JSpinner();
118     final JSpinner end = new JSpinner();
119     start.setPreferredSize(new Dimension(80, 20));
120     end.setPreferredSize(new Dimension(80, 20));
121     final FeatureRenderer me = this;
122     final JLabel colour = new JLabel();
123     colour.setOpaque(true);
124     // colour.setBorder(BorderFactory.createEtchedBorder());
125     colour.setMaximumSize(new Dimension(30, 16));
126     colour.addMouseListener(new MouseAdapter()
127     {
128       FeatureColourChooser fcc = null;
129
130       @Override
131       public void mousePressed(MouseEvent evt)
132       {
133         if (fcol.isSimpleColour())
134         {
135           Color col = JColorChooser.showDialog(Desktop.desktop,
136                   MessageManager.getString("label.select_feature_colour"),
137                   ColorUtils.getColor(fcol.getColour()));
138           if (col != null)
139           {
140             fcol = new FeatureColour(new Colour(col));
141             updateColourButton(bigPanel, colour, fcol);
142           }
143         }
144         else
145         {
146           if (fcc == null)
147           {
148             final String type = features[featureIndex].getType();
149             fcc = new FeatureColourChooser(me, type);
150             fcc.setRequestFocusEnabled(true);
151             fcc.requestFocus();
152
153             fcc.addActionListener(new ActionListener()
154             {
155
156               @Override
157               public void actionPerformed(ActionEvent e)
158               {
159                 fcol = fcc.getLastColour();
160                 fcc = null;
161                 setColour(type, fcol);
162                 updateColourButton(bigPanel, colour, fcol);
163               }
164             });
165
166           }
167         }
168       }
169     });
170     JPanel tmp = new JPanel();
171     JPanel panel = new JPanel(new GridLayout(3, 1));
172
173     // /////////////////////////////////////
174     // /MULTIPLE FEATURES AT SELECTED RESIDUE
175     if (!newFeatures && features.length > 1)
176     {
177       panel = new JPanel(new GridLayout(4, 1));
178       tmp = new JPanel();
179       tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
180       overlaps = new JComboBox();
181       for (int i = 0; i < features.length; i++)
182       {
183         overlaps.addItem(features[i].getType() + "/"
184                 + features[i].getBegin() + "-" + features[i].getEnd()
185                 + " (" + features[i].getFeatureGroup() + ")");
186       }
187
188       tmp.add(overlaps);
189
190       overlaps.addItemListener(new ItemListener()
191       {
192         @Override
193         public void itemStateChanged(ItemEvent e)
194         {
195           int index = overlaps.getSelectedIndex();
196           if (index != -1)
197           {
198             featureIndex = index;
199             name.setText(features[index].getType());
200             description.setText(features[index].getDescription());
201             source.setText(features[index].getFeatureGroup());
202             start.setValue(new Integer(features[index].getBegin()));
203             end.setValue(new Integer(features[index].getEnd()));
204
205             SearchResults highlight = new SearchResults();
206             highlight.addResult(sequences[0], features[index].getBegin(),
207                     features[index].getEnd());
208
209             ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
210
211           }
212           FeatureColourI col = getFeatureStyle(name.getText());
213           if (col == null)
214           {
215             col = new FeatureColour(new Colour(UserColourScheme
216                     .createColourFromName(name.getText())));
217           }
218           oldcol = fcol = col;
219           updateColourButton(bigPanel, colour, col);
220         }
221       });
222
223       panel.add(tmp);
224     }
225     // ////////
226     // ////////////////////////////////////
227
228     tmp = new JPanel();
229     panel.add(tmp);
230     tmp.add(new JLabel(MessageManager.getString("label.name"), 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 = JOptionPane.showInternalOptionDialog(Desktop.desktop,
334             bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
335             JOptionPane.QUESTION_MESSAGE, null, options,
336             MessageManager.getString("action.ok"));
337
338     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
339
340     if (reply == JOptionPane.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 == JOptionPane.NO_OPTION)
360       {
361         sequences[0].getDatasetSequence().deleteFeature(sf);
362       }
363       else if (reply == JOptionPane.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 == JOptionPane.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 colour
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(ColorUtils.getColor(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 }