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;
40 import java.util.Arrays;
41 import java.util.Comparator;
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)
78 if (ap != null && ap.getSeqPanel() != null
79 && ap.getSeqPanel().seqCanvas != null
80 && ap.getSeqPanel().seqCanvas.fr != null)
82 transferSettings(ap.getSeqPanel().seqCanvas.fr);
87 // // Feature Editing Dialog
88 // // Will be refactored in next release.
90 static String lastFeatureAdded;
92 static String lastFeatureGroupAdded;
94 static String lastDescriptionAdded;
100 boolean amendFeatures(final SequenceI[] sequences,
101 final SequenceFeature[] features, boolean newFeatures,
102 final AlignmentPanel ap)
107 final JPanel bigPanel = new JPanel(new BorderLayout());
108 final JComboBox overlaps;
109 final JTextField name = new JTextField(25);
110 final JTextField source = new JTextField(25);
111 final JTextArea description = new JTextArea(3, 25);
112 final JSpinner start = new JSpinner();
113 final JSpinner end = new JSpinner();
114 start.setPreferredSize(new Dimension(80, 20));
115 end.setPreferredSize(new Dimension(80, 20));
116 final FeatureRenderer me = this;
117 final JLabel colour = new JLabel();
118 colour.setOpaque(true);
119 // colour.setBorder(BorderFactory.createEtchedBorder());
120 colour.setMaximumSize(new Dimension(30, 16));
121 colour.addMouseListener(new MouseAdapter()
123 FeatureColourChooser fcc = null;
126 public void mousePressed(MouseEvent evt)
128 if (fcol instanceof Color)
130 Color col = JColorChooser.showDialog(Desktop.desktop,
131 MessageManager.getString("label.select_feature_colour"),
136 updateColourButton(bigPanel, colour, 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 Object col = getFeatureStyle(name.getText());
211 col = new jalview.schemes.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 (col2 instanceof Color)
435 colour.setBackground((Color) col2);
439 colour.setBackground(bigPanel.getBackground());
440 colour.setForeground(Color.black);
441 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
442 // colour.setForeground(colour.getBackground());
447 * Orders features in render precedence (last in order is last to render, so
448 * displayed on top of other features)
452 public void orderFeatures(Comparator<String> order)
454 Arrays.sort(renderOrder, order);