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.HashMap;
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;
65 public class FeatureRenderer extends
66 jalview.renderer.seqfeatures.FeatureRenderer
73 * Creates a new FeatureRenderer object
77 public FeatureRenderer(AlignmentPanel alignPanel)
81 if (alignPanel.getSeqPanel() != null
82 && alignPanel.getSeqPanel().seqCanvas != null
83 && alignPanel.getSeqPanel().seqCanvas.fr != null)
85 transferSettings(alignPanel.getSeqPanel().seqCanvas.fr);
90 // // Feature Editing Dialog
91 // // Will be refactored in next release.
93 static String lastFeatureAdded;
95 static String lastFeatureGroupAdded;
97 static String lastDescriptionAdded;
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>
113 * the sequences features are to be created on (if creating
114 * features), or a single sequence (if amending features)
116 * the current features at the position (if amending), or template
117 * new features with start/end position set (if creating)
119 * true to create features, false to amend or delete
121 * the feature type to set on new features; if null, defaults to the
122 * type of the last new feature created if any, failing that to
127 protected boolean amendFeatures(final List<SequenceI> sequences,
128 final List<SequenceFeature> features, boolean create,
129 final AlignmentPanel alignPanel, String featureType)
134 final JPanel mainPanel = new JPanel(new BorderLayout());
135 final JTextField name = new JTextField(25);
136 final JTextField source = new JTextField(25);
137 final JTextArea description = new JTextArea(3, 25);
138 final JSpinner start = new JSpinner();
139 final JSpinner end = new JSpinner();
140 start.setPreferredSize(new Dimension(80, 20));
141 end.setPreferredSize(new Dimension(80, 20));
142 final FeatureRenderer me = this;
143 final JLabel colour = new JLabel();
144 colour.setOpaque(true);
145 // colour.setBorder(BorderFactory.createEtchedBorder());
146 colour.setMaximumSize(new Dimension(30, 16));
147 colour.addMouseListener(new MouseAdapter()
149 FeatureColourChooser fcc = null;
152 public void mousePressed(MouseEvent evt)
154 if (fcol.isSimpleColour())
156 Color col = JColorChooser.showDialog(Desktop.desktop,
157 MessageManager.getString("label.select_feature_colour"),
161 fcol = new FeatureColour(col);
162 updateColourButton(mainPanel, colour, new FeatureColour(col));
169 final String ft = features.get(featureIndex).getType();
170 final String type = ft == null ? lastFeatureAdded : ft;
171 fcc = new FeatureColourChooser(me, type);
172 fcc.setRequestFocusEnabled(true);
175 fcc.addActionListener(new ActionListener()
179 public void actionPerformed(ActionEvent e)
181 fcol = fcc.getLastColour();
183 setColour(type, fcol);
184 updateColourButton(mainPanel, colour, fcol);
192 JPanel gridPanel = new JPanel(new GridLayout(3, 1));
194 if (!create && features.size() > 1)
197 * more than one feature at selected position - add a drop-down
198 * to choose the feature to amend
200 gridPanel = new JPanel(new GridLayout(4, 1));
201 JPanel choosePanel = new JPanel();
202 choosePanel.add(new JLabel(MessageManager
203 .getString("label.select_feature")
205 final JComboBox<String> overlaps = new JComboBox<String>();
206 for (SequenceFeature sf : features)
208 String text = sf.getType() + "/" + sf.getBegin() + "-"
209 + sf.getEnd() + " (" + sf.getFeatureGroup() + ")";
210 overlaps.addItem(text);
212 choosePanel.add(overlaps);
214 overlaps.addItemListener(new ItemListener()
217 public void itemStateChanged(ItemEvent e)
219 int index = overlaps.getSelectedIndex();
222 featureIndex = index;
223 SequenceFeature sf = features.get(index);
224 name.setText(sf.getType());
225 description.setText(sf.getDescription());
226 source.setText(sf.getFeatureGroup());
227 start.setValue(new Integer(sf.getBegin()));
228 end.setValue(new Integer(sf.getEnd()));
230 SearchResultsI highlight = new SearchResults();
231 highlight.addResult(sequences.get(0), sf.getBegin(),
234 alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
237 FeatureColourI col = getFeatureStyle(name.getText());
240 col = new FeatureColour(ColorUtils
241 .createColourFromName(name.getText()));
244 updateColourButton(mainPanel, colour, col);
248 gridPanel.add(choosePanel);
251 // ////////////////////////////////////
253 JPanel namePanel = new JPanel();
254 gridPanel.add(namePanel);
255 namePanel.add(new JLabel(MessageManager.getString("label.name:"),
259 JPanel groupPanel = new JPanel();
260 gridPanel.add(groupPanel);
261 groupPanel.add(new JLabel(MessageManager.getString("label.group:"),
263 groupPanel.add(source);
265 JPanel colourPanel = new JPanel();
266 gridPanel.add(colourPanel);
267 colourPanel.add(new JLabel(MessageManager.getString("label.colour"),
269 colourPanel.add(colour);
270 colour.setPreferredSize(new Dimension(150, 15));
271 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
272 colour.setForeground(Color.black);
273 colour.setHorizontalAlignment(SwingConstants.CENTER);
274 colour.setVerticalAlignment(SwingConstants.CENTER);
275 colour.setHorizontalTextPosition(SwingConstants.CENTER);
276 colour.setVerticalTextPosition(SwingConstants.CENTER);
277 mainPanel.add(gridPanel, BorderLayout.NORTH);
279 JPanel descriptionPanel = new JPanel();
280 descriptionPanel.add(new JLabel(MessageManager
281 .getString("label.description:"),
283 description.setFont(JvSwingUtils.getTextAreaFont());
284 description.setLineWrap(true);
285 descriptionPanel.add(new JScrollPane(description));
289 mainPanel.add(descriptionPanel, BorderLayout.SOUTH);
291 JPanel startEndPanel = new JPanel();
292 startEndPanel.add(new JLabel(MessageManager.getString("label.start"),
294 startEndPanel.add(start);
295 startEndPanel.add(new JLabel(MessageManager.getString("label.end"),
297 startEndPanel.add(end);
298 mainPanel.add(startEndPanel, BorderLayout.CENTER);
302 mainPanel.add(descriptionPanel, BorderLayout.CENTER);
305 SequenceFeature firstFeature = features.get(0);
306 if (featureType != null)
308 lastFeatureAdded = featureType;
312 if (lastFeatureAdded == null)
314 if (firstFeature.type != null)
316 lastFeatureAdded = firstFeature.type;
320 lastFeatureAdded = "feature_1";
325 if (lastFeatureGroupAdded == null)
327 if (firstFeature.featureGroup != null)
329 lastFeatureGroupAdded = firstFeature.featureGroup;
333 lastFeatureGroupAdded = "Jalview";
339 name.setText(lastFeatureAdded);
340 source.setText(lastFeatureGroupAdded);
344 name.setText(firstFeature.getType());
345 source.setText(firstFeature.getFeatureGroup());
348 start.setValue(new Integer(firstFeature.getBegin()));
349 end.setValue(new Integer(firstFeature.getEnd()));
350 description.setText(firstFeature.getDescription());
351 updateColourButton(mainPanel, colour,
352 (oldcol = fcol = getFeatureStyle(name.getText())));
356 options = new Object[] { MessageManager.getString("label.amend"),
357 MessageManager.getString("action.delete"),
358 MessageManager.getString("action.cancel") };
362 options = new Object[] { MessageManager.getString("action.ok"),
363 MessageManager.getString("action.cancel") };
366 String title = create ? MessageManager
367 .getString("label.create_new_sequence_features")
368 : MessageManager.formatMessage("label.amend_delete_features",
369 new String[] { sequences.get(0).getName() });
374 int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
375 mainPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
376 JvOptionPane.QUESTION_MESSAGE, null, options,
377 MessageManager.getString("action.ok"));
379 FeaturesFile ffile = new FeaturesFile();
381 if (reply == JvOptionPane.OK_OPTION && name.getText().length() > 0)
383 lastFeatureAdded = name.getText().trim();
384 lastFeatureGroupAdded = source.getText().trim();
385 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
386 // TODO: determine if the null feature group is valid
387 if (lastFeatureGroupAdded.length() < 1)
389 lastFeatureGroupAdded = null;
395 SequenceFeature sf = features.get(featureIndex);
397 if (reply == JvOptionPane.NO_OPTION)
400 * NO_OPTION corresponds to the Delete button
402 sequences.get(0).getDatasetSequence().deleteFeature(sf);
404 else if (reply == JvOptionPane.YES_OPTION)
407 * YES_OPTION corresponds to the Amend button
409 boolean typeChanged = !lastFeatureAdded.equals(sf.type);
410 String newType = lastFeatureAdded;
411 String newFeatureGroup = lastFeatureGroupAdded;
412 String newDescription = lastDescriptionAdded;
414 setColour(newType, fcol);
415 getFeaturesDisplayed().setVisible(newType);
416 int newBegin = sf.begin;
420 newBegin = ((Integer) start.getValue()).intValue();
421 newEnd = ((Integer) end.getValue()).intValue();
422 } catch (NumberFormatException ex)
424 // JSpinner doesn't accept invalid format data :-)
428 * replace the feature by deleting it and adding a new one
429 * (to ensure integrity of SequenceFeatures data store)
431 sequences.get(0).deleteFeature(sf);
432 SequenceFeature newSf = new SequenceFeature(newType,
433 newDescription, newBegin, newEnd, sf.getScore(),
435 // ensure any additional properties are copied
436 if (sf.otherDetails != null)
438 newSf.otherDetails = new HashMap<String, Object>(sf.otherDetails);
440 ffile.parseDescriptionHTML(newSf, false);
441 // add any additional links not parsed from description
442 if (sf.links != null)
444 for (String link : sf.links)
449 // amend features only gets one sequence to act on
450 sequences.get(0).addSequenceFeature(newSf);
459 // NEW FEATURES ADDED
461 if (reply == JvOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
463 for (int i = 0; i < sequences.size(); i++)
465 SequenceFeature sf = features.get(i);
466 sf.type = lastFeatureAdded;
467 // fix for JAL-1538 - always set feature group here
468 sf.featureGroup = lastFeatureGroupAdded;
469 sf.description = lastDescriptionAdded;
470 sequences.get(i).addSequenceFeature(sf);
471 ffile.parseDescriptionHTML(sf, false);
474 if (lastFeatureGroupAdded != null)
476 setGroupVisibility(lastFeatureGroupAdded, true);
478 setColour(lastFeatureAdded, fcol);
479 setVisible(lastFeatureAdded);
481 findAllFeatures(false);
483 alignPanel.paintAlignment(true);
493 alignPanel.paintAlignment(true);
499 * update the amend feature button dependent on the given style
505 protected void updateColourButton(JPanel bigPanel, JLabel colour,
509 colour.setIcon(null);
510 colour.setToolTipText(null);
513 if (col.isSimpleColour())
515 colour.setBackground(col.getColour());
519 colour.setBackground(bigPanel.getBackground());
520 colour.setForeground(Color.black);
521 FeatureSettings.renderGraduatedColor(colour, col);
526 * Orders features in render precedence (last in order is last to render, so
527 * displayed on top of other features)
531 public void orderFeatures(Comparator<String> order)
533 Arrays.sort(renderOrder, order);