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