2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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.io.FeaturesFile;
29 import jalview.schemes.FeatureColour;
30 import jalview.util.ColorUtils;
31 import jalview.util.MessageManager;
33 import java.awt.BorderLayout;
34 import java.awt.Color;
35 import java.awt.Dimension;
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.ArrayList;
45 import java.util.Arrays;
46 import java.util.Comparator;
47 import java.util.HashMap;
48 import java.util.List;
50 import java.util.Map.Entry;
52 import javax.swing.JColorChooser;
53 import javax.swing.JComboBox;
54 import javax.swing.JLabel;
55 import javax.swing.JPanel;
56 import javax.swing.JScrollPane;
57 import javax.swing.JSpinner;
58 import javax.swing.JTextArea;
59 import javax.swing.JTextField;
60 import javax.swing.SwingConstants;
61 import javax.swing.event.DocumentEvent;
62 import javax.swing.event.DocumentListener;
70 public class FeatureRenderer
71 extends jalview.renderer.seqfeatures.FeatureRenderer
74 * defaults for creating a new feature are the last created
75 * feature type and group
77 static String lastFeatureAdded = "feature_1";
79 static String lastFeatureGroupAdded = "Jalview";
86 * the colour scheme selected by the user for the current feature type
87 * when amending features
94 * Creates a new FeatureRenderer object
98 public FeatureRenderer(AlignmentPanel alignPanel)
100 super(alignPanel.av);
101 this.ap = alignPanel;
102 if (alignPanel.getSeqPanel() != null
103 && alignPanel.getSeqPanel().seqCanvas != null
104 && alignPanel.getSeqPanel().seqCanvas.fr != null)
106 transferSettings(alignPanel.getSeqPanel().seqCanvas.fr);
111 * Presents a dialog allowing the user to add new features, or amend or delete
112 * existing features. Currently this can be on
114 * <li>double-click on a sequence - Amend/Delete features at position</li>
115 * <li>Create sequence feature from pop-up menu on selected region</li>
116 * <li>Create features for pattern matches from Find</li>
118 * If the supplied feature type is null, show (and update on confirm) the type
119 * and group of the last new feature created (with initial defaults of
120 * "feature_1" and "Jalview").
123 * the sequences features are to be created on (if creating
124 * features), or a single sequence (if amending features)
126 * the current features at the position (if amending), or template
127 * new feature(s) with start/end position set (if creating)
129 * true to create features, false to amend or delete
133 protected boolean amendFeatures(final List<SequenceI> sequences,
134 final List<SequenceFeature> features, boolean create,
135 final AlignmentPanel alignPanel)
138 * save original colours, for reset on Cancel
140 Map<String, FeatureColourI> oldColours = new HashMap<>();
141 for (SequenceFeature sf : features)
143 String type = sf.getType();
146 oldColours.put(type, getFeatureStyle(type));
152 final JPanel mainPanel = new JPanel(new BorderLayout());
154 final JTextField name = new JTextField(25);
155 name.getDocument().addDocumentListener(new DocumentListener()
158 public void insertUpdate(DocumentEvent e)
160 warnIfTypeHidden(mainPanel, name.getText());
164 public void removeUpdate(DocumentEvent e)
166 warnIfTypeHidden(mainPanel, name.getText());
170 public void changedUpdate(DocumentEvent e)
172 warnIfTypeHidden(mainPanel, name.getText());
176 final JTextField group = new JTextField(25);
177 group.getDocument().addDocumentListener(new DocumentListener()
180 public void insertUpdate(DocumentEvent e)
182 warnIfGroupHidden(mainPanel, group.getText());
186 public void removeUpdate(DocumentEvent e)
188 warnIfGroupHidden(mainPanel, group.getText());
192 public void changedUpdate(DocumentEvent e)
194 warnIfGroupHidden(mainPanel, group.getText());
198 final JTextArea description = new JTextArea(3, 25);
199 final JSpinner start = new JSpinner();
200 final JSpinner end = new JSpinner();
201 start.setPreferredSize(new Dimension(80, 20));
202 end.setPreferredSize(new Dimension(80, 20));
203 final FeatureRenderer me = this;
204 final JLabel colour = new JLabel();
205 colour.setOpaque(true);
206 // colour.setBorder(BorderFactory.createEtchedBorder());
207 colour.setMaximumSize(new Dimension(30, 16));
208 colour.addMouseListener(new MouseAdapter()
210 FeatureColourChooser fcc = null;
213 public void mousePressed(MouseEvent evt)
215 final String ft = features.get(featureIndex).getType();
216 final String type = ft == null ? lastFeatureAdded : ft;
217 if (fcol.isSimpleColour())
219 Color col = JColorChooser.showDialog(Desktop.desktop,
220 MessageManager.getString("label.select_feature_colour"),
224 fcol = new FeatureColour(col);
225 setColour(type, fcol);
226 alignPanel.paintAlignment(true, true);
227 updateColourButton(mainPanel, colour, fcol);
234 fcc = new FeatureColourChooser(me, type);
235 fcc.setRequestFocusEnabled(true);
238 fcc.addActionListener(new ActionListener()
242 public void actionPerformed(ActionEvent e)
244 fcol = fcc.getLastColour();
246 setColour(type, fcol);
247 updateColourButton(mainPanel, colour, fcol);
255 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
257 if (!create && features.size() > 1)
260 * more than one feature at selected position -
261 * add a drop-down to choose the feature to amend
262 * space pad text if necessary to make entries distinct
264 gridPanel = new JPanel(new GridLayout(4, 1));
265 JPanel choosePanel = new JPanel();
266 choosePanel.add(new JLabel(
267 MessageManager.getString("label.select_feature") + ":"));
268 final JComboBox<String> overlaps = new JComboBox<>();
269 List<String> added = new ArrayList<>();
270 for (SequenceFeature sf : features)
272 String text = String.format("%s/%d-%d (%s)", sf.getType(),
273 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
274 while (added.contains(text))
278 overlaps.addItem(text);
281 choosePanel.add(overlaps);
283 overlaps.addItemListener(new ItemListener()
286 public void itemStateChanged(ItemEvent e)
288 int index = overlaps.getSelectedIndex();
291 featureIndex = index;
292 SequenceFeature sf = features.get(index);
293 name.setText(sf.getType());
294 description.setText(sf.getDescription());
295 group.setText(sf.getFeatureGroup());
296 start.setValue(new Integer(sf.getBegin()));
297 end.setValue(new Integer(sf.getEnd()));
299 SearchResultsI highlight = new SearchResults();
300 highlight.addResult(sequences.get(0), sf.getBegin(),
303 alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(
306 FeatureColourI col = getFeatureStyle(name.getText());
309 col = new FeatureColour(
310 ColorUtils.createColourFromName(name.getText()));
313 updateColourButton(mainPanel, colour, col);
317 gridPanel.add(choosePanel);
320 JPanel namePanel = new JPanel();
321 gridPanel.add(namePanel);
322 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
326 JPanel groupPanel = new JPanel();
327 gridPanel.add(groupPanel);
328 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
330 groupPanel.add(group);
332 JPanel colourPanel = new JPanel();
333 gridPanel.add(colourPanel);
334 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
336 colourPanel.add(colour);
337 colour.setPreferredSize(new Dimension(150, 15));
338 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
339 colour.setForeground(Color.black);
340 colour.setHorizontalAlignment(SwingConstants.CENTER);
341 colour.setVerticalAlignment(SwingConstants.CENTER);
342 colour.setHorizontalTextPosition(SwingConstants.CENTER);
343 colour.setVerticalTextPosition(SwingConstants.CENTER);
344 mainPanel.add(gridPanel, BorderLayout.NORTH);
346 JPanel descriptionPanel = new JPanel();
347 descriptionPanel.add(new JLabel(
348 MessageManager.getString("label.description:"), JLabel.RIGHT));
349 description.setFont(JvSwingUtils.getTextAreaFont());
350 description.setLineWrap(true);
351 descriptionPanel.add(new JScrollPane(description));
355 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
357 JPanel startEndPanel = new JPanel();
358 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
360 startEndPanel.add(start);
361 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
363 startEndPanel.add(end);
364 mainPanel.add(startEndPanel, BorderLayout.CENTER);
368 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
372 * default feature type and group to that of the first feature supplied,
373 * or to the last feature created if not supplied (null value)
375 SequenceFeature firstFeature = features.get(0);
376 boolean useLastDefaults = firstFeature.getType() == null;
377 final String featureType = useLastDefaults ? lastFeatureAdded
378 : firstFeature.getType();
379 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
380 : firstFeature.getFeatureGroup();
381 name.setText(featureType);
382 group.setText(featureGroup);
384 start.setValue(new Integer(firstFeature.getBegin()));
385 end.setValue(new Integer(firstFeature.getEnd()));
386 description.setText(firstFeature.getDescription());
387 fcol = getFeatureStyle(featureType);
388 oldColours.put(featureType, fcol);
389 updateColourButton(mainPanel, colour, fcol);
393 options = new Object[] { MessageManager.getString("label.amend"),
394 MessageManager.getString("action.delete"),
395 MessageManager.getString("action.cancel") };
399 options = new Object[] { MessageManager.getString("action.ok"),
400 MessageManager.getString("action.cancel") };
403 String title = create
404 ? MessageManager.getString("label.create_new_sequence_features")
405 : MessageManager.formatMessage("label.amend_delete_features",
407 { sequences.get(0).getName() });
412 int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
413 mainPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
414 JvOptionPane.QUESTION_MESSAGE, null, options,
415 MessageManager.getString("action.ok"));
417 FeaturesFile ffile = new FeaturesFile();
419 final String enteredType = name.getText().trim();
420 final String enteredGroup = group.getText().trim();
421 final String enteredDescription = description.getText().replaceAll("\n", " ");
423 if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
426 * update default values only if creating using default values
430 lastFeatureAdded = enteredType;
431 lastFeatureGroupAdded = enteredGroup;
432 // TODO: determine if the null feature group is valid
433 if (lastFeatureGroupAdded.length() < 1)
435 lastFeatureGroupAdded = null;
442 SequenceFeature sf = features.get(featureIndex);
444 if (reply == JvOptionPane.NO_OPTION)
447 * NO_OPTION corresponds to the Delete button
449 sequences.get(0).getDatasetSequence().deleteFeature(sf);
450 // update Feature Settings for removal of feature / group
453 else if (reply == JvOptionPane.YES_OPTION)
456 * YES_OPTION corresponds to the Amend button
457 * need to refresh Feature Settings if type, group or colour changed;
458 * note we don't force the feature to be visible - the user has been
459 * warned if a hidden feature type or group was entered
461 boolean refreshSettings = (!featureType.equals(enteredType) || !featureGroup
462 .equals(enteredGroup));
463 refreshSettings |= (fcol != oldColours.get(featureType));
464 setColour(enteredType, fcol);
465 int newBegin = sf.begin;
469 newBegin = ((Integer) start.getValue()).intValue();
470 newEnd = ((Integer) end.getValue()).intValue();
471 } catch (NumberFormatException ex)
473 // JSpinner doesn't accept invalid format data :-)
477 * replace the feature by deleting it and adding a new one
478 * (to ensure integrity of SequenceFeatures data store)
480 sequences.get(0).deleteFeature(sf);
481 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
482 newBegin, newEnd, enteredGroup, sf.getScore());
483 newSf.setDescription(enteredDescription);
484 ffile.parseDescriptionHTML(newSf, false);
485 // amend features dialog only updates one sequence at a time
486 sequences.get(0).addSequenceFeature(newSf);
495 // Cancel: reset any colour changes applied
496 resetColours(oldColours);
500 // NEW FEATURES ADDED
502 if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
504 for (int i = 0; i < sequences.size(); i++)
506 SequenceFeature sf = features.get(i);
507 SequenceFeature sf2 = new SequenceFeature(enteredType,
508 enteredDescription, sf.getBegin(), sf.getEnd(),
510 ffile.parseDescriptionHTML(sf2, false);
511 sequences.get(i).addSequenceFeature(sf2);
514 setColour(enteredType, fcol);
518 alignPanel.paintAlignment(true, true);
524 // Cancel: reset any colour changes applied
525 resetColours(oldColours);
530 alignPanel.paintAlignment(true, true);
536 * Reset features to their original colours (on Cancel)
540 private void resetColours(Map<String, FeatureColourI> colours)
542 for (Entry<String, FeatureColourI> entry : colours.entrySet())
544 setColour(entry.getKey(), entry.getValue());
549 * Show a warning message if the entered type is one that is currently hidden
554 protected void warnIfTypeHidden(JPanel panel, String type)
556 if (getRenderOrder().contains(type))
558 if (!showFeatureOfType(type))
560 String msg = MessageManager.formatMessage("label.warning_hidden",
561 MessageManager.getString("label.feature_type"), type);
562 JvOptionPane.showMessageDialog(panel, msg, "",
563 JvOptionPane.OK_OPTION);
569 * Show a warning message if the entered group is one that is currently hidden
574 protected void warnIfGroupHidden(JPanel panel, String group)
576 if (featureGroups.containsKey(group) && !featureGroups.get(group))
578 String msg = MessageManager.formatMessage("label.warning_hidden",
579 MessageManager.getString("label.group"), group);
580 JvOptionPane.showMessageDialog(panel, msg, "",
581 JvOptionPane.OK_OPTION);
586 * update the amend feature button dependent on the given style
592 protected void updateColourButton(JPanel bigPanel, JLabel colour,
596 colour.setIcon(null);
597 colour.setToolTipText(null);
600 if (col.isSimpleColour())
602 colour.setBackground(col.getColour());
606 colour.setBackground(bigPanel.getBackground());
607 colour.setForeground(Color.black);
608 FeatureSettings.renderGraduatedColor(colour, col);
613 * Orders features in render precedence (last in order is last to render, so
614 * displayed on top of other features)
618 public void orderFeatures(Comparator<String> order)
620 Arrays.sort(renderOrder, order);