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.schemes.FeatureColour;
29 import jalview.util.ColorUtils;
30 import jalview.util.MessageManager;
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Dimension;
36 import java.awt.GridLayout;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.ItemEvent;
40 import java.awt.event.ItemListener;
41 import java.awt.event.MouseAdapter;
42 import java.awt.event.MouseEvent;
43 import java.util.Arrays;
44 import java.util.Comparator;
46 import javax.swing.JColorChooser;
47 import javax.swing.JComboBox;
48 import javax.swing.JLabel;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.JSpinner;
52 import javax.swing.JTextArea;
53 import javax.swing.JTextField;
54 import javax.swing.SwingConstants;
62 public class FeatureRenderer extends
63 jalview.renderer.seqfeatures.FeatureRenderer
70 * Creates a new FeatureRenderer object.
75 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")
177 overlaps = new JComboBox();
178 for (int i = 0; i < features.length; i++)
180 overlaps.addItem(features[i].getType() + "/"
181 + features[i].getBegin() + "-" + features[i].getEnd()
182 + " (" + features[i].getFeatureGroup() + ")");
187 overlaps.addItemListener(new ItemListener()
190 public void itemStateChanged(ItemEvent e)
192 int index = overlaps.getSelectedIndex();
195 featureIndex = index;
196 name.setText(features[index].getType());
197 description.setText(features[index].getDescription());
198 source.setText(features[index].getFeatureGroup());
199 start.setValue(new Integer(features[index].getBegin()));
200 end.setValue(new Integer(features[index].getEnd()));
202 SearchResultsI highlight = new SearchResults();
203 highlight.addResult(sequences[0], features[index].getBegin(),
204 features[index].getEnd());
206 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
209 FeatureColourI col = getFeatureStyle(name.getText());
212 col = new FeatureColour(ColorUtils
213 .createColourFromName(name.getText()));
216 updateColourButton(bigPanel, colour, col);
223 // ////////////////////////////////////
227 tmp.add(new JLabel(MessageManager.getString("label.name:"),
233 tmp.add(new JLabel(MessageManager.getString("label.group:"),
239 tmp.add(new JLabel(MessageManager.getString("label.colour"),
242 colour.setPreferredSize(new Dimension(150, 15));
243 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
244 colour.setForeground(Color.black);
245 colour.setHorizontalAlignment(SwingConstants.CENTER);
246 colour.setVerticalAlignment(SwingConstants.CENTER);
247 colour.setHorizontalTextPosition(SwingConstants.CENTER);
248 colour.setVerticalTextPosition(SwingConstants.CENTER);
249 bigPanel.add(panel, BorderLayout.NORTH);
251 panel = new JPanel();
252 panel.add(new JLabel(MessageManager.getString("label.description:"),
254 description.setFont(JvSwingUtils.getTextAreaFont());
255 description.setLineWrap(true);
256 panel.add(new JScrollPane(description));
260 bigPanel.add(panel, BorderLayout.SOUTH);
262 panel = new JPanel();
263 panel.add(new JLabel(MessageManager.getString("label.start"),
266 panel.add(new JLabel(MessageManager.getString("label.end"),
269 bigPanel.add(panel, BorderLayout.CENTER);
273 bigPanel.add(panel, BorderLayout.CENTER);
276 if (lastFeatureAdded == null)
278 if (features[0].type != null)
280 lastFeatureAdded = features[0].type;
284 lastFeatureAdded = "feature_1";
288 if (lastFeatureGroupAdded == null)
290 if (features[0].featureGroup != null)
292 lastFeatureGroupAdded = features[0].featureGroup;
296 lastFeatureGroupAdded = "Jalview";
302 name.setText(lastFeatureAdded);
303 source.setText(lastFeatureGroupAdded);
307 name.setText(features[0].getType());
308 source.setText(features[0].getFeatureGroup());
311 start.setValue(new Integer(features[0].getBegin()));
312 end.setValue(new Integer(features[0].getEnd()));
313 description.setText(features[0].getDescription());
314 updateColourButton(bigPanel, colour,
315 (oldcol = fcol = getFeatureStyle(name.getText())));
319 options = new Object[] { "Amend", "Delete", "Cancel" };
323 options = new Object[] { "OK", "Cancel" };
326 String title = newFeatures ? MessageManager
327 .getString("label.create_new_sequence_features")
328 : MessageManager.formatMessage("label.amend_delete_features",
329 new String[] { sequences[0].getName() });
331 int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
332 bigPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
333 JvOptionPane.QUESTION_MESSAGE, null, options,
334 MessageManager.getString("action.ok"));
336 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
338 if (reply == JvOptionPane.OK_OPTION && name.getText().length() > 0)
340 lastFeatureAdded = name.getText().trim();
341 lastFeatureGroupAdded = source.getText().trim();
342 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
343 // TODO: determine if the null feature group is valid
344 if (lastFeatureGroupAdded.length() < 1)
346 lastFeatureGroupAdded = null;
352 SequenceFeature sf = features[featureIndex];
354 if (reply == JvOptionPane.NO_OPTION)
356 sequences[0].getDatasetSequence().deleteFeature(sf);
358 else if (reply == JvOptionPane.YES_OPTION)
360 sf.type = lastFeatureAdded;
361 sf.featureGroup = lastFeatureGroupAdded;
362 sf.description = lastDescriptionAdded;
364 setColour(sf.type, fcol);
365 getFeaturesDisplayed().setVisible(sf.type);
369 sf.begin = ((Integer) start.getValue()).intValue();
370 sf.end = ((Integer) end.getValue()).intValue();
371 } catch (NumberFormatException ex)
375 ffile.parseDescriptionHTML(sf, false);
379 // NEW FEATURES ADDED
381 if (reply == JvOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
383 for (int i = 0; i < sequences.length; i++)
385 features[i].type = lastFeatureAdded;
386 // fix for JAL-1538 - always set feature group here
387 features[i].featureGroup = lastFeatureGroupAdded;
388 features[i].description = lastDescriptionAdded;
389 sequences[i].addSequenceFeature(features[i]);
390 ffile.parseDescriptionHTML(features[i], false);
393 if (lastFeatureGroupAdded != null)
395 setGroupVisibility(lastFeatureGroupAdded, true);
397 setColour(lastFeatureAdded, fcol);
398 setVisible(lastFeatureAdded);
400 findAllFeatures(false);
402 ap.paintAlignment(true);
412 ap.paintAlignment(true);
418 * update the amend feature button dependent on the given style
424 protected void updateColourButton(JPanel bigPanel, JLabel colour,
428 colour.setIcon(null);
429 colour.setToolTipText(null);
432 if (col.isSimpleColour())
434 colour.setBackground(col.getColour());
438 colour.setBackground(bigPanel.getBackground());
439 colour.setForeground(Color.black);
440 FeatureSettings.renderGraduatedColor(colour, col);
445 * Orders features in render precedence (last in order is last to render, so
446 * displayed on top of other features)
450 public void orderFeatures(Comparator<String> order)
452 Arrays.sort(renderOrder, order);