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;
56 import java.util.List;
64 public class FeatureRenderer
65 extends jalview.renderer.seqfeatures.FeatureRenderer
68 * creating a new feature defaults to the type and group as
69 * the last one created
71 static String lastFeatureAdded = "feature_1";
73 static String lastFeatureGroupAdded = "Jalview";
75 // Holds web links for feature groups and feature types
76 // in the form label|link
77 Hashtable featureLinks = null;
80 * Creates a new FeatureRenderer object.
84 public FeatureRenderer(AlignmentViewport av)
92 boolean deleteFeature = false;
94 FeatureColourPanel colourPanel;
96 class FeatureColourPanel extends Panel
100 private Color maxCol;
102 private boolean isColourByLabel, isGcol;
105 * render a feature style in the amend feature dialog box
107 public void updateColor(FeatureColourI newcol)
111 if (newcol.isSimpleColour())
113 bg = newcol.getColour();
118 if (newcol.isAboveThreshold())
122 else if (newcol.isBelowThreshold())
127 if (isColourByLabel = newcol.isColourByLabel())
129 setBackground(bg = Color.white);
130 vlabel += " (by Label)";
134 setBackground(bg = newcol.getMinColour());
135 maxCol = newcol.getMaxColour();
149 public void paint(Graphics g)
151 Dimension d = getSize();
156 g.setColor(Color.white);
157 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
158 g.setColor(Color.black);
159 Font f = new Font("Verdana", Font.PLAIN, 10);
161 g.drawString(MessageManager.getString("label.label"), 0, 0);
166 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
175 * Shows a dialog allowing the user to create, or amend or delete, sequence
176 * features. If null in the supplied feature(s), feature type and group
177 * default to those for the last feature created (with initial defaults of
178 * "feature_1" and "Jalview").
186 boolean amendFeatures(final List<SequenceI> sequences,
187 final List<SequenceFeature> features, boolean create,
188 final AlignmentPanel ap)
190 final Panel bigPanel = new Panel(new BorderLayout());
191 final TextField name = new TextField(16);
192 final TextField group = new TextField(16);
193 final TextArea description = new TextArea(3, 35);
194 final TextField start = new TextField(8);
195 final TextField end = new TextField(8);
196 final Choice overlaps;
197 Button deleteButton = new Button("Delete");
198 deleteFeature = false;
200 name.addTextListener(new TextListener()
203 public void textValueChanged(TextEvent e)
205 warnIfTypeHidden(ap.alignFrame, name.getText());
208 group.addTextListener(new TextListener()
211 public void textValueChanged(TextEvent e)
213 warnIfGroupHidden(ap.alignFrame, group.getText());
216 colourPanel = new FeatureColourPanel();
217 colourPanel.setSize(110, 15);
218 final FeatureRenderer fr = this;
220 Panel panel = new Panel(new GridLayout(3, 1));
222 featureIndex = 0; // feature to be amended.
225 // /////////////////////////////////////
226 // /MULTIPLE FEATURES AT SELECTED RESIDUE
227 if (!create && features.size() > 1)
229 panel = new Panel(new GridLayout(4, 1));
231 tmp.add(new Label("Select Feature: "));
232 overlaps = new Choice();
233 for (SequenceFeature sf : features)
235 String item = sf.getType() + "/" + sf.getBegin() + "-"
237 if (sf.getFeatureGroup() != null)
239 item += " (" + sf.getFeatureGroup() + ")";
241 overlaps.addItem(item);
246 overlaps.addItemListener(new java.awt.event.ItemListener()
249 public void itemStateChanged(java.awt.event.ItemEvent e)
251 int index = overlaps.getSelectedIndex();
254 featureIndex = index;
255 SequenceFeature sf = features.get(index);
256 name.setText(sf.getType());
257 description.setText(sf.getDescription());
258 group.setText(sf.getFeatureGroup());
259 start.setText(sf.getBegin() + "");
260 end.setText(sf.getEnd() + "");
262 SearchResultsI highlight = new SearchResults();
263 highlight.addResult(sequences.get(0), sf.getBegin(),
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:"),
294 tmp.add(new Label(MessageManager.getString("label.group:"),
300 tmp.add(new Label(MessageManager.getString("label.colour"),
302 tmp.add(colourPanel);
304 bigPanel.add(panel, BorderLayout.NORTH);
307 panel.add(new Label(MessageManager.getString("label.description:"),
309 panel.add(new ScrollPane().add(description));
313 bigPanel.add(panel, BorderLayout.SOUTH);
316 panel.add(new Label(MessageManager.getString("label.start"),
319 panel.add(new Label(MessageManager.getString("label.end"),
322 bigPanel.add(panel, BorderLayout.CENTER);
326 bigPanel.add(panel, BorderLayout.CENTER);
330 * use defaults for type and group (and update them on Confirm) only
331 * if feature type has not been supplied by the caller
332 * (e.g. for Amend, or create features from Find)
334 SequenceFeature firstFeature = features.get(0);
335 boolean useLastDefaults = firstFeature.getType() == null;
336 String featureType = useLastDefaults ? lastFeatureAdded
337 : firstFeature.getType();
338 String featureGroup = useLastDefaults ? lastFeatureGroupAdded
339 : firstFeature.getFeatureGroup();
341 String title = create
342 ? MessageManager.getString("label.create_new_sequence_features")
343 : MessageManager.formatMessage("label.amend_delete_features",
345 { sequences.get(0).getName() });
347 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
350 dialog.setMainPanel(bigPanel);
352 name.setText(featureType);
353 group.setText(featureGroup);
357 dialog.ok.setLabel(MessageManager.getString("label.amend"));
358 dialog.buttonPanel.add(deleteButton, 1);
359 deleteButton.addActionListener(new ActionListener()
362 public void actionPerformed(ActionEvent evt)
364 deleteFeature = true;
365 dialog.setVisible(false);
370 start.setText(firstFeature.getBegin() + "");
371 end.setText(firstFeature.getEnd() + "");
372 description.setText(firstFeature.getDescription());
373 // lookup (or generate) the feature colour
374 FeatureColourI fcol = getFeatureStyle(name.getText());
375 // simply display the feature color in a box
376 colourPanel.updateColor(fcol);
377 dialog.setResizable(true);
378 // TODO: render the graduated color in the box.
379 colourPanel.addMouseListener(new MouseAdapter()
382 public void mousePressed(MouseEvent evt)
384 if (!colourPanel.isGcol)
386 new UserDefinedColours(fr, ap.alignFrame);
390 new FeatureColourChooser(ap.alignFrame, name.getText());
391 dialog.transferFocus();
395 dialog.setVisible(true);
397 FeaturesFile ffile = new FeaturesFile();
400 * only update default type and group if we used defaults
402 final String enteredType = name.getText().trim();
403 final String enteredGroup = group.getText().trim();
404 final String enteredDesc = description.getText().replace('\n', ' ');
406 if (dialog.accept && useLastDefaults)
408 lastFeatureAdded = enteredType;
409 lastFeatureGroupAdded = enteredGroup;
414 SequenceFeature sf = features.get(featureIndex);
417 if (!colourPanel.isGcol)
419 // update colour - otherwise its already done.
420 setColour(enteredType,
421 new FeatureColour(colourPanel.getBackground()));
423 int newBegin = sf.begin;
427 newBegin = Integer.parseInt(start.getText());
428 newEnd = Integer.parseInt(end.getText());
429 } catch (NumberFormatException ex)
435 * replace the feature by deleting it and adding a new one
436 * (to ensure integrity of SequenceFeatures data store)
438 sequences.get(0).deleteFeature(sf);
439 SequenceFeature newSf = new SequenceFeature(sf, enteredType,
440 newBegin, newEnd, enteredGroup, sf.getScore());
441 newSf.setDescription(enteredDesc);
442 ffile.parseDescriptionHTML(newSf, false);
443 // amend features dialog only updates one sequence at a time
444 sequences.get(0).addSequenceFeature(newSf);
445 boolean typeOrGroupChanged = (!featureType.equals(newSf.getType()) || !featureGroup
446 .equals(newSf.getFeatureGroup()));
448 ffile.parseDescriptionHTML(sf, false);
449 if (typeOrGroupChanged)
456 sequences.get(0).deleteFeature(sf);
457 // ensure Feature Settings reflects removal of feature / group
466 if (dialog.accept && name.getText().length() > 0)
468 for (int i = 0; i < sequences.size(); i++)
470 SequenceFeature sf = features.get(i);
471 SequenceFeature sf2 = new SequenceFeature(enteredType,
472 enteredDesc, sf.getBegin(), sf.getEnd(), enteredGroup);
473 ffile.parseDescriptionHTML(sf2, false);
474 sequences.get(i).addSequenceFeature(sf2);
477 Color newColour = colourPanel.getBackground();
478 // setColour(lastFeatureAdded, fcol);
480 setColour(enteredType, new FeatureColour(newColour)); // was fcol
485 // no update to the alignment
489 // refresh the alignment and the feature settings dialog
490 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
492 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
494 // findAllFeatures();
496 ap.paintAlignment(true, true);
501 protected void warnIfGroupHidden(Frame frame, String group)
503 if (featureGroups.containsKey(group) && !featureGroups.get(group))
505 String msg = MessageManager.formatMessage("label.warning_hidden",
506 MessageManager.getString("label.group"), group);
507 showWarning(frame, msg);
511 protected void warnIfTypeHidden(Frame frame, String type)
513 if (getRenderOrder().contains(type))
515 if (!showFeatureOfType(type))
517 String msg = MessageManager.formatMessage("label.warning_hidden",
518 MessageManager.getString("label.feature_type"), type);
519 showWarning(frame, msg);
528 protected void showWarning(Frame frame, String msg)
530 JVDialog d = new JVDialog(frame, "", true, 350, 200);
531 Panel mp = new Panel();
532 d.ok.setLabel(MessageManager.getString("action.ok"));
533 d.cancel.setVisible(false);
534 mp.setLayout(new FlowLayout());
535 mp.add(new Label(msg));