Merge branch 'develop' into features/JAL-2094_colourInterface
[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(ap.av);
81     this.ap = ap;
82     if (ap != null && ap.getSeqPanel() != null
83             && ap.getSeqPanel().seqCanvas != null
84             && ap.getSeqPanel().seqCanvas.fr != null)
85     {
86       transferSettings(ap.getSeqPanel().seqCanvas.fr);
87     }
88   }
89
90   // // /////////////
91   // // Feature Editing Dialog
92   // // Will be refactored in next release.
93
94   static String lastFeatureAdded;
95
96   static String lastFeatureGroupAdded;
97
98   static String lastDescriptionAdded;
99
100   FeatureColourI oldcol, fcol;
101
102   int featureIndex = 0;
103
104   boolean amendFeatures(final SequenceI[] sequences,
105           final SequenceFeature[] features, boolean newFeatures,
106           final AlignmentPanel ap)
107   {
108
109     featureIndex = 0;
110
111     final JPanel bigPanel = new JPanel(new BorderLayout());
112     final JComboBox overlaps;
113     final JTextField name = new JTextField(25);
114     final JTextField source = new JTextField(25);
115     final JTextArea description = new JTextArea(3, 25);
116     final JSpinner start = new JSpinner();
117     final JSpinner end = new JSpinner();
118     start.setPreferredSize(new Dimension(80, 20));
119     end.setPreferredSize(new Dimension(80, 20));
120     final FeatureRenderer me = this;
121     final JLabel colour = new JLabel();
122     colour.setOpaque(true);
123     // colour.setBorder(BorderFactory.createEtchedBorder());
124     colour.setMaximumSize(new Dimension(30, 16));
125     colour.addMouseListener(new MouseAdapter()
126     {
127       FeatureColourChooser fcc = null;
128
129       @Override
130       public void mousePressed(MouseEvent evt)
131       {
132         if (fcol.isSimpleColour())
133         {
134           Color col = JColorChooser.showDialog(Desktop.desktop,
135                   MessageManager.getString("label.select_feature_colour"),
136                   ColorUtils.getColor(fcol.getColour()));
137           if (col != null)
138           {
139             fcol = new FeatureColour(new Colour(col));
140             updateColourButton(bigPanel, colour, fcol);
141           }
142         }
143         else
144         {
145           if (fcc == null)
146           {
147             final String type = features[featureIndex].getType();
148             fcc = new FeatureColourChooser(me, type);
149             fcc.setRequestFocusEnabled(true);
150             fcc.requestFocus();
151
152             fcc.addActionListener(new ActionListener()
153             {
154
155               @Override
156               public void actionPerformed(ActionEvent e)
157               {
158                 fcol = fcc.getLastColour();
159                 fcc = null;
160                 setColour(type, fcol);
161                 updateColourButton(bigPanel, colour, fcol);
162               }
163             });
164
165           }
166         }
167       }
168     });
169     JPanel tmp = new JPanel();
170     JPanel panel = new JPanel(new GridLayout(3, 1));
171
172     // /////////////////////////////////////
173     // /MULTIPLE FEATURES AT SELECTED RESIDUE
174     if (!newFeatures && features.length > 1)
175     {
176       panel = new JPanel(new GridLayout(4, 1));
177       tmp = new JPanel();
178       tmp.add(new JLabel(MessageManager.getString("label.select_feature")
179               + ":"));
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:"),
231             JLabel.RIGHT));
232     tmp.add(name);
233
234     tmp = new JPanel();
235     panel.add(tmp);
236     tmp.add(new JLabel(MessageManager.getString("label.group:"),
237             JLabel.RIGHT));
238     tmp.add(source);
239
240     tmp = new JPanel();
241     panel.add(tmp);
242     tmp.add(new JLabel(MessageManager.getString("label.colour"),
243             JLabel.RIGHT));
244     tmp.add(colour);
245     colour.setPreferredSize(new Dimension(150, 15));
246     colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
247     colour.setForeground(Color.black);
248     colour.setHorizontalAlignment(SwingConstants.CENTER);
249     colour.setVerticalAlignment(SwingConstants.CENTER);
250     colour.setHorizontalTextPosition(SwingConstants.CENTER);
251     colour.setVerticalTextPosition(SwingConstants.CENTER);
252     bigPanel.add(panel, BorderLayout.NORTH);
253
254     panel = new JPanel();
255     panel.add(new JLabel(MessageManager.getString("label.description:"),
256             JLabel.RIGHT));
257     description.setFont(JvSwingUtils.getTextAreaFont());
258     description.setLineWrap(true);
259     panel.add(new JScrollPane(description));
260
261     if (!newFeatures)
262     {
263       bigPanel.add(panel, BorderLayout.SOUTH);
264
265       panel = new JPanel();
266       panel.add(new JLabel(MessageManager.getString("label.start"),
267               JLabel.RIGHT));
268       panel.add(start);
269       panel.add(new JLabel(MessageManager.getString("label.end"),
270               JLabel.RIGHT));
271       panel.add(end);
272       bigPanel.add(panel, BorderLayout.CENTER);
273     }
274     else
275     {
276       bigPanel.add(panel, BorderLayout.CENTER);
277     }
278
279     if (lastFeatureAdded == null)
280     {
281       if (features[0].type != null)
282       {
283         lastFeatureAdded = features[0].type;
284       }
285       else
286       {
287         lastFeatureAdded = "feature_1";
288       }
289     }
290
291     if (lastFeatureGroupAdded == null)
292     {
293       if (features[0].featureGroup != null)
294       {
295         lastFeatureGroupAdded = features[0].featureGroup;
296       }
297       else
298       {
299         lastFeatureGroupAdded = "Jalview";
300       }
301     }
302
303     if (newFeatures)
304     {
305       name.setText(lastFeatureAdded);
306       source.setText(lastFeatureGroupAdded);
307     }
308     else
309     {
310       name.setText(features[0].getType());
311       source.setText(features[0].getFeatureGroup());
312     }
313
314     start.setValue(new Integer(features[0].getBegin()));
315     end.setValue(new Integer(features[0].getEnd()));
316     description.setText(features[0].getDescription());
317     updateColourButton(bigPanel, colour,
318             (oldcol = fcol = getFeatureStyle(name.getText())));
319     Object[] options;
320     if (!newFeatures)
321     {
322       options = new Object[] { "Amend", "Delete", "Cancel" };
323     }
324     else
325     {
326       options = new Object[] { "OK", "Cancel" };
327     }
328
329     String title = newFeatures ? MessageManager
330             .getString("label.create_new_sequence_features")
331             : MessageManager.formatMessage("label.amend_delete_features",
332                     new String[] { sequences[0].getName() });
333
334     int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
335             bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
336             JOptionPane.QUESTION_MESSAGE, null, options,
337             MessageManager.getString("action.ok"));
338
339     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
340
341     if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
342     {
343       // This ensures that the last sequence
344       // is refreshed and new features are rendered
345       lastSeq = null;
346       lastFeatureAdded = name.getText().trim();
347       lastFeatureGroupAdded = source.getText().trim();
348       lastDescriptionAdded = description.getText().replaceAll("\n", " ");
349       // TODO: determine if the null feature group is valid
350       if (lastFeatureGroupAdded.length() < 1)
351       {
352         lastFeatureGroupAdded = null;
353       }
354     }
355
356     if (!newFeatures)
357     {
358       SequenceFeature sf = features[featureIndex];
359
360       if (reply == JOptionPane.NO_OPTION)
361       {
362         sequences[0].getDatasetSequence().deleteFeature(sf);
363       }
364       else if (reply == JOptionPane.YES_OPTION)
365       {
366         sf.type = lastFeatureAdded;
367         sf.featureGroup = lastFeatureGroupAdded;
368         sf.description = lastDescriptionAdded;
369
370         setColour(sf.type, fcol);
371         getFeaturesDisplayed().setVisible(sf.type);
372
373         try
374         {
375           sf.begin = ((Integer) start.getValue()).intValue();
376           sf.end = ((Integer) end.getValue()).intValue();
377         } catch (NumberFormatException ex)
378         {
379         }
380
381         ffile.parseDescriptionHTML(sf, false);
382       }
383     }
384     else
385     // NEW FEATURES ADDED
386     {
387       if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
388       {
389         for (int i = 0; i < sequences.length; i++)
390         {
391           features[i].type = lastFeatureAdded;
392           // fix for JAL-1538 - always set feature group here
393           features[i].featureGroup = lastFeatureGroupAdded;
394           features[i].description = lastDescriptionAdded;
395           sequences[i].addSequenceFeature(features[i]);
396           ffile.parseDescriptionHTML(features[i], false);
397         }
398
399         if (lastFeatureGroupAdded != null)
400         {
401           setGroupVisibility(lastFeatureGroupAdded, true);
402         }
403         setColour(lastFeatureAdded, fcol);
404         setVisible(lastFeatureAdded);
405
406         findAllFeatures(false);
407
408         ap.paintAlignment(true);
409
410         return true;
411       }
412       else
413       {
414         return false;
415       }
416     }
417
418     ap.paintAlignment(true);
419
420     return true;
421   }
422
423   /**
424    * update the amend feature button dependent on the given style
425    * 
426    * @param bigPanel
427    * @param colour
428    * @param col
429 <<<<<<< HEAD
430 =======
431    * @param col
432 >>>>>>> refs/heads/develop
433    */
434   protected void updateColourButton(JPanel bigPanel, JLabel colour,
435           FeatureColourI col)
436   {
437     colour.removeAll();
438     colour.setIcon(null);
439     colour.setToolTipText(null);
440     colour.setText("");
441
442     if (col.isSimpleColour())
443     {
444       colour.setBackground(ColorUtils.getColor(col.getColour()));
445     }
446     else
447     {
448       colour.setBackground(bigPanel.getBackground());
449       colour.setForeground(Color.black);
450       FeatureSettings.renderGraduatedColor(colour, col);
451     }
452   }
453
454   /**
455    * Orders features in render precedence (last in order is last to render, so
456    * displayed on top of other features)
457    * 
458    * @param order
459    */
460   public void orderFeatures(Comparator<String> order)
461   {
462     Arrays.sort(renderOrder, order);
463   }
464 }