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.Arrays;
45 import java.util.Comparator;
46 import java.util.List;
48 import javax.swing.JColorChooser;
49 import javax.swing.JComboBox;
50 import javax.swing.JLabel;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollPane;
53 import javax.swing.JSpinner;
54 import javax.swing.JTextArea;
55 import javax.swing.JTextField;
56 import javax.swing.SwingConstants;
57 import javax.swing.event.DocumentEvent;
58 import javax.swing.event.DocumentListener;
66 public class FeatureRenderer extends
67 jalview.renderer.seqfeatures.FeatureRenderer
70 * defaults for creating a new feature are the last created
71 * feature type and group
73 static String lastFeatureAdded = "feature_1";
75 static String lastFeatureGroupAdded = "Jalview";
82 * Creates a new FeatureRenderer object
86 public FeatureRenderer(AlignmentPanel alignPanel)
90 if (alignPanel.getSeqPanel() != null
91 && alignPanel.getSeqPanel().seqCanvas != null
92 && alignPanel.getSeqPanel().seqCanvas.fr != null)
94 transferSettings(alignPanel.getSeqPanel().seqCanvas.fr);
98 FeatureColourI oldcol, fcol;
100 int featureIndex = 0;
103 * Presents a dialog allowing the user to add new features, or amend or delete
104 * existing features. Currently this can be on
106 * <li>double-click on a sequence - Amend/Delete features at position</li>
107 * <li>Create sequence feature from pop-up menu on selected region</li>
108 * <li>Create features for pattern matches from Find</li>
110 * If the supplied feature type is null, show (and update on confirm) the type
111 * and group of the last new feature created (with initial defaults of
112 * "feature_1" and "Jalview").
115 * the sequences features are to be created on (if creating
116 * features), or a single sequence (if amending features)
118 * the current features at the position (if amending), or template
119 * new feature(s) with start/end position set (if creating)
121 * true to create features, false to amend or delete
125 protected boolean amendFeatures(final List<SequenceI> sequences,
126 final List<SequenceFeature> features, boolean create,
127 final AlignmentPanel alignPanel)
131 final JPanel mainPanel = new JPanel(new BorderLayout());
133 final JTextField name = new JTextField(25);
134 name.getDocument().addDocumentListener(new DocumentListener()
137 public void insertUpdate(DocumentEvent e)
139 warnIfTypeHidden(mainPanel, name.getText());
143 public void removeUpdate(DocumentEvent e)
145 warnIfTypeHidden(mainPanel, name.getText());
149 public void changedUpdate(DocumentEvent e)
151 warnIfTypeHidden(mainPanel, name.getText());
155 final JTextField group = new JTextField(25);
156 group.getDocument().addDocumentListener(new DocumentListener()
159 public void insertUpdate(DocumentEvent e)
161 warnIfGroupHidden(mainPanel, group.getText());
165 public void removeUpdate(DocumentEvent e)
167 warnIfGroupHidden(mainPanel, group.getText());
171 public void changedUpdate(DocumentEvent e)
173 warnIfGroupHidden(mainPanel, group.getText());
177 final JTextArea description = new JTextArea(3, 25);
178 final JSpinner start = new JSpinner();
179 final JSpinner end = new JSpinner();
180 start.setPreferredSize(new Dimension(80, 20));
181 end.setPreferredSize(new Dimension(80, 20));
182 final FeatureRenderer me = this;
183 final JLabel colour = new JLabel();
184 colour.setOpaque(true);
185 // colour.setBorder(BorderFactory.createEtchedBorder());
186 colour.setMaximumSize(new Dimension(30, 16));
187 colour.addMouseListener(new MouseAdapter()
189 FeatureColourChooser fcc = null;
192 public void mousePressed(MouseEvent evt)
194 if (fcol.isSimpleColour())
196 Color col = JColorChooser.showDialog(Desktop.desktop,
197 MessageManager.getString("label.select_feature_colour"),
201 fcol = new FeatureColour(col);
202 updateColourButton(mainPanel, colour, fcol);
209 final String ft = features.get(featureIndex).getType();
210 final String type = ft == null ? lastFeatureAdded : ft;
211 fcc = new FeatureColourChooser(me, type);
212 fcc.setRequestFocusEnabled(true);
215 fcc.addActionListener(new ActionListener()
219 public void actionPerformed(ActionEvent e)
221 fcol = fcc.getLastColour();
223 setColour(type, fcol);
224 updateColourButton(mainPanel, colour, fcol);
232 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
234 if (!create && features.size() > 1)
237 * more than one feature at selected position - add a drop-down
238 * to choose the feature to amend
240 gridPanel = new JPanel(new GridLayout(4, 1));
241 JPanel choosePanel = new JPanel();
242 choosePanel.add(new JLabel(MessageManager
243 .getString("label.select_feature")
245 final JComboBox<String> overlaps = new JComboBox<String>();
246 for (SequenceFeature sf : features)
248 String text = sf.getType() + "/" + sf.getBegin() + "-"
249 + sf.getEnd() + " (" + sf.getFeatureGroup() + ")";
250 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 alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(
278 FeatureColourI col = getFeatureStyle(name.getText());
281 col = new FeatureColour(ColorUtils
282 .createColourFromName(name.getText()));
285 updateColourButton(mainPanel, colour, col);
289 gridPanel.add(choosePanel);
292 JPanel namePanel = new JPanel();
293 gridPanel.add(namePanel);
294 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
298 JPanel groupPanel = new JPanel();
299 gridPanel.add(groupPanel);
300 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
302 groupPanel.add(group);
304 JPanel colourPanel = new JPanel();
305 gridPanel.add(colourPanel);
306 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
308 colourPanel.add(colour);
309 colour.setPreferredSize(new Dimension(150, 15));
310 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
311 colour.setForeground(Color.black);
312 colour.setHorizontalAlignment(SwingConstants.CENTER);
313 colour.setVerticalAlignment(SwingConstants.CENTER);
314 colour.setHorizontalTextPosition(SwingConstants.CENTER);
315 colour.setVerticalTextPosition(SwingConstants.CENTER);
316 mainPanel.add(gridPanel, BorderLayout.NORTH);
318 JPanel descriptionPanel = new JPanel();
319 descriptionPanel.add(new JLabel(MessageManager
320 .getString("label.description:"),
322 description.setFont(JvSwingUtils.getTextAreaFont());
323 description.setLineWrap(true);
324 descriptionPanel.add(new JScrollPane(description));
328 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
330 JPanel startEndPanel = new JPanel();
331 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
333 startEndPanel.add(start);
334 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
336 startEndPanel.add(end);
337 mainPanel.add(startEndPanel, BorderLayout.CENTER);
341 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
345 * default feature type and group to that of the first feature supplied,
346 * or to the last feature created if not supplied (null value)
348 SequenceFeature firstFeature = features.get(0);
349 boolean useLastDefaults = firstFeature.getType() == null;
350 final String featureType = useLastDefaults ? lastFeatureAdded
351 : firstFeature.getType();
352 final String featureGroup = useLastDefaults ? lastFeatureGroupAdded
353 : firstFeature.getFeatureGroup();
354 name.setText(featureType);
355 group.setText(featureGroup);
357 start.setValue(new Integer(firstFeature.getBegin()));
358 end.setValue(new Integer(firstFeature.getEnd()));
359 description.setText(firstFeature.getDescription());
360 updateColourButton(mainPanel, colour,
361 (oldcol = fcol = getFeatureStyle(featureType)));
365 options = new Object[] { MessageManager.getString("label.amend"),
366 MessageManager.getString("action.delete"),
367 MessageManager.getString("action.cancel") };
371 options = new Object[] { MessageManager.getString("action.ok"),
372 MessageManager.getString("action.cancel") };
375 String title = create ? MessageManager
376 .getString("label.create_new_sequence_features")
377 : MessageManager.formatMessage("label.amend_delete_features",
378 new String[] { sequences.get(0).getName() });
383 int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
384 mainPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
385 JvOptionPane.QUESTION_MESSAGE, null, options,
386 MessageManager.getString("action.ok"));
388 FeaturesFile ffile = new FeaturesFile();
390 final String enteredType = name.getText().trim();
391 final String enteredGroup = group.getText().trim();
392 final String enteredDescription = description.getText().replaceAll("\n", " ");
394 if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
397 * update default values only if creating using default values
401 lastFeatureAdded = enteredType;
402 lastFeatureGroupAdded = enteredGroup;
403 // TODO: determine if the null feature group is valid
404 if (lastFeatureGroupAdded.length() < 1)
406 lastFeatureGroupAdded = null;
413 SequenceFeature sf = features.get(featureIndex);
415 if (reply == JvOptionPane.NO_OPTION)
418 * NO_OPTION corresponds to the Delete button
420 sequences.get(0).getDatasetSequence().deleteFeature(sf);
421 // update Feature Settings for removal of feature / group
424 else if (reply == JvOptionPane.YES_OPTION)
427 * YES_OPTION corresponds to the Amend button
428 * need to refresh Feature Settings if type, group or colour changed;
429 * note we don't force the feature to be visible - the user has been
430 * warned if a hidden feature type or group was entered
432 boolean refreshSettings = (!featureType.equals(enteredType) || !featureGroup
433 .equals(enteredGroup));
434 refreshSettings |= (fcol != oldcol);
435 setColour(enteredType, fcol);
436 int newBegin = sf.begin;
440 newBegin = ((Integer) start.getValue()).intValue();
441 newEnd = ((Integer) end.getValue()).intValue();
442 } catch (NumberFormatException ex)
444 // JSpinner doesn't accept invalid format data :-)
448 * replace the feature by deleting it and adding a new one
449 * (to ensure integrity of SequenceFeatures data store)
451 sequences.get(0).deleteFeature(sf);
452 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
453 newBegin, newEnd, enteredGroup, sf.getScore());
454 sf.setDescription(enteredDescription);
455 ffile.parseDescriptionHTML(newSf, false);
456 // amend features dialog only updates one sequence at a time
457 sequences.get(0).addSequenceFeature(newSf);
466 // NEW FEATURES ADDED
468 if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
470 for (int i = 0; i < sequences.size(); i++)
472 SequenceFeature sf = features.get(i);
473 SequenceFeature sf2 = new SequenceFeature(enteredType,
474 enteredDescription, sf.getBegin(), sf.getEnd(),
476 ffile.parseDescriptionHTML(sf2, false);
477 sequences.get(i).addSequenceFeature(sf2);
480 setColour(enteredType, fcol);
484 alignPanel.paintAlignment(true);
494 alignPanel.paintAlignment(true);
500 * Show a warning message if the entered type is one that is currently hidden
505 protected void warnIfTypeHidden(JPanel panel, String type)
507 if (getRenderOrder().contains(type))
509 if (!showFeatureOfType(type))
511 String msg = MessageManager.formatMessage("label.warning_hidden",
512 MessageManager.getString("label.feature_type"), type);
513 JvOptionPane.showMessageDialog(panel, msg, "",
514 JvOptionPane.OK_OPTION);
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 (featureGroups.containsKey(group) && !featureGroups.get(group))
529 String msg = MessageManager.formatMessage("label.warning_hidden",
530 MessageManager.getString("label.group"), group);
531 JvOptionPane.showMessageDialog(panel, msg, "", JvOptionPane.OK_OPTION);
536 * update the amend feature button dependent on the given style
542 protected void updateColourButton(JPanel bigPanel, JLabel colour,
546 colour.setIcon(null);
547 colour.setToolTipText(null);
550 if (col.isSimpleColour())
552 colour.setBackground(col.getColour());
556 colour.setBackground(bigPanel.getBackground());
557 colour.setForeground(Color.black);
558 FeatureSettings.renderGraduatedColor(colour, col);
563 * Orders features in render precedence (last in order is last to render, so
564 * displayed on top of other features)
568 public void orderFeatures(Comparator<String> order)
570 Arrays.sort(renderOrder, order);