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.
21 package jalview.appletgui;
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.io.FeaturesFile;
29 import jalview.schemes.FeatureColour;
30 import jalview.util.ColorUtils;
31 import jalview.util.MessageManager;
32 import jalview.viewmodel.AlignmentViewport;
34 import java.awt.BorderLayout;
35 import java.awt.Button;
36 import java.awt.Choice;
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.FlowLayout;
41 import java.awt.Frame;
42 import java.awt.Graphics;
43 import java.awt.GridLayout;
44 import java.awt.Label;
45 import java.awt.Panel;
46 import java.awt.ScrollPane;
47 import java.awt.TextArea;
48 import java.awt.TextField;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.MouseAdapter;
52 import java.awt.event.MouseEvent;
53 import java.awt.event.TextEvent;
54 import java.awt.event.TextListener;
55 import java.util.Hashtable;
63 public class FeatureRenderer extends
64 jalview.renderer.seqfeatures.FeatureRenderer
67 * creating a new feature defaults to the type and group as
68 * the last one created
70 static String lastFeatureAdded = "feature_1";
72 static String lastFeatureGroupAdded = "Jalview";
74 // Holds web links for feature groups and feature types
75 // in the form label|link
76 Hashtable featureLinks = null;
79 * Creates a new FeatureRenderer object.
83 public FeatureRenderer(AlignmentViewport av)
91 boolean deleteFeature = false;
93 FeatureColourPanel colourPanel;
95 class FeatureColourPanel extends Panel
101 private boolean isColourByLabel, isGcol;
104 * render a feature style in the amend feature dialog box
106 public void updateColor(FeatureColourI newcol)
110 if (newcol.isSimpleColour())
112 bg = newcol.getColour();
117 if (newcol.isAboveThreshold())
121 else if (newcol.isBelowThreshold())
126 if (isColourByLabel = newcol.isColourByLabel())
128 setBackground(bg = Color.white);
129 vlabel += " (by Label)";
133 setBackground(bg = newcol.getMinColour());
134 maxCol = newcol.getMaxColour();
148 public void paint(Graphics g)
150 Dimension d = getSize();
155 g.setColor(Color.white);
156 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
157 g.setColor(Color.black);
158 Font f = new Font("Verdana", Font.PLAIN, 10);
160 g.drawString(MessageManager.getString("label.label"), 0, 0);
165 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
174 * Shows a dialog allowing the user to create, or amend or delete, sequence
175 * features. If null in the supplied feature(s), feature type and group
176 * default to those for the last feature created (with initial defaults of
177 * "feature_1" and "Jalview").
185 boolean amendFeatures(final SequenceI[] sequences,
186 final SequenceFeature[] features, boolean create,
187 final AlignmentPanel ap)
189 final Panel bigPanel = new Panel(new BorderLayout());
190 final TextField name = new TextField(16);
191 final TextField group = new TextField(16);
192 final TextArea description = new TextArea(3, 35);
193 final TextField start = new TextField(8);
194 final TextField end = new TextField(8);
195 final Choice overlaps;
196 Button deleteButton = new Button("Delete");
197 deleteFeature = false;
199 name.addTextListener(new TextListener()
202 public void textValueChanged(TextEvent e)
204 warnIfTypeHidden(ap.alignFrame, name.getText());
207 group.addTextListener(new TextListener()
210 public void textValueChanged(TextEvent e)
212 warnIfGroupHidden(ap.alignFrame, group.getText());
215 colourPanel = new FeatureColourPanel();
216 colourPanel.setSize(110, 15);
217 final FeatureRenderer fr = this;
219 Panel panel = new Panel(new GridLayout(3, 1));
221 featureIndex = 0; // feature to be amended.
224 // /////////////////////////////////////
225 // /MULTIPLE FEATURES AT SELECTED RESIDUE
226 if (!create && features.length > 1)
228 panel = new Panel(new GridLayout(4, 1));
230 tmp.add(new Label("Select Feature: "));
231 overlaps = new Choice();
232 for (int i = 0; i < features.length; i++)
234 String item = features[i].getType() + "/" + features[i].getBegin()
235 + "-" + features[i].getEnd();
237 if (features[i].getFeatureGroup() != null)
239 item += " (" + features[i].getFeatureGroup() + ")";
242 overlaps.addItem(item);
247 overlaps.addItemListener(new java.awt.event.ItemListener()
250 public void itemStateChanged(java.awt.event.ItemEvent e)
252 int index = overlaps.getSelectedIndex();
255 featureIndex = index;
256 name.setText(features[index].getType());
257 description.setText(features[index].getDescription());
258 group.setText(features[index].getFeatureGroup());
259 start.setText(features[index].getBegin() + "");
260 end.setText(features[index].getEnd() + "");
262 SearchResultsI highlight = new SearchResults();
263 highlight.addResult(sequences[0], features[index].getBegin(),
264 features[index].getEnd());
266 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
269 FeatureColourI col = getFeatureStyle(name.getText());
272 Color generatedColour = ColorUtils
273 .createColourFromName(name.getText());
274 col = new FeatureColour(generatedColour);
277 colourPanel.updateColor(col);
284 // ////////////////////////////////////
288 tmp.add(new Label(MessageManager.getString("label.name:"), Label.RIGHT));
293 tmp.add(new Label(MessageManager.getString("label.group:"), Label.RIGHT));
298 tmp.add(new Label(MessageManager.getString("label.colour"), Label.RIGHT));
299 tmp.add(colourPanel);
301 bigPanel.add(panel, BorderLayout.NORTH);
304 panel.add(new Label(MessageManager.getString("label.description:"),
306 panel.add(new ScrollPane().add(description));
310 bigPanel.add(panel, BorderLayout.SOUTH);
313 panel.add(new Label(MessageManager.getString("label.start"),
316 panel.add(new Label(MessageManager.getString("label.end"),
319 bigPanel.add(panel, BorderLayout.CENTER);
323 bigPanel.add(panel, BorderLayout.CENTER);
327 * use defaults for type and group (and update them on Confirm) only
328 * if feature type has not been supplied by the caller
329 * (e.g. for Amend, or create features from Find)
331 boolean useLastDefaults = features[0].getType() == null;
332 String featureType = useLastDefaults ? lastFeatureAdded : features[0]
334 String featureGroup = useLastDefaults ? lastFeatureGroupAdded
335 : features[0].getFeatureGroup();
337 String title = create ? MessageManager
338 .getString("label.create_new_sequence_features")
339 : MessageManager.formatMessage("label.amend_delete_features",
340 new String[] { sequences[0].getName() });
342 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
345 dialog.setMainPanel(bigPanel);
347 name.setText(featureType);
348 group.setText(featureGroup);
352 dialog.ok.setLabel(MessageManager.getString("label.amend"));
353 dialog.buttonPanel.add(deleteButton, 1);
354 deleteButton.addActionListener(new ActionListener()
357 public void actionPerformed(ActionEvent evt)
359 deleteFeature = true;
360 dialog.setVisible(false);
365 start.setText(features[0].getBegin() + "");
366 end.setText(features[0].getEnd() + "");
367 description.setText(features[0].getDescription());
368 // lookup (or generate) the feature colour
369 FeatureColourI fcol = getFeatureStyle(name.getText());
370 // simply display the feature color in a box
371 colourPanel.updateColor(fcol);
372 dialog.setResizable(true);
373 // TODO: render the graduated color in the box.
374 colourPanel.addMouseListener(new MouseAdapter()
377 public void mousePressed(MouseEvent evt)
379 if (!colourPanel.isGcol)
381 new UserDefinedColours(fr, ap.alignFrame);
385 new FeatureColourChooser(ap.alignFrame, name.getText());
386 dialog.transferFocus();
390 dialog.setVisible(true);
392 FeaturesFile ffile = new FeaturesFile();
395 * only update default type and group if we used defaults
397 String enteredType = name.getText().trim();
398 if (dialog.accept && useLastDefaults)
400 lastFeatureAdded = enteredType;
401 lastFeatureGroupAdded = group.getText().trim();
406 SequenceFeature sf = features[featureIndex];
409 sf.type = enteredType;
410 sf.featureGroup = group.getText().trim();
411 if (sf.featureGroup != null && sf.featureGroup.length() < 1)
413 sf.featureGroup = null;
415 sf.description = description.getText().replace('\n', ' ');
416 if (!colourPanel.isGcol)
418 // update colour - otherwise its already done.
419 setColour(sf.type, new FeatureColour(colourPanel.getBackground()));
423 sf.begin = Integer.parseInt(start.getText());
424 sf.end = Integer.parseInt(end.getText());
425 } catch (NumberFormatException ex)
429 boolean typeOrGroupChanged = (!featureType.equals(sf.type) || !featureGroup
430 .equals(sf.featureGroup));
432 ffile.parseDescriptionHTML(sf, false);
433 if (typeOrGroupChanged)
440 sequences[0].deleteFeature(sf);
441 // ensure Feature Settings reflects removal of feature / group
450 if (dialog.accept && name.getText().length() > 0)
452 for (int i = 0; i < sequences.length; i++)
454 features[i].type = enteredType;
455 features[i].featureGroup = group.getText().trim();
456 features[i].description = description.getText()
458 sequences[i].addSequenceFeature(features[i]);
459 ffile.parseDescriptionHTML(features[i], false);
462 Color newColour = colourPanel.getBackground();
463 // setColour(lastFeatureAdded, fcol);
465 setColour(enteredType, new FeatureColour(newColour)); // was fcol
470 // no update to the alignment
474 // refresh the alignment and the feature settings dialog
475 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
477 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
479 // findAllFeatures();
481 ap.paintAlignment(true);
486 protected void warnIfGroupHidden(Frame frame, String group)
488 if (featureGroups.containsKey(group) && !featureGroups.get(group))
490 String msg = MessageManager.formatMessage("label.warning_hidden",
491 MessageManager.getString("label.group"), group);
492 showWarning(frame, msg);
496 protected void warnIfTypeHidden(Frame frame, String type)
498 if (getRenderOrder().contains(type))
500 if (!showFeatureOfType(type))
502 String msg = MessageManager.formatMessage("label.warning_hidden",
503 MessageManager.getString("label.feature_type"), type);
504 showWarning(frame, msg);
513 protected void showWarning(Frame frame, String msg)
515 JVDialog d = new JVDialog(frame, "", true, 350, 200);
516 Panel mp = new Panel();
517 d.ok.setLabel(MessageManager.getString("action.ok"));
518 d.cancel.setVisible(false);
519 mp.setLayout(new FlowLayout());
520 mp.add(new Label(msg));