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.JOptionPane;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.JSpinner;
53 import javax.swing.JTextArea;
54 import javax.swing.JTextField;
55 import javax.swing.SwingConstants;
63 public class FeatureRenderer extends
64 jalview.renderer.seqfeatures.FeatureRenderer implements
65 jalview.api.FeatureRenderer
72 * Creates a new FeatureRenderer object.
77 public FeatureRenderer(AlignmentPanel ap)
81 if (ap != null && ap.getSeqPanel() != null
82 && ap.getSeqPanel().seqCanvas != null
83 && ap.getSeqPanel().seqCanvas.fr != null)
85 transferSettings(ap.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;
103 boolean amendFeatures(final SequenceI[] sequences,
104 final SequenceFeature[] features, boolean newFeatures,
105 final AlignmentPanel ap)
110 final JPanel bigPanel = new JPanel(new BorderLayout());
111 final JComboBox overlaps;
112 final JTextField name = new JTextField(25);
113 final JTextField source = new JTextField(25);
114 final JTextArea description = new JTextArea(3, 25);
115 final JSpinner start = new JSpinner();
116 final JSpinner end = new JSpinner();
117 start.setPreferredSize(new Dimension(80, 20));
118 end.setPreferredSize(new Dimension(80, 20));
119 final FeatureRenderer me = this;
120 final JLabel colour = new JLabel();
121 colour.setOpaque(true);
122 // colour.setBorder(BorderFactory.createEtchedBorder());
123 colour.setMaximumSize(new Dimension(30, 16));
124 colour.addMouseListener(new MouseAdapter()
126 FeatureColourChooser fcc = null;
129 public void mousePressed(MouseEvent evt)
131 if (fcol.isSimpleColour())
133 Color col = JColorChooser.showDialog(Desktop.desktop,
134 MessageManager.getString("label.select_feature_colour"),
138 fcol = new FeatureColour(col);
139 updateColourButton(bigPanel, colour, new FeatureColour(col));
146 final String type = features[featureIndex].getType();
147 fcc = new FeatureColourChooser(me, type);
148 fcc.setRequestFocusEnabled(true);
151 fcc.addActionListener(new ActionListener()
155 public void actionPerformed(ActionEvent e)
157 fcol = fcc.getLastColour();
159 setColour(type, fcol);
160 updateColourButton(bigPanel, colour, fcol);
168 JPanel tmp = new JPanel();
169 JPanel panel = new JPanel(new GridLayout(3, 1));
171 // /////////////////////////////////////
172 // /MULTIPLE FEATURES AT SELECTED RESIDUE
173 if (!newFeatures && features.length > 1)
175 panel = new JPanel(new GridLayout(4, 1));
177 tmp.add(new JLabel(MessageManager.getString("label.select_feature")
179 overlaps = new JComboBox();
180 for (int i = 0; i < features.length; i++)
182 overlaps.addItem(features[i].getType() + "/"
183 + features[i].getBegin() + "-" + features[i].getEnd()
184 + " (" + features[i].getFeatureGroup() + ")");
189 overlaps.addItemListener(new ItemListener()
192 public void itemStateChanged(ItemEvent e)
194 int index = overlaps.getSelectedIndex();
197 featureIndex = index;
198 name.setText(features[index].getType());
199 description.setText(features[index].getDescription());
200 source.setText(features[index].getFeatureGroup());
201 start.setValue(new Integer(features[index].getBegin()));
202 end.setValue(new Integer(features[index].getEnd()));
204 SearchResultsI highlight = new SearchResults();
205 highlight.addResult(sequences[0], features[index].getBegin(),
206 features[index].getEnd());
208 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
211 FeatureColourI col = getFeatureStyle(name.getText());
214 col = new FeatureColour(ColorUtils
215 .createColourFromName(name.getText()));
218 updateColourButton(bigPanel, colour, col);
225 // ////////////////////////////////////
229 tmp.add(new JLabel(MessageManager.getString("label.name:"),
235 tmp.add(new JLabel(MessageManager.getString("label.group:"),
241 tmp.add(new JLabel(MessageManager.getString("label.colour"),
244 colour.setPreferredSize(new Dimension(150, 15));
245 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
246 colour.setForeground(Color.black);
247 colour.setHorizontalAlignment(SwingConstants.CENTER);
248 colour.setVerticalAlignment(SwingConstants.CENTER);
249 colour.setHorizontalTextPosition(SwingConstants.CENTER);
250 colour.setVerticalTextPosition(SwingConstants.CENTER);
251 bigPanel.add(panel, BorderLayout.NORTH);
253 panel = new JPanel();
254 panel.add(new JLabel(MessageManager.getString("label.description:"),
256 description.setFont(JvSwingUtils.getTextAreaFont());
257 description.setLineWrap(true);
258 panel.add(new JScrollPane(description));
262 bigPanel.add(panel, BorderLayout.SOUTH);
264 panel = new JPanel();
265 panel.add(new JLabel(MessageManager.getString("label.start"),
268 panel.add(new JLabel(MessageManager.getString("label.end"),
271 bigPanel.add(panel, BorderLayout.CENTER);
275 bigPanel.add(panel, BorderLayout.CENTER);
278 if (lastFeatureAdded == null)
280 if (features[0].type != null)
282 lastFeatureAdded = features[0].type;
286 lastFeatureAdded = "feature_1";
290 if (lastFeatureGroupAdded == null)
292 if (features[0].featureGroup != null)
294 lastFeatureGroupAdded = features[0].featureGroup;
298 lastFeatureGroupAdded = "Jalview";
304 name.setText(lastFeatureAdded);
305 source.setText(lastFeatureGroupAdded);
309 name.setText(features[0].getType());
310 source.setText(features[0].getFeatureGroup());
313 start.setValue(new Integer(features[0].getBegin()));
314 end.setValue(new Integer(features[0].getEnd()));
315 description.setText(features[0].getDescription());
316 updateColourButton(bigPanel, colour,
317 (oldcol = fcol = getFeatureStyle(name.getText())));
321 options = new Object[] { "Amend", "Delete", "Cancel" };
325 options = new Object[] { "OK", "Cancel" };
328 String title = newFeatures ? MessageManager
329 .getString("label.create_new_sequence_features")
330 : MessageManager.formatMessage("label.amend_delete_features",
331 new String[] { sequences[0].getName() });
333 int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
334 bigPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
335 JvOptionPane.QUESTION_MESSAGE, null, options,
336 MessageManager.getString("action.ok"));
338 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
340 if (reply == JvOptionPane.OK_OPTION && name.getText().length() > 0)
342 // This ensures that the last sequence
343 // is refreshed and new features are rendered
345 lastFeatureAdded = name.getText().trim();
346 lastFeatureGroupAdded = source.getText().trim();
347 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
348 // TODO: determine if the null feature group is valid
349 if (lastFeatureGroupAdded.length() < 1)
351 lastFeatureGroupAdded = null;
357 SequenceFeature sf = features[featureIndex];
359 if (reply == JvOptionPane.NO_OPTION)
361 sequences[0].getDatasetSequence().deleteFeature(sf);
363 else if (reply == JvOptionPane.YES_OPTION)
365 sf.type = lastFeatureAdded;
366 sf.featureGroup = lastFeatureGroupAdded;
367 sf.description = lastDescriptionAdded;
369 setColour(sf.type, fcol);
370 getFeaturesDisplayed().setVisible(sf.type);
374 sf.begin = ((Integer) start.getValue()).intValue();
375 sf.end = ((Integer) end.getValue()).intValue();
376 } catch (NumberFormatException ex)
380 ffile.parseDescriptionHTML(sf, false);
384 // NEW FEATURES ADDED
386 if (reply == JvOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
388 for (int i = 0; i < sequences.length; i++)
390 features[i].type = lastFeatureAdded;
391 // fix for JAL-1538 - always set feature group here
392 features[i].featureGroup = lastFeatureGroupAdded;
393 features[i].description = lastDescriptionAdded;
394 sequences[i].addSequenceFeature(features[i]);
395 ffile.parseDescriptionHTML(features[i], false);
398 if (lastFeatureGroupAdded != null)
400 setGroupVisibility(lastFeatureGroupAdded, true);
402 setColour(lastFeatureAdded, fcol);
403 setVisible(lastFeatureAdded);
405 findAllFeatures(false);
407 ap.paintAlignment(true);
417 ap.paintAlignment(true);
423 * update the amend feature button dependent on the given style
429 protected void updateColourButton(JPanel bigPanel, JLabel colour,
433 colour.setIcon(null);
434 colour.setToolTipText(null);
437 if (col.isSimpleColour())
439 colour.setBackground(col.getColour());
443 colour.setBackground(bigPanel.getBackground());
444 colour.setForeground(Color.black);
445 FeatureSettings.renderGraduatedColor(colour, col);
450 * Orders features in render precedence (last in order is last to render, so
451 * displayed on top of other features)
455 public void orderFeatures(Comparator<String> order)
457 Arrays.sort(renderOrder, order);