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.datamodel.SearchResults;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.FeaturesFile;
27 import jalview.schemes.AnnotationColourGradient;
28 import jalview.schemes.GraduatedColor;
29 import jalview.schemes.UserColourScheme;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.AlignmentViewport;
33 import java.awt.BorderLayout;
34 import javax.swing.JButton;
35 import java.awt.Color;
36 import java.awt.Dimension;
38 import java.awt.Graphics;
39 import java.awt.GridLayout;
40 import javax.swing.JLabel;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTextArea;
44 import javax.swing.JTextField;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.util.Hashtable;
55 public class FeatureRenderer extends
56 jalview.renderer.seqfeatures.FeatureRenderer
59 // Holds web links for feature groups and feature types
60 // in the form label|link
61 Hashtable featureLinks = null;
64 * Creates a new FeatureRenderer object.
69 public FeatureRenderer(AlignmentViewport av)
74 setTransparencyAvailable(!System.getProperty("java.version")
78 static String lastFeatureAdded;
80 static String lastFeatureGroupAdded;
82 static String lastDescriptionAdded;
86 boolean deleteFeature = false;
88 FeatureColourPanel colourPanel;
90 class FeatureColourPanel extends JPanel
96 private boolean isColourByLabel, isGcol;
99 * render a feature style in the amend feature dialog box
101 public void updateColor(Object newcol)
104 Color bg, col = null;
105 GraduatedColor gcol = null;
107 if (newcol instanceof Color)
110 col = (Color) newcol;
113 else if (newcol instanceof GraduatedColor)
116 gcol = (GraduatedColor) newcol;
123 .getString("error.invalid_colour_for_mycheckbox"));
127 setBackground(bg = col);
131 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
134 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
137 if (isColourByLabel = gcol.isColourByLabel())
139 setBackground(bg = Color.white);
140 vlabel += " (by Label)";
144 setBackground(bg = gcol.getMinColor());
145 maxCol = gcol.getMaxColor();
158 public void paint(Graphics g)
160 Dimension d = getSize();
165 g.setColor(Color.white);
166 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
167 g.setColor(Color.black);
168 Font f = new Font("Verdana", Font.PLAIN, 10);
170 g.drawString(MessageManager.getString("label.label"), 0, 0);
175 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
183 boolean amendFeatures(final SequenceI[] sequences,
184 final SequenceFeature[] features, boolean newFeatures,
185 final AlignmentPanel ap)
187 JPanel bigPanel = new JPanel(new BorderLayout());
188 final JTextField name = new JTextField(16);
189 final JTextField source = new JTextField(16);
190 final JTextArea description = new JTextArea(3, 35);
191 final JTextField start = new JTextField(8);
192 final JTextField end = new JTextField(8);
193 final Choice overlaps;
194 JButton deleteButton = new JButton("Delete");
195 deleteFeature = false;
197 colourPanel = new FeatureColourPanel();
198 colourPanel.setSize(110, 15);
199 final FeatureRenderer fr = this;
201 JPanel panel = new JPanel(new GridLayout(3, 1));
203 featureIndex = 0; // feature to be amended.
206 // /////////////////////////////////////
207 // /MULTIPLE FEATURES AT SELECTED RESIDUE
208 if (!newFeatures && features.length > 1)
210 panel = new JPanel(new GridLayout(4, 1));
212 tmp.add(new JLabel("Select Feature: "));
213 overlaps = new Choice();
214 for (int i = 0; i < features.length; i++)
216 String item = features[i].getType() + "/" + features[i].getBegin()
217 + "-" + features[i].getEnd();
219 if (features[i].getFeatureGroup() != null)
221 item += " (" + features[i].getFeatureGroup() + ")";
224 overlaps.addItem(item);
229 overlaps.addItemListener(new java.awt.event.ItemListener()
231 public void itemStateChanged(java.awt.event.ItemEvent e)
233 int index = overlaps.getSelectedIndex();
236 featureIndex = index;
237 name.setText(features[index].getType());
238 description.setText(features[index].getDescription());
239 source.setText(features[index].getFeatureGroup());
240 start.setText(features[index].getBegin() + "");
241 end.setText(features[index].getEnd() + "");
243 SearchResults highlight = new SearchResults();
244 highlight.addResult(sequences[0], features[index].getBegin(),
245 features[index].getEnd());
247 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
250 Object col = getFeatureStyle(name.getText());
253 col = new UserColourScheme().createColourFromName(name
257 colourPanel.updateColor(col);
264 // ////////////////////////////////////
268 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
273 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
278 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
279 tmp.add(colourPanel);
281 bigPanel.add(panel, BorderLayout.NORTH);
283 panel = new JPanel();
284 panel.add(new JLabel("Description: ", JLabel.RIGHT));
285 panel.add(new JScrollPane().add(description));
289 bigPanel.add(panel, BorderLayout.SOUTH);
291 panel = new JPanel();
292 panel.add(new JLabel(" Start:", JLabel.RIGHT));
294 panel.add(new JLabel(" End:", JLabel.RIGHT));
296 bigPanel.add(panel, BorderLayout.CENTER);
300 bigPanel.add(panel, BorderLayout.CENTER);
303 if (lastFeatureAdded == null)
305 if (features[0].type != null)
307 lastFeatureAdded = features[0].type;
311 lastFeatureAdded = "feature_1";
315 if (lastFeatureGroupAdded == null)
317 if (features[0].featureGroup != null)
319 lastFeatureGroupAdded = features[0].featureGroup;
323 lastFeatureAdded = "Jalview";
327 String title = newFeatures ? MessageManager
328 .getString("label.create_new_sequence_features")
329 : MessageManager.formatMessage("label.amend_delete_features",
331 { sequences[0].getName() });
333 final JVDialog dialog = new JVDialog(ap.alignFrame, title, true, 385,
336 dialog.setMainPanel(bigPanel);
340 name.setText(lastFeatureAdded);
341 source.setText(lastFeatureGroupAdded);
345 dialog.ok.setLabel(MessageManager.getString("label.amend"));
346 dialog.buttonPanel.add(deleteButton, 1);
347 deleteButton.addActionListener(new ActionListener()
349 public void actionPerformed(ActionEvent evt)
351 deleteFeature = true;
352 dialog.setVisible(false);
355 name.setText(features[0].getType());
356 source.setText(features[0].getFeatureGroup());
359 start.setText(features[0].getBegin() + "");
360 end.setText(features[0].getEnd() + "");
361 description.setText(features[0].getDescription());
362 Color col = getColour(name.getText());
365 col = new UserColourScheme()
366 .createColourFromName(name.getText());
368 Object fcol = getFeatureStyle(name.getText());
369 // simply display the feature color in a box
370 colourPanel.updateColor(fcol);
371 dialog.setResizable(true);
372 // TODO: render the graduated color in the box.
373 colourPanel.addMouseListener(new java.awt.event.MouseAdapter()
375 public void mousePressed(java.awt.event.MouseEvent evt)
377 if (!colourPanel.isGcol)
379 new UserDefinedColours(fr, ap.alignFrame);
383 FeatureColourChooser fcc = new FeatureColourChooser(
384 ap.alignFrame, name.getText());
385 dialog.transferFocus();
389 dialog.setVisible(true);
391 FeaturesFile ffile = new FeaturesFile();
395 // This ensures that the last sequence
396 // is refreshed and new features are rendered
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, 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, 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 (((AlignViewport) av).featureSettings != null)
474 ((AlignViewport) av).featureSettings.refreshTable();
476 // findAllFeatures();
478 ap.paintAlignment(true);