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;
42 import java.util.Arrays;
43 import java.util.Comparator;
45 import javax.swing.JColorChooser;
46 import javax.swing.JComboBox;
47 import javax.swing.JLabel;
48 import javax.swing.JOptionPane;
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 implements
64 jalview.api.FeatureRenderer
71 * Creates a new FeatureRenderer object.
76 public FeatureRenderer(AlignmentPanel ap)
80 if (ap != null && ap.getSeqPanel() != null
81 && ap.getSeqPanel().seqCanvas != null
82 && ap.getSeqPanel().seqCanvas.fr != null)
84 transferSettings(ap.getSeqPanel().seqCanvas.fr);
89 // // Feature Editing Dialog
90 // // Will be refactored in next release.
92 static String lastFeatureAdded;
94 static String lastFeatureGroupAdded;
96 static String lastDescriptionAdded;
98 FeatureColourI oldcol, fcol;
100 int featureIndex = 0;
102 boolean amendFeatures(final SequenceI[] sequences,
103 final SequenceFeature[] features, boolean newFeatures,
104 final AlignmentPanel ap)
109 final JPanel bigPanel = new JPanel(new BorderLayout());
110 final JComboBox overlaps;
111 final JTextField name = new JTextField(25);
112 final JTextField source = new JTextField(25);
113 final JTextArea description = new JTextArea(3, 25);
114 final JSpinner start = new JSpinner();
115 final JSpinner end = new JSpinner();
116 start.setPreferredSize(new Dimension(80, 20));
117 end.setPreferredSize(new Dimension(80, 20));
118 final FeatureRenderer me = this;
119 final JLabel colour = new JLabel();
120 colour.setOpaque(true);
121 // colour.setBorder(BorderFactory.createEtchedBorder());
122 colour.setMaximumSize(new Dimension(30, 16));
123 colour.addMouseListener(new MouseAdapter()
125 FeatureColourChooser fcc = null;
128 public void mousePressed(MouseEvent evt)
130 if (fcol.isSimpleColour())
132 Color col = JColorChooser.showDialog(Desktop.desktop,
133 MessageManager.getString("label.select_feature_colour"),
137 fcol = new FeatureColour(col);
138 updateColourButton(bigPanel, colour, new FeatureColour(col));
145 final String type = features[featureIndex].getType();
146 fcc = new FeatureColourChooser(me, type);
147 fcc.setRequestFocusEnabled(true);
150 fcc.addActionListener(new ActionListener()
154 public void actionPerformed(ActionEvent e)
156 fcol = fcc.getLastColour();
158 setColour(type, fcol);
159 updateColourButton(bigPanel, colour, fcol);
167 JPanel tmp = new JPanel();
168 JPanel panel = new JPanel(new GridLayout(3, 1));
170 // /////////////////////////////////////
171 // /MULTIPLE FEATURES AT SELECTED RESIDUE
172 if (!newFeatures && features.length > 1)
174 panel = new JPanel(new GridLayout(4, 1));
176 tmp.add(new JLabel(MessageManager.getString("label.select_feature")
178 overlaps = new JComboBox();
179 for (int i = 0; i < features.length; i++)
181 overlaps.addItem(features[i].getType() + "/"
182 + features[i].getBegin() + "-" + features[i].getEnd()
183 + " (" + features[i].getFeatureGroup() + ")");
188 overlaps.addItemListener(new ItemListener()
191 public void itemStateChanged(ItemEvent e)
193 int index = overlaps.getSelectedIndex();
196 featureIndex = index;
197 name.setText(features[index].getType());
198 description.setText(features[index].getDescription());
199 source.setText(features[index].getFeatureGroup());
200 start.setValue(new Integer(features[index].getBegin()));
201 end.setValue(new Integer(features[index].getEnd()));
203 SearchResults highlight = new SearchResults();
204 highlight.addResult(sequences[0], features[index].getBegin(),
205 features[index].getEnd());
207 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
210 FeatureColourI col = getFeatureStyle(name.getText());
213 col = new FeatureColour(UserColourScheme
214 .createColourFromName(name.getText()));
217 updateColourButton(bigPanel, colour, col);
224 // ////////////////////////////////////
228 tmp.add(new JLabel(MessageManager.getString("label.name:"),
234 tmp.add(new JLabel(MessageManager.getString("label.group:"),
240 tmp.add(new JLabel(MessageManager.getString("label.colour"),
243 colour.setPreferredSize(new Dimension(150, 15));
244 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
245 colour.setForeground(Color.black);
246 colour.setHorizontalAlignment(SwingConstants.CENTER);
247 colour.setVerticalAlignment(SwingConstants.CENTER);
248 colour.setHorizontalTextPosition(SwingConstants.CENTER);
249 colour.setVerticalTextPosition(SwingConstants.CENTER);
250 bigPanel.add(panel, BorderLayout.NORTH);
252 panel = new JPanel();
253 panel.add(new JLabel(MessageManager.getString("label.description:"),
255 description.setFont(JvSwingUtils.getTextAreaFont());
256 description.setLineWrap(true);
257 panel.add(new JScrollPane(description));
261 bigPanel.add(panel, BorderLayout.SOUTH);
263 panel = new JPanel();
264 panel.add(new JLabel(MessageManager.getString("label.start"),
267 panel.add(new JLabel(MessageManager.getString("label.end"),
270 bigPanel.add(panel, BorderLayout.CENTER);
274 bigPanel.add(panel, BorderLayout.CENTER);
277 if (lastFeatureAdded == null)
279 if (features[0].type != null)
281 lastFeatureAdded = features[0].type;
285 lastFeatureAdded = "feature_1";
289 if (lastFeatureGroupAdded == null)
291 if (features[0].featureGroup != null)
293 lastFeatureGroupAdded = features[0].featureGroup;
297 lastFeatureGroupAdded = "Jalview";
303 name.setText(lastFeatureAdded);
304 source.setText(lastFeatureGroupAdded);
308 name.setText(features[0].getType());
309 source.setText(features[0].getFeatureGroup());
312 start.setValue(new Integer(features[0].getBegin()));
313 end.setValue(new Integer(features[0].getEnd()));
314 description.setText(features[0].getDescription());
315 updateColourButton(bigPanel, colour,
316 (oldcol = fcol = getFeatureStyle(name.getText())));
320 options = new Object[] { "Amend", "Delete", "Cancel" };
324 options = new Object[] { "OK", "Cancel" };
327 String title = newFeatures ? MessageManager
328 .getString("label.create_new_sequence_features")
329 : MessageManager.formatMessage("label.amend_delete_features",
330 new String[] { sequences[0].getName() });
332 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
333 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
334 JOptionPane.QUESTION_MESSAGE, null, options,
335 MessageManager.getString("action.ok"));
337 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
339 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
341 // This ensures that the last sequence
342 // is refreshed and new features are rendered
344 lastFeatureAdded = name.getText().trim();
345 lastFeatureGroupAdded = source.getText().trim();
346 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
347 // TODO: determine if the null feature group is valid
348 if (lastFeatureGroupAdded.length() < 1)
350 lastFeatureGroupAdded = null;
356 SequenceFeature sf = features[featureIndex];
358 if (reply == JOptionPane.NO_OPTION)
360 sequences[0].getDatasetSequence().deleteFeature(sf);
362 else if (reply == JOptionPane.YES_OPTION)
364 sf.type = lastFeatureAdded;
365 sf.featureGroup = lastFeatureGroupAdded;
366 sf.description = lastDescriptionAdded;
368 setColour(sf.type, fcol);
369 getFeaturesDisplayed().setVisible(sf.type);
373 sf.begin = ((Integer) start.getValue()).intValue();
374 sf.end = ((Integer) end.getValue()).intValue();
375 } catch (NumberFormatException ex)
379 ffile.parseDescriptionHTML(sf, false);
383 // NEW FEATURES ADDED
385 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
387 for (int i = 0; i < sequences.length; i++)
389 features[i].type = lastFeatureAdded;
390 // fix for JAL-1538 - always set feature group here
391 features[i].featureGroup = lastFeatureGroupAdded;
392 features[i].description = lastDescriptionAdded;
393 sequences[i].addSequenceFeature(features[i]);
394 ffile.parseDescriptionHTML(features[i], false);
397 if (lastFeatureGroupAdded != null)
399 setGroupVisibility(lastFeatureGroupAdded, true);
401 setColour(lastFeatureAdded, fcol);
402 setVisible(lastFeatureAdded);
404 findAllFeatures(false);
406 ap.paintAlignment(true);
416 ap.paintAlignment(true);
422 * update the amend feature button dependent on the given style
428 protected void updateColourButton(JPanel bigPanel, JLabel colour,
432 colour.setIcon(null);
433 colour.setToolTipText(null);
436 if (col.isSimpleColour())
438 colour.setBackground(col.getColour());
442 colour.setBackground(bigPanel.getBackground());
443 colour.setForeground(Color.black);
444 FeatureSettings.renderGraduatedColor(colour, col);
449 * Orders features in render precedence (last in order is last to render, so
450 * displayed on top of other features)
454 public void orderFeatures(Comparator<String> order)
456 Arrays.sort(renderOrder, order);