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.datamodel.SearchResults;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.GraduatedColor;
27 import jalview.util.MessageManager;
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.Dimension;
33 import java.awt.GridLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38 import java.awt.event.MouseAdapter;
39 import java.awt.event.MouseEvent;
41 import javax.swing.JColorChooser;
42 import javax.swing.JComboBox;
43 import javax.swing.JLabel;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JSpinner;
48 import javax.swing.JTextArea;
49 import javax.swing.JTextField;
50 import javax.swing.SwingConstants;
58 public class FeatureRenderer extends
59 jalview.renderer.seqfeatures.FeatureRenderer implements
60 jalview.api.FeatureRenderer
67 * Creates a new FeatureRenderer object.
72 public FeatureRenderer(AlignmentPanel ap)
77 if (ap != null && ap.getSeqPanel() != null
78 && ap.getSeqPanel().seqCanvas != null
79 && ap.getSeqPanel().seqCanvas.fr != null)
81 transferSettings(ap.getSeqPanel().seqCanvas.fr);
86 // // Feature Editing Dialog
87 // // Will be refactored in next release.
89 static String lastFeatureAdded;
91 static String lastFeatureGroupAdded;
93 static String lastDescriptionAdded;
99 boolean amendFeatures(final SequenceI[] sequences,
100 final SequenceFeature[] features, boolean newFeatures,
101 final AlignmentPanel ap)
106 final JPanel bigPanel = new JPanel(new BorderLayout());
107 final JComboBox overlaps;
108 final JTextField name = new JTextField(25);
109 final JTextField source = new JTextField(25);
110 final JTextArea description = new JTextArea(3, 25);
111 final JSpinner start = new JSpinner();
112 final JSpinner end = new JSpinner();
113 start.setPreferredSize(new Dimension(80, 20));
114 end.setPreferredSize(new Dimension(80, 20));
115 final FeatureRenderer me = this;
116 final JLabel colour = new JLabel();
117 colour.setOpaque(true);
118 // colour.setBorder(BorderFactory.createEtchedBorder());
119 colour.setMaximumSize(new Dimension(30, 16));
120 colour.addMouseListener(new MouseAdapter()
122 FeatureColourChooser fcc = null;
124 public void mousePressed(MouseEvent evt)
126 if (fcol instanceof Color)
128 Color col = JColorChooser.showDialog(Desktop.desktop,
129 MessageManager.getString("label.select_feature_colour"),
134 updateColourButton(bigPanel, colour, col);
142 final String type = features[featureIndex].getType();
143 fcc = new FeatureColourChooser(me, type);
144 fcc.setRequestFocusEnabled(true);
147 fcc.addActionListener(new ActionListener()
150 public void actionPerformed(ActionEvent e)
152 fcol = fcc.getLastColour();
154 setColour(type, fcol);
155 updateColourButton(bigPanel, colour, fcol);
163 JPanel tmp = new JPanel();
164 JPanel panel = new JPanel(new GridLayout(3, 1));
166 // /////////////////////////////////////
167 // /MULTIPLE FEATURES AT SELECTED RESIDUE
168 if (!newFeatures && features.length > 1)
170 panel = new JPanel(new GridLayout(4, 1));
172 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
173 overlaps = new JComboBox();
174 for (int i = 0; i < features.length; i++)
176 overlaps.addItem(features[i].getType() + "/"
177 + features[i].getBegin() + "-" + features[i].getEnd()
178 + " (" + features[i].getFeatureGroup() + ")");
183 overlaps.addItemListener(new ItemListener()
185 public void itemStateChanged(ItemEvent e)
187 int index = overlaps.getSelectedIndex();
190 featureIndex = index;
191 name.setText(features[index].getType());
192 description.setText(features[index].getDescription());
193 source.setText(features[index].getFeatureGroup());
194 start.setValue(new Integer(features[index].getBegin()));
195 end.setValue(new Integer(features[index].getEnd()));
197 SearchResults highlight = new SearchResults();
198 highlight.addResult(sequences[0], features[index].getBegin(),
199 features[index].getEnd());
201 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
204 Object col = getFeatureStyle(name.getText());
207 col = new jalview.schemes.UserColourScheme()
208 .createColourFromName(name.getText());
211 updateColourButton(bigPanel, colour, col);
218 // ////////////////////////////////////
222 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
227 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
233 tmp.add(new JLabel(MessageManager.getString("label.colour"),
236 colour.setPreferredSize(new Dimension(150, 15));
237 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
238 colour.setForeground(Color.black);
239 colour.setHorizontalAlignment(SwingConstants.CENTER);
240 colour.setVerticalAlignment(SwingConstants.CENTER);
241 colour.setHorizontalTextPosition(SwingConstants.CENTER);
242 colour.setVerticalTextPosition(SwingConstants.CENTER);
243 bigPanel.add(panel, BorderLayout.NORTH);
245 panel = new JPanel();
246 panel.add(new JLabel(MessageManager.getString("label.description"),
248 description.setFont(JvSwingUtils.getTextAreaFont());
249 description.setLineWrap(true);
250 panel.add(new JScrollPane(description));
254 bigPanel.add(panel, BorderLayout.SOUTH);
256 panel = new JPanel();
257 panel.add(new JLabel(MessageManager.getString("label.start"),
260 panel.add(new JLabel(MessageManager.getString("label.end"),
263 bigPanel.add(panel, BorderLayout.CENTER);
267 bigPanel.add(panel, BorderLayout.CENTER);
270 if (lastFeatureAdded == null)
272 if (features[0].type != null)
274 lastFeatureAdded = features[0].type;
278 lastFeatureAdded = "feature_1";
282 if (lastFeatureGroupAdded == null)
284 if (features[0].featureGroup != null)
286 lastFeatureGroupAdded = features[0].featureGroup;
290 lastFeatureGroupAdded = "Jalview";
296 name.setText(lastFeatureAdded);
297 source.setText(lastFeatureGroupAdded);
301 name.setText(features[0].getType());
302 source.setText(features[0].getFeatureGroup());
305 start.setValue(new Integer(features[0].getBegin()));
306 end.setValue(new Integer(features[0].getEnd()));
307 description.setText(features[0].getDescription());
308 updateColourButton(bigPanel, colour,
309 (oldcol = fcol = getFeatureStyle(name.getText())));
313 options = new Object[] { "Amend", "Delete", "Cancel" };
317 options = new Object[] { "OK", "Cancel" };
320 String title = newFeatures ? MessageManager
321 .getString("label.create_new_sequence_features")
322 : MessageManager.formatMessage("label.amend_delete_features",
323 new String[] { sequences[0].getName() });
325 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
326 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
327 JOptionPane.QUESTION_MESSAGE, null, options,
328 MessageManager.getString("action.ok"));
330 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
332 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
334 // This ensures that the last sequence
335 // is refreshed and new features are rendered
337 lastFeatureAdded = name.getText().trim();
338 lastFeatureGroupAdded = source.getText().trim();
339 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
340 // TODO: determine if the null feature group is valid
341 if (lastFeatureGroupAdded.length() < 1)
343 lastFeatureGroupAdded = null;
349 SequenceFeature sf = features[featureIndex];
351 if (reply == JOptionPane.NO_OPTION)
353 sequences[0].getDatasetSequence().deleteFeature(sf);
355 else if (reply == JOptionPane.YES_OPTION)
357 sf.type = lastFeatureAdded;
358 sf.featureGroup = lastFeatureGroupAdded;
359 sf.description = lastDescriptionAdded;
361 setColour(sf.type, fcol);
362 getFeaturesDisplayed().setVisible(sf.type);
366 sf.begin = ((Integer) start.getValue()).intValue();
367 sf.end = ((Integer) end.getValue()).intValue();
368 } catch (NumberFormatException ex)
372 ffile.parseDescriptionHTML(sf, false);
376 // NEW FEATURES ADDED
378 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
380 for (int i = 0; i < sequences.length; i++)
382 features[i].type = lastFeatureAdded;
383 // fix for JAL-1538 - always set feature group here
384 features[i].featureGroup = lastFeatureGroupAdded;
385 features[i].description = lastDescriptionAdded;
386 sequences[i].addSequenceFeature(features[i]);
387 ffile.parseDescriptionHTML(features[i], false);
390 if (lastFeatureGroupAdded != null)
392 setGroupVisibility(lastFeatureGroupAdded, true);
394 setColour(lastFeatureAdded, fcol);
395 setVisible(lastFeatureAdded);
397 findAllFeatures(false);
399 ap.paintAlignment(true);
409 ap.paintAlignment(true);
415 * update the amend feature button dependent on the given style
421 protected void updateColourButton(JPanel bigPanel, JLabel colour,
425 colour.setIcon(null);
426 colour.setToolTipText(null);
429 if (col2 instanceof Color)
431 colour.setBackground((Color) col2);
435 colour.setBackground(bigPanel.getBackground());
436 colour.setForeground(Color.black);
437 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
438 // colour.setForeground(colour.getBackground());