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;
79 * Presents a dialog allowing the user to add new features, or amend or delete
80 * existing features. Currently this can be on
82 * <li>double-click on a sequence - Amend/Delete features at position</li>
83 * <li>Create sequence feature from pop-up menu on selected region</li>
84 * <li>Create features for pattern matches from Find</li>
86 * If the supplied feature type is null, show (and update on confirm) the type
87 * and group of the last new feature created (with initial defaults of
88 * "feature_1" and "Jalview").
91 * the sequences features are to be created on (if creating
92 * features), or a single sequence (if amending features)
94 * the current features at the position (if amending), or template
95 * new feature(s) with start/end position set (if creating)
97 * true to create features, false to amend or delete
99 * @param responseHandler
100 * boolean true RunResponse is run if features are created
102 public boolean amendFeatures(final List<SequenceI> sequences,
103 final List<SequenceFeature> features, boolean create,
104 final Runnable responseHandler)
108 final JPanel mainPanel = new JPanel(new BorderLayout());
110 final JTextField name = new JTextField(25);
111 name.getDocument().addDocumentListener(new DocumentListener()
114 public void insertUpdate(DocumentEvent e)
116 warnIfTypeHidden(mainPanel, name.getText());
120 public void removeUpdate(DocumentEvent e)
122 warnIfTypeHidden(mainPanel, name.getText());
126 public void changedUpdate(DocumentEvent e)
128 warnIfTypeHidden(mainPanel, name.getText());
132 final JTextField group = new JTextField(25);
133 group.getDocument().addDocumentListener(new DocumentListener()
136 public void insertUpdate(DocumentEvent e)
138 warnIfGroupHidden(mainPanel, group.getText());
142 public void removeUpdate(DocumentEvent e)
144 warnIfGroupHidden(mainPanel, group.getText());
148 public void changedUpdate(DocumentEvent e)
150 warnIfGroupHidden(mainPanel, group.getText());
154 final JTextArea description = new JTextArea(3, 25);
155 final JSpinner start = new JSpinner();
156 final JSpinner end = new JSpinner();
157 start.setPreferredSize(new Dimension(80, 20));
158 end.setPreferredSize(new Dimension(80, 20));
159 final JLabel colour = new JLabel();
160 colour.setOpaque(true);
161 // colour.setBorder(BorderFactory.createEtchedBorder());
162 colour.setMaximumSize(new Dimension(30, 16));
163 colour.addMouseListener(new MouseAdapter()
166 * open colour chooser on click in colour panel
169 public void mousePressed(MouseEvent evt)
171 if (featureColour.isSimpleColour())
173 String title = MessageManager
174 .getString("label.select_feature_colour");
175 ColourChooserListener listener = new ColourChooserListener()
178 public void colourSelected(Color c)
180 featureColour = new FeatureColour(c);
181 updateColourButton(mainPanel, colour, featureColour);
184 JalviewColourChooser.showColourChooser(Desktop.getDesktop(),
185 title, featureColour.getColour(), listener);
190 * variable colour dialog - on OK, refetch the updated
191 * feature colour and update this display
193 final String ft = features.get(featureIndex).getType();
194 final String type = ft == null ? lastFeatureAdded : ft;
195 FeatureTypeSettings fcc = new FeatureTypeSettings(
197 fcc.setRequestFocusEnabled(true);
199 fcc.addActionListener(new ActionListener()
202 public void actionPerformed(ActionEvent e)
204 featureColour = fr.getFeatureStyle(ft);
205 fr.setColour(type, featureColour);
206 updateColourButton(mainPanel, colour, featureColour);
212 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
214 if (!create && features.size() > 1)
217 * more than one feature at selected position -
218 * add a drop-down to choose the feature to amend
219 * space pad text if necessary to make entries distinct
221 gridPanel = new JPanel(new GridLayout(4, 1));
222 JPanel choosePanel = new JPanel();
223 choosePanel.add(new JLabel(
224 MessageManager.getString("label.select_feature") + ":"));
225 final JComboBox<String> overlaps = new JComboBox<>();
226 List<String> added = new ArrayList<>();
227 for (SequenceFeature sf : features)
229 String text = String.format("%s/%d-%d (%s)", sf.getType(),
230 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
231 while (added.contains(text))
235 overlaps.addItem(text);
238 choosePanel.add(overlaps);
240 overlaps.addItemListener(new ItemListener()
243 public void itemStateChanged(ItemEvent e)
245 int index = overlaps.getSelectedIndex();
248 featureIndex = index;
249 SequenceFeature sf = features.get(index);
250 name.setText(sf.getType());
251 description.setText(sf.getDescription());
252 group.setText(sf.getFeatureGroup());
253 start.setValue(new Integer(sf.getBegin()));
254 end.setValue(new Integer(sf.getEnd()));
256 SearchResultsI highlight = new SearchResults();
257 highlight.addResult(sequences.get(0), sf.getBegin(),
260 ap.getSeqPanel().seqCanvas
261 .highlightSearchResults(highlight, false);
263 FeatureColourI col = fr.getFeatureStyle(name.getText());
266 col = new FeatureColour(
267 ColorUtils.createColourFromName(name.getText()));
269 oldColour = featureColour = col;
270 updateColourButton(mainPanel, colour, col);
274 gridPanel.add(choosePanel);
277 JPanel namePanel = new JPanel();
278 gridPanel.add(namePanel);
279 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
283 JPanel groupPanel = new JPanel();
284 gridPanel.add(groupPanel);
285 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
287 groupPanel.add(group);
289 JPanel colourPanel = new JPanel();
290 gridPanel.add(colourPanel);
291 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
293 colourPanel.add(colour);
294 colour.setPreferredSize(new Dimension(150, 15));
295 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
296 colour.setForeground(Color.black);
297 colour.setHorizontalAlignment(SwingConstants.CENTER);
298 colour.setVerticalAlignment(SwingConstants.CENTER);
299 colour.setHorizontalTextPosition(SwingConstants.CENTER);
300 colour.setVerticalTextPosition(SwingConstants.CENTER);
301 mainPanel.add(gridPanel, BorderLayout.NORTH);
303 JPanel descriptionPanel = new JPanel();
304 descriptionPanel.add(new JLabel(
305 MessageManager.getString("label.description:"), JLabel.RIGHT));
306 description.setFont(JvSwingUtils.getTextAreaFont());
307 description.setLineWrap(true);
308 descriptionPanel.add(new JScrollPane(description));
312 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
314 JPanel startEndPanel = new JPanel();
315 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
317 startEndPanel.add(start);
318 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
320 startEndPanel.add(end);
321 mainPanel.add(startEndPanel, BorderLayout.CENTER);
325 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
329 * default feature type and group to that of the first feature supplied,
330 * or to the last feature created if not supplied (null value)
332 SequenceFeature firstFeature = features.get(0);
333 boolean useLastDefaults = firstFeature.getType() == null;
334 final String featureType = useLastDefaults ? lastFeatureAdded
335 : firstFeature.getType();
336 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
337 : firstFeature.getFeatureGroup();
338 name.setText(featureType);
339 group.setText(featureGroup);
341 start.setValue(new Integer(firstFeature.getBegin()));
342 end.setValue(new Integer(firstFeature.getEnd()));
343 description.setText(firstFeature.getDescription());
344 featureColour = fr.getFeatureStyle(featureType);
345 oldColour = featureColour;
346 updateColourButton(mainPanel, colour, oldColour);
350 options = new Object[] { MessageManager.getString("label.amend"),
351 MessageManager.getString("action.delete"),
352 MessageManager.getString("action.cancel") };
356 options = new Object[] { MessageManager.getString("action.ok"),
357 MessageManager.getString("action.cancel") };
360 String title = create
361 ? MessageManager.getString("label.create_new_sequence_features")
362 : MessageManager.formatMessage("label.amend_delete_features",
364 { sequences.get(0).getName() });
367 * register responses and show the dialog
369 JvOptionPane.newOptionDialog(Desktop.desktop).response(
371 new RunResponse(JvOptionPane.OK_OPTION)
375 final String enteredType = name.getText().trim();
376 final String enteredGroup = group.getText().trim();
377 final String enteredDescription = description.getText()
378 .replaceAll("\n", " ");
379 if (enteredType.length() > 0)
383 * update default values only if creating using default values
387 lastFeatureAdded = enteredType;
388 lastFeatureGroupAdded = enteredGroup;
389 // TODO: determine if the null feature group is valid
390 if (lastFeatureGroupAdded.length() < 1)
392 lastFeatureGroupAdded = null;
399 // NEW FEATURES ADDED
400 if (enteredType.length() > 0)
402 for (int i = 0; i < sequences.size(); i++)
404 SequenceFeature sf = features.get(i);
405 SequenceFeature sf2 = new SequenceFeature(enteredType,
406 enteredDescription, sf.getBegin(),
407 sf.getEnd(), enteredGroup);
408 new FeaturesFile().parseDescriptionHTML(sf2, false);
409 sequences.get(i).addSequenceFeature(sf2);
412 fr.setColour(enteredType, featureColour);
416 responseHandler.run();
419 SequenceFeature sf = features.get(featureIndex);
421 * Feature amended - YES_OPTION corresponds to the Amend button
422 * need to refresh Feature Settings if type, group or colour changed;
423 * note we don't force the feature to be visible - the user has been
424 * warned if a hidden feature type or group was entered
426 boolean refreshSettings = (!featureType.equals(enteredType)
427 || !featureGroup.equals(enteredGroup));
428 refreshSettings |= (featureColour != oldColour);
429 fr.setColour(enteredType, featureColour);
430 int newBegin = sf.begin;
434 newBegin = ((Integer) start.getValue()).intValue();
435 newEnd = ((Integer) end.getValue()).intValue();
436 } catch (NumberFormatException ex)
438 // JSpinner doesn't accept invalid format data :-)
442 * replace the feature by deleting it and adding a new one
443 * (to ensure integrity of SequenceFeatures data store)
445 sequences.get(0).deleteFeature(sf);
446 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
447 newBegin, newEnd, enteredGroup, sf.getScore());
448 newSf.setDescription(enteredDescription);
449 new FeaturesFile().parseDescriptionHTML(newSf, false);
450 // amend features dialog only updates one sequence at a time
451 sequences.get(0).addSequenceFeature(newSf);
458 ap.getSeqPanel().seqCanvas.highlightSearchResults(null,
460 ap.paintAlignment(true, true);
462 }).response(new RunResponse(JvOptionPane.NO_OPTION)
466 SequenceFeature sf = features.get(featureIndex);
468 * NO_OPTION corresponds to the Delete button
470 sequences.get(0).getDatasetSequence().deleteFeature(sf);
471 // update Feature Settings for removal of feature / group
473 ap.getSeqPanel().seqCanvas.highlightSearchResults(null,
475 ap.paintAlignment(true, true);
477 }).defaultResponse(new Runnable()
481 ap.getSeqPanel().seqCanvas.highlightSearchResults(null,
483 ap.paintAlignment(true, true);
485 }).showInternalDialog(mainPanel, title,
486 JvOptionPane.YES_NO_CANCEL_OPTION,
487 JvOptionPane.QUESTION_MESSAGE, null, options,
488 MessageManager.getString("action.ok"));
493 * update the amend feature button dependent on the given style
499 protected void updateColourButton(JPanel bigPanel, JLabel colour,
503 colour.setIcon(null);
504 colour.setToolTipText(null);
507 if (col.isSimpleColour())
509 colour.setBackground(col.getColour());
513 colour.setBackground(bigPanel.getBackground());
514 colour.setForeground(Color.black);
515 FeatureSettings.renderGraduatedColor(colour, col);
520 * Show a warning message if the entered group is one that is currently hidden
525 protected void warnIfGroupHidden(JPanel panel, String group)
527 if (!fr.isGroupVisible(group))
529 String msg = MessageManager.formatMessage("label.warning_hidden",
530 MessageManager.getString("label.group"), group);
531 JvOptionPane.showMessageDialog(panel, msg, "",
532 JvOptionPane.OK_OPTION);
537 * Show a warning message if the entered type is one that is currently hidden
542 protected void warnIfTypeHidden(JPanel panel, String type)
544 if (fr.getRenderOrder().contains(type))
546 if (!fr.showFeatureOfType(type))
548 String msg = MessageManager.formatMessage("label.warning_hidden",
549 MessageManager.getString("label.feature_type"), type);
550 JvOptionPane.showMessageDialog(panel, msg, "",
551 JvOptionPane.OK_OPTION);