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;
41 import javax.swing.JColorChooser;
42 import javax.swing.JComboBox;
43 import javax.swing.JLabel;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JSpinner;
48 import javax.swing.JTextArea;
49 import javax.swing.JTextField;
50 import javax.swing.SwingConstants;
58 public class FeatureRenderer extends jalview.renderer.seqfeatures.FeatureRenderer implements jalview.api.FeatureRenderer
65 * Creates a new FeatureRenderer object.
70 public FeatureRenderer(AlignmentPanel ap)
75 if (ap != null && ap.getSeqPanel() != null && ap.getSeqPanel().seqCanvas != null
76 && ap.getSeqPanel().seqCanvas.fr != null)
78 transferSettings(ap.getSeqPanel().seqCanvas.fr);
83 // // Feature Editing Dialog
84 // // Will be refactored in next release.
86 static String lastFeatureAdded;
88 static String lastFeatureGroupAdded;
90 static String lastDescriptionAdded;
96 boolean amendFeatures(final SequenceI[] sequences,
97 final SequenceFeature[] features, boolean newFeatures,
98 final AlignmentPanel ap)
103 final JPanel bigPanel = new JPanel(new BorderLayout());
104 final JComboBox overlaps;
105 final JTextField name = new JTextField(25);
106 final JTextField source = new JTextField(25);
107 final JTextArea description = new JTextArea(3, 25);
108 final JSpinner start = new JSpinner();
109 final JSpinner end = new JSpinner();
110 start.setPreferredSize(new Dimension(80, 20));
111 end.setPreferredSize(new Dimension(80, 20));
112 final FeatureRenderer me = this;
113 final JLabel colour = new JLabel();
114 colour.setOpaque(true);
115 // colour.setBorder(BorderFactory.createEtchedBorder());
116 colour.setMaximumSize(new Dimension(30, 16));
117 colour.addMouseListener(new MouseAdapter()
119 FeatureColourChooser fcc = null;
121 public void mousePressed(MouseEvent evt)
123 if (fcol instanceof Color)
125 Color col = JColorChooser.showDialog(Desktop.desktop,
126 MessageManager.getString("label.select_feature_colour"), ((Color) fcol));
130 updateColourButton(bigPanel, colour, col);
138 final String type = features[featureIndex].getType();
139 fcc = new FeatureColourChooser(me, type);
140 fcc.setRequestFocusEnabled(true);
143 fcc.addActionListener(new ActionListener()
146 public void actionPerformed(ActionEvent e)
148 fcol = fcc.getLastColour();
150 setColour(type, fcol);
151 updateColourButton(bigPanel, colour, fcol);
159 JPanel tmp = new JPanel();
160 JPanel panel = new JPanel(new GridLayout(3, 1));
162 // /////////////////////////////////////
163 // /MULTIPLE FEATURES AT SELECTED RESIDUE
164 if (!newFeatures && features.length > 1)
166 panel = new JPanel(new GridLayout(4, 1));
168 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
169 overlaps = new JComboBox();
170 for (int i = 0; i < features.length; i++)
172 overlaps.addItem(features[i].getType() + "/"
173 + features[i].getBegin() + "-" + features[i].getEnd()
174 + " (" + features[i].getFeatureGroup() + ")");
179 overlaps.addItemListener(new ItemListener()
181 public void itemStateChanged(ItemEvent e)
183 int index = overlaps.getSelectedIndex();
186 featureIndex = index;
187 name.setText(features[index].getType());
188 description.setText(features[index].getDescription());
189 source.setText(features[index].getFeatureGroup());
190 start.setValue(new Integer(features[index].getBegin()));
191 end.setValue(new Integer(features[index].getEnd()));
193 SearchResults highlight = new SearchResults();
194 highlight.addResult(sequences[0], features[index].getBegin(),
195 features[index].getEnd());
197 ap.getSeqPanel().seqCanvas.highlightSearchResults(highlight);
200 Object col = getFeatureStyle(name.getText());
203 col = new jalview.schemes.UserColourScheme()
204 .createColourFromName(name.getText());
207 updateColourButton(bigPanel, colour, col);
214 // ////////////////////////////////////
218 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
223 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
229 tmp.add(new JLabel(MessageManager.getString("label.colour"),
232 colour.setPreferredSize(new Dimension(150, 15));
233 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
234 colour.setForeground(Color.black);
235 colour.setHorizontalAlignment(SwingConstants.CENTER);
236 colour.setVerticalAlignment(SwingConstants.CENTER);
237 colour.setHorizontalTextPosition(SwingConstants.CENTER);
238 colour.setVerticalTextPosition(SwingConstants.CENTER);
239 bigPanel.add(panel, BorderLayout.NORTH);
241 panel = new JPanel();
242 panel.add(new JLabel(MessageManager.getString("label.description"),
244 description.setFont(JvSwingUtils.getTextAreaFont());
245 description.setLineWrap(true);
246 panel.add(new JScrollPane(description));
250 bigPanel.add(panel, BorderLayout.SOUTH);
252 panel = new JPanel();
253 panel.add(new JLabel(MessageManager.getString("label.start"),
256 panel.add(new JLabel(MessageManager.getString("label.end"),
259 bigPanel.add(panel, BorderLayout.CENTER);
263 bigPanel.add(panel, BorderLayout.CENTER);
266 if (lastFeatureAdded == null)
268 if (features[0].type != null)
270 lastFeatureAdded = features[0].type;
274 lastFeatureAdded = "feature_1";
278 if (lastFeatureGroupAdded == null)
280 if (features[0].featureGroup != null)
282 lastFeatureGroupAdded = features[0].featureGroup;
286 lastFeatureGroupAdded = "Jalview";
292 name.setText(lastFeatureAdded);
293 source.setText(lastFeatureGroupAdded);
297 name.setText(features[0].getType());
298 source.setText(features[0].getFeatureGroup());
301 start.setValue(new Integer(features[0].getBegin()));
302 end.setValue(new Integer(features[0].getEnd()));
303 description.setText(features[0].getDescription());
304 updateColourButton(bigPanel, colour,
305 (oldcol = fcol = getFeatureStyle(name.getText())));
309 options = new Object[]
310 { "Amend", "Delete", "Cancel" };
314 options = new Object[]
318 String title = newFeatures ? MessageManager.getString("label.create_new_sequence_features")
319 : MessageManager.formatMessage("label.amend_delete_features", new String[]{sequences[0].getName()});
321 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
322 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
323 JOptionPane.QUESTION_MESSAGE, null, options, MessageManager.getString("action.ok"));
325 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
327 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
329 // This ensures that the last sequence
330 // is refreshed and new features are rendered
332 lastFeatureAdded = name.getText().trim();
333 lastFeatureGroupAdded = source.getText().trim();
334 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
335 // TODO: determine if the null feature group is valid
336 if (lastFeatureGroupAdded.length() < 1)
338 lastFeatureGroupAdded = null;
344 SequenceFeature sf = features[featureIndex];
346 if (reply == JOptionPane.NO_OPTION)
348 sequences[0].getDatasetSequence().deleteFeature(sf);
350 else if (reply == JOptionPane.YES_OPTION)
352 sf.type = lastFeatureAdded;
353 sf.featureGroup = lastFeatureGroupAdded;
354 sf.description = lastDescriptionAdded;
356 setColour(sf.type, fcol);
357 getFeaturesDisplayed().setVisible(sf.type);
361 sf.begin = ((Integer) start.getValue()).intValue();
362 sf.end = ((Integer) end.getValue()).intValue();
363 } catch (NumberFormatException ex)
367 ffile.parseDescriptionHTML(sf, false);
371 // NEW FEATURES ADDED
373 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
375 for (int i = 0; i < sequences.length; i++)
377 features[i].type = lastFeatureAdded;
378 // fix for JAL-1538 - always set feature group here
379 features[i].featureGroup = lastFeatureGroupAdded;
380 features[i].description = lastDescriptionAdded;
381 sequences[i].addSequenceFeature(features[i]);
382 ffile.parseDescriptionHTML(features[i], false);
385 if (lastFeatureGroupAdded != null)
387 setGroupVisibility(lastFeatureGroupAdded, true);
389 setColour(lastFeatureAdded, fcol);
390 setVisible(lastFeatureAdded);
392 findAllFeatures(false);
394 ap.paintAlignment(true);
404 ap.paintAlignment(true);
411 * update the amend feature button dependent on the given style
417 protected void updateColourButton(JPanel bigPanel, JLabel colour,
421 colour.setIcon(null);
422 colour.setToolTipText(null);
425 if (col2 instanceof Color)
427 colour.setBackground((Color) col2);
431 colour.setBackground(bigPanel.getBackground());
432 colour.setForeground(Color.black);
433 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
434 // colour.setForeground(colour.getBackground());