3 import jalview.api.FeatureColourI;
4 import jalview.datamodel.SearchResults;
5 import jalview.datamodel.SearchResultsI;
6 import jalview.datamodel.SequenceFeature;
7 import jalview.datamodel.SequenceI;
8 import jalview.gui.JalviewColourChooser.ColourChooserListener;
9 import jalview.io.FeaturesFile;
10 import jalview.schemes.FeatureColour;
11 import jalview.util.ColorUtils;
12 import jalview.util.MessageManager;
14 import java.awt.BorderLayout;
15 import java.awt.Color;
16 import java.awt.Dimension;
18 import java.awt.GridLayout;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.awt.event.ItemEvent;
22 import java.awt.event.ItemListener;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.swing.JComboBox;
29 import javax.swing.JLabel;
30 import javax.swing.JPanel;
31 import javax.swing.JScrollPane;
32 import javax.swing.JSpinner;
33 import javax.swing.JTextArea;
34 import javax.swing.JTextField;
35 import javax.swing.SpinnerNumberModel;
36 import javax.swing.SwingConstants;
37 import javax.swing.event.ChangeEvent;
38 import javax.swing.event.ChangeListener;
39 import javax.swing.event.DocumentEvent;
40 import javax.swing.event.DocumentListener;
43 * Provides a dialog allowing the user to add new features, or amend or delete
46 public class FeatureEditor
49 * defaults for creating a new feature are the last created
50 * feature type and group
52 static String lastFeatureAdded = "feature_1";
54 static String lastFeatureGroupAdded = "Jalview";
57 * the sequence(s) with features to be created / amended
59 final List<SequenceI> sequences;
62 * the features (or template features) to be created / amended
64 final List<SequenceFeature> features;
67 * true if the dialog is to create a new feature, false if
68 * for amend or delete of existing feature(s)
70 final boolean forCreate;
73 * index into the list of features
77 FeatureColourI oldColour;
79 FeatureColourI featureColour;
89 JTextArea description;
104 * if true create a new feature, else amend or delete an existing
107 public FeatureEditor(AlignmentPanel alignPanel, List<SequenceI> seqs,
108 List<SequenceFeature> feats, boolean create)
111 fr = alignPanel.getSeqPanel().seqCanvas.fr;
114 this.forCreate = create;
120 * Initialise the layout and controls
122 protected void init()
126 mainPanel = new JPanel(new BorderLayout());
128 name = new JTextField(25);
129 name.getDocument().addDocumentListener(new DocumentListener()
132 public void insertUpdate(DocumentEvent e)
134 warnIfTypeHidden(mainPanel, name.getText());
138 public void removeUpdate(DocumentEvent e)
140 warnIfTypeHidden(mainPanel, name.getText());
144 public void changedUpdate(DocumentEvent e)
146 warnIfTypeHidden(mainPanel, name.getText());
150 group = new JTextField(25);
151 group.getDocument().addDocumentListener(new DocumentListener()
154 public void insertUpdate(DocumentEvent e)
156 warnIfGroupHidden(mainPanel, group.getText());
160 public void removeUpdate(DocumentEvent e)
162 warnIfGroupHidden(mainPanel, group.getText());
166 public void changedUpdate(DocumentEvent e)
168 warnIfGroupHidden(mainPanel, group.getText());
172 description = new JTextArea(3, 25);
174 start = new JSpinner();
175 end = new JSpinner();
176 start.setPreferredSize(new Dimension(80, 20));
177 end.setPreferredSize(new Dimension(80, 20));
180 * ensure that start can never be more than end
182 start.addChangeListener(new ChangeListener()
185 public void stateChanged(ChangeEvent e)
187 Integer startVal = (Integer) start.getValue();
188 ((SpinnerNumberModel) end.getModel()).setMinimum(startVal);
191 end.addChangeListener(new ChangeListener()
194 public void stateChanged(ChangeEvent e)
196 Integer endVal = (Integer) end.getValue();
197 ((SpinnerNumberModel) start.getModel()).setMaximum(endVal);
201 final JLabel colour = new JLabel();
202 colour.setOpaque(true);
203 colour.setMaximumSize(new Dimension(30, 16));
204 colour.addMouseListener(new MouseAdapter()
207 public void mousePressed(MouseEvent evt)
209 if (featureColour.isSimpleColour())
212 * open colour chooser on click in colour panel
214 String title = MessageManager
215 .getString("label.select_feature_colour");
216 ColourChooserListener listener = new ColourChooserListener()
219 public void colourSelected(Color c)
221 featureColour = new FeatureColour(c);
222 updateColourButton(mainPanel, colour, featureColour);
225 JalviewColourChooser.showColourChooser(Desktop.getDesktop(),
226 title, featureColour.getColour(), listener);
231 * variable colour dialog - on OK, refetch the updated
232 * feature colour and update this display
234 final String ft = features.get(featureIndex).getType();
235 final String type = ft == null ? lastFeatureAdded : ft;
236 FeatureTypeSettings fcc = new FeatureTypeSettings(fr, type);
237 fcc.setRequestFocusEnabled(true);
239 fcc.addActionListener(new ActionListener()
242 public void actionPerformed(ActionEvent e)
244 featureColour = fr.getFeatureStyle(ft);
245 fr.setColour(type, featureColour);
246 updateColourButton(mainPanel, colour, featureColour);
252 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
254 if (!forCreate && features.size() > 1)
257 * more than one feature at selected position -
258 * add a drop-down to choose the feature to amend
259 * space pad text if necessary to make entries distinct
261 gridPanel = new JPanel(new GridLayout(4, 1));
262 JPanel choosePanel = new JPanel();
263 choosePanel.add(new JLabel(
264 MessageManager.getString("label.select_feature") + ":"));
265 final JComboBox<String> overlaps = new JComboBox<>();
266 List<String> added = new ArrayList<>();
267 for (SequenceFeature sf : features)
269 String text = String.format("%s/%d-%d (%s)", sf.getType(),
270 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
271 while (added.contains(text))
275 overlaps.addItem(text);
278 choosePanel.add(overlaps);
280 overlaps.addItemListener(new ItemListener()
283 public void itemStateChanged(ItemEvent e)
285 int index = overlaps.getSelectedIndex();
288 featureIndex = index;
289 SequenceFeature sf = features.get(index);
290 name.setText(sf.getType());
291 description.setText(sf.getDescription());
292 group.setText(sf.getFeatureGroup());
293 start.setValue(new Integer(sf.getBegin()));
294 end.setValue(new Integer(sf.getEnd()));
295 ((SpinnerNumberModel) start.getModel()).setMaximum(sf.getEnd());
296 ((SpinnerNumberModel) end.getModel()).setMinimum(sf.getBegin());
298 SearchResultsI highlight = new SearchResults();
299 highlight.addResult(sequences.get(0), sf.getBegin(),
302 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
304 FeatureColourI col = fr.getFeatureStyle(name.getText());
307 col = new FeatureColour(
308 ColorUtils.createColourFromName(name.getText()));
310 oldColour = featureColour = col;
311 updateColourButton(mainPanel, colour, col);
315 gridPanel.add(choosePanel);
318 JPanel namePanel = new JPanel();
319 gridPanel.add(namePanel);
320 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
324 JPanel groupPanel = new JPanel();
325 gridPanel.add(groupPanel);
326 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
328 groupPanel.add(group);
330 JPanel colourPanel = new JPanel();
331 gridPanel.add(colourPanel);
332 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
334 colourPanel.add(colour);
335 colour.setPreferredSize(new Dimension(150, 15));
336 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
337 colour.setForeground(Color.black);
338 colour.setHorizontalAlignment(SwingConstants.CENTER);
339 colour.setVerticalAlignment(SwingConstants.CENTER);
340 colour.setHorizontalTextPosition(SwingConstants.CENTER);
341 colour.setVerticalTextPosition(SwingConstants.CENTER);
342 mainPanel.add(gridPanel, BorderLayout.NORTH);
344 JPanel descriptionPanel = new JPanel();
345 descriptionPanel.add(new JLabel(
346 MessageManager.getString("label.description:"), JLabel.RIGHT));
347 description.setFont(JvSwingUtils.getTextAreaFont());
348 description.setLineWrap(true);
349 descriptionPanel.add(new JScrollPane(description));
353 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
355 JPanel startEndPanel = new JPanel();
356 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
358 startEndPanel.add(start);
359 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
361 startEndPanel.add(end);
362 mainPanel.add(startEndPanel, BorderLayout.CENTER);
366 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
370 * default feature type and group to that of the first feature supplied,
371 * or to the last feature created if not supplied (null value)
373 SequenceFeature firstFeature = features.get(0);
374 boolean useLastDefaults = firstFeature.getType() == null;
375 final String featureType = useLastDefaults ? lastFeatureAdded
376 : firstFeature.getType();
377 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
378 : firstFeature.getFeatureGroup();
379 name.setText(featureType);
380 group.setText(featureGroup);
382 start.setValue(new Integer(firstFeature.getBegin()));
383 end.setValue(new Integer(firstFeature.getEnd()));
384 ((SpinnerNumberModel) start.getModel()).setMaximum(firstFeature.getEnd());
385 ((SpinnerNumberModel) end.getModel()).setMinimum(firstFeature.getBegin());
387 description.setText(firstFeature.getDescription());
388 featureColour = fr.getFeatureStyle(featureType);
389 oldColour = featureColour;
390 updateColourButton(mainPanel, colour, oldColour);
394 * Presents a dialog allowing the user to add new features, or amend or delete
395 * an existing feature. Currently this can be on
397 * <li>double-click on a sequence - Amend/Delete a selected feature at the
399 * <li>Create sequence feature(s) from pop-up menu on selected region</li>
400 * <li>Create features for pattern matches from Find</li>
402 * If the supplied feature type is null, show (and update on confirm) the type
403 * and group of the last new feature created (with initial defaults of
404 * "feature_1" and "Jalview").
406 public void showDialog()
408 Runnable okAction = forCreate ? getCreateAction() : getAmendAction();
409 Runnable cancelAction = getCancelAction();
412 * set dialog action handlers for OK (create/Amend) and Cancel options
413 * also for Delete if applicable (when amending features)
415 JvOptionPane dialog = JvOptionPane.newOptionDialog(Desktop.desktop)
416 .setResponseHandler(0, okAction).setResponseHandler(2, cancelAction);
419 dialog.setResponseHandler(1, getDeleteAction());
423 Object[] options = null;
426 title = MessageManager
427 .getString("label.create_new_sequence_features");
428 options = new Object[] { MessageManager.getString("action.ok"),
429 MessageManager.getString("action.cancel") };
433 title = MessageManager.formatMessage("label.amend_delete_features",
435 { sequences.get(0).getName() });
436 options = new Object[] { MessageManager.getString("label.amend"),
437 MessageManager.getString("action.delete"),
438 MessageManager.getString("action.cancel") };
441 dialog.showInternalDialog(mainPanel, title,
442 JvOptionPane.YES_NO_CANCEL_OPTION,
443 JvOptionPane.PLAIN_MESSAGE, null, options,
444 MessageManager.getString("action.ok"));
448 * Answers an action to run on Cancel in the dialog. This is just to remove
449 * any feature highlighting from the display. Changes in the dialog are not
450 * applied until it is dismissed with OK, Amend or Delete, so there are no
451 * updates to reset on Cancel.
455 protected Runnable getCancelAction()
457 Runnable okAction = new Runnable()
462 ap.highlightSearchResults(null);
463 ap.paintAlignment(false, false);
470 * Returns the action to be run on OK in the dialog when creating one or more
471 * sequence features. Note these may have a pre-supplied feature type (such as
472 * a Find pattern), or none, in which case the feature type and group default
473 * to those last added through this dialog. The action includes refreshing the
474 * Feature Settings panel (if it is open), to show any new feature type, or
475 * amended colour for an existing type.
479 protected Runnable getCreateAction()
481 Runnable okAction = new Runnable()
483 boolean useLastDefaults = features.get(0).getType() == null;
487 final String enteredType = name.getText().trim();
488 final String enteredGroup = group.getText().trim();
489 final String enteredDescription = description.getText()
490 .replaceAll("\n", " ");
491 if (enteredType.length() > 0)
494 * update default values only if creating using default values
498 lastFeatureAdded = enteredType;
499 lastFeatureGroupAdded = enteredGroup;
500 // TODO: determine if the null feature group is valid
501 if (lastFeatureGroupAdded.length() < 1)
503 lastFeatureGroupAdded = null;
508 if (enteredType.length() > 0)
510 for (int i = 0; i < sequences.size(); i++)
512 SequenceFeature sf = features.get(i);
513 SequenceFeature sf2 = new SequenceFeature(enteredType,
514 enteredDescription, sf.getBegin(), sf.getEnd(),
516 new FeaturesFile().parseDescriptionHTML(sf2, false);
517 sequences.get(i).addSequenceFeature(sf2);
520 fr.setColour(enteredType, featureColour);
531 * Answers the action to run on Delete in the dialog. Note this includes
532 * refreshing the Feature Settings (if open) in case the only instance of a
533 * feature type or group has been deleted.
537 protected Runnable getDeleteAction()
539 Runnable deleteAction = new Runnable()
543 SequenceFeature sf = features.get(featureIndex);
544 sequences.get(0).getDatasetSequence().deleteFeature(sf);
546 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
547 ap.paintAlignment(true, true);
554 * update the amend feature button dependent on the given style
560 protected void updateColourButton(JPanel bigPanel, JLabel colour,
564 colour.setIcon(null);
567 if (col.isSimpleColour())
569 colour.setToolTipText(null);
570 colour.setBackground(col.getColour());
574 colour.setBackground(bigPanel.getBackground());
575 colour.setForeground(Color.black);
576 colour.setToolTipText(FeatureSettings.getColorTooltip(col, false));
577 FeatureSettings.renderGraduatedColor(colour, col);
582 * Show a warning message if the entered group is one that is currently hidden
587 protected void warnIfGroupHidden(JPanel panel, String group)
589 if (!fr.isGroupVisible(group))
591 String msg = MessageManager.formatMessage("label.warning_hidden",
592 MessageManager.getString("label.group"), group);
593 JvOptionPane.showMessageDialog(panel, msg, "",
594 JvOptionPane.OK_OPTION);
599 * Show a warning message if the entered type is one that is currently hidden
604 protected void warnIfTypeHidden(JPanel panel, String type)
606 if (fr.getRenderOrder().contains(type))
608 if (!fr.showFeatureOfType(type))
610 String msg = MessageManager.formatMessage("label.warning_hidden",
611 MessageManager.getString("label.feature_type"), type);
612 JvOptionPane.showMessageDialog(panel, msg, "",
613 JvOptionPane.OK_OPTION);
619 * On closing the dialog - ensure feature display is turned on, to show any
620 * new features - remove highlighting of the last selected feature - repaint
621 * the panel to show any changes
623 protected void repaintPanel()
625 ap.alignFrame.showSeqFeatures.setSelected(true);
626 ap.av.setShowSequenceFeatures(true);
627 ap.av.setSearchResults(null);
628 ap.paintAlignment(true, true);
632 * Returns the action to be run on OK in the dialog when amending a feature.
633 * Note this may include refreshing the Feature Settings panel (if it is
634 * open), if feature type, group or colour has changed (but not for
635 * description or extent).
639 protected Runnable getAmendAction()
641 Runnable okAction = new Runnable()
643 boolean useLastDefaults = features.get(0).getType() == null;
645 String featureType = name.getText();
647 String featureGroup = group.getText();
651 final String enteredType = name.getText().trim();
652 final String enteredGroup = group.getText().trim();
653 final String enteredDescription = description.getText()
654 .replaceAll("\n", " ");
655 if (enteredType.length() > 0)
659 * update default values only if creating using default values
663 lastFeatureAdded = enteredType;
664 lastFeatureGroupAdded = enteredGroup;
665 // TODO: determine if the null feature group is valid
666 if (lastFeatureGroupAdded.length() < 1)
668 lastFeatureGroupAdded = null;
673 SequenceFeature sf = features.get(featureIndex);
676 * Need to refresh Feature Settings if type, group or colour changed;
677 * note we don't force the feature to be visible - the user has been
678 * warned if a hidden feature type or group was entered
680 boolean refreshSettings = (!featureType.equals(enteredType)
681 || !featureGroup.equals(enteredGroup));
682 refreshSettings |= (featureColour != oldColour);
683 fr.setColour(enteredType, featureColour);
684 int newBegin = sf.begin;
688 newBegin = ((Integer) start.getValue()).intValue();
689 newEnd = ((Integer) end.getValue()).intValue();
690 } catch (NumberFormatException ex)
692 // JSpinner doesn't accept invalid format data :-)
696 * 'amend' the feature by deleting it and adding a new one
697 * (to ensure integrity of SequenceFeatures data store)
698 * note this dialog only updates one sequence at a time
700 sequences.get(0).deleteFeature(sf);
701 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
702 newBegin, newEnd, enteredGroup, sf.getScore());
703 newSf.setDescription(enteredDescription);
704 new FeaturesFile().parseDescriptionHTML(newSf, false);
705 sequences.get(0).addSequenceFeature(newSf);