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.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.FeatureColour;
28 import jalview.schemes.UserColourScheme;
29 import jalview.util.MessageManager;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
35 import java.awt.GridLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.ItemEvent;
39 import java.awt.event.ItemListener;
40 import java.awt.event.MouseAdapter;
41 import java.awt.event.MouseEvent;
43 import javax.swing.JColorChooser;
44 import javax.swing.JComboBox;
45 import javax.swing.JLabel;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JSpinner;
50 import javax.swing.JTextArea;
51 import javax.swing.JTextField;
52 import javax.swing.SwingConstants;
60 public class FeatureRenderer extends
61 jalview.renderer.seqfeatures.FeatureRenderer implements
62 jalview.api.FeatureRenderer
69 * Creates a new FeatureRenderer object.
74 public FeatureRenderer(AlignmentPanel ap)
79 if (ap != null && ap.getSeqPanel() != null
80 && ap.getSeqPanel().seqCanvas != null
81 && ap.getSeqPanel().seqCanvas.fr != null)
83 transferSettings(ap.getSeqPanel().seqCanvas.fr);
88 // // Feature Editing Dialog
89 // // Will be refactored in next release.
91 static String lastFeatureAdded;
93 static String lastFeatureGroupAdded;
95 static String lastDescriptionAdded;
97 FeatureColourI oldcol, fcol;
101 boolean amendFeatures(final SequenceI[] sequences,
102 final SequenceFeature[] features, boolean newFeatures,
103 final AlignmentPanel ap)
108 final JPanel bigPanel = new JPanel(new BorderLayout());
109 final JComboBox overlaps;
110 final JTextField name = new JTextField(25);
111 final JTextField source = new JTextField(25);
112 final JTextArea description = new JTextArea(3, 25);
113 final JSpinner start = new JSpinner();
114 final JSpinner end = new JSpinner();
115 start.setPreferredSize(new Dimension(80, 20));
116 end.setPreferredSize(new Dimension(80, 20));
117 final FeatureRenderer me = this;
118 final JLabel colour = new JLabel();
119 colour.setOpaque(true);
120 // colour.setBorder(BorderFactory.createEtchedBorder());
121 colour.setMaximumSize(new Dimension(30, 16));
122 colour.addMouseListener(new MouseAdapter()
124 FeatureColourChooser fcc = null;
127 public void mousePressed(MouseEvent evt)
129 if (fcol.isSimpleColour())
131 Color col = JColorChooser.showDialog(Desktop.desktop,
132 MessageManager.getString("label.select_feature_colour"),
136 fcol = new FeatureColour(col);
137 updateColourButton(bigPanel, colour, new FeatureColour(col));
144 final String type = features[featureIndex].getType();
145 fcc = new FeatureColourChooser(me, type);
146 fcc.setRequestFocusEnabled(true);
149 fcc.addActionListener(new ActionListener()
153 public void actionPerformed(ActionEvent e)
155 fcol = fcc.getLastColour();
157 setColour(type, fcol);
158 updateColourButton(bigPanel, colour, fcol);
166 JPanel tmp = new JPanel();
167 JPanel panel = new JPanel(new GridLayout(3, 1));
169 // /////////////////////////////////////
170 // /MULTIPLE FEATURES AT SELECTED RESIDUE
171 if (!newFeatures && features.length > 1)
173 panel = new JPanel(new GridLayout(4, 1));
175 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
176 overlaps = new JComboBox();
177 for (int i = 0; i < features.length; i++)
179 overlaps.addItem(features[i].getType() + "/"
180 + features[i].getBegin() + "-" + features[i].getEnd()
181 + " (" + features[i].getFeatureGroup() + ")");
186 overlaps.addItemListener(new ItemListener()
189 public void itemStateChanged(ItemEvent e)
191 int index = overlaps.getSelectedIndex();
194 featureIndex = index;
195 name.setText(features[index].getType());
196 description.setText(features[index].getDescription());
197 source.setText(features[index].getFeatureGroup());
198 start.setValue(new Integer(features[index].getBegin()));
199 end.setValue(new Integer(features[index].getEnd()));
201 SearchResults highlight = new SearchResults();
202 highlight.addResult(sequences[0], features[index].getBegin(),
203 features[index].getEnd());
205 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
208 FeatureColourI col = getFeatureStyle(name.getText());
211 col = new FeatureColour(UserColourScheme
212 .createColourFromName(name.getText()));
215 updateColourButton(bigPanel, colour, col);
222 // ////////////////////////////////////
226 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
231 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
237 tmp.add(new JLabel(MessageManager.getString("label.colour"),
240 colour.setPreferredSize(new Dimension(150, 15));
241 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
242 colour.setForeground(Color.black);
243 colour.setHorizontalAlignment(SwingConstants.CENTER);
244 colour.setVerticalAlignment(SwingConstants.CENTER);
245 colour.setHorizontalTextPosition(SwingConstants.CENTER);
246 colour.setVerticalTextPosition(SwingConstants.CENTER);
247 bigPanel.add(panel, BorderLayout.NORTH);
249 panel = new JPanel();
250 panel.add(new JLabel(MessageManager.getString("label.description"),
252 description.setFont(JvSwingUtils.getTextAreaFont());
253 description.setLineWrap(true);
254 panel.add(new JScrollPane(description));
258 bigPanel.add(panel, BorderLayout.SOUTH);
260 panel = new JPanel();
261 panel.add(new JLabel(MessageManager.getString("label.start"),
264 panel.add(new JLabel(MessageManager.getString("label.end"),
267 bigPanel.add(panel, BorderLayout.CENTER);
271 bigPanel.add(panel, BorderLayout.CENTER);
274 if (lastFeatureAdded == null)
276 if (features[0].type != null)
278 lastFeatureAdded = features[0].type;
282 lastFeatureAdded = "feature_1";
286 if (lastFeatureGroupAdded == null)
288 if (features[0].featureGroup != null)
290 lastFeatureGroupAdded = features[0].featureGroup;
294 lastFeatureGroupAdded = "Jalview";
300 name.setText(lastFeatureAdded);
301 source.setText(lastFeatureGroupAdded);
305 name.setText(features[0].getType());
306 source.setText(features[0].getFeatureGroup());
309 start.setValue(new Integer(features[0].getBegin()));
310 end.setValue(new Integer(features[0].getEnd()));
311 description.setText(features[0].getDescription());
312 updateColourButton(bigPanel, colour,
313 (oldcol = fcol = getFeatureStyle(name.getText())));
317 options = new Object[] { "Amend", "Delete", "Cancel" };
321 options = new Object[] { "OK", "Cancel" };
324 String title = newFeatures ? MessageManager
325 .getString("label.create_new_sequence_features")
326 : MessageManager.formatMessage("label.amend_delete_features",
327 new String[] { sequences[0].getName() });
329 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
330 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
331 JOptionPane.QUESTION_MESSAGE, null, options,
332 MessageManager.getString("action.ok"));
334 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
336 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
338 // This ensures that the last sequence
339 // is refreshed and new features are rendered
341 lastFeatureAdded = name.getText().trim();
342 lastFeatureGroupAdded = source.getText().trim();
343 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
344 // TODO: determine if the null feature group is valid
345 if (lastFeatureGroupAdded.length() < 1)
347 lastFeatureGroupAdded = null;
353 SequenceFeature sf = features[featureIndex];
355 if (reply == JOptionPane.NO_OPTION)
357 sequences[0].getDatasetSequence().deleteFeature(sf);
359 else if (reply == JOptionPane.YES_OPTION)
361 sf.type = lastFeatureAdded;
362 sf.featureGroup = lastFeatureGroupAdded;
363 sf.description = lastDescriptionAdded;
365 setColour(sf.type, fcol);
366 getFeaturesDisplayed().setVisible(sf.type);
370 sf.begin = ((Integer) start.getValue()).intValue();
371 sf.end = ((Integer) end.getValue()).intValue();
372 } catch (NumberFormatException ex)
376 ffile.parseDescriptionHTML(sf, false);
380 // NEW FEATURES ADDED
382 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
384 for (int i = 0; i < sequences.length; i++)
386 features[i].type = lastFeatureAdded;
387 // fix for JAL-1538 - always set feature group here
388 features[i].featureGroup = lastFeatureGroupAdded;
389 features[i].description = lastDescriptionAdded;
390 sequences[i].addSequenceFeature(features[i]);
391 ffile.parseDescriptionHTML(features[i], false);
394 if (lastFeatureGroupAdded != null)
396 setGroupVisibility(lastFeatureGroupAdded, true);
398 setColour(lastFeatureAdded, fcol);
399 setVisible(lastFeatureAdded);
401 findAllFeatures(false);
403 ap.paintAlignment(true);
413 ap.paintAlignment(true);
419 * update the amend feature button dependent on the given style
425 protected void updateColourButton(JPanel bigPanel, JLabel colour,
429 colour.setIcon(null);
430 colour.setToolTipText(null);
433 if (col.isSimpleColour())
435 colour.setBackground(col.getColour());
439 colour.setBackground(bigPanel.getBackground());
440 colour.setForeground(Color.black);
441 FeatureSettings.renderGraduatedColor(colour, col);