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.Colour;
29 import jalview.schemes.FeatureColour;
30 import jalview.schemes.UserColourScheme;
31 import jalview.util.ColorUtils;
32 import jalview.util.MessageManager;
33 import jalview.viewmodel.AlignmentViewport;
35 import java.awt.BorderLayout;
36 import java.awt.Button;
37 import java.awt.Choice;
38 import java.awt.Color;
39 import java.awt.Dimension;
41 import java.awt.Graphics;
42 import java.awt.GridLayout;
43 import java.awt.Label;
44 import java.awt.Panel;
45 import java.awt.ScrollPane;
46 import java.awt.TextArea;
47 import java.awt.TextField;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.MouseAdapter;
51 import java.awt.event.MouseEvent;
52 import java.util.Hashtable;
60 public class FeatureRenderer extends
61 jalview.renderer.seqfeatures.FeatureRenderer
64 * Creates a new FeatureRenderer object.
69 public FeatureRenderer(AlignmentViewport av)
75 static String lastFeatureAdded;
77 static String lastFeatureGroupAdded;
79 static String lastDescriptionAdded;
83 boolean deleteFeature = false;
85 FeatureColourPanel colourPanel;
87 class FeatureColourPanel extends Panel
93 private boolean isColourByLabel, isGcol;
96 * render a feature style in the amend feature dialog box
98 public void updateColor(FeatureColourI newcol)
102 if (newcol.isSimpleColour())
104 bg = ColorUtils.getColor(newcol.getColour());
109 if (newcol.isAboveThreshold())
113 else if (newcol.isBelowThreshold())
118 if (isColourByLabel = newcol.isColourByLabel())
120 setBackground(bg = Color.white);
121 vlabel += " (by Label)";
125 setBackground(bg = ColorUtils.getColor(newcol.getMinColour()));
126 maxCol = ColorUtils.getColor(newcol.getMaxColour());
140 public void paint(Graphics g)
142 Dimension d = getSize();
147 g.setColor(Color.white);
148 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
149 g.setColor(Color.black);
150 Font f = new Font("Verdana", Font.PLAIN, 10);
152 g.drawString(MessageManager.getString("label.label"), 0, 0);
157 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
165 boolean amendFeatures(final SequenceI[] sequences,
166 final SequenceFeature[] features, boolean newFeatures,
167 final AlignmentPanel ap)
169 Panel bigPanel = new Panel(new BorderLayout());
170 final TextField name = new TextField(16);
171 final TextField source = new TextField(16);
172 final TextArea description = new TextArea(3, 35);
173 final TextField start = new TextField(8);
174 final TextField end = new TextField(8);
175 final Choice overlaps;
176 Button deleteButton = new Button("Delete");
177 deleteFeature = false;
179 colourPanel = new FeatureColourPanel();
180 colourPanel.setSize(110, 15);
181 final FeatureRenderer fr = this;
183 Panel panel = new Panel(new GridLayout(3, 1));
185 featureIndex = 0; // feature to be amended.
188 // /////////////////////////////////////
189 // /MULTIPLE FEATURES AT SELECTED RESIDUE
190 if (!newFeatures && features.length > 1)
192 panel = new Panel(new GridLayout(4, 1));
194 tmp.add(new Label("Select Feature: "));
195 overlaps = new Choice();
196 for (int i = 0; i < features.length; i++)
198 String item = features[i].getType() + "/" + features[i].getBegin()
199 + "-" + features[i].getEnd();
201 if (features[i].getFeatureGroup() != null)
203 item += " (" + features[i].getFeatureGroup() + ")";
206 overlaps.addItem(item);
211 overlaps.addItemListener(new java.awt.event.ItemListener()
214 public void itemStateChanged(java.awt.event.ItemEvent e)
216 int index = overlaps.getSelectedIndex();
219 featureIndex = index;
220 name.setText(features[index].getType());
221 description.setText(features[index].getDescription());
222 source.setText(features[index].getFeatureGroup());
223 start.setText(features[index].getBegin() + "");
224 end.setText(features[index].getEnd() + "");
226 SearchResults highlight = new SearchResults();
227 highlight.addResult(sequences[0], features[index].getBegin(),
228 features[index].getEnd());
230 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
233 FeatureColourI col = getFeatureStyle(name.getText());
236 Color generatedColour = UserColourScheme
237 .createColourFromName(name.getText());
238 col = new FeatureColour(new Colour(generatedColour));
241 colourPanel.updateColor(col);
248 // ////////////////////////////////////
252 tmp.add(new Label("Name: ", Label.RIGHT));
257 tmp.add(new Label("Group: ", Label.RIGHT));
262 tmp.add(new Label("Colour: ", Label.RIGHT));
263 tmp.add(colourPanel);
265 bigPanel.add(panel, BorderLayout.NORTH);
268 panel.add(new Label("Description: ", Label.RIGHT));
269 panel.add(new ScrollPane().add(description));
273 bigPanel.add(panel, BorderLayout.SOUTH);
276 panel.add(new Label(" Start:", Label.RIGHT));
278 panel.add(new Label(" End:", Label.RIGHT));
280 bigPanel.add(panel, BorderLayout.CENTER);
284 bigPanel.add(panel, BorderLayout.CENTER);
287 if (lastFeatureAdded == null)
289 if (features[0].type != null)
291 lastFeatureAdded = features[0].type;
295 lastFeatureAdded = "feature_1";
299 if (lastFeatureGroupAdded == null)
301 if (features[0].featureGroup != null)
303 lastFeatureGroupAdded = features[0].featureGroup;
307 lastFeatureAdded = "Jalview";
311 String title = newFeatures ? MessageManager
312 .getString("label.create_new_sequence_features")
313 : MessageManager.formatMessage("label.amend_delete_features",
314 new String[] { sequences[0].getName() });
316 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
319 dialog.setMainPanel(bigPanel);
323 name.setText(lastFeatureAdded);
324 source.setText(lastFeatureGroupAdded);
328 dialog.ok.setLabel(MessageManager.getString("label.amend"));
329 dialog.buttonPanel.add(deleteButton, 1);
330 deleteButton.addActionListener(new ActionListener()
333 public void actionPerformed(ActionEvent evt)
335 deleteFeature = true;
336 dialog.setVisible(false);
339 name.setText(features[0].getType());
340 source.setText(features[0].getFeatureGroup());
343 start.setText(features[0].getBegin() + "");
344 end.setText(features[0].getEnd() + "");
345 description.setText(features[0].getDescription());
346 // lookup (or generate) the feature colour
347 FeatureColourI fcol = getFeatureStyle(name.getText());
348 // simply display the feature color in a box
349 colourPanel.updateColor(fcol);
350 dialog.setResizable(true);
351 // TODO: render the graduated color in the box.
352 colourPanel.addMouseListener(new MouseAdapter()
355 public void mousePressed(MouseEvent evt)
357 if (!colourPanel.isGcol)
359 new UserDefinedColours(fr, ap.alignFrame);
363 new FeatureColourChooser(ap.alignFrame, name.getText());
364 dialog.transferFocus();
368 dialog.setVisible(true);
370 FeaturesFile ffile = new FeaturesFile();
374 // This ensures that the last sequence
375 // is refreshed and new features are rendered
377 lastFeatureAdded = name.getText().trim();
378 lastFeatureGroupAdded = source.getText().trim();
379 lastDescriptionAdded = description.getText().replace('\n', ' ');
382 if (lastFeatureGroupAdded != null && lastFeatureGroupAdded.length() < 1)
384 lastFeatureGroupAdded = null;
390 SequenceFeature sf = features[featureIndex];
393 sf.type = lastFeatureAdded;
394 sf.featureGroup = lastFeatureGroupAdded;
395 sf.description = lastDescriptionAdded;
396 if (!colourPanel.isGcol)
398 // update colour - otherwise its already done.
401 new FeatureColour(new Colour(colourPanel.getBackground())));
405 sf.begin = Integer.parseInt(start.getText());
406 sf.end = Integer.parseInt(end.getText());
407 } catch (NumberFormatException ex)
411 ffile.parseDescriptionHTML(sf, false);
412 setVisible(lastFeatureAdded); // if user edited name then make sure new
417 sequences[0].deleteFeature(sf);
423 if (dialog.accept && name.getText().length() > 0)
425 for (int i = 0; i < sequences.length; i++)
427 features[i].type = lastFeatureAdded;
428 features[i].featureGroup = lastFeatureGroupAdded;
429 features[i].description = lastDescriptionAdded;
430 sequences[i].addSequenceFeature(features[i]);
431 ffile.parseDescriptionHTML(features[i], false);
434 Color newColour = colourPanel.getBackground();
435 // setColour(lastFeatureAdded, fcol);
437 if (lastFeatureGroupAdded != null)
439 setGroupVisibility(lastFeatureGroupAdded, true);
441 setColour(lastFeatureAdded,
442 new FeatureColour(new Colour(newColour))); // was fcol
443 setVisible(lastFeatureAdded);
444 findAllFeatures(false); // different to original applet behaviour ?
445 // findAllFeatures();
449 // no update to the alignment
453 // refresh the alignment and the feature settings dialog
454 if (((jalview.appletgui.AlignViewport) av).featureSettings != null)
456 ((jalview.appletgui.AlignViewport) av).featureSettings.refreshTable();
458 // findAllFeatures();
460 ap.paintAlignment(true);