2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.FeatureColourI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.FeaturesFile;
29 import jalview.schemes.FeatureColour;
30 import jalview.util.ColorUtils;
31 import jalview.util.MessageManager;
33 import java.awt.BorderLayout;
34 import java.awt.Color;
35 import java.awt.Dimension;
37 import java.awt.GridLayout;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.ItemEvent;
41 import java.awt.event.ItemListener;
42 import java.awt.event.MouseAdapter;
43 import java.awt.event.MouseEvent;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Comparator;
47 import java.util.List;
49 import javax.swing.JColorChooser;
50 import javax.swing.JComboBox;
51 import javax.swing.JLabel;
52 import javax.swing.JPanel;
53 import javax.swing.JScrollPane;
54 import javax.swing.JSpinner;
55 import javax.swing.JTextArea;
56 import javax.swing.JTextField;
57 import javax.swing.SwingConstants;
58 import javax.swing.event.DocumentEvent;
59 import javax.swing.event.DocumentListener;
67 public class FeatureRenderer extends
68 jalview.renderer.seqfeatures.FeatureRenderer
71 * defaults for creating a new feature are the last created
72 * feature type and group
74 static String lastFeatureAdded = "feature_1";
76 static String lastFeatureGroupAdded = "Jalview";
83 * Creates a new FeatureRenderer object
87 public FeatureRenderer(AlignmentPanel alignPanel)
91 if (alignPanel.getSeqPanel() != null
92 && alignPanel.getSeqPanel().seqCanvas != null
93 && alignPanel.getSeqPanel().seqCanvas.fr != null)
95 transferSettings(alignPanel.getSeqPanel().seqCanvas.fr);
99 FeatureColourI oldcol, fcol;
101 int featureIndex = 0;
104 * Presents a dialog allowing the user to add new features, or amend or delete
105 * existing features. Currently this can be on
107 * <li>double-click on a sequence - Amend/Delete features at position</li>
108 * <li>Create sequence feature from pop-up menu on selected region</li>
109 * <li>Create features for pattern matches from Find</li>
111 * If the supplied feature type is null, show (and update on confirm) the type
112 * and group of the last new feature created (with initial defaults of
113 * "feature_1" and "Jalview").
116 * the sequences features are to be created on (if creating
117 * features), or a single sequence (if amending features)
119 * the current features at the position (if amending), or template
120 * new feature(s) with start/end position set (if creating)
122 * true to create features, false to amend or delete
126 protected boolean amendFeatures(final List<SequenceI> sequences,
127 final List<SequenceFeature> features, boolean create,
128 final AlignmentPanel alignPanel)
132 final JPanel mainPanel = new JPanel(new BorderLayout());
134 final JTextField name = new JTextField(25);
135 name.getDocument().addDocumentListener(new DocumentListener()
138 public void insertUpdate(DocumentEvent e)
140 warnIfTypeHidden(mainPanel, name.getText());
144 public void removeUpdate(DocumentEvent e)
146 warnIfTypeHidden(mainPanel, name.getText());
150 public void changedUpdate(DocumentEvent e)
152 warnIfTypeHidden(mainPanel, name.getText());
156 final JTextField group = new JTextField(25);
157 group.getDocument().addDocumentListener(new DocumentListener()
160 public void insertUpdate(DocumentEvent e)
162 warnIfGroupHidden(mainPanel, group.getText());
166 public void removeUpdate(DocumentEvent e)
168 warnIfGroupHidden(mainPanel, group.getText());
172 public void changedUpdate(DocumentEvent e)
174 warnIfGroupHidden(mainPanel, group.getText());
178 final JTextArea description = new JTextArea(3, 25);
179 final JSpinner start = new JSpinner();
180 final JSpinner end = new JSpinner();
181 start.setPreferredSize(new Dimension(80, 20));
182 end.setPreferredSize(new Dimension(80, 20));
183 final FeatureRenderer me = this;
184 final JLabel colour = new JLabel();
185 colour.setOpaque(true);
186 // colour.setBorder(BorderFactory.createEtchedBorder());
187 colour.setMaximumSize(new Dimension(30, 16));
188 colour.addMouseListener(new MouseAdapter()
190 FeatureColourChooser fcc = null;
193 public void mousePressed(MouseEvent evt)
195 if (fcol.isSimpleColour())
197 Color col = JColorChooser.showDialog(Desktop.desktop,
198 MessageManager.getString("label.select_feature_colour"),
202 fcol = new FeatureColour(col);
203 updateColourButton(mainPanel, colour, fcol);
210 final String ft = features.get(featureIndex).getType();
211 final String type = ft == null ? lastFeatureAdded : ft;
212 fcc = new FeatureColourChooser(me, type);
213 fcc.setRequestFocusEnabled(true);
216 fcc.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 fcol = fcc.getLastColour();
224 setColour(type, fcol);
225 updateColourButton(mainPanel, colour, fcol);
233 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
235 if (!create && features.size() > 1)
238 * more than one feature at selected position -
239 * add a drop-down to choose the feature to amend
240 * space pad text if necessary to make entries distinct
242 gridPanel = new JPanel(new GridLayout(4, 1));
243 JPanel choosePanel = new JPanel();
244 choosePanel.add(new JLabel(MessageManager
245 .getString("label.select_feature")
247 final JComboBox<String> overlaps = new JComboBox<String>();
248 List<String> added = new ArrayList<>();
249 for (SequenceFeature sf : features)
251 String text = String.format("%s/%d-%d (%s)", sf.getType(),
252 sf.getBegin(), sf.getEnd(), sf.getFeatureGroup());
253 while (added.contains(text))
257 overlaps.addItem(text);
260 choosePanel.add(overlaps);
262 overlaps.addItemListener(new ItemListener()
265 public void itemStateChanged(ItemEvent e)
267 int index = overlaps.getSelectedIndex();
270 featureIndex = index;
271 SequenceFeature sf = features.get(index);
272 name.setText(sf.getType());
273 description.setText(sf.getDescription());
274 group.setText(sf.getFeatureGroup());
275 start.setValue(new Integer(sf.getBegin()));
276 end.setValue(new Integer(sf.getEnd()));
278 SearchResultsI highlight = new SearchResults();
279 highlight.addResult(sequences.get(0), sf.getBegin(),
282 alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(
286 FeatureColourI col = getFeatureStyle(name.getText());
289 col = new FeatureColour(ColorUtils
290 .createColourFromName(name.getText()));
293 updateColourButton(mainPanel, colour, col);
297 gridPanel.add(choosePanel);
300 JPanel namePanel = new JPanel();
301 gridPanel.add(namePanel);
302 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
306 JPanel groupPanel = new JPanel();
307 gridPanel.add(groupPanel);
308 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
310 groupPanel.add(group);
312 JPanel colourPanel = new JPanel();
313 gridPanel.add(colourPanel);
314 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
316 colourPanel.add(colour);
317 colour.setPreferredSize(new Dimension(150, 15));
318 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
319 colour.setForeground(Color.black);
320 colour.setHorizontalAlignment(SwingConstants.CENTER);
321 colour.setVerticalAlignment(SwingConstants.CENTER);
322 colour.setHorizontalTextPosition(SwingConstants.CENTER);
323 colour.setVerticalTextPosition(SwingConstants.CENTER);
324 mainPanel.add(gridPanel, BorderLayout.NORTH);
326 JPanel descriptionPanel = new JPanel();
327 descriptionPanel.add(new JLabel(MessageManager
328 .getString("label.description:"),
330 description.setFont(JvSwingUtils.getTextAreaFont());
331 description.setLineWrap(true);
332 descriptionPanel.add(new JScrollPane(description));
336 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
338 JPanel startEndPanel = new JPanel();
339 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
341 startEndPanel.add(start);
342 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
344 startEndPanel.add(end);
345 mainPanel.add(startEndPanel, BorderLayout.CENTER);
349 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
353 * default feature type and group to that of the first feature supplied,
354 * or to the last feature created if not supplied (null value)
356 SequenceFeature firstFeature = features.get(0);
357 boolean useLastDefaults = firstFeature.getType() == null;
358 final String featureType = useLastDefaults ? lastFeatureAdded
359 : firstFeature.getType();
360 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
361 : firstFeature.getFeatureGroup();
362 name.setText(featureType);
363 group.setText(featureGroup);
365 start.setValue(new Integer(firstFeature.getBegin()));
366 end.setValue(new Integer(firstFeature.getEnd()));
367 description.setText(firstFeature.getDescription());
368 updateColourButton(mainPanel, colour,
369 (oldcol = fcol = getFeatureStyle(featureType)));
373 options = new Object[] { MessageManager.getString("label.amend"),
374 MessageManager.getString("action.delete"),
375 MessageManager.getString("action.cancel") };
379 options = new Object[] { MessageManager.getString("action.ok"),
380 MessageManager.getString("action.cancel") };
383 String title = create ? MessageManager
384 .getString("label.create_new_sequence_features")
385 : MessageManager.formatMessage("label.amend_delete_features",
386 new String[] { sequences.get(0).getName() });
391 int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
392 mainPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
393 JvOptionPane.QUESTION_MESSAGE, null, options,
394 MessageManager.getString("action.ok"));
396 FeaturesFile ffile = new FeaturesFile();
398 final String enteredType = name.getText().trim();
399 final String enteredGroup = group.getText().trim();
400 final String enteredDescription = description.getText().replaceAll("\n", " ");
402 if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
405 * update default values only if creating using default values
409 lastFeatureAdded = enteredType;
410 lastFeatureGroupAdded = enteredGroup;
411 // TODO: determine if the null feature group is valid
412 if (lastFeatureGroupAdded.length() < 1)
414 lastFeatureGroupAdded = null;
421 SequenceFeature sf = features.get(featureIndex);
423 if (reply == JvOptionPane.NO_OPTION)
426 * NO_OPTION corresponds to the Delete button
428 sequences.get(0).getDatasetSequence().deleteFeature(sf);
429 // update Feature Settings for removal of feature / group
432 else if (reply == JvOptionPane.YES_OPTION)
435 * 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.equals(enteredType) || !featureGroup
441 .equals(enteredGroup));
442 refreshSettings |= (fcol != oldcol);
443 setColour(enteredType, fcol);
444 int newBegin = sf.begin;
448 newBegin = ((Integer) start.getValue()).intValue();
449 newEnd = ((Integer) end.getValue()).intValue();
450 } catch (NumberFormatException ex)
452 // JSpinner doesn't accept invalid format data :-)
456 * replace the feature by deleting it and adding a new one
457 * (to ensure integrity of SequenceFeatures data store)
459 sequences.get(0).deleteFeature(sf);
460 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
461 newBegin, newEnd, enteredGroup, sf.getScore());
462 newSf.setDescription(enteredDescription);
463 ffile.parseDescriptionHTML(newSf, false);
464 // amend features dialog only updates one sequence at a time
465 sequences.get(0).addSequenceFeature(newSf);
474 // NEW FEATURES ADDED
476 if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
478 for (int i = 0; i < sequences.size(); i++)
480 SequenceFeature sf = features.get(i);
481 SequenceFeature sf2 = new SequenceFeature(enteredType,
482 enteredDescription, sf.getBegin(), sf.getEnd(),
484 ffile.parseDescriptionHTML(sf2, false);
485 sequences.get(i).addSequenceFeature(sf2);
488 setColour(enteredType, fcol);
492 alignPanel.paintAlignment(true);
502 alignPanel.paintAlignment(true);
508 * Show a warning message if the entered type is one that is currently hidden
513 protected void warnIfTypeHidden(JPanel panel, String type)
515 if (getRenderOrder().contains(type))
517 if (!showFeatureOfType(type))
519 String msg = MessageManager.formatMessage("label.warning_hidden",
520 MessageManager.getString("label.feature_type"), type);
521 JvOptionPane.showMessageDialog(panel, msg, "",
522 JvOptionPane.OK_OPTION);
528 * Show a warning message if the entered group is one that is currently hidden
533 protected void warnIfGroupHidden(JPanel panel, String group)
535 if (featureGroups.containsKey(group) && !featureGroups.get(group))
537 String msg = MessageManager.formatMessage("label.warning_hidden",
538 MessageManager.getString("label.group"), group);
539 JvOptionPane.showMessageDialog(panel, msg, "", JvOptionPane.OK_OPTION);
544 * update the amend feature button dependent on the given style
550 protected void updateColourButton(JPanel bigPanel, JLabel colour,
554 colour.setIcon(null);
555 colour.setToolTipText(null);
558 if (col.isSimpleColour())
560 colour.setBackground(col.getColour());
564 colour.setBackground(bigPanel.getBackground());
565 colour.setForeground(Color.black);
566 FeatureSettings.renderGraduatedColor(colour, col);
571 * Orders features in render precedence (last in order is last to render, so
572 * displayed on top of other features)
576 public void orderFeatures(Comparator<String> order)
578 Arrays.sort(renderOrder, order);