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.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.FeaturesFile;
28 import jalview.schemes.FeatureColour;
29 import jalview.schemes.UserColourScheme;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.AlignmentViewport;
33 import java.awt.BorderLayout;
34 import java.awt.Button;
35 import java.awt.Choice;
36 import java.awt.Color;
37 import java.awt.Dimension;
39 import java.awt.Graphics;
40 import java.awt.GridLayout;
41 import java.awt.Label;
42 import java.awt.Panel;
43 import java.awt.ScrollPane;
44 import java.awt.TextArea;
45 import java.awt.TextField;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.MouseAdapter;
49 import java.awt.event.MouseEvent;
50 import java.util.Hashtable;
58 public class FeatureRenderer extends
59 jalview.renderer.seqfeatures.FeatureRenderer
62 // Holds web links for feature groups and feature types
63 // in the form label|link
64 Hashtable featureLinks = null;
67 * Creates a new FeatureRenderer object.
71 public FeatureRenderer(AlignmentViewport av)
77 static String lastFeatureAdded;
79 static String lastFeatureGroupAdded;
81 static String lastDescriptionAdded;
85 boolean deleteFeature = false;
87 FeatureColourPanel colourPanel;
89 class FeatureColourPanel extends Panel
95 private boolean isColourByLabel, isGcol;
98 * render a feature style in the amend feature dialog box
100 public void updateColor(FeatureColourI newcol)
104 if (newcol.isSimpleColour())
106 bg = newcol.getColour();
111 if (newcol.isAboveThreshold())
115 else if (newcol.isBelowThreshold())
120 if (isColourByLabel = newcol.isColourByLabel())
122 setBackground(bg = Color.white);
123 vlabel += " (by Label)";
127 setBackground(bg = newcol.getMinColour());
128 maxCol = newcol.getMaxColour();
142 public void paint(Graphics g)
144 Dimension d = getSize();
149 g.setColor(Color.white);
150 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
151 g.setColor(Color.black);
152 Font f = new Font("Verdana", Font.PLAIN, 10);
154 g.drawString(MessageManager.getString("label.label"), 0, 0);
159 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
167 boolean amendFeatures(final SequenceI[] sequences,
168 final SequenceFeature[] features, boolean newFeatures,
169 final AlignmentPanel ap)
171 Panel bigPanel = new Panel(new BorderLayout());
172 final TextField name = new TextField(16);
173 final TextField source = new TextField(16);
174 final TextArea description = new TextArea(3, 35);
175 final TextField start = new TextField(8);
176 final TextField end = new TextField(8);
177 final Choice overlaps;
178 Button deleteButton = new Button("Delete");
179 deleteFeature = false;
181 colourPanel = new FeatureColourPanel();
182 colourPanel.setSize(110, 15);
183 final FeatureRenderer fr = this;
185 Panel panel = new Panel(new GridLayout(3, 1));
187 featureIndex = 0; // feature to be amended.
190 // /////////////////////////////////////
191 // /MULTIPLE FEATURES AT SELECTED RESIDUE
192 if (!newFeatures && features.length > 1)
194 panel = new Panel(new GridLayout(4, 1));
196 tmp.add(new Label("Select Feature: "));
197 overlaps = new Choice();
198 for (int i = 0; i < features.length; i++)
200 String item = features[i].getType() + "/" + features[i].getBegin()
201 + "-" + features[i].getEnd();
203 if (features[i].getFeatureGroup() != null)
205 item += " (" + features[i].getFeatureGroup() + ")";
208 overlaps.addItem(item);
213 overlaps.addItemListener(new java.awt.event.ItemListener()
216 public void itemStateChanged(java.awt.event.ItemEvent e)
218 int index = overlaps.getSelectedIndex();
221 featureIndex = index;
222 name.setText(features[index].getType());
223 description.setText(features[index].getDescription());
224 source.setText(features[index].getFeatureGroup());
225 start.setText(features[index].getBegin() + "");
226 end.setText(features[index].getEnd() + "");
228 SearchResults highlight = new SearchResults();
229 highlight.addResult(sequences[0], features[index].getBegin(),
230 features[index].getEnd());
232 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
235 FeatureColourI col = getFeatureStyle(name.getText());
238 Color generatedColour = UserColourScheme
239 .createColourFromName(name.getText());
240 col = new FeatureColour(generatedColour);
243 colourPanel.updateColor(col);
250 // ////////////////////////////////////
254 tmp.add(new Label(MessageManager.getString("label.name:"), Label.RIGHT));
259 tmp.add(new Label(MessageManager.getString("label.group:"), Label.RIGHT));
264 tmp.add(new Label(MessageManager.getString("label.colour"), Label.RIGHT));
265 tmp.add(colourPanel);
267 bigPanel.add(panel, BorderLayout.NORTH);
270 panel.add(new Label(MessageManager.getString("label.description:"),
272 panel.add(new ScrollPane().add(description));
276 bigPanel.add(panel, BorderLayout.SOUTH);
279 panel.add(new Label(MessageManager.getString("label.start"),
282 panel.add(new Label(MessageManager.getString("label.end"),
285 bigPanel.add(panel, BorderLayout.CENTER);
289 bigPanel.add(panel, BorderLayout.CENTER);
292 if (lastFeatureAdded == null)
294 if (features[0].type != null)
296 lastFeatureAdded = features[0].type;
300 lastFeatureAdded = "feature_1";
304 if (lastFeatureGroupAdded == null)
306 if (features[0].featureGroup != null)
308 lastFeatureGroupAdded = features[0].featureGroup;
312 lastFeatureAdded = "Jalview";
316 String title = newFeatures ? MessageManager
317 .getString("label.create_new_sequence_features")
318 : MessageManager.formatMessage("label.amend_delete_features",
319 new String[] { sequences[0].getName() });
321 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
324 dialog.setMainPanel(bigPanel);
328 name.setText(lastFeatureAdded);
329 source.setText(lastFeatureGroupAdded);
333 dialog.ok.setLabel(MessageManager.getString("label.amend"));
334 dialog.buttonPanel.add(deleteButton, 1);
335 deleteButton.addActionListener(new ActionListener()
338 public void actionPerformed(ActionEvent evt)
340 deleteFeature = true;
341 dialog.setVisible(false);
344 name.setText(features[0].getType());
345 source.setText(features[0].getFeatureGroup());
348 start.setText(features[0].getBegin() + "");
349 end.setText(features[0].getEnd() + "");
350 description.setText(features[0].getDescription());
351 // lookup (or generate) the feature colour
352 FeatureColourI fcol = getFeatureStyle(name.getText());
353 // simply display the feature color in a box
354 colourPanel.updateColor(fcol);
355 dialog.setResizable(true);
356 // TODO: render the graduated color in the box.
357 colourPanel.addMouseListener(new MouseAdapter()
360 public void mousePressed(MouseEvent evt)
362 if (!colourPanel.isGcol)
364 new UserDefinedColours(fr, ap.alignFrame);
368 new FeatureColourChooser(ap.alignFrame, name.getText());
369 dialog.transferFocus();
373 dialog.setVisible(true);
375 FeaturesFile ffile = new FeaturesFile();
379 // This ensures that the last sequence
380 // is refreshed and new features are rendered
382 lastFeatureAdded = name.getText().trim();
383 lastFeatureGroupAdded = source.getText().trim();
384 lastDescriptionAdded = description.getText().replace('\n', ' ');
387 if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
389 lastFeatureGroupAdded = null;
395 SequenceFeature sf = features[featureIndex];
398 sf.type = lastFeatureAdded;
399 sf.featureGroup = lastFeatureGroupAdded;
400 sf.description = lastDescriptionAdded;
401 if (!colourPanel.isGcol)
403 // update colour - otherwise its already done.
404 setColour(sf.type, new FeatureColour(colourPanel.getBackground()));
408 sf.begin = Integer.parseInt(start.getText());
409 sf.end = Integer.parseInt(end.getText());
410 } catch (NumberFormatException ex)
414 ffile.parseDescriptionHTML(sf, false);
415 setVisible(lastFeatureAdded); // if user edited name then make sure new
420 sequences[0].deleteFeature(sf);
426 if (dialog.accept && name.getText().length() > 0)
428 for (int i = 0; i < sequences.length; i++)
430 features[i].type = lastFeatureAdded;
431 features[i].featureGroup = lastFeatureGroupAdded;
432 features[i].description = lastDescriptionAdded;
433 sequences[i].addSequenceFeature(features[i]);
434 ffile.parseDescriptionHTML(features[i], false);
437 Color newColour = colourPanel.getBackground();
438 // setColour(lastFeatureAdded, fcol);
440 if (lastFeatureGroupAdded != null)
442 setGroupVisibility(lastFeatureGroupAdded, true);
444 setColour(lastFeatureAdded, new FeatureColour(newColour)); // was fcol
445 setVisible(lastFeatureAdded);
446 findAllFeatures(false); // different to original applet behaviour ?
447 // findAllFeatures();
451 // no update to the alignment
455 // refresh the alignment and the feature settings dialog
456 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
458 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
460 // findAllFeatures();
462 ap.paintAlignment(true);