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 // Holds web links for feature groups and feature types
68 // in the form label|link
69 Hashtable featureLinks = null;
72 * Creates a new FeatureRenderer object.
76 public FeatureRenderer(AlignmentViewport av)
82 static String lastFeatureAdded;
84 static String lastFeatureGroupAdded;
86 static String lastDescriptionAdded;
90 boolean deleteFeature = false;
92 FeatureColourPanel colourPanel;
94 class FeatureColourPanel extends Panel
100 private boolean isColourByLabel, isGcol;
103 * render a feature style in the amend feature dialog box
105 public void updateColor(FeatureColourI newcol)
109 if (newcol.isSimpleColour())
111 bg = newcol.getColour();
116 if (newcol.isAboveThreshold())
120 else if (newcol.isBelowThreshold())
125 if (isColourByLabel = newcol.isColourByLabel())
127 setBackground(bg = Color.white);
128 vlabel += " (by Label)";
132 setBackground(bg = newcol.getMinColour());
133 maxCol = newcol.getMaxColour();
147 public void paint(Graphics g)
149 Dimension d = getSize();
154 g.setColor(Color.white);
155 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
156 g.setColor(Color.black);
157 Font f = new Font("Verdana", Font.PLAIN, 10);
159 g.drawString(MessageManager.getString("label.label"), 0, 0);
164 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
173 * Shows a dialog allowing the user to create, or amend or delete, sequence
183 boolean amendFeatures(final SequenceI[] sequences,
184 final SequenceFeature[] features, boolean create,
185 final AlignmentPanel ap, String featureType)
187 final Panel bigPanel = new Panel(new BorderLayout());
188 final TextField name = new TextField(16);
189 final TextField group = new TextField(16);
190 final TextArea description = new TextArea(3, 35);
191 final TextField start = new TextField(8);
192 final TextField end = new TextField(8);
193 final Choice overlaps;
194 Button deleteButton = new Button("Delete");
195 deleteFeature = false;
197 name.addTextListener(new TextListener()
200 public void textValueChanged(TextEvent e)
202 warnIfTypeHidden(ap.alignFrame, name.getText());
205 group.addTextListener(new TextListener()
208 public void textValueChanged(TextEvent e)
210 warnIfGroupHidden(ap.alignFrame, group.getText());
213 colourPanel = new FeatureColourPanel();
214 colourPanel.setSize(110, 15);
215 final FeatureRenderer fr = this;
217 Panel panel = new Panel(new GridLayout(3, 1));
219 featureIndex = 0; // feature to be amended.
222 // /////////////////////////////////////
223 // /MULTIPLE FEATURES AT SELECTED RESIDUE
224 if (!create && features.length > 1)
226 panel = new Panel(new GridLayout(4, 1));
228 tmp.add(new Label("Select Feature: "));
229 overlaps = new Choice();
230 for (int i = 0; i < features.length; i++)
232 String item = features[i].getType() + "/" + features[i].getBegin()
233 + "-" + features[i].getEnd();
235 if (features[i].getFeatureGroup() != null)
237 item += " (" + features[i].getFeatureGroup() + ")";
240 overlaps.addItem(item);
245 overlaps.addItemListener(new java.awt.event.ItemListener()
248 public void itemStateChanged(java.awt.event.ItemEvent e)
250 int index = overlaps.getSelectedIndex();
253 featureIndex = index;
254 name.setText(features[index].getType());
255 description.setText(features[index].getDescription());
256 group.setText(features[index].getFeatureGroup());
257 start.setText(features[index].getBegin() + "");
258 end.setText(features[index].getEnd() + "");
260 SearchResultsI highlight = new SearchResults();
261 highlight.addResult(sequences[0], features[index].getBegin(),
262 features[index].getEnd());
264 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
267 FeatureColourI col = getFeatureStyle(name.getText());
270 Color generatedColour = ColorUtils
271 .createColourFromName(name.getText());
272 col = new FeatureColour(generatedColour);
275 colourPanel.updateColor(col);
282 // ////////////////////////////////////
286 tmp.add(new Label(MessageManager.getString("label.name:"), Label.RIGHT));
291 tmp.add(new Label(MessageManager.getString("label.group:"), Label.RIGHT));
296 tmp.add(new Label(MessageManager.getString("label.colour"), Label.RIGHT));
297 tmp.add(colourPanel);
299 bigPanel.add(panel, BorderLayout.NORTH);
302 panel.add(new Label(MessageManager.getString("label.description:"),
304 panel.add(new ScrollPane().add(description));
308 bigPanel.add(panel, BorderLayout.SOUTH);
311 panel.add(new Label(MessageManager.getString("label.start"),
314 panel.add(new Label(MessageManager.getString("label.end"),
317 bigPanel.add(panel, BorderLayout.CENTER);
321 bigPanel.add(panel, BorderLayout.CENTER);
324 if (featureType != null)
326 lastFeatureAdded = featureType;
330 if (lastFeatureAdded == null)
332 if (features[0].type != null)
334 lastFeatureAdded = features[0].type;
338 lastFeatureAdded = "feature_1";
343 if (lastFeatureGroupAdded == null)
345 if (features[0].featureGroup != null)
347 lastFeatureGroupAdded = features[0].featureGroup;
351 lastFeatureAdded = "Jalview";
355 String title = create ? MessageManager
356 .getString("label.create_new_sequence_features")
357 : MessageManager.formatMessage("label.amend_delete_features",
358 new String[] { sequences[0].getName() });
360 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
363 dialog.setMainPanel(bigPanel);
367 name.setText(lastFeatureAdded);
368 group.setText(lastFeatureGroupAdded);
372 dialog.ok.setLabel(MessageManager.getString("label.amend"));
373 dialog.buttonPanel.add(deleteButton, 1);
374 deleteButton.addActionListener(new ActionListener()
377 public void actionPerformed(ActionEvent evt)
379 deleteFeature = true;
380 dialog.setVisible(false);
383 name.setText(features[0].getType());
384 group.setText(features[0].getFeatureGroup());
387 start.setText(features[0].getBegin() + "");
388 end.setText(features[0].getEnd() + "");
389 description.setText(features[0].getDescription());
390 // lookup (or generate) the feature colour
391 FeatureColourI fcol = getFeatureStyle(name.getText());
392 // simply display the feature color in a box
393 colourPanel.updateColor(fcol);
394 dialog.setResizable(true);
395 // TODO: render the graduated color in the box.
396 colourPanel.addMouseListener(new MouseAdapter()
399 public void mousePressed(MouseEvent evt)
401 if (!colourPanel.isGcol)
403 new UserDefinedColours(fr, ap.alignFrame);
407 new FeatureColourChooser(ap.alignFrame, name.getText());
408 dialog.transferFocus();
412 dialog.setVisible(true);
414 FeaturesFile ffile = new FeaturesFile();
418 lastFeatureAdded = name.getText().trim();
419 lastFeatureGroupAdded = group.getText().trim();
420 lastDescriptionAdded = description.getText().replace('\n', ' ');
423 if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
425 lastFeatureGroupAdded = null;
431 SequenceFeature sf = features[featureIndex];
434 boolean typeOrGroupChanged = (!lastFeatureAdded.equals(sf.type) || !lastFeatureGroupAdded
435 .equals(sf.featureGroup));
436 sf.type = lastFeatureAdded;
437 sf.featureGroup = lastFeatureGroupAdded;
438 sf.description = lastDescriptionAdded;
439 if (!colourPanel.isGcol)
441 // update colour - otherwise its already done.
442 setColour(sf.type, new FeatureColour(colourPanel.getBackground()));
446 sf.begin = Integer.parseInt(start.getText());
447 sf.end = Integer.parseInt(end.getText());
448 } catch (NumberFormatException ex)
452 ffile.parseDescriptionHTML(sf, false);
453 if (typeOrGroupChanged)
460 sequences[0].deleteFeature(sf);
461 // ensure Feature Settings reflects removal of feature / group
471 if (dialog.accept && name.getText().length() > 0)
473 for (int i = 0; i < sequences.length; i++)
475 features[i].type = lastFeatureAdded;
476 features[i].featureGroup = lastFeatureGroupAdded;
477 features[i].description = lastDescriptionAdded;
478 sequences[i].addSequenceFeature(features[i]);
479 ffile.parseDescriptionHTML(features[i], false);
482 Color newColour = colourPanel.getBackground();
483 // setColour(lastFeatureAdded, fcol);
485 setColour(lastFeatureAdded, new FeatureColour(newColour)); // was fcol
490 // no update to the alignment
494 // refresh the alignment and the feature settings dialog
495 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
497 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
499 // findAllFeatures();
501 ap.paintAlignment(true);
506 protected void warnIfGroupHidden(Frame frame, String group)
508 if (featureGroups.containsKey(group) && !featureGroups.get(group))
510 String msg = MessageManager.formatMessage("label.warning_hidden",
511 MessageManager.getString("label.group"), group);
512 showWarning(frame, msg);
516 protected void warnIfTypeHidden(Frame frame, String type)
518 if (getRenderOrder().contains(type))
520 if (!showFeatureOfType(type))
522 String msg = MessageManager.formatMessage("label.warning_hidden",
523 MessageManager.getString("label.feature_type"), type);
524 showWarning(frame, msg);
533 protected void showWarning(Frame frame, String msg)
535 JVDialog d = new JVDialog(frame, "", true, 350, 200);
536 Panel mp = new Panel();
537 d.ok.setLabel(MessageManager.getString("action.ok"));
538 d.cancel.setVisible(false);
539 mp.setLayout(new FlowLayout());
540 mp.add(new Label(msg));