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;
40 import java.awt.Graphics;
41 import java.awt.GridLayout;
42 import java.awt.Label;
43 import java.awt.Panel;
44 import java.awt.ScrollPane;
45 import java.awt.TextArea;
46 import java.awt.TextField;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 import java.awt.event.MouseAdapter;
50 import java.awt.event.MouseEvent;
51 import java.util.Hashtable;
59 public class FeatureRenderer extends
60 jalview.renderer.seqfeatures.FeatureRenderer
63 // Holds web links for feature groups and feature types
64 // in the form label|link
65 Hashtable featureLinks = null;
68 * Creates a new FeatureRenderer object.
72 public FeatureRenderer(AlignmentViewport av)
78 static String lastFeatureAdded;
80 static String lastFeatureGroupAdded;
82 static String lastDescriptionAdded;
86 boolean deleteFeature = false;
88 FeatureColourPanel colourPanel;
90 class FeatureColourPanel extends Panel
96 private boolean isColourByLabel, isGcol;
99 * render a feature style in the amend feature dialog box
101 public void updateColor(FeatureColourI newcol)
105 if (newcol.isSimpleColour())
107 bg = newcol.getColour();
112 if (newcol.isAboveThreshold())
116 else if (newcol.isBelowThreshold())
121 if (isColourByLabel = newcol.isColourByLabel())
123 setBackground(bg = Color.white);
124 vlabel += " (by Label)";
128 setBackground(bg = newcol.getMinColour());
129 maxCol = newcol.getMaxColour();
143 public void paint(Graphics g)
145 Dimension d = getSize();
150 g.setColor(Color.white);
151 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
152 g.setColor(Color.black);
153 Font f = new Font("Verdana", Font.PLAIN, 10);
155 g.drawString(MessageManager.getString("label.label"), 0, 0);
160 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
169 * Shows a dialog allowing the user to create, or amend or delete, sequence
179 boolean amendFeatures(final SequenceI[] sequences,
180 final SequenceFeature[] features, boolean create,
181 final AlignmentPanel ap, String featureType)
183 Panel bigPanel = new Panel(new BorderLayout());
184 final TextField name = new TextField(16);
185 final TextField source = new TextField(16);
186 final TextArea description = new TextArea(3, 35);
187 final TextField start = new TextField(8);
188 final TextField end = new TextField(8);
189 final Choice overlaps;
190 Button deleteButton = new Button("Delete");
191 deleteFeature = false;
193 colourPanel = new FeatureColourPanel();
194 colourPanel.setSize(110, 15);
195 final FeatureRenderer fr = this;
197 Panel panel = new Panel(new GridLayout(3, 1));
199 featureIndex = 0; // feature to be amended.
202 // /////////////////////////////////////
203 // /MULTIPLE FEATURES AT SELECTED RESIDUE
204 if (!create && features.length > 1)
206 panel = new Panel(new GridLayout(4, 1));
208 tmp.add(new Label("Select Feature: "));
209 overlaps = new Choice();
210 for (int i = 0; i < features.length; i++)
212 String item = features[i].getType() + "/" + features[i].getBegin()
213 + "-" + features[i].getEnd();
215 if (features[i].getFeatureGroup() != null)
217 item += " (" + features[i].getFeatureGroup() + ")";
220 overlaps.addItem(item);
225 overlaps.addItemListener(new java.awt.event.ItemListener()
228 public void itemStateChanged(java.awt.event.ItemEvent e)
230 int index = overlaps.getSelectedIndex();
233 featureIndex = index;
234 name.setText(features[index].getType());
235 description.setText(features[index].getDescription());
236 source.setText(features[index].getFeatureGroup());
237 start.setText(features[index].getBegin() + "");
238 end.setText(features[index].getEnd() + "");
240 SearchResultsI highlight = new SearchResults();
241 highlight.addResult(sequences[0], features[index].getBegin(),
242 features[index].getEnd());
244 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
247 FeatureColourI col = getFeatureStyle(name.getText());
250 Color generatedColour = ColorUtils
251 .createColourFromName(name.getText());
252 col = new FeatureColour(generatedColour);
255 colourPanel.updateColor(col);
262 // ////////////////////////////////////
266 tmp.add(new Label(MessageManager.getString("label.name:"), Label.RIGHT));
271 tmp.add(new Label(MessageManager.getString("label.group:"), Label.RIGHT));
276 tmp.add(new Label(MessageManager.getString("label.colour"), Label.RIGHT));
277 tmp.add(colourPanel);
279 bigPanel.add(panel, BorderLayout.NORTH);
282 panel.add(new Label(MessageManager.getString("label.description:"),
284 panel.add(new ScrollPane().add(description));
288 bigPanel.add(panel, BorderLayout.SOUTH);
291 panel.add(new Label(MessageManager.getString("label.start"),
294 panel.add(new Label(MessageManager.getString("label.end"),
297 bigPanel.add(panel, BorderLayout.CENTER);
301 bigPanel.add(panel, BorderLayout.CENTER);
304 if (featureType != null)
306 lastFeatureAdded = featureType;
310 if (lastFeatureAdded == null)
312 if (features[0].type != null)
314 lastFeatureAdded = features[0].type;
318 lastFeatureAdded = "feature_1";
323 if (lastFeatureGroupAdded == null)
325 if (features[0].featureGroup != null)
327 lastFeatureGroupAdded = features[0].featureGroup;
331 lastFeatureAdded = "Jalview";
335 String title = create ? MessageManager
336 .getString("label.create_new_sequence_features")
337 : MessageManager.formatMessage("label.amend_delete_features",
338 new String[] { sequences[0].getName() });
340 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
343 dialog.setMainPanel(bigPanel);
347 name.setText(lastFeatureAdded);
348 source.setText(lastFeatureGroupAdded);
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);
363 name.setText(features[0].getType());
364 source.setText(features[0].getFeatureGroup());
367 start.setText(features[0].getBegin() + "");
368 end.setText(features[0].getEnd() + "");
369 description.setText(features[0].getDescription());
370 // lookup (or generate) the feature colour
371 FeatureColourI fcol = getFeatureStyle(name.getText());
372 // simply display the feature color in a box
373 colourPanel.updateColor(fcol);
374 dialog.setResizable(true);
375 // TODO: render the graduated color in the box.
376 colourPanel.addMouseListener(new MouseAdapter()
379 public void mousePressed(MouseEvent evt)
381 if (!colourPanel.isGcol)
383 new UserDefinedColours(fr, ap.alignFrame);
387 new FeatureColourChooser(ap.alignFrame, name.getText());
388 dialog.transferFocus();
392 dialog.setVisible(true);
394 FeaturesFile ffile = new FeaturesFile();
398 lastFeatureAdded = name.getText().trim();
399 lastFeatureGroupAdded = source.getText().trim();
400 lastDescriptionAdded = description.getText().replace('\n', ' ');
403 if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
405 lastFeatureGroupAdded = null;
411 SequenceFeature sf = features[featureIndex];
414 sf.type = lastFeatureAdded;
415 sf.featureGroup = lastFeatureGroupAdded;
416 sf.description = lastDescriptionAdded;
417 if (!colourPanel.isGcol)
419 // update colour - otherwise its already done.
420 setColour(sf.type, new FeatureColour(colourPanel.getBackground()));
424 sf.begin = Integer.parseInt(start.getText());
425 sf.end = Integer.parseInt(end.getText());
426 } catch (NumberFormatException ex)
430 ffile.parseDescriptionHTML(sf, false);
431 setVisible(lastFeatureAdded); // if user edited name then make sure new
436 sequences[0].deleteFeature(sf);
442 if (dialog.accept && name.getText().length() > 0)
444 for (int i = 0; i < sequences.length; i++)
446 features[i].type = lastFeatureAdded;
447 features[i].featureGroup = lastFeatureGroupAdded;
448 features[i].description = lastDescriptionAdded;
449 sequences[i].addSequenceFeature(features[i]);
450 ffile.parseDescriptionHTML(features[i], false);
453 Color newColour = colourPanel.getBackground();
454 // setColour(lastFeatureAdded, fcol);
456 if (lastFeatureGroupAdded != null)
458 setGroupVisibility(lastFeatureGroupAdded, true);
460 setColour(lastFeatureAdded, new FeatureColour(newColour)); // was fcol
461 setVisible(lastFeatureAdded);
462 findAllFeatures(false); // different to original applet behaviour ?
463 // findAllFeatures();
467 // no update to the alignment
471 // refresh the alignment and the feature settings dialog
472 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
474 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
476 // findAllFeatures();
478 ap.paintAlignment(true);