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