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;
13 import jalview.util.dialogrunner.RunResponse;
15 import java.awt.BorderLayout;
16 import java.awt.Color;
17 import java.awt.Dimension;
19 import java.awt.GridLayout;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.event.ItemEvent;
23 import java.awt.event.ItemListener;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.swing.JComboBox;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.JSpinner;
34 import javax.swing.JTextArea;
35 import javax.swing.JTextField;
36 import javax.swing.SwingConstants;
37 import javax.swing.event.DocumentEvent;
38 import javax.swing.event.DocumentListener;
41 * Provides a dialog allowing the user to add new features, or amend or delete
44 public class FeatureEditor
47 * defaults for creating a new feature are the last created
48 * feature type and group
50 static String lastFeatureAdded = "feature_1";
52 static String lastFeatureGroupAdded = "Jalview";
55 * the sequence(s) with features to be created / amended
57 final List<SequenceI> sequences;
60 * the features (or template features) to be created / amended
62 final List<SequenceFeature> features;
65 * true if the dialog is to create a new feature, false if
66 * for amend or delete of existing feature(s)
68 final boolean forCreate;
71 * index into the list of features
75 FeatureColourI oldColour;
77 FeatureColourI featureColour;
87 JTextArea description;
102 * if true create a new feature, else amend or delete an existing
105 public FeatureEditor(AlignmentPanel alignPanel, List<SequenceI> seqs,
106 List<SequenceFeature> feats, boolean create)
109 fr = alignPanel.getSeqPanel().seqCanvas.fr;
112 this.forCreate = create;
118 * Initialise the layout and controls
120 protected void init()
124 mainPanel = new JPanel(new BorderLayout());
126 name = new JTextField(25);
127 name.getDocument().addDocumentListener(new DocumentListener()
130 public void insertUpdate(DocumentEvent e)
132 warnIfTypeHidden(mainPanel, name.getText());
136 public void removeUpdate(DocumentEvent e)
138 warnIfTypeHidden(mainPanel, name.getText());
142 public void changedUpdate(DocumentEvent e)
144 warnIfTypeHidden(mainPanel, name.getText());
148 group = new JTextField(25);
149 group.getDocument().addDocumentListener(new DocumentListener()
152 public void insertUpdate(DocumentEvent e)
154 warnIfGroupHidden(mainPanel, group.getText());
158 public void removeUpdate(DocumentEvent e)
160 warnIfGroupHidden(mainPanel, group.getText());
164 public void changedUpdate(DocumentEvent e)
166 warnIfGroupHidden(mainPanel, group.getText());
170 description = new JTextArea(3, 25);
171 start = new JSpinner();
172 end = new JSpinner();
173 start.setPreferredSize(new Dimension(80, 20));
174 end.setPreferredSize(new Dimension(80, 20));
175 final JLabel colour = new JLabel();
176 colour.setOpaque(true);
177 colour.setMaximumSize(new Dimension(30, 16));
178 colour.addMouseListener(new MouseAdapter()
181 public void mousePressed(MouseEvent evt)
183 if (featureColour.isSimpleColour())
186 * open colour chooser on click in colour panel
188 String title = MessageManager
189 .getString("label.select_feature_colour");
190 ColourChooserListener listener = new ColourChooserListener()
193 public void colourSelected(Color c)
195 featureColour = new FeatureColour(c);
196 updateColourButton(mainPanel, colour, featureColour);
199 JalviewColourChooser.showColourChooser(Desktop.getDesktop(),
200 title, featureColour.getColour(), listener);
205 * variable colour dialog - on OK, refetch the updated
206 * feature colour and update this display
208 final String ft = features.get(featureIndex).getType();
209 final String type = ft == null ? lastFeatureAdded : ft;
210 FeatureTypeSettings fcc = new FeatureTypeSettings(fr, type);
211 fcc.setRequestFocusEnabled(true);
213 fcc.addActionListener(new ActionListener()
216 public void actionPerformed(ActionEvent e)
218 featureColour = fr.getFeatureStyle(ft);
219 fr.setColour(type, featureColour);
220 updateColourButton(mainPanel, colour, featureColour);
226 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
228 if (!forCreate && features.size() > 1)
231 * more than one feature at selected position -
232 * add a drop-down to choose the feature to amend
233 * space pad text if necessary to make entries distinct
235 gridPanel = new JPanel(new GridLayout(4, 1));
236 JPanel choosePanel = new JPanel();
237 choosePanel.add(new JLabel(
238 MessageManager.getString("label.select_feature") + ":"));
239 final JComboBox<String> overlaps = new JComboBox<>();
240 List<String> added = new ArrayList<>();
241 for (SequenceFeature sf : features)
243 String text = String.format("%s/%d-%d (%s)", sf.getType(),
244 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
245 while (added.contains(text))
249 overlaps.addItem(text);
252 choosePanel.add(overlaps);
254 overlaps.addItemListener(new ItemListener()
257 public void itemStateChanged(ItemEvent e)
259 int index = overlaps.getSelectedIndex();
262 featureIndex = index;
263 SequenceFeature sf = features.get(index);
264 name.setText(sf.getType());
265 description.setText(sf.getDescription());
266 group.setText(sf.getFeatureGroup());
267 start.setValue(new Integer(sf.getBegin()));
268 end.setValue(new Integer(sf.getEnd()));
270 SearchResultsI highlight = new SearchResults();
271 highlight.addResult(sequences.get(0), sf.getBegin(),
274 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
276 FeatureColourI col = fr.getFeatureStyle(name.getText());
279 col = new FeatureColour(
280 ColorUtils.createColourFromName(name.getText()));
282 oldColour = featureColour = col;
283 updateColourButton(mainPanel, colour, col);
287 gridPanel.add(choosePanel);
290 JPanel namePanel = new JPanel();
291 gridPanel.add(namePanel);
292 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
296 JPanel groupPanel = new JPanel();
297 gridPanel.add(groupPanel);
298 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
300 groupPanel.add(group);
302 JPanel colourPanel = new JPanel();
303 gridPanel.add(colourPanel);
304 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
306 colourPanel.add(colour);
307 colour.setPreferredSize(new Dimension(150, 15));
308 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
309 colour.setForeground(Color.black);
310 colour.setHorizontalAlignment(SwingConstants.CENTER);
311 colour.setVerticalAlignment(SwingConstants.CENTER);
312 colour.setHorizontalTextPosition(SwingConstants.CENTER);
313 colour.setVerticalTextPosition(SwingConstants.CENTER);
314 mainPanel.add(gridPanel, BorderLayout.NORTH);
316 JPanel descriptionPanel = new JPanel();
317 descriptionPanel.add(new JLabel(
318 MessageManager.getString("label.description:"), JLabel.RIGHT));
319 description.setFont(JvSwingUtils.getTextAreaFont());
320 description.setLineWrap(true);
321 descriptionPanel.add(new JScrollPane(description));
325 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
327 JPanel startEndPanel = new JPanel();
328 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
330 startEndPanel.add(start);
331 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
333 startEndPanel.add(end);
334 mainPanel.add(startEndPanel, BorderLayout.CENTER);
338 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
342 * default feature type and group to that of the first feature supplied,
343 * or to the last feature created if not supplied (null value)
345 SequenceFeature firstFeature = features.get(0);
346 boolean useLastDefaults = firstFeature.getType() == null;
347 final String featureType = useLastDefaults ? lastFeatureAdded
348 : firstFeature.getType();
349 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
350 : firstFeature.getFeatureGroup();
351 name.setText(featureType);
352 group.setText(featureGroup);
354 start.setValue(new Integer(firstFeature.getBegin()));
355 end.setValue(new Integer(firstFeature.getEnd()));
356 description.setText(firstFeature.getDescription());
357 featureColour = fr.getFeatureStyle(featureType);
358 oldColour = featureColour;
359 updateColourButton(mainPanel, colour, oldColour);
363 * Presents a dialog allowing the user to add new features, or amend or delete
364 * an existing feature. Currently this can be on
366 * <li>double-click on a sequence - Amend/Delete a selected feature at the
368 * <li>Create sequence feature(s) from pop-up menu on selected region</li>
369 * <li>Create features for pattern matches from Find</li>
371 * If the supplied feature type is null, show (and update on confirm) the type
372 * and group of the last new feature created (with initial defaults of
373 * "feature_1" and "Jalview").
375 public void showDialog()
377 RunResponse okAction = forCreate ? getCreateAction() : getAmendAction();
378 RunResponse cancelAction = getCancelAction();
381 * set dialog action handlers for OK (create/Amend) and Cancel options
382 * also for Delete if applicable (when amending features)
384 JvOptionPane dialog = JvOptionPane.newOptionDialog(Desktop.desktop)
385 .response(okAction).response(cancelAction);
388 dialog.response(getDeleteAction());
392 Object[] options = null;
395 title = MessageManager
396 .getString("label.create_new_sequence_features");
397 options = new Object[] { MessageManager.getString("action.ok"),
398 MessageManager.getString("action.cancel") };
402 title = MessageManager.formatMessage("label.amend_delete_features",
404 { sequences.get(0).getName() });
405 options = new Object[] { MessageManager.getString("label.amend"),
406 MessageManager.getString("action.delete"),
407 MessageManager.getString("action.cancel") };
410 dialog.showInternalDialog(mainPanel, title,
411 JvOptionPane.YES_NO_CANCEL_OPTION,
412 JvOptionPane.PLAIN_MESSAGE, null, options,
413 MessageManager.getString("action.ok"));
417 * Answers an action to run on Cancel in the dialog. This is just to remove
418 * any feature highlighting from the display. Changes in the dialog are not
419 * applied until it is dismissed with OK, Amend or Delete, so there are no
420 * updates to reset on Cancel.
424 protected RunResponse getCancelAction()
426 RunResponse okAction = new RunResponse(JvOptionPane.CANCEL_OPTION)
431 ap.highlightSearchResults(null);
432 ap.paintAlignment(false, false);
439 * Returns the action to be run on OK in the dialog when creating one or more
440 * sequence features. Note these may have a pre-supplied feature type (such as
441 * a Find pattern), or none, in which case the feature type and group default
442 * to those last added through this dialog. The action includes refreshing the
443 * Feature Settings panel (if it is open), to show any new feature type, or
444 * amended colour for an existing type.
448 protected RunResponse getCreateAction()
450 RunResponse okAction = new RunResponse(JvOptionPane.OK_OPTION)
452 boolean useLastDefaults = features.get(0).getType() == null;
456 final String enteredType = name.getText().trim();
457 final String enteredGroup = group.getText().trim();
458 final String enteredDescription = description.getText()
459 .replaceAll("\n", " ");
460 if (enteredType.length() > 0)
463 * update default values only if creating using default values
467 lastFeatureAdded = enteredType;
468 lastFeatureGroupAdded = enteredGroup;
469 // TODO: determine if the null feature group is valid
470 if (lastFeatureGroupAdded.length() < 1)
472 lastFeatureGroupAdded = null;
477 if (enteredType.length() > 0)
479 for (int i = 0; i < sequences.size(); i++)
481 SequenceFeature sf = features.get(i);
482 SequenceFeature sf2 = new SequenceFeature(enteredType,
483 enteredDescription, sf.getBegin(), sf.getEnd(),
485 new FeaturesFile().parseDescriptionHTML(sf2, false);
486 sequences.get(i).addSequenceFeature(sf2);
489 fr.setColour(enteredType, featureColour);
500 * Answers the action to run on Delete in the dialog. Note this includes
501 * refreshing the Feature Settings (if open) in case the only instance of a
502 * feature type or group has been deleted.
506 protected RunResponse getDeleteAction()
508 RunResponse deleteAction = new RunResponse(JvOptionPane.NO_OPTION)
512 SequenceFeature sf = features.get(featureIndex);
513 sequences.get(0).getDatasetSequence().deleteFeature(sf);
515 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
516 ap.paintAlignment(true, true);
523 * update the amend feature button dependent on the given style
529 protected void updateColourButton(JPanel bigPanel, JLabel colour,
533 colour.setIcon(null);
536 if (col.isSimpleColour())
538 colour.setToolTipText(null);
539 colour.setBackground(col.getColour());
543 colour.setBackground(bigPanel.getBackground());
544 colour.setForeground(Color.black);
545 colour.setToolTipText(FeatureSettings.getColorTooltip(col, false));
546 FeatureSettings.renderGraduatedColor(colour, col);
551 * Show a warning message if the entered group is one that is currently hidden
556 protected void warnIfGroupHidden(JPanel panel, String group)
558 if (!fr.isGroupVisible(group))
560 String msg = MessageManager.formatMessage("label.warning_hidden",
561 MessageManager.getString("label.group"), group);
562 JvOptionPane.showMessageDialog(panel, msg, "",
563 JvOptionPane.OK_OPTION);
568 * Show a warning message if the entered type is one that is currently hidden
573 protected void warnIfTypeHidden(JPanel panel, String type)
575 if (fr.getRenderOrder().contains(type))
577 if (!fr.showFeatureOfType(type))
579 String msg = MessageManager.formatMessage("label.warning_hidden",
580 MessageManager.getString("label.feature_type"), type);
581 JvOptionPane.showMessageDialog(panel, msg, "",
582 JvOptionPane.OK_OPTION);
588 * On closing the dialog - ensure feature display is turned on, to show any
589 * new features - remove highlighting of the last selected feature - repaint
590 * the panel to show any changes
592 protected void repaintPanel()
594 ap.alignFrame.showSeqFeatures.setSelected(true);
595 ap.av.setShowSequenceFeatures(true);
596 ap.av.setSearchResults(null);
597 ap.paintAlignment(true, true);
601 * Returns the action to be run on OK in the dialog when amending a feature.
602 * Note this may include refreshing the Feature Settings panel (if it is
603 * open), if feature type, group or colour has changed (but not for
604 * description or extent).
608 protected RunResponse getAmendAction()
610 RunResponse okAction = new RunResponse(JvOptionPane.OK_OPTION)
612 boolean useLastDefaults = features.get(0).getType() == null;
614 String featureType = name.getText();
616 String featureGroup = group.getText();
620 final String enteredType = name.getText().trim();
621 final String enteredGroup = group.getText().trim();
622 final String enteredDescription = description.getText()
623 .replaceAll("\n", " ");
624 if (enteredType.length() > 0)
628 * update default values only if creating using default values
632 lastFeatureAdded = enteredType;
633 lastFeatureGroupAdded = enteredGroup;
634 // TODO: determine if the null feature group is valid
635 if (lastFeatureGroupAdded.length() < 1)
637 lastFeatureGroupAdded = null;
642 SequenceFeature sf = features.get(featureIndex);
645 * Need to refresh Feature Settings if type, group or colour changed;
646 * note we don't force the feature to be visible - the user has been
647 * warned if a hidden feature type or group was entered
649 boolean refreshSettings = (!featureType.equals(enteredType)
650 || !featureGroup.equals(enteredGroup));
651 refreshSettings |= (featureColour != oldColour);
652 fr.setColour(enteredType, featureColour);
653 int newBegin = sf.begin;
657 newBegin = ((Integer) start.getValue()).intValue();
658 newEnd = ((Integer) end.getValue()).intValue();
659 } catch (NumberFormatException ex)
661 // JSpinner doesn't accept invalid format data :-)
665 * 'amend' the feature by deleting it and adding a new one
666 * (to ensure integrity of SequenceFeatures data store)
667 * note this dialog only updates one sequence at a time
669 sequences.get(0).deleteFeature(sf);
670 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
671 newBegin, newEnd, enteredGroup, sf.getScore());
672 newSf.setDescription(enteredDescription);
673 new FeaturesFile().parseDescriptionHTML(newSf, false);
674 sequences.get(0).addSequenceFeature(newSf);