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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
27 import java.awt.GridLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.ItemEvent;
31 import java.awt.event.ItemListener;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.Callable;
38 import javax.swing.JComboBox;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JSpinner;
43 import javax.swing.JTextArea;
44 import javax.swing.JTextField;
45 import javax.swing.SpinnerNumberModel;
46 import javax.swing.SwingConstants;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
49 import javax.swing.event.DocumentEvent;
50 import javax.swing.event.DocumentListener;
52 import jalview.api.FeatureColourI;
53 import jalview.datamodel.SearchResults;
54 import jalview.datamodel.SearchResultsI;
55 import jalview.datamodel.SequenceFeature;
56 import jalview.datamodel.SequenceI;
57 import jalview.gui.JalviewColourChooser.ColourChooserListener;
58 import jalview.io.FeaturesFile;
59 import jalview.schemes.FeatureColour;
60 import jalview.util.ColorUtils;
61 import jalview.util.MessageManager;
64 * Provides a dialog allowing the user to add new features, or amend or delete
67 public class FeatureEditor
70 * defaults for creating a new feature are the last created
71 * feature type and group
73 static String lastFeatureAdded = "feature_1";
75 static String lastFeatureGroupAdded = "Jalview";
78 * the sequence(s) with features to be created / amended
80 final List<SequenceI> sequences;
83 * the features (or template features) to be created / amended
85 final List<SequenceFeature> features;
88 * true if the dialog is to create a new feature, false if
89 * for amend or delete of existing feature(s)
91 final boolean forCreate;
94 * index into the list of features
98 FeatureColourI oldColour;
100 FeatureColourI featureColour;
110 JTextArea description;
125 * if true create a new feature, else amend or delete an existing
128 public FeatureEditor(AlignmentPanel alignPanel, List<SequenceI> seqs,
129 List<SequenceFeature> feats, boolean create)
132 fr = alignPanel.getSeqPanel().seqCanvas.fr;
135 this.forCreate = create;
141 * Initialise the layout and controls
143 protected void init()
147 mainPanel = new JPanel(new BorderLayout());
149 name = new JTextField(25);
150 name.getDocument().addDocumentListener(new DocumentListener()
153 public void insertUpdate(DocumentEvent e)
155 warnIfTypeHidden(mainPanel, name.getText());
159 public void removeUpdate(DocumentEvent e)
161 warnIfTypeHidden(mainPanel, name.getText());
165 public void changedUpdate(DocumentEvent e)
167 warnIfTypeHidden(mainPanel, name.getText());
171 group = new JTextField(25);
172 group.getDocument().addDocumentListener(new DocumentListener()
175 public void insertUpdate(DocumentEvent e)
177 warnIfGroupHidden(mainPanel, group.getText());
181 public void removeUpdate(DocumentEvent e)
183 warnIfGroupHidden(mainPanel, group.getText());
187 public void changedUpdate(DocumentEvent e)
189 warnIfGroupHidden(mainPanel, group.getText());
193 description = new JTextArea(3, 25);
195 start = new JSpinner();
196 end = new JSpinner();
197 start.setPreferredSize(new Dimension(80, 20));
198 end.setPreferredSize(new Dimension(80, 20));
201 * ensure that start can never be more than end
203 start.addChangeListener(new ChangeListener()
206 public void stateChanged(ChangeEvent e)
208 Integer startVal = (Integer) start.getValue();
209 ((SpinnerNumberModel) end.getModel()).setMinimum(startVal);
212 end.addChangeListener(new ChangeListener()
215 public void stateChanged(ChangeEvent e)
217 Integer endVal = (Integer) end.getValue();
218 ((SpinnerNumberModel) start.getModel()).setMaximum(endVal);
222 final JLabel colour = new JLabel();
223 colour.setOpaque(true);
224 colour.setMaximumSize(new Dimension(30, 16));
225 colour.addMouseListener(new MouseAdapter()
228 public void mousePressed(MouseEvent evt)
230 if (featureColour.isSimpleColour())
233 * open colour chooser on click in colour panel
235 String title = MessageManager
236 .getString("label.select_feature_colour");
237 ColourChooserListener listener = new ColourChooserListener()
240 public void colourSelected(Color c)
242 featureColour = new FeatureColour(c);
243 updateColourButton(mainPanel, colour, featureColour);
246 JalviewColourChooser.showColourChooser(Desktop.getDesktop(),
247 title, featureColour.getColour(), listener);
252 * variable colour dialog - on OK, refetch the updated
253 * feature colour and update this display
255 final String ft = features.get(featureIndex).getType();
256 final String type = ft == null ? lastFeatureAdded : ft;
257 FeatureTypeSettings fcc = new FeatureTypeSettings(fr, type);
258 fcc.setRequestFocusEnabled(true);
260 fcc.addActionListener(new ActionListener()
263 public void actionPerformed(ActionEvent e)
265 featureColour = fr.getFeatureStyle(ft);
266 fr.setColour(type, featureColour);
267 updateColourButton(mainPanel, colour, featureColour);
273 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
275 if (!forCreate && features.size() > 1)
278 * more than one feature at selected position -
279 * add a drop-down to choose the feature to amend
280 * space pad text if necessary to make entries distinct
282 gridPanel = new JPanel(new GridLayout(4, 1));
283 JPanel choosePanel = new JPanel();
284 choosePanel.add(new JLabel(
285 MessageManager.getString("label.select_feature") + ":"));
286 final JComboBox<String> overlaps = new JComboBox<>();
287 List<String> added = new ArrayList<>();
288 for (SequenceFeature sf : features)
290 String text = String.format("%s/%d-%d (%s)", sf.getType(),
291 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
292 while (added.contains(text))
296 overlaps.addItem(text);
299 choosePanel.add(overlaps);
301 overlaps.addItemListener(new ItemListener()
304 public void itemStateChanged(ItemEvent e)
306 int index = overlaps.getSelectedIndex();
309 featureIndex = index;
310 SequenceFeature sf = features.get(index);
311 name.setText(sf.getType());
312 description.setText(sf.getDescription());
313 group.setText(sf.getFeatureGroup());
314 start.setValue(new Integer(sf.getBegin()));
315 end.setValue(new Integer(sf.getEnd()));
316 ((SpinnerNumberModel) start.getModel()).setMaximum(sf.getEnd());
317 ((SpinnerNumberModel) end.getModel()).setMinimum(sf.getBegin());
319 SearchResultsI highlight = new SearchResults();
320 highlight.addResult(sequences.get(0), sf.getBegin(),
323 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
325 FeatureColourI col = fr.getFeatureStyle(name.getText());
328 col = new FeatureColour(
329 ColorUtils.createColourFromName(name.getText()));
331 oldColour = featureColour = col;
332 updateColourButton(mainPanel, colour, col);
336 gridPanel.add(choosePanel);
339 JPanel namePanel = new JPanel();
340 gridPanel.add(namePanel);
341 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
345 JPanel groupPanel = new JPanel();
346 gridPanel.add(groupPanel);
347 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
349 groupPanel.add(group);
351 JPanel colourPanel = new JPanel();
352 gridPanel.add(colourPanel);
353 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
355 colourPanel.add(colour);
356 colour.setPreferredSize(new Dimension(150, 15));
357 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
358 colour.setForeground(Color.black);
359 colour.setHorizontalAlignment(SwingConstants.CENTER);
360 colour.setVerticalAlignment(SwingConstants.CENTER);
361 colour.setHorizontalTextPosition(SwingConstants.CENTER);
362 colour.setVerticalTextPosition(SwingConstants.CENTER);
363 mainPanel.add(gridPanel, BorderLayout.NORTH);
365 JPanel descriptionPanel = new JPanel();
366 descriptionPanel.add(new JLabel(
367 MessageManager.getString("label.description:"), JLabel.RIGHT));
368 description.setFont(JvSwingUtils.getTextAreaFont());
369 description.setLineWrap(true);
370 descriptionPanel.add(new JScrollPane(description));
374 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
376 JPanel startEndPanel = new JPanel();
377 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
379 startEndPanel.add(start);
380 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
382 startEndPanel.add(end);
383 mainPanel.add(startEndPanel, BorderLayout.CENTER);
387 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
391 * default feature type and group to that of the first feature supplied,
392 * or to the last feature created if not supplied (null value)
394 SequenceFeature firstFeature = features.get(0);
395 boolean useLastDefaults = firstFeature.getType() == null;
396 final String featureType = useLastDefaults ? lastFeatureAdded
397 : firstFeature.getType();
398 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
399 : firstFeature.getFeatureGroup();
400 name.setText(featureType);
401 group.setText(featureGroup);
403 start.setValue(new Integer(firstFeature.getBegin()));
404 end.setValue(new Integer(firstFeature.getEnd()));
405 ((SpinnerNumberModel) start.getModel())
406 .setMaximum(firstFeature.getEnd());
407 ((SpinnerNumberModel) end.getModel())
408 .setMinimum(firstFeature.getBegin());
410 description.setText(firstFeature.getDescription());
411 featureColour = fr.getFeatureStyle(featureType);
412 oldColour = featureColour;
413 updateColourButton(mainPanel, colour, oldColour);
417 * Presents a dialog allowing the user to add new features, or amend or delete
418 * an existing feature. Currently this can be on
420 * <li>double-click on a sequence - Amend/Delete a selected feature at the
422 * <li>Create sequence feature(s) from pop-up menu on selected region</li>
423 * <li>Create features for pattern matches from Find</li>
425 * If the supplied feature type is null, show (and update on confirm) the type
426 * and group of the last new feature created (with initial defaults of
427 * "feature_1" and "Jalview").
429 public void showDialog()
431 Callable<Void> okAction = forCreate ? getCreateAction()
433 Callable<Void> cancelAction = getCancelAction();
436 * set dialog action handlers for OK (create/Amend) and Cancel options
437 * also for Delete if applicable (when amending features)
439 JvOptionPane dialog = JvOptionPane.newOptionDialog(ap.alignFrame)
440 .setResponseHandler(0, okAction)
441 .setResponseHandler(2, cancelAction);
444 dialog.setResponseHandler(1, getDeleteAction());
448 Object[] options = null;
451 title = MessageManager
452 .getString("label.create_new_sequence_features");
453 options = new Object[] { MessageManager.getString("action.ok"),
454 MessageManager.getString("action.cancel") };
458 title = MessageManager.formatMessage("label.amend_delete_features",
460 { sequences.get(0).getName() });
461 options = new Object[] { MessageManager.getString("label.amend"),
462 MessageManager.getString("action.delete"),
463 MessageManager.getString("action.cancel") };
466 dialog.showInternalDialog(mainPanel, title,
467 JvOptionPane.YES_NO_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE,
468 null, options, MessageManager.getString("action.ok"));
472 * Answers an action to run on Cancel in the dialog. This is just to remove
473 * any feature highlighting from the display. Changes in the dialog are not
474 * applied until it is dismissed with OK, Amend or Delete, so there are no
475 * updates to reset on Cancel.
479 protected Callable getCancelAction()
481 Callable<Void> okAction = () -> {
482 ap.highlightSearchResults(null);
483 ap.paintAlignment(false, false);
490 * Returns the action to be run on OK in the dialog when creating one or more
491 * sequence features. Note these may have a pre-supplied feature type (such as
492 * a Find pattern), or none, in which case the feature type and group default
493 * to those last added through this dialog. The action includes refreshing the
494 * Feature Settings panel (if it is open), to show any new feature type, or
495 * amended colour for an existing type.
499 protected Callable getCreateAction()
501 Callable<Void> okAction = new Callable()
503 boolean useLastDefaults = features.get(0).getType() == null;
508 final String enteredType = name.getText().trim();
509 final String enteredGroup = group.getText().trim();
510 final String enteredDescription = description.getText()
511 .replaceAll("\n", " ");
512 if (enteredType.length() > 0)
515 * update default values only if creating using default values
519 lastFeatureAdded = enteredType;
520 lastFeatureGroupAdded = enteredGroup;
521 // TODO: determine if the null feature group is valid
522 if (lastFeatureGroupAdded.length() < 1)
524 lastFeatureGroupAdded = null;
529 if (enteredType.length() > 0)
531 for (int i = 0; i < sequences.size(); i++)
533 SequenceFeature sf = features.get(i);
534 SequenceFeature sf2 = new SequenceFeature(enteredType,
535 enteredDescription, sf.getBegin(), sf.getEnd(),
537 new FeaturesFile().parseDescriptionHTML(sf2, false);
538 sequences.get(i).addSequenceFeature(sf2);
541 fr.setColour(enteredType, featureColour);
553 * Answers the action to run on Delete in the dialog. Note this includes
554 * refreshing the Feature Settings (if open) in case the only instance of a
555 * feature type or group has been deleted.
559 protected Callable getDeleteAction()
561 Callable<Void> deleteAction = () -> {
562 SequenceFeature sf = features.get(featureIndex);
563 sequences.get(0).getDatasetSequence().deleteFeature(sf);
565 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
566 ap.paintAlignment(true, true);
573 * update the amend feature button dependent on the given style
579 protected void updateColourButton(JPanel bigPanel, JLabel colour,
583 colour.setIcon(null);
586 if (col.isSimpleColour())
588 colour.setToolTipText(null);
589 colour.setBackground(col.getColour());
593 colour.setBackground(bigPanel.getBackground());
594 colour.setForeground(Color.black);
595 colour.setToolTipText(FeatureSettings.getColorTooltip(col, false));
596 FeatureSettings.renderGraduatedColor(colour, col);
601 * Show a warning message if the entered group is one that is currently hidden
606 protected void warnIfGroupHidden(JPanel panel, String group)
608 if (!fr.isGroupVisible(group))
610 String msg = MessageManager.formatMessage("label.warning_hidden",
611 MessageManager.getString("label.group"), group);
612 JvOptionPane.showMessageDialog(panel, msg, "",
613 JvOptionPane.OK_OPTION);
618 * Show a warning message if the entered type is one that is currently hidden
623 protected void warnIfTypeHidden(JPanel panel, String type)
625 if (fr.getRenderOrder().contains(type))
627 if (!fr.showFeatureOfType(type))
629 String msg = MessageManager.formatMessage("label.warning_hidden",
630 MessageManager.getString("label.feature_type"), type);
631 JvOptionPane.showMessageDialog(panel, msg, "",
632 JvOptionPane.OK_OPTION);
638 * On closing the dialog - ensure feature display is turned on, to show any
639 * new features - remove highlighting of the last selected feature - repaint
640 * the panel to show any changes
642 protected void repaintPanel()
644 ap.alignFrame.showSeqFeatures.setSelected(true);
645 ap.av.setShowSequenceFeatures(true);
646 ap.av.setSearchResults(null);
647 ap.paintAlignment(true, true);
651 * Returns the action to be run on OK in the dialog when amending a feature.
652 * Note this may include refreshing the Feature Settings panel (if it is
653 * open), if feature type, group or colour has changed (but not for
654 * description or extent).
658 protected Callable getAmendAction()
660 Callable<Void> okAction = new Callable()
662 boolean useLastDefaults = features.get(0).getType() == null;
664 String featureType = name.getText();
666 String featureGroup = group.getText();
671 final String enteredType = name.getText().trim();
672 final String enteredGroup = group.getText().trim();
673 final String enteredDescription = description.getText()
674 .replaceAll("\n", " ");
675 if (enteredType.length() > 0)
679 * update default values only if creating using default values
683 lastFeatureAdded = enteredType;
684 lastFeatureGroupAdded = enteredGroup;
685 // TODO: determine if the null feature group is valid
686 if (lastFeatureGroupAdded.length() < 1)
688 lastFeatureGroupAdded = null;
693 SequenceFeature sf = features.get(featureIndex);
696 * Need to refresh Feature Settings if type, group or colour changed;
697 * note we don't force the feature to be visible - the user has been
698 * warned if a hidden feature type or group was entered
700 boolean refreshSettings = (!featureType.equals(enteredType)
701 || !featureGroup.equals(enteredGroup));
702 refreshSettings |= (featureColour != oldColour);
703 fr.setColour(enteredType, featureColour);
704 int newBegin = sf.begin;
708 newBegin = ((Integer) start.getValue()).intValue();
709 newEnd = ((Integer) end.getValue()).intValue();
710 } catch (NumberFormatException ex)
712 // JSpinner doesn't accept invalid format data :-)
716 * 'amend' the feature by deleting it and adding a new one
717 * (to ensure integrity of SequenceFeatures data store)
718 * note this dialog only updates one sequence at a time
720 sequences.get(0).deleteFeature(sf);
721 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
722 newBegin, newEnd, enteredGroup, sf.getScore());
723 newSf.setDescription(enteredDescription);
724 new FeaturesFile().parseDescriptionHTML(newSf, false);
725 sequences.get(0).addSequenceFeature(newSf);