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.gui.JalviewColourChooser.ColourChooserListener;
29 import jalview.io.FeaturesFile;
30 import jalview.schemes.FeatureColour;
31 import jalview.util.ColorUtils;
32 import jalview.util.MessageManager;
34 import java.awt.BorderLayout;
35 import java.awt.Color;
36 import java.awt.Dimension;
38 import java.awt.GridLayout;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43 import java.awt.event.MouseAdapter;
44 import java.awt.event.MouseEvent;
45 import java.util.ArrayList;
46 import java.util.List;
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.SpinnerNumberModel;
56 import javax.swing.SwingConstants;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.event.DocumentEvent;
60 import javax.swing.event.DocumentListener;
63 * Provides a dialog allowing the user to add new features, or amend or delete
66 public class FeatureEditor
69 * defaults for creating a new feature are the last created
70 * feature type and group
72 static String lastFeatureAdded = "feature_1";
74 static String lastFeatureGroupAdded = "Jalview";
77 * the sequence(s) with features to be created / amended
79 final List<SequenceI> sequences;
82 * the features (or template features) to be created / amended
84 final List<SequenceFeature> features;
87 * true if the dialog is to create a new feature, false if
88 * for amend or delete of existing feature(s)
90 final boolean forCreate;
93 * index into the list of features
97 FeatureColourI oldColour;
99 FeatureColourI featureColour;
109 JTextArea description;
124 * if true create a new feature, else amend or delete an existing
127 public FeatureEditor(AlignmentPanel alignPanel, List<SequenceI> seqs,
128 List<SequenceFeature> feats, boolean create)
131 fr = alignPanel.getSeqPanel().seqCanvas.fr;
134 this.forCreate = create;
140 * Initialise the layout and controls
142 protected void init()
146 mainPanel = new JPanel(new BorderLayout());
148 name = new JTextField(25);
149 name.getDocument().addDocumentListener(new DocumentListener()
152 public void insertUpdate(DocumentEvent e)
154 warnIfTypeHidden(mainPanel, name.getText());
158 public void removeUpdate(DocumentEvent e)
160 warnIfTypeHidden(mainPanel, name.getText());
164 public void changedUpdate(DocumentEvent e)
166 warnIfTypeHidden(mainPanel, name.getText());
170 group = new JTextField(25);
171 group.getDocument().addDocumentListener(new DocumentListener()
174 public void insertUpdate(DocumentEvent e)
176 warnIfGroupHidden(mainPanel, group.getText());
180 public void removeUpdate(DocumentEvent e)
182 warnIfGroupHidden(mainPanel, group.getText());
186 public void changedUpdate(DocumentEvent e)
188 warnIfGroupHidden(mainPanel, group.getText());
192 description = new JTextArea(3, 25);
194 start = new JSpinner();
195 end = new JSpinner();
196 start.setPreferredSize(new Dimension(80, 20));
197 end.setPreferredSize(new Dimension(80, 20));
200 * ensure that start can never be more than end
202 start.addChangeListener(new ChangeListener()
205 public void stateChanged(ChangeEvent e)
207 Integer startVal = (Integer) start.getValue();
208 ((SpinnerNumberModel) end.getModel()).setMinimum(startVal);
211 end.addChangeListener(new ChangeListener()
214 public void stateChanged(ChangeEvent e)
216 Integer endVal = (Integer) end.getValue();
217 ((SpinnerNumberModel) start.getModel()).setMaximum(endVal);
221 final JLabel colour = new JLabel();
222 colour.setOpaque(true);
223 colour.setMaximumSize(new Dimension(30, 16));
224 colour.addMouseListener(new MouseAdapter()
227 public void mousePressed(MouseEvent evt)
229 if (featureColour.isSimpleColour())
232 * open colour chooser on click in colour panel
234 String title = MessageManager
235 .getString("label.select_feature_colour");
236 ColourChooserListener listener = new ColourChooserListener()
239 public void colourSelected(Color c)
241 featureColour = new FeatureColour(c);
242 updateColourButton(mainPanel, colour, featureColour);
245 JalviewColourChooser.showColourChooser(Desktop.getDesktopPane(),
246 title, featureColour.getColour(), listener);
251 * variable colour dialog - on OK, refetch the updated
252 * feature colour and update this display
254 final String ft = features.get(featureIndex).getType();
255 final String type = ft == null ? lastFeatureAdded : ft;
256 FeatureTypeSettings fcc = new FeatureTypeSettings(fr, type);
257 fcc.setRequestFocusEnabled(true);
259 fcc.addActionListener(new ActionListener()
262 public void actionPerformed(ActionEvent e)
264 featureColour = fr.getFeatureStyle(ft);
265 fr.setColour(type, featureColour);
266 updateColourButton(mainPanel, colour, featureColour);
272 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
274 if (!forCreate && features.size() > 1)
277 * more than one feature at selected position -
278 * add a drop-down to choose the feature to amend
279 * space pad text if necessary to make entries distinct
281 gridPanel = new JPanel(new GridLayout(4, 1));
282 JPanel choosePanel = new JPanel();
283 choosePanel.add(new JLabel(
284 MessageManager.getString("label.select_feature") + ":"));
285 final JComboBox<String> overlaps = new JComboBox<>();
286 List<String> added = new ArrayList<>();
287 for (SequenceFeature sf : features)
289 String text = String.format("%s/%d-%d (%s)", sf.getType(),
290 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
291 while (added.contains(text))
295 overlaps.addItem(text);
298 choosePanel.add(overlaps);
300 overlaps.addItemListener(new ItemListener()
303 public void itemStateChanged(ItemEvent e)
305 int index = overlaps.getSelectedIndex();
308 featureIndex = index;
309 SequenceFeature sf = features.get(index);
310 name.setText(sf.getType());
311 description.setText(sf.getDescription());
312 group.setText(sf.getFeatureGroup());
313 start.setValue(new Integer(sf.getBegin()));
314 end.setValue(new Integer(sf.getEnd()));
315 ((SpinnerNumberModel) start.getModel()).setMaximum(sf.getEnd());
316 ((SpinnerNumberModel) end.getModel()).setMinimum(sf.getBegin());
318 SearchResultsI highlight = new SearchResults();
319 highlight.addResult(sequences.get(0), sf.getBegin(),
322 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
324 FeatureColourI col = fr.getFeatureStyle(name.getText());
327 col = new FeatureColour(
328 ColorUtils.createColourFromName(name.getText()));
330 oldColour = featureColour = col;
331 updateColourButton(mainPanel, colour, col);
335 gridPanel.add(choosePanel);
338 JPanel namePanel = new JPanel();
339 gridPanel.add(namePanel);
340 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
344 JPanel groupPanel = new JPanel();
345 gridPanel.add(groupPanel);
346 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
348 groupPanel.add(group);
350 JPanel colourPanel = new JPanel();
351 gridPanel.add(colourPanel);
352 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
354 colourPanel.add(colour);
355 colour.setPreferredSize(new Dimension(150, 15));
356 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
357 colour.setForeground(Color.black);
358 colour.setHorizontalAlignment(SwingConstants.CENTER);
359 colour.setVerticalAlignment(SwingConstants.CENTER);
360 colour.setHorizontalTextPosition(SwingConstants.CENTER);
361 colour.setVerticalTextPosition(SwingConstants.CENTER);
362 mainPanel.add(gridPanel, BorderLayout.NORTH);
364 JPanel descriptionPanel = new JPanel();
365 descriptionPanel.add(new JLabel(
366 MessageManager.getString("label.description:"), JLabel.RIGHT));
367 description.setFont(JvSwingUtils.getTextAreaFont());
368 description.setLineWrap(true);
369 descriptionPanel.add(new JScrollPane(description));
373 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
375 JPanel startEndPanel = new JPanel();
376 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
378 startEndPanel.add(start);
379 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
381 startEndPanel.add(end);
382 mainPanel.add(startEndPanel, BorderLayout.CENTER);
386 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
390 * default feature type and group to that of the first feature supplied,
391 * or to the last feature created if not supplied (null value)
393 SequenceFeature firstFeature = features.get(0);
394 boolean useLastDefaults = firstFeature.getType() == null;
395 final String featureType = useLastDefaults ? lastFeatureAdded
396 : firstFeature.getType();
397 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
398 : firstFeature.getFeatureGroup();
399 name.setText(featureType);
400 group.setText(featureGroup);
402 start.setValue(new Integer(firstFeature.getBegin()));
403 end.setValue(new Integer(firstFeature.getEnd()));
404 ((SpinnerNumberModel) start.getModel()).setMaximum(firstFeature.getEnd());
405 ((SpinnerNumberModel) end.getModel()).setMinimum(firstFeature.getBegin());
407 description.setText(firstFeature.getDescription());
408 featureColour = fr.getFeatureStyle(featureType);
409 oldColour = featureColour;
410 updateColourButton(mainPanel, colour, oldColour);
414 * Presents a dialog allowing the user to add new features, or amend or delete
415 * an existing feature. Currently this can be on
417 * <li>double-click on a sequence - Amend/Delete a selected feature at the
419 * <li>Create sequence feature(s) from pop-up menu on selected region</li>
420 * <li>Create features for pattern matches from Find</li>
422 * If the supplied feature type is null, show (and update on confirm) the type
423 * and group of the last new feature created (with initial defaults of
424 * "feature_1" and "Jalview").
426 public void showDialog()
428 Runnable okAction = forCreate ? getCreateAction() : getAmendAction();
429 Runnable cancelAction = getCancelAction();
432 * set dialog action handlers for OK (create/Amend) and Cancel options
433 * also for Delete if applicable (when amending features)
435 JvOptionPane dialog = JvOptionPane.newOptionDialog(ap.alignFrame)
436 .setResponseHandler(0, okAction)
437 .setResponseHandler(2, cancelAction);
440 dialog.setResponseHandler(1, getDeleteAction());
444 Object[] options = null;
447 title = MessageManager
448 .getString("label.create_new_sequence_features");
449 options = new Object[] { MessageManager.getString("action.ok"),
450 MessageManager.getString("action.cancel") };
454 title = MessageManager.formatMessage("label.amend_delete_features",
456 { sequences.get(0).getName() });
457 options = new Object[] { MessageManager.getString("label.amend"),
458 MessageManager.getString("action.delete"),
459 MessageManager.getString("action.cancel") };
462 dialog.showInternalDialog(mainPanel, title,
463 JvOptionPane.YES_NO_CANCEL_OPTION,
464 JvOptionPane.PLAIN_MESSAGE, null, options,
465 MessageManager.getString("action.ok"));
469 * Answers an action to run on Cancel in the dialog. This is just to remove
470 * any feature highlighting from the display. Changes in the dialog are not
471 * applied until it is dismissed with OK, Amend or Delete, so there are no
472 * updates to reset on Cancel.
476 protected Runnable getCancelAction()
478 Runnable okAction = new Runnable()
483 ap.highlightSearchResults(null);
484 ap.paintAlignment(false, false);
491 * Returns the action to be run on OK in the dialog when creating one or more
492 * sequence features. Note these may have a pre-supplied feature type (such as
493 * a Find pattern), or none, in which case the feature type and group default
494 * to those last added through this dialog. The action includes refreshing the
495 * Feature Settings panel (if it is open), to show any new feature type, or
496 * amended colour for an existing type.
500 protected Runnable getCreateAction()
502 Runnable okAction = new Runnable()
504 boolean useLastDefaults = features.get(0).getType() == null;
509 final String enteredType = name.getText().trim();
510 final String enteredGroup = group.getText().trim();
511 final String enteredDescription = description.getText()
512 .replaceAll("\n", " ");
513 if (enteredType.length() > 0)
516 * update default values only if creating using default values
520 lastFeatureAdded = enteredType;
521 lastFeatureGroupAdded = enteredGroup;
522 // TODO: determine if the null feature group is valid
523 if (lastFeatureGroupAdded.length() < 1)
525 lastFeatureGroupAdded = null;
530 if (enteredType.length() > 0)
532 for (int i = 0; i < sequences.size(); i++)
534 SequenceFeature sf = features.get(i);
535 SequenceFeature sf2 = new SequenceFeature(enteredType,
536 enteredDescription, sf.getBegin(), sf.getEnd(),
538 new FeaturesFile().parseDescriptionHTML(sf2, false);
539 sequences.get(i).addSequenceFeature(sf2);
542 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 Runnable getDeleteAction()
561 Runnable deleteAction = new Runnable()
566 SequenceFeature sf = features.get(featureIndex);
567 sequences.get(0).getDatasetSequence().deleteFeature(sf);
569 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
570 ap.paintAlignment(true, true);
577 * update the amend feature button dependent on the given style
583 protected void updateColourButton(JPanel bigPanel, JLabel colour,
587 colour.setIcon(null);
590 if (col.isSimpleColour())
592 colour.setToolTipText(null);
593 colour.setBackground(col.getColour());
597 colour.setBackground(bigPanel.getBackground());
598 colour.setForeground(Color.black);
599 colour.setToolTipText(FeatureSettings.getColorTooltip(col, false));
600 FeatureSettings.renderGraduatedColor(colour, col);
605 * Show a warning message if the entered group is one that is currently hidden
610 protected void warnIfGroupHidden(JPanel panel, String group)
612 if (!fr.isGroupVisible(group))
614 String msg = MessageManager.formatMessage("label.warning_hidden",
615 MessageManager.getString("label.group"), group);
616 JvOptionPane.showMessageDialog(panel, msg, "",
617 JvOptionPane.OK_OPTION);
622 * Show a warning message if the entered type is one that is currently hidden
627 protected void warnIfTypeHidden(JPanel panel, String type)
629 if (fr.getRenderOrder().contains(type))
631 if (!fr.showFeatureOfType(type))
633 String msg = MessageManager.formatMessage("label.warning_hidden",
634 MessageManager.getString("label.feature_type"), type);
635 JvOptionPane.showMessageDialog(panel, msg, "",
636 JvOptionPane.OK_OPTION);
642 * On closing the dialog - ensure feature display is turned on, to show any
643 * new features - remove highlighting of the last selected feature - repaint
644 * the panel to show any changes
646 protected void repaintPanel()
648 ap.alignFrame.showSeqFeatures.setSelected(true);
649 ap.av.setShowSequenceFeatures(true);
650 ap.av.setSearchResults(null);
651 ap.paintAlignment(true, true);
655 * Returns the action to be run on OK in the dialog when amending a feature.
656 * Note this may include refreshing the Feature Settings panel (if it is
657 * open), if feature type, group or colour has changed (but not for
658 * description or extent).
662 protected Runnable getAmendAction()
664 Runnable okAction = new Runnable()
666 boolean useLastDefaults = features.get(0).getType() == null;
668 String featureType = name.getText();
670 String featureGroup = group.getText();
674 final String enteredType = name.getText().trim();
675 final String enteredGroup = group.getText().trim();
676 final String enteredDescription = description.getText()
677 .replaceAll("\n", " ");
678 if (enteredType.length() > 0)
682 * update default values only if creating using default values
686 lastFeatureAdded = enteredType;
687 lastFeatureGroupAdded = enteredGroup;
688 // TODO: determine if the null feature group is valid
689 if (lastFeatureGroupAdded.length() < 1)
691 lastFeatureGroupAdded = null;
696 SequenceFeature sf = features.get(featureIndex);
699 * Need to refresh Feature Settings if type, group or colour changed;
700 * note we don't force the feature to be visible - the user has been
701 * warned if a hidden feature type or group was entered
703 boolean refreshSettings = (!featureType.equals(enteredType)
704 || !featureGroup.equals(enteredGroup));
705 refreshSettings |= (featureColour != oldColour);
706 fr.setColour(enteredType, featureColour);
707 int newBegin = sf.begin;
711 newBegin = ((Integer) start.getValue()).intValue();
712 newEnd = ((Integer) end.getValue()).intValue();
713 } catch (NumberFormatException ex)
715 // JSpinner doesn't accept invalid format data :-)
719 * 'amend' the feature by deleting it and adding a new one
720 * (to ensure integrity of SequenceFeatures data store)
721 * note this dialog only updates one sequence at a time
723 sequences.get(0).deleteFeature(sf);
724 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
725 newBegin, newEnd, enteredGroup, sf.getScore());
726 newSf.setDescription(enteredDescription);
727 new FeaturesFile().parseDescriptionHTML(newSf, false);
728 sequences.get(0).addSequenceFeature(newSf);