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())
385 .setMaximum(firstFeature.getEnd());
386 ((SpinnerNumberModel) end.getModel())
387 .setMinimum(firstFeature.getBegin());
389 description.setText(firstFeature.getDescription());
390 featureColour = fr.getFeatureStyle(featureType);
391 oldColour = featureColour;
392 updateColourButton(mainPanel, colour, oldColour);
396 * Presents a dialog allowing the user to add new features, or amend or delete
397 * an existing feature. Currently this can be on
399 * <li>double-click on a sequence - Amend/Delete a selected feature at the
401 * <li>Create sequence feature(s) from pop-up menu on selected region</li>
402 * <li>Create features for pattern matches from Find</li>
404 * If the supplied feature type is null, show (and update on confirm) the type
405 * and group of the last new feature created (with initial defaults of
406 * "feature_1" and "Jalview").
408 public void showDialog()
410 Runnable okAction = forCreate ? getCreateAction() : getAmendAction();
411 Runnable cancelAction = getCancelAction();
414 * set dialog action handlers for OK (create/Amend) and Cancel options
415 * also for Delete if applicable (when amending features)
417 JvOptionPane dialog = JvOptionPane.newOptionDialog(Desktop.desktop)
418 .setResponseHandler(0, okAction)
419 .setResponseHandler(2, cancelAction);
422 dialog.setResponseHandler(1, getDeleteAction());
426 Object[] options = null;
429 title = MessageManager
430 .getString("label.create_new_sequence_features");
431 options = new Object[] { MessageManager.getString("action.ok"),
432 MessageManager.getString("action.cancel") };
436 title = MessageManager.formatMessage("label.amend_delete_features",
438 { sequences.get(0).getName() });
439 options = new Object[] { MessageManager.getString("label.amend"),
440 MessageManager.getString("action.delete"),
441 MessageManager.getString("action.cancel") };
444 dialog.showInternalDialog(mainPanel, title,
445 JvOptionPane.YES_NO_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE,
446 null, options, MessageManager.getString("action.ok"));
450 * Answers an action to run on Cancel in the dialog. This is just to remove
451 * any feature highlighting from the display. Changes in the dialog are not
452 * applied until it is dismissed with OK, Amend or Delete, so there are no
453 * updates to reset on Cancel.
457 protected Runnable getCancelAction()
459 Runnable okAction = new Runnable()
464 ap.highlightSearchResults(null);
465 ap.paintAlignment(false, false);
472 * Returns the action to be run on OK in the dialog when creating one or more
473 * sequence features. Note these may have a pre-supplied feature type (such as
474 * a Find pattern), or none, in which case the feature type and group default
475 * to those last added through this dialog. The action includes refreshing the
476 * Feature Settings panel (if it is open), to show any new feature type, or
477 * amended colour for an existing type.
481 protected Runnable getCreateAction()
483 Runnable okAction = new Runnable()
485 boolean useLastDefaults = features.get(0).getType() == null;
489 final String enteredType = name.getText().trim();
490 final String enteredGroup = group.getText().trim();
491 final String enteredDescription = description.getText()
492 .replaceAll("\n", " ");
493 if (enteredType.length() > 0)
496 * update default values only if creating using default values
500 lastFeatureAdded = enteredType;
501 lastFeatureGroupAdded = enteredGroup;
502 // TODO: determine if the null feature group is valid
503 if (lastFeatureGroupAdded.length() < 1)
505 lastFeatureGroupAdded = null;
510 if (enteredType.length() > 0)
512 for (int i = 0; i < sequences.size(); i++)
514 SequenceFeature sf = features.get(i);
515 SequenceFeature sf2 = new SequenceFeature(enteredType,
516 enteredDescription, sf.getBegin(), sf.getEnd(),
518 new FeaturesFile().parseDescriptionHTML(sf2, false);
519 sequences.get(i).addSequenceFeature(sf2);
522 fr.setColour(enteredType, featureColour);
533 * Answers the action to run on Delete in the dialog. Note this includes
534 * refreshing the Feature Settings (if open) in case the only instance of a
535 * feature type or group has been deleted.
539 protected Runnable getDeleteAction()
541 Runnable deleteAction = new Runnable()
545 SequenceFeature sf = features.get(featureIndex);
546 sequences.get(0).getDatasetSequence().deleteFeature(sf);
548 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
549 ap.paintAlignment(true, true);
556 * update the amend feature button dependent on the given style
562 protected void updateColourButton(JPanel bigPanel, JLabel colour,
566 colour.setIcon(null);
569 if (col.isSimpleColour())
571 colour.setToolTipText(null);
572 colour.setBackground(col.getColour());
576 colour.setBackground(bigPanel.getBackground());
577 colour.setForeground(Color.black);
578 colour.setToolTipText(FeatureSettings.getColorTooltip(col, false));
579 FeatureSettings.renderGraduatedColor(colour, col);
584 * Show a warning message if the entered group is one that is currently hidden
589 protected void warnIfGroupHidden(JPanel panel, String group)
591 if (!fr.isGroupVisible(group))
593 String msg = MessageManager.formatMessage("label.warning_hidden",
594 MessageManager.getString("label.group"), group);
595 JvOptionPane.showMessageDialog(panel, msg, "",
596 JvOptionPane.OK_OPTION);
601 * Show a warning message if the entered type is one that is currently hidden
606 protected void warnIfTypeHidden(JPanel panel, String type)
608 if (fr.getRenderOrder().contains(type))
610 if (!fr.showFeatureOfType(type))
612 String msg = MessageManager.formatMessage("label.warning_hidden",
613 MessageManager.getString("label.feature_type"), type);
614 JvOptionPane.showMessageDialog(panel, msg, "",
615 JvOptionPane.OK_OPTION);
621 * On closing the dialog - ensure feature display is turned on, to show any
622 * new features - remove highlighting of the last selected feature - repaint
623 * the panel to show any changes
625 protected void repaintPanel()
627 ap.alignFrame.showSeqFeatures.setSelected(true);
628 ap.av.setShowSequenceFeatures(true);
629 ap.av.setSearchResults(null);
630 ap.paintAlignment(true, true);
634 * Returns the action to be run on OK in the dialog when amending a feature.
635 * Note this may include refreshing the Feature Settings panel (if it is
636 * open), if feature type, group or colour has changed (but not for
637 * description or extent).
641 protected Runnable getAmendAction()
643 Runnable okAction = new Runnable()
645 boolean useLastDefaults = features.get(0).getType() == null;
647 String featureType = name.getText();
649 String featureGroup = group.getText();
653 final String enteredType = name.getText().trim();
654 final String enteredGroup = group.getText().trim();
655 final String enteredDescription = description.getText()
656 .replaceAll("\n", " ");
657 if (enteredType.length() > 0)
661 * update default values only if creating using default values
665 lastFeatureAdded = enteredType;
666 lastFeatureGroupAdded = enteredGroup;
667 // TODO: determine if the null feature group is valid
668 if (lastFeatureGroupAdded.length() < 1)
670 lastFeatureGroupAdded = null;
675 SequenceFeature sf = features.get(featureIndex);
678 * Need to refresh Feature Settings if type, group or colour changed;
679 * note we don't force the feature to be visible - the user has been
680 * warned if a hidden feature type or group was entered
682 boolean refreshSettings = (!featureType.equals(enteredType)
683 || !featureGroup.equals(enteredGroup));
684 refreshSettings |= (featureColour != oldColour);
685 fr.setColour(enteredType, featureColour);
686 int newBegin = sf.begin;
690 newBegin = ((Integer) start.getValue()).intValue();
691 newEnd = ((Integer) end.getValue()).intValue();
692 } catch (NumberFormatException ex)
694 // JSpinner doesn't accept invalid format data :-)
698 * 'amend' the feature by deleting it and adding a new one
699 * (to ensure integrity of SequenceFeatures data store)
700 * note this dialog only updates one sequence at a time
702 sequences.get(0).deleteFeature(sf);
703 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
704 newBegin, newEnd, enteredGroup, sf.getScore());
705 newSf.setDescription(enteredDescription);
706 new FeaturesFile().parseDescriptionHTML(newSf, false);
707 sequences.get(0).addSequenceFeature(newSf);