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 * index into a list of features if more than one selected for editing
59 FeatureColourI oldColour;
61 FeatureColourI featureColour;
72 public FeatureEditor(AlignmentPanel alignPanel)
75 fr = alignPanel.getSeqPanel().seqCanvas.fr;
78 public void amendFeatures(final List<SequenceI> sequences,
79 final List<SequenceFeature> features,
80 final Runnable responseHandler)
82 amendFeatures(sequences, features, false, responseHandler);
85 public void createFeatures(final List<SequenceI> sequences,
86 final List<SequenceFeature> features,
87 final Runnable responseHandler)
89 amendFeatures(sequences, features, true, responseHandler);
93 * Presents a dialog allowing the user to add new features, or amend or delete
94 * existing features. Currently this can be on
96 * <li>double-click on a sequence - Amend/Delete features at position</li>
97 * <li>Create sequence feature from pop-up menu on selected region</li>
98 * <li>Create features for pattern matches from Find</li>
100 * If the supplied feature type is null, show (and update on confirm) the type
101 * and group of the last new feature created (with initial defaults of
102 * "feature_1" and "Jalview").
105 * the sequences features are to be created on (if creating
106 * features), or a single sequence (if amending features)
108 * the current features at the position (if amending), or template
109 * new feature(s) with start/end position set (if creating)
111 * true to create features, false to amend or delete
113 * @param responseHandler
114 * boolean true RunResponse is run if features are created
116 private void amendFeatures(final List<SequenceI> sequences,
117 final List<SequenceFeature> features, boolean create,
118 final Runnable responseHandler)
122 final JPanel mainPanel = new JPanel(new BorderLayout());
124 final JTextField name = new JTextField(25);
125 name.getDocument().addDocumentListener(new DocumentListener()
128 public void insertUpdate(DocumentEvent e)
130 warnIfTypeHidden(mainPanel, name.getText());
134 public void removeUpdate(DocumentEvent e)
136 warnIfTypeHidden(mainPanel, name.getText());
140 public void changedUpdate(DocumentEvent e)
142 warnIfTypeHidden(mainPanel, name.getText());
146 final JTextField group = new JTextField(25);
147 group.getDocument().addDocumentListener(new DocumentListener()
150 public void insertUpdate(DocumentEvent e)
152 warnIfGroupHidden(mainPanel, group.getText());
156 public void removeUpdate(DocumentEvent e)
158 warnIfGroupHidden(mainPanel, group.getText());
162 public void changedUpdate(DocumentEvent e)
164 warnIfGroupHidden(mainPanel, group.getText());
168 final JTextArea description = new JTextArea(3, 25);
169 final JSpinner start = new JSpinner();
170 final JSpinner end = new JSpinner();
171 start.setPreferredSize(new Dimension(80, 20));
172 end.setPreferredSize(new Dimension(80, 20));
173 final JLabel colour = new JLabel();
174 colour.setOpaque(true);
175 // colour.setBorder(BorderFactory.createEtchedBorder());
176 colour.setMaximumSize(new Dimension(30, 16));
177 colour.addMouseListener(new MouseAdapter()
180 * open colour chooser on click in colour panel
183 public void mousePressed(MouseEvent evt)
185 if (featureColour.isSimpleColour())
187 String title = MessageManager
188 .getString("label.select_feature_colour");
189 ColourChooserListener listener = new ColourChooserListener()
192 public void colourSelected(Color c)
194 featureColour = new FeatureColour(c);
195 updateColourButton(mainPanel, colour, featureColour);
198 JalviewColourChooser.showColourChooser(Desktop.getDesktop(),
199 title, featureColour.getColour(), listener);
204 * variable colour dialog - on OK, refetch the updated
205 * feature colour and update this display
207 final String ft = features.get(featureIndex).getType();
208 final String type = ft == null ? lastFeatureAdded : ft;
209 FeatureTypeSettings fcc = new FeatureTypeSettings(fr, type);
210 fcc.setRequestFocusEnabled(true);
212 fcc.addActionListener(new ActionListener()
215 public void actionPerformed(ActionEvent e)
217 featureColour = fr.getFeatureStyle(ft);
218 fr.setColour(type, featureColour);
219 updateColourButton(mainPanel, colour, featureColour);
225 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
227 if (!create && features.size() > 1)
230 * more than one feature at selected position -
231 * add a drop-down to choose the feature to amend
232 * space pad text if necessary to make entries distinct
234 gridPanel = new JPanel(new GridLayout(4, 1));
235 JPanel choosePanel = new JPanel();
236 choosePanel.add(new JLabel(
237 MessageManager.getString("label.select_feature") + ":"));
238 final JComboBox<String> overlaps = new JComboBox<>();
239 List<String> added = new ArrayList<>();
240 for (SequenceFeature sf : features)
242 String text = String.format("%s/%d-%d (%s)", sf.getType(),
243 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
244 while (added.contains(text))
248 overlaps.addItem(text);
251 choosePanel.add(overlaps);
253 overlaps.addItemListener(new ItemListener()
256 public void itemStateChanged(ItemEvent e)
258 int index = overlaps.getSelectedIndex();
261 featureIndex = index;
262 SequenceFeature sf = features.get(index);
263 name.setText(sf.getType());
264 description.setText(sf.getDescription());
265 group.setText(sf.getFeatureGroup());
266 start.setValue(new Integer(sf.getBegin()));
267 end.setValue(new Integer(sf.getEnd()));
269 SearchResultsI highlight = new SearchResults();
270 highlight.addResult(sequences.get(0), sf.getBegin(),
273 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 options = new Object[] { MessageManager.getString("label.amend"),
364 MessageManager.getString("action.delete"),
365 MessageManager.getString("action.cancel") };
369 options = new Object[] { MessageManager.getString("action.ok"),
370 MessageManager.getString("action.cancel") };
373 String title = create
374 ? MessageManager.getString("label.create_new_sequence_features")
375 : MessageManager.formatMessage("label.amend_delete_features",
377 { sequences.get(0).getName() });
380 * register responses and show the dialog
382 JvOptionPane.newOptionDialog(Desktop.desktop).response(
384 new RunResponse(JvOptionPane.OK_OPTION)
388 final String enteredType = name.getText().trim();
389 final String enteredGroup = group.getText().trim();
390 final String enteredDescription = description.getText()
391 .replaceAll("\n", " ");
392 if (enteredType.length() > 0)
396 * update default values only if creating using default values
400 lastFeatureAdded = enteredType;
401 lastFeatureGroupAdded = enteredGroup;
402 // TODO: determine if the null feature group is valid
403 if (lastFeatureGroupAdded.length() < 1)
405 lastFeatureGroupAdded = null;
412 // NEW FEATURES ADDED
413 if (enteredType.length() > 0)
415 for (int i = 0; i < sequences.size(); i++)
417 SequenceFeature sf = features.get(i);
418 SequenceFeature sf2 = new SequenceFeature(enteredType,
419 enteredDescription, sf.getBegin(),
420 sf.getEnd(), enteredGroup);
421 new FeaturesFile().parseDescriptionHTML(sf2, false);
422 sequences.get(i).addSequenceFeature(sf2);
425 fr.setColour(enteredType, featureColour);
429 responseHandler.run();
434 SequenceFeature sf = features.get(featureIndex);
436 * Feature amended - YES_OPTION corresponds to the Amend button
437 * need to refresh Feature Settings if type, group or colour changed;
438 * note we don't force the feature to be visible - the user has been
439 * warned if a hidden feature type or group was entered
441 boolean refreshSettings = (!featureType
443 || !featureGroup.equals(enteredGroup));
444 refreshSettings |= (featureColour != oldColour);
445 fr.setColour(enteredType, featureColour);
446 int newBegin = sf.begin;
450 newBegin = ((Integer) start.getValue()).intValue();
451 newEnd = ((Integer) end.getValue()).intValue();
452 } catch (NumberFormatException ex)
454 // JSpinner doesn't accept invalid format data :-)
458 * replace the feature by deleting it and adding a new one
459 * (to ensure integrity of SequenceFeatures data store)
461 sequences.get(0).deleteFeature(sf);
462 SequenceFeature newSf = new SequenceFeature(sf,
463 enteredType, newBegin, newEnd, enteredGroup,
465 newSf.setDescription(enteredDescription);
466 new FeaturesFile().parseDescriptionHTML(newSf, false);
467 // amend features dialog only updates one sequence at a time
468 sequences.get(0).addSequenceFeature(newSf);
476 ap.getSeqPanel().seqCanvas.highlightSearchResults(null,
478 ap.paintAlignment(true, true);
480 }).response(new RunResponse(JvOptionPane.NO_OPTION)
484 SequenceFeature sf = features.get(featureIndex);
486 * NO_OPTION corresponds to the Delete button
488 sequences.get(0).getDatasetSequence().deleteFeature(sf);
489 // update Feature Settings for removal of feature / group
491 ap.getSeqPanel().seqCanvas.highlightSearchResults(null,
493 ap.paintAlignment(true, true);
495 }).defaultResponse(new Runnable()
499 ap.getSeqPanel().seqCanvas.highlightSearchResults(null,
501 ap.paintAlignment(true, true);
503 }).showInternalDialog(mainPanel, title,
504 JvOptionPane.YES_NO_CANCEL_OPTION,
505 JvOptionPane.QUESTION_MESSAGE, null, options,
506 MessageManager.getString("action.ok"));
510 * update the amend feature button dependent on the given style
516 protected void updateColourButton(JPanel bigPanel, JLabel colour,
520 colour.setIcon(null);
521 colour.setToolTipText(null);
524 if (col.isSimpleColour())
526 colour.setBackground(col.getColour());
530 colour.setBackground(bigPanel.getBackground());
531 colour.setForeground(Color.black);
532 FeatureSettings.renderGraduatedColor(colour, col);
537 * Show a warning message if the entered group is one that is currently hidden
542 protected void warnIfGroupHidden(JPanel panel, String group)
544 if (!fr.isGroupVisible(group))
546 String msg = MessageManager.formatMessage("label.warning_hidden",
547 MessageManager.getString("label.group"), group);
548 JvOptionPane.showMessageDialog(panel, msg, "",
549 JvOptionPane.OK_OPTION);
554 * Show a warning message if the entered type is one that is currently hidden
559 protected void warnIfTypeHidden(JPanel panel, String type)
561 if (fr.getRenderOrder().contains(type))
563 if (!fr.showFeatureOfType(type))
565 String msg = MessageManager.formatMessage("label.warning_hidden",
566 MessageManager.getString("label.feature_type"), type);
567 JvOptionPane.showMessageDialog(panel, msg, "",
568 JvOptionPane.OK_OPTION);