2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
25 import java.awt.event.*;
26 import java.awt.image.*;
27 import java.beans.PropertyChangeListener;
31 import jalview.datamodel.*;
32 import jalview.schemes.GraduatedColor;
33 import jalview.util.MessageManager;
41 public class FeatureRenderer extends jalview.renderer.seqfeatures.FeatureRenderer implements jalview.api.FeatureRenderer
48 * Creates a new FeatureRenderer object.
53 public FeatureRenderer(AlignmentPanel ap)
58 if (ap != null && ap.getSeqPanel() != null && ap.getSeqPanel().seqCanvas != null
59 && ap.getSeqPanel().seqCanvas.fr != null)
61 transferSettings(ap.getSeqPanel().seqCanvas.fr);
66 // // Feature Editing Dialog
67 // // Will be refactored in next release.
69 static String lastFeatureAdded;
71 static String lastFeatureGroupAdded;
73 static String lastDescriptionAdded;
79 boolean amendFeatures(final SequenceI[] sequences,
80 final SequenceFeature[] features, boolean newFeatures,
81 final AlignmentPanel ap)
86 final JPanel bigPanel = new JPanel(new BorderLayout());
87 final JComboBox overlaps;
88 final JTextField name = new JTextField(25);
89 final JTextField source = new JTextField(25);
90 final JTextArea description = new JTextArea(3, 25);
91 final JSpinner start = new JSpinner();
92 final JSpinner end = new JSpinner();
93 start.setPreferredSize(new Dimension(80, 20));
94 end.setPreferredSize(new Dimension(80, 20));
95 final FeatureRenderer me = this;
96 final JLabel colour = new JLabel();
97 colour.setOpaque(true);
98 // colour.setBorder(BorderFactory.createEtchedBorder());
99 colour.setMaximumSize(new Dimension(30, 16));
100 colour.addMouseListener(new MouseAdapter()
102 FeatureColourChooser fcc = null;
104 public void mousePressed(MouseEvent evt)
106 if (fcol instanceof Color)
108 Color col = JColorChooser.showDialog(Desktop.desktop,
109 MessageManager.getString("label.select_feature_colour"), ((Color) fcol));
113 updateColourButton(bigPanel, colour, col);
121 final String type = features[featureIndex].getType();
122 fcc = new FeatureColourChooser(me, type);
123 fcc.setRequestFocusEnabled(true);
126 fcc.addActionListener(new ActionListener()
129 public void actionPerformed(ActionEvent e)
131 fcol = fcc.getLastColour();
133 setColour(type, fcol);
134 updateColourButton(bigPanel, colour, fcol);
142 JPanel tmp = new JPanel();
143 JPanel panel = new JPanel(new GridLayout(3, 1));
145 // /////////////////////////////////////
146 // /MULTIPLE FEATURES AT SELECTED RESIDUE
147 if (!newFeatures && features.length > 1)
149 panel = new JPanel(new GridLayout(4, 1));
151 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
152 overlaps = new JComboBox();
153 for (int i = 0; i < features.length; i++)
155 overlaps.addItem(features[i].getType() + "/"
156 + features[i].getBegin() + "-" + features[i].getEnd()
157 + " (" + features[i].getFeatureGroup() + ")");
162 overlaps.addItemListener(new ItemListener()
164 public void itemStateChanged(ItemEvent e)
166 int index = overlaps.getSelectedIndex();
169 featureIndex = index;
170 name.setText(features[index].getType());
171 description.setText(features[index].getDescription());
172 source.setText(features[index].getFeatureGroup());
173 start.setValue(new Integer(features[index].getBegin()));
174 end.setValue(new Integer(features[index].getEnd()));
176 SearchResults highlight = new SearchResults();
177 highlight.addResult(sequences[0], features[index].getBegin(),
178 features[index].getEnd());
180 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
183 Object col = getFeatureStyle(name.getText());
186 col = new jalview.schemes.UserColourScheme()
187 .createColourFromName(name.getText());
190 updateColourButton(bigPanel, colour, col);
197 // ////////////////////////////////////
201 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
206 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
212 tmp.add(new JLabel(MessageManager.getString("label.colour"),
215 colour.setPreferredSize(new Dimension(150, 15));
216 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
217 colour.setForeground(Color.black);
218 colour.setHorizontalAlignment(SwingConstants.CENTER);
219 colour.setVerticalAlignment(SwingConstants.CENTER);
220 colour.setHorizontalTextPosition(SwingConstants.CENTER);
221 colour.setVerticalTextPosition(SwingConstants.CENTER);
222 bigPanel.add(panel, BorderLayout.NORTH);
224 panel = new JPanel();
225 panel.add(new JLabel(MessageManager.getString("label.description"),
227 description.setFont(JvSwingUtils.getTextAreaFont());
228 description.setLineWrap(true);
229 panel.add(new JScrollPane(description));
233 bigPanel.add(panel, BorderLayout.SOUTH);
235 panel = new JPanel();
236 panel.add(new JLabel(MessageManager.getString("label.start"),
239 panel.add(new JLabel(MessageManager.getString("label.end"),
242 bigPanel.add(panel, BorderLayout.CENTER);
246 bigPanel.add(panel, BorderLayout.CENTER);
249 if (lastFeatureAdded == null)
251 if (features[0].type != null)
253 lastFeatureAdded = features[0].type;
257 lastFeatureAdded = "feature_1";
261 if (lastFeatureGroupAdded == null)
263 if (features[0].featureGroup != null)
265 lastFeatureGroupAdded = features[0].featureGroup;
269 lastFeatureGroupAdded = "Jalview";
275 name.setText(lastFeatureAdded);
276 source.setText(lastFeatureGroupAdded);
280 name.setText(features[0].getType());
281 source.setText(features[0].getFeatureGroup());
284 start.setValue(new Integer(features[0].getBegin()));
285 end.setValue(new Integer(features[0].getEnd()));
286 description.setText(features[0].getDescription());
287 updateColourButton(bigPanel, colour,
288 (oldcol = fcol = getFeatureStyle(name.getText())));
292 options = new Object[]
293 { "Amend", "Delete", "Cancel" };
297 options = new Object[]
301 String title = newFeatures ? MessageManager.getString("label.create_new_sequence_features")
302 : MessageManager.formatMessage("label.amend_delete_features", new String[]{sequences[0].getName()});
304 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
305 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
306 JOptionPane.QUESTION_MESSAGE, null, options, MessageManager.getString("action.ok"));
308 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
310 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
312 // This ensures that the last sequence
313 // is refreshed and new features are rendered
315 lastFeatureAdded = name.getText().trim();
316 lastFeatureGroupAdded = source.getText().trim();
317 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
318 // TODO: determine if the null feature group is valid
319 if (lastFeatureGroupAdded.length() < 1)
320 lastFeatureGroupAdded = null;
325 SequenceFeature sf = features[featureIndex];
327 if (reply == JOptionPane.NO_OPTION)
329 sequences[0].getDatasetSequence().deleteFeature(sf);
331 else if (reply == JOptionPane.YES_OPTION)
333 sf.type = lastFeatureAdded;
334 sf.featureGroup = lastFeatureGroupAdded;
335 sf.description = lastDescriptionAdded;
337 setColour(sf.type, fcol);
338 getFeaturesDisplayed().setVisible(sf.type);
342 sf.begin = ((Integer) start.getValue()).intValue();
343 sf.end = ((Integer) end.getValue()).intValue();
344 } catch (NumberFormatException ex)
348 ffile.parseDescriptionHTML(sf, false);
352 // NEW FEATURES ADDED
354 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
356 for (int i = 0; i < sequences.length; i++)
358 features[i].type = lastFeatureAdded;
359 // fix for JAL-1538 - always set feature group here
360 features[i].featureGroup = lastFeatureGroupAdded;
361 features[i].description = lastDescriptionAdded;
362 sequences[i].addSequenceFeature(features[i]);
363 ffile.parseDescriptionHTML(features[i], false);
367 if (lastFeatureGroupAdded != null)
369 setGroupVisibility(lastFeatureGroupAdded, true);
371 setColour(lastFeatureAdded, fcol);
372 setVisible(lastFeatureAdded);
374 findAllFeatures(false);
376 ap.paintAlignment(true);
386 ap.paintAlignment(true);
393 * update the amend feature button dependent on the given style
399 protected void updateColourButton(JPanel bigPanel, JLabel colour,
403 colour.setIcon(null);
404 colour.setToolTipText(null);
407 if (col2 instanceof Color)
409 colour.setBackground((Color) col2);
413 colour.setBackground(bigPanel.getBackground());
414 colour.setForeground(Color.black);
415 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
416 // colour.setForeground(colour.getBackground());