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);
275 FeatureColourI col = fr.getFeatureStyle(name.getText());
278 col = new FeatureColour(
279 ColorUtils.createColourFromName(name.getText()));
281 oldColour = featureColour = col;
282 updateColourButton(mainPanel, colour, col);
286 gridPanel.add(choosePanel);
289 JPanel namePanel = new JPanel();
290 gridPanel.add(namePanel);
291 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
295 JPanel groupPanel = new JPanel();
296 gridPanel.add(groupPanel);
297 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
299 groupPanel.add(group);
301 JPanel colourPanel = new JPanel();
302 gridPanel.add(colourPanel);
303 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
305 colourPanel.add(colour);
306 colour.setPreferredSize(new Dimension(150, 15));
307 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
308 colour.setForeground(Color.black);
309 colour.setHorizontalAlignment(SwingConstants.CENTER);
310 colour.setVerticalAlignment(SwingConstants.CENTER);
311 colour.setHorizontalTextPosition(SwingConstants.CENTER);
312 colour.setVerticalTextPosition(SwingConstants.CENTER);
313 mainPanel.add(gridPanel, BorderLayout.NORTH);
315 JPanel descriptionPanel = new JPanel();
316 descriptionPanel.add(new JLabel(
317 MessageManager.getString("label.description:"), JLabel.RIGHT));
318 description.setFont(JvSwingUtils.getTextAreaFont());
319 description.setLineWrap(true);
320 descriptionPanel.add(new JScrollPane(description));
324 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
326 JPanel startEndPanel = new JPanel();
327 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
329 startEndPanel.add(start);
330 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
332 startEndPanel.add(end);
333 mainPanel.add(startEndPanel, BorderLayout.CENTER);
337 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
341 * default feature type and group to that of the first feature supplied,
342 * or to the last feature created if not supplied (null value)
344 SequenceFeature firstFeature = features.get(0);
345 boolean useLastDefaults = firstFeature.getType() == null;
346 final String featureType = useLastDefaults ? lastFeatureAdded
347 : firstFeature.getType();
348 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
349 : firstFeature.getFeatureGroup();
350 name.setText(featureType);
351 group.setText(featureGroup);
353 start.setValue(new Integer(firstFeature.getBegin()));
354 end.setValue(new Integer(firstFeature.getEnd()));
355 description.setText(firstFeature.getDescription());
356 featureColour = fr.getFeatureStyle(featureType);
357 oldColour = featureColour;
358 updateColourButton(mainPanel, colour, oldColour);
362 options = new Object[] { MessageManager.getString("label.amend"),
363 MessageManager.getString("action.delete"),
364 MessageManager.getString("action.cancel") };
368 options = new Object[] { MessageManager.getString("action.ok"),
369 MessageManager.getString("action.cancel") };
372 String title = create
373 ? MessageManager.getString("label.create_new_sequence_features")
374 : MessageManager.formatMessage("label.amend_delete_features",
376 { sequences.get(0).getName() });
379 * register responses and show the dialog
381 JvOptionPane.newOptionDialog(Desktop.desktop).response(
383 new RunResponse(JvOptionPane.OK_OPTION)
387 final String enteredType = name.getText().trim();
388 final String enteredGroup = group.getText().trim();
389 final String enteredDescription = description.getText()
390 .replaceAll("\n", " ");
391 if (enteredType.length() > 0)
395 * update default values only if creating using default values
399 lastFeatureAdded = enteredType;
400 lastFeatureGroupAdded = enteredGroup;
401 // TODO: determine if the null feature group is valid
402 if (lastFeatureGroupAdded.length() < 1)
404 lastFeatureGroupAdded = null;
411 // NEW FEATURES ADDED
412 if (enteredType.length() > 0)
414 for (int i = 0; i < sequences.size(); i++)
416 SequenceFeature sf = features.get(i);
417 SequenceFeature sf2 = new SequenceFeature(enteredType,
418 enteredDescription, sf.getBegin(),
419 sf.getEnd(), enteredGroup);
420 new FeaturesFile().parseDescriptionHTML(sf2, false);
421 sequences.get(i).addSequenceFeature(sf2);
424 fr.setColour(enteredType, featureColour);
428 responseHandler.run();
433 SequenceFeature sf = features.get(featureIndex);
435 * Feature amended - YES_OPTION corresponds to the Amend button
436 * need to refresh Feature Settings if type, group or colour changed;
437 * note we don't force the feature to be visible - the user has been
438 * warned if a hidden feature type or group was entered
440 boolean refreshSettings = (!featureType
442 || !featureGroup.equals(enteredGroup));
443 refreshSettings |= (featureColour != oldColour);
444 fr.setColour(enteredType, featureColour);
445 int newBegin = sf.begin;
449 newBegin = ((Integer) start.getValue()).intValue();
450 newEnd = ((Integer) end.getValue()).intValue();
451 } catch (NumberFormatException ex)
453 // JSpinner doesn't accept invalid format data :-)
457 * replace the feature by deleting it and adding a new one
458 * (to ensure integrity of SequenceFeatures data store)
460 sequences.get(0).deleteFeature(sf);
461 SequenceFeature newSf = new SequenceFeature(sf,
462 enteredType, newBegin, newEnd, enteredGroup,
464 newSf.setDescription(enteredDescription);
465 new FeaturesFile().parseDescriptionHTML(newSf, false);
466 // amend features dialog only updates one sequence at a time
467 sequences.get(0).addSequenceFeature(newSf);
476 * suppress fastPaint here - if feature colour changed,
477 * we need to repaint the whole alignment
479 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
480 ap.paintAlignment(true, true);
482 }).response(new RunResponse(JvOptionPane.NO_OPTION)
486 SequenceFeature sf = features.get(featureIndex);
488 * NO_OPTION corresponds to the Delete button
490 sequences.get(0).getDatasetSequence().deleteFeature(sf);
491 // update Feature Settings for removal of feature / group
493 ap.getSeqPanel().seqCanvas.highlightSearchResults(null);
494 ap.paintAlignment(true, true);
496 }).defaultResponse(new Runnable()
500 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);