Merge branch 'features/JAL-2110_crossRefDuplications' into develop
[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.FeatureColour;
28 import jalview.schemes.UserColourScheme;
29 import jalview.util.MessageManager;
30
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.Font;
35 import java.awt.GridLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.ItemEvent;
39 import java.awt.event.ItemListener;
40 import java.awt.event.MouseAdapter;
41 import java.awt.event.MouseEvent;
42 import java.util.Arrays;
43 import java.util.Comparator;
44
45 import javax.swing.JColorChooser;
46 import javax.swing.JComboBox;
47 import javax.swing.JLabel;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.JSpinner;
52 import javax.swing.JTextArea;
53 import javax.swing.JTextField;
54 import javax.swing.SwingConstants;
55
56 /**
57  * DOCUMENT ME!
58  * 
59  * @author $author$
60  * @version $Revision$
61  */
62 public class FeatureRenderer extends
63         jalview.renderer.seqfeatures.FeatureRenderer implements
64         jalview.api.FeatureRenderer
65 {
66   Color resBoxColour;
67
68   AlignmentPanel ap;
69
70   /**
71    * Creates a new FeatureRenderer object.
72    * 
73    * @param av
74    *          DOCUMENT ME!
75    */
76   public FeatureRenderer(AlignmentPanel ap)
77   {
78     super();
79     this.ap = ap;
80     this.av = ap.av;
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       overlaps = new JComboBox();
179       for (int i = 0; i < features.length; i++)
180       {
181         overlaps.addItem(features[i].getType() + "/"
182                 + features[i].getBegin() + "-" + features[i].getEnd()
183                 + " (" + features[i].getFeatureGroup() + ")");
184       }
185
186       tmp.add(overlaps);
187
188       overlaps.addItemListener(new ItemListener()
189       {
190         @Override
191         public void itemStateChanged(ItemEvent e)
192         {
193           int index = overlaps.getSelectedIndex();
194           if (index != -1)
195           {
196             featureIndex = index;
197             name.setText(features[index].getType());
198             description.setText(features[index].getDescription());
199             source.setText(features[index].getFeatureGroup());
200             start.setValue(new Integer(features[index].getBegin()));
201             end.setValue(new Integer(features[index].getEnd()));
202
203             SearchResults highlight = new SearchResults();
204             highlight.addResult(sequences[0], features[index].getBegin(),
205                     features[index].getEnd());
206
207             ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
208
209           }
210           FeatureColourI col = getFeatureStyle(name.getText());
211           if (col == null)
212           {
213             col = new FeatureColour(UserColourScheme
214                     .createColourFromName(name.getText()));
215           }
216           oldcol = fcol = col;
217           updateColourButton(bigPanel, colour, col);
218         }
219       });
220
221       panel.add(tmp);
222     }
223     // ////////
224     // ////////////////////////////////////
225
226     tmp = new JPanel();
227     panel.add(tmp);
228     tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
229     tmp.add(name);
230
231     tmp = new JPanel();
232     panel.add(tmp);
233     tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
234             JLabel.RIGHT));
235     tmp.add(source);
236
237     tmp = new JPanel();
238     panel.add(tmp);
239     tmp.add(new JLabel(MessageManager.getString("label.colour"),
240             JLabel.RIGHT));
241     tmp.add(colour);
242     colour.setPreferredSize(new Dimension(150, 15));
243     colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
244     colour.setForeground(Color.black);
245     colour.setHorizontalAlignment(SwingConstants.CENTER);
246     colour.setVerticalAlignment(SwingConstants.CENTER);
247     colour.setHorizontalTextPosition(SwingConstants.CENTER);
248     colour.setVerticalTextPosition(SwingConstants.CENTER);
249     bigPanel.add(panel, BorderLayout.NORTH);
250
251     panel = new JPanel();
252     panel.add(new JLabel(MessageManager.getString("label.description"),
253             JLabel.RIGHT));
254     description.setFont(JvSwingUtils.getTextAreaFont());
255     description.setLineWrap(true);
256     panel.add(new JScrollPane(description));
257
258     if (!newFeatures)
259     {
260       bigPanel.add(panel, BorderLayout.SOUTH);
261
262       panel = new JPanel();
263       panel.add(new JLabel(MessageManager.getString("label.start"),
264               JLabel.RIGHT));
265       panel.add(start);
266       panel.add(new JLabel(MessageManager.getString("label.end"),
267               JLabel.RIGHT));
268       panel.add(end);
269       bigPanel.add(panel, BorderLayout.CENTER);
270     }
271     else
272     {
273       bigPanel.add(panel, BorderLayout.CENTER);
274     }
275
276     if (lastFeatureAdded == null)
277     {
278       if (features[0].type != null)
279       {
280         lastFeatureAdded = features[0].type;
281       }
282       else
283       {
284         lastFeatureAdded = "feature_1";
285       }
286     }
287
288     if (lastFeatureGroupAdded == null)
289     {
290       if (features[0].featureGroup != null)
291       {
292         lastFeatureGroupAdded = features[0].featureGroup;
293       }
294       else
295       {
296         lastFeatureGroupAdded = "Jalview";
297       }
298     }
299
300     if (newFeatures)
301     {
302       name.setText(lastFeatureAdded);
303       source.setText(lastFeatureGroupAdded);
304     }
305     else
306     {
307       name.setText(features[0].getType());
308       source.setText(features[0].getFeatureGroup());
309     }
310
311     start.setValue(new Integer(features[0].getBegin()));
312     end.setValue(new Integer(features[0].getEnd()));
313     description.setText(features[0].getDescription());
314     updateColourButton(bigPanel, colour,
315             (oldcol = fcol = getFeatureStyle(name.getText())));
316     Object[] options;
317     if (!newFeatures)
318     {
319       options = new Object[] { "Amend", "Delete", "Cancel" };
320     }
321     else
322     {
323       options = new Object[] { "OK", "Cancel" };
324     }
325
326     String title = newFeatures ? MessageManager
327             .getString("label.create_new_sequence_features")
328             : MessageManager.formatMessage("label.amend_delete_features",
329                     new String[] { sequences[0].getName() });
330
331     int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
332             bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
333             JOptionPane.QUESTION_MESSAGE, null, options,
334             MessageManager.getString("action.ok"));
335
336     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
337
338     if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
339     {
340       // This ensures that the last sequence
341       // is refreshed and new features are rendered
342       lastSeq = null;
343       lastFeatureAdded = name.getText().trim();
344       lastFeatureGroupAdded = source.getText().trim();
345       lastDescriptionAdded = description.getText().replaceAll("\n", " ");
346       // TODO: determine if the null feature group is valid
347       if (lastFeatureGroupAdded.length() < 1)
348       {
349         lastFeatureGroupAdded = null;
350       }
351     }
352
353     if (!newFeatures)
354     {
355       SequenceFeature sf = features[featureIndex];
356
357       if (reply == JOptionPane.NO_OPTION)
358       {
359         sequences[0].getDatasetSequence().deleteFeature(sf);
360       }
361       else if (reply == JOptionPane.YES_OPTION)
362       {
363         sf.type = lastFeatureAdded;
364         sf.featureGroup = lastFeatureGroupAdded;
365         sf.description = lastDescriptionAdded;
366
367         setColour(sf.type, fcol);
368         getFeaturesDisplayed().setVisible(sf.type);
369
370         try
371         {
372           sf.begin = ((Integer) start.getValue()).intValue();
373           sf.end = ((Integer) end.getValue()).intValue();
374         } catch (NumberFormatException ex)
375         {
376         }
377
378         ffile.parseDescriptionHTML(sf, false);
379       }
380     }
381     else
382     // NEW FEATURES ADDED
383     {
384       if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
385       {
386         for (int i = 0; i < sequences.length; i++)
387         {
388           features[i].type = lastFeatureAdded;
389           // fix for JAL-1538 - always set feature group here
390           features[i].featureGroup = lastFeatureGroupAdded;
391           features[i].description = lastDescriptionAdded;
392           sequences[i].addSequenceFeature(features[i]);
393           ffile.parseDescriptionHTML(features[i], false);
394         }
395
396         if (lastFeatureGroupAdded != null)
397         {
398           setGroupVisibility(lastFeatureGroupAdded, true);
399         }
400         setColour(lastFeatureAdded, fcol);
401         setVisible(lastFeatureAdded);
402
403         findAllFeatures(false);
404
405         ap.paintAlignment(true);
406
407         return true;
408       }
409       else
410       {
411         return false;
412       }
413     }
414
415     ap.paintAlignment(true);
416
417     return true;
418   }
419
420   /**
421    * update the amend feature button dependent on the given style
422    * 
423    * @param bigPanel
424    * @param col
425    * @param col
426    */
427   protected void updateColourButton(JPanel bigPanel, JLabel colour,
428           FeatureColourI col)
429   {
430     colour.removeAll();
431     colour.setIcon(null);
432     colour.setToolTipText(null);
433     colour.setText("");
434
435     if (col.isSimpleColour())
436     {
437       colour.setBackground(col.getColour());
438     }
439     else
440     {
441       colour.setBackground(bigPanel.getBackground());
442       colour.setForeground(Color.black);
443       FeatureSettings.renderGraduatedColor(colour, col);
444     }
445   }
446
447   /**
448    * Orders features in render precedence (last in order is last to render, so
449    * displayed on top of other features)
450    * 
451    * @param order
452    */
453   public void orderFeatures(Comparator<String> order)
454   {
455     Arrays.sort(renderOrder, order);
456   }
457 }