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);
168 boolean amendFeatures(final SequenceI[] sequences,
169 final SequenceFeature[] features, boolean newFeatures,
170 final AlignmentPanel ap)
172 Panel bigPanel = new Panel(new BorderLayout());
173 final TextField name = new TextField(16);
174 final TextField source = new TextField(16);
175 final TextArea description = new TextArea(3, 35);
176 final TextField start = new TextField(8);
177 final TextField end = new TextField(8);
178 final Choice overlaps;
179 Button deleteButton = new Button("Delete");
180 deleteFeature = false;
182 colourPanel = new FeatureColourPanel();
183 colourPanel.setSize(110, 15);
184 final FeatureRenderer fr = this;
186 Panel panel = new Panel(new GridLayout(3, 1));
188 featureIndex = 0; // feature to be amended.
191 // /////////////////////////////////////
192 // /MULTIPLE FEATURES AT SELECTED RESIDUE
193 if (!newFeatures && features.length > 1)
195 panel = new Panel(new GridLayout(4, 1));
197 tmp.add(new Label("Select Feature: "));
198 overlaps = new Choice();
199 for (int i = 0; i < features.length; i++)
201 String item = features[i].getType() + "/" + features[i].getBegin()
202 + "-" + features[i].getEnd();
204 if (features[i].getFeatureGroup() != null)
206 item += " (" + features[i].getFeatureGroup() + ")";
209 overlaps.addItem(item);
214 overlaps.addItemListener(new java.awt.event.ItemListener()
217 public void itemStateChanged(java.awt.event.ItemEvent e)
219 int index = overlaps.getSelectedIndex();
222 featureIndex = index;
223 name.setText(features[index].getType());
224 description.setText(features[index].getDescription());
225 source.setText(features[index].getFeatureGroup());
226 start.setText(features[index].getBegin() + "");
227 end.setText(features[index].getEnd() + "");
229 SearchResultsI highlight = new SearchResults();
230 highlight.addResult(sequences[0], features[index].getBegin(),
231 features[index].getEnd());
233 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
236 FeatureColourI col = getFeatureStyle(name.getText());
239 Color generatedColour = ColorUtils
240 .createColourFromName(name.getText());
241 col = new FeatureColour(generatedColour);
244 colourPanel.updateColor(col);
251 // ////////////////////////////////////
255 tmp.add(new Label(MessageManager.getString("label.name:"), Label.RIGHT));
260 tmp.add(new Label(MessageManager.getString("label.group:"), Label.RIGHT));
265 tmp.add(new Label(MessageManager.getString("label.colour"), Label.RIGHT));
266 tmp.add(colourPanel);
268 bigPanel.add(panel, BorderLayout.NORTH);
271 panel.add(new Label(MessageManager.getString("label.description:"),
273 panel.add(new ScrollPane().add(description));
277 bigPanel.add(panel, BorderLayout.SOUTH);
280 panel.add(new Label(MessageManager.getString("label.start"),
283 panel.add(new Label(MessageManager.getString("label.end"),
286 bigPanel.add(panel, BorderLayout.CENTER);
290 bigPanel.add(panel, BorderLayout.CENTER);
293 if (lastFeatureAdded == null)
295 if (features[0].type != null)
297 lastFeatureAdded = features[0].type;
301 lastFeatureAdded = "feature_1";
305 if (lastFeatureGroupAdded == null)
307 if (features[0].featureGroup != null)
309 lastFeatureGroupAdded = features[0].featureGroup;
313 lastFeatureAdded = "Jalview";
317 String title = newFeatures ? MessageManager
318 .getString("label.create_new_sequence_features")
319 : MessageManager.formatMessage("label.amend_delete_features",
320 new String[] { sequences[0].getName() });
322 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
325 dialog.setMainPanel(bigPanel);
329 name.setText(lastFeatureAdded);
330 source.setText(lastFeatureGroupAdded);
334 dialog.ok.setLabel(MessageManager.getString("label.amend"));
335 dialog.buttonPanel.add(deleteButton, 1);
336 deleteButton.addActionListener(new ActionListener()
339 public void actionPerformed(ActionEvent evt)
341 deleteFeature = true;
342 dialog.setVisible(false);
345 name.setText(features[0].getType());
346 source.setText(features[0].getFeatureGroup());
349 start.setText(features[0].getBegin() + "");
350 end.setText(features[0].getEnd() + "");
351 description.setText(features[0].getDescription());
352 // lookup (or generate) the feature colour
353 FeatureColourI fcol = getFeatureStyle(name.getText());
354 // simply display the feature color in a box
355 colourPanel.updateColor(fcol);
356 dialog.setResizable(true);
357 // TODO: render the graduated color in the box.
358 colourPanel.addMouseListener(new MouseAdapter()
361 public void mousePressed(MouseEvent evt)
363 if (!colourPanel.isGcol)
365 new UserDefinedColours(fr, ap.alignFrame);
369 new FeatureColourChooser(ap.alignFrame, name.getText());
370 dialog.transferFocus();
374 dialog.setVisible(true);
376 FeaturesFile ffile = new FeaturesFile();
380 // This ensures that the last sequence
381 // is refreshed and new features are rendered
383 lastFeatureAdded = name.getText().trim();
384 lastFeatureGroupAdded = source.getText().trim();
385 lastDescriptionAdded = description.getText().replace('\n', ' ');
388 if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
390 lastFeatureGroupAdded = null;
396 SequenceFeature sf = features[featureIndex];
399 sf.type = lastFeatureAdded;
400 sf.featureGroup = lastFeatureGroupAdded;
401 sf.description = lastDescriptionAdded;
402 if (!colourPanel.isGcol)
404 // update colour - otherwise its already done.
405 setColour(sf.type, new FeatureColour(colourPanel.getBackground()));
409 sf.begin = Integer.parseInt(start.getText());
410 sf.end = Integer.parseInt(end.getText());
411 } catch (NumberFormatException ex)
415 ffile.parseDescriptionHTML(sf, false);
416 setVisible(lastFeatureAdded); // if user edited name then make sure new
421 sequences[0].deleteFeature(sf);
427 if (dialog.accept && name.getText().length() > 0)
429 for (int i = 0; i < sequences.length; i++)
431 features[i].type = lastFeatureAdded;
432 features[i].featureGroup = lastFeatureGroupAdded;
433 features[i].description = lastDescriptionAdded;
434 sequences[i].addSequenceFeature(features[i]);
435 ffile.parseDescriptionHTML(features[i], false);
438 Color newColour = colourPanel.getBackground();
439 // setColour(lastFeatureAdded, fcol);
441 if (lastFeatureGroupAdded != null)
443 setGroupVisibility(lastFeatureGroupAdded, true);
445 setColour(lastFeatureAdded, new FeatureColour(newColour)); // was fcol
446 setVisible(lastFeatureAdded);
447 findAllFeatures(false); // different to original applet behaviour ?
448 // findAllFeatures();
452 // no update to the alignment
456 // refresh the alignment and the feature settings dialog
457 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
459 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
461 // findAllFeatures();
463 ap.paintAlignment(true);