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.io.FeaturesFile;
27 import jalview.schemes.GraduatedColor;
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 jalview.renderer.seqfeatures.FeatureRenderer implements jalview.api.FeatureRenderer
67 * Creates a new FeatureRenderer object.
72 public FeatureRenderer(AlignmentPanel ap)
77 if (ap != null && ap.getSeqPanel() != null && ap.getSeqPanel().seqCanvas != null
78 && ap.getSeqPanel().seqCanvas.fr != null)
80 transferSettings(ap.getSeqPanel().seqCanvas.fr);
85 // // Feature Editing Dialog
86 // // Will be refactored in next release.
88 static String lastFeatureAdded;
90 static String lastFeatureGroupAdded;
92 static String lastDescriptionAdded;
98 boolean amendFeatures(final SequenceI[] sequences,
99 final SequenceFeature[] features, boolean newFeatures,
100 final AlignmentPanel ap)
105 final JPanel bigPanel = new JPanel(new BorderLayout());
106 final JComboBox overlaps;
107 final JTextField name = new JTextField(25);
108 final JTextField source = new JTextField(25);
109 final JTextArea description = new JTextArea(3, 25);
110 final JSpinner start = new JSpinner();
111 final JSpinner end = new JSpinner();
112 start.setPreferredSize(new Dimension(80, 20));
113 end.setPreferredSize(new Dimension(80, 20));
114 final FeatureRenderer me = this;
115 final JLabel colour = new JLabel();
116 colour.setOpaque(true);
117 // colour.setBorder(BorderFactory.createEtchedBorder());
118 colour.setMaximumSize(new Dimension(30, 16));
119 colour.addMouseListener(new MouseAdapter()
121 FeatureColourChooser fcc = null;
123 public void mousePressed(MouseEvent evt)
125 if (fcol instanceof Color)
127 Color col = JColorChooser.showDialog(Desktop.desktop,
128 MessageManager.getString("label.select_feature_colour"), ((Color) fcol));
132 updateColourButton(bigPanel, colour, col);
140 final String type = features[featureIndex].getType();
141 fcc = new FeatureColourChooser(me, type);
142 fcc.setRequestFocusEnabled(true);
145 fcc.addActionListener(new ActionListener()
148 public void actionPerformed(ActionEvent e)
150 fcol = fcc.getLastColour();
152 setColour(type, fcol);
153 updateColourButton(bigPanel, colour, fcol);
161 JPanel tmp = new JPanel();
162 JPanel panel = new JPanel(new GridLayout(3, 1));
164 // /////////////////////////////////////
165 // /MULTIPLE FEATURES AT SELECTED RESIDUE
166 if (!newFeatures && features.length > 1)
168 panel = new JPanel(new GridLayout(4, 1));
170 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
171 overlaps = new JComboBox();
172 for (int i = 0; i < features.length; i++)
174 overlaps.addItem(features[i].getType() + "/"
175 + features[i].getBegin() + "-" + features[i].getEnd()
176 + " (" + features[i].getFeatureGroup() + ")");
181 overlaps.addItemListener(new ItemListener()
183 public void itemStateChanged(ItemEvent e)
185 int index = overlaps.getSelectedIndex();
188 featureIndex = index;
189 name.setText(features[index].getType());
190 description.setText(features[index].getDescription());
191 source.setText(features[index].getFeatureGroup());
192 start.setValue(new Integer(features[index].getBegin()));
193 end.setValue(new Integer(features[index].getEnd()));
195 SearchResults highlight = new SearchResults();
196 highlight.addResult(sequences[0], features[index].getBegin(),
197 features[index].getEnd());
199 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
202 Object col = getFeatureStyle(name.getText());
205 col = new UserColourScheme()
206 .createColourFromName(name.getText());
209 updateColourButton(bigPanel, colour, col);
216 // ////////////////////////////////////
220 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
225 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
231 tmp.add(new JLabel(MessageManager.getString("label.colour"),
234 colour.setPreferredSize(new Dimension(150, 15));
235 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
236 colour.setForeground(Color.black);
237 colour.setHorizontalAlignment(SwingConstants.CENTER);
238 colour.setVerticalAlignment(SwingConstants.CENTER);
239 colour.setHorizontalTextPosition(SwingConstants.CENTER);
240 colour.setVerticalTextPosition(SwingConstants.CENTER);
241 bigPanel.add(panel, BorderLayout.NORTH);
243 panel = new JPanel();
244 panel.add(new JLabel(MessageManager.getString("label.description"),
246 description.setFont(JvSwingUtils.getTextAreaFont());
247 description.setLineWrap(true);
248 panel.add(new JScrollPane(description));
252 bigPanel.add(panel, BorderLayout.SOUTH);
254 panel = new JPanel();
255 panel.add(new JLabel(MessageManager.getString("label.start"),
258 panel.add(new JLabel(MessageManager.getString("label.end"),
261 bigPanel.add(panel, BorderLayout.CENTER);
265 bigPanel.add(panel, BorderLayout.CENTER);
268 if (lastFeatureAdded == null)
270 if (features[0].type != null)
272 lastFeatureAdded = features[0].type;
276 lastFeatureAdded = "feature_1";
280 if (lastFeatureGroupAdded == null)
282 if (features[0].featureGroup != null)
284 lastFeatureGroupAdded = features[0].featureGroup;
288 lastFeatureGroupAdded = "Jalview";
294 name.setText(lastFeatureAdded);
295 source.setText(lastFeatureGroupAdded);
299 name.setText(features[0].getType());
300 source.setText(features[0].getFeatureGroup());
303 start.setValue(new Integer(features[0].getBegin()));
304 end.setValue(new Integer(features[0].getEnd()));
305 description.setText(features[0].getDescription());
306 updateColourButton(bigPanel, colour,
307 (oldcol = fcol = getFeatureStyle(name.getText())));
311 options = new Object[]
312 { "Amend", "Delete", "Cancel" };
316 options = new Object[]
320 String title = newFeatures ? MessageManager.getString("label.create_new_sequence_features")
321 : MessageManager.formatMessage("label.amend_delete_features", new String[]{sequences[0].getName()});
323 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
324 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
325 JOptionPane.QUESTION_MESSAGE, null, options, MessageManager.getString("action.ok"));
327 FeaturesFile ffile = new FeaturesFile();
329 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
331 // This ensures that the last sequence
332 // is refreshed and new features are rendered
334 lastFeatureAdded = name.getText().trim();
335 lastFeatureGroupAdded = source.getText().trim();
336 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
337 // TODO: determine if the null feature group is valid
338 if (lastFeatureGroupAdded.length() < 1)
340 lastFeatureGroupAdded = null;
346 SequenceFeature sf = features[featureIndex];
348 if (reply == JOptionPane.NO_OPTION)
350 sequences[0].getDatasetSequence().deleteFeature(sf);
352 else if (reply == JOptionPane.YES_OPTION)
354 sf.type = lastFeatureAdded;
355 sf.featureGroup = lastFeatureGroupAdded;
356 sf.description = lastDescriptionAdded;
358 setColour(sf.type, fcol);
359 getFeaturesDisplayed().setVisible(sf.type);
363 sf.begin = ((Integer) start.getValue()).intValue();
364 sf.end = ((Integer) end.getValue()).intValue();
365 } catch (NumberFormatException ex)
369 ffile.parseDescriptionHTML(sf, false);
373 // NEW FEATURES ADDED
375 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
377 for (int i = 0; i < sequences.length; i++)
379 features[i].type = lastFeatureAdded;
380 // fix for JAL-1538 - always set feature group here
381 features[i].featureGroup = lastFeatureGroupAdded;
382 features[i].description = lastDescriptionAdded;
383 sequences[i].addSequenceFeature(features[i]);
384 ffile.parseDescriptionHTML(features[i], false);
387 if (lastFeatureGroupAdded != null)
389 setGroupVisibility(lastFeatureGroupAdded, true);
391 setColour(lastFeatureAdded, fcol);
392 setVisible(lastFeatureAdded);
394 findAllFeatures(false);
396 ap.paintAlignment(true);
406 ap.paintAlignment(true);
413 * update the amend feature button dependent on the given style
419 protected void updateColourButton(JPanel bigPanel, JLabel colour,
423 colour.setIcon(null);
424 colour.setToolTipText(null);
427 if (col2 instanceof Color)
429 colour.setBackground((Color) col2);
433 colour.setBackground(bigPanel.getBackground());
434 colour.setForeground(Color.black);
435 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
436 // colour.setForeground(colour.getBackground());