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.GraphLine;
25 import jalview.schemes.AnnotationColourGradient;
26 import jalview.schemes.FeatureColour;
27 import jalview.util.MessageManager;
29 import java.awt.Checkbox;
30 import java.awt.Choice;
31 import java.awt.Color;
32 import java.awt.Dimension;
33 import java.awt.FlowLayout;
35 import java.awt.Frame;
36 import java.awt.GridLayout;
37 import java.awt.Label;
38 import java.awt.Panel;
39 import java.awt.Scrollbar;
40 import java.awt.TextField;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.AdjustmentEvent;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.FocusAdapter;
46 import java.awt.event.FocusEvent;
47 import java.awt.event.ItemEvent;
48 import java.awt.event.ItemListener;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
52 public class FeatureColourChooser extends Panel implements ActionListener,
53 AdjustmentListener, ItemListener, MouseListener
56 * the absolute min-max range of a feature score is scaled to
57 * 1000 positions on the colour threshold slider
59 private static final int SCALE_FACTOR_1K = 1000;
61 private static final String COLON = ":";
63 private JVDialog frame;
67 private FeatureRenderer fr;
69 private FeatureSettings fs = null;
71 private FeatureColourI cs;
73 private FeatureColourI oldcs;
75 private boolean adjusting = false;
77 private float min, max;
79 private String type = null;
81 private AlignFrame af = null;
83 private Panel minColour = new Panel();
85 private Panel maxColour = new Panel();
87 private Choice threshold = new Choice();
89 private Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
91 private TextField thresholdValue = new TextField(20);
93 private Checkbox thresholdIsMin = new Checkbox();
95 private Checkbox colourFromLabel = new Checkbox();
97 private GraphLine threshline;
100 * Constructor given a context AlignFrame and a feature type. This is used
101 * when opening the graduated colour dialog from the Amend Feature dialog.
106 public FeatureColourChooser(AlignFrame alignFrame, String featureType)
108 this.af = alignFrame;
109 init(alignFrame.getSeqcanvas().getFeatureRenderer(), featureType);
113 * Constructor given a context FeatureSettings and a feature type. This is
114 * used when opening the graduated colour dialog from Feature Settings.
119 public FeatureColourChooser(FeatureSettings fsettings, String featureType)
122 init(fsettings.fr, featureType);
125 private void init(FeatureRenderer frenderer, String featureType)
127 this.type = featureType;
129 float mm[] = fr.getMinMax().get(type)[0];
132 threshline = new GraphLine((max - min) / 2f, "Threshold", Color.black);
133 oldcs = fr.getFeatureColours().get(type);
134 if (oldcs.isGraduatedColour())
136 threshline.value = oldcs.getThreshold();
137 cs = new FeatureColour((FeatureColour) oldcs, min, max);
141 // promote original color to a graduated color
142 Color bl = Color.black;
143 if (oldcs.isSimpleColour())
145 bl = oldcs.getColour();
147 // original colour becomes the maximum colour
148 cs = new FeatureColour(Color.white, bl, mm[0], mm[1]);
150 minColour.setBackground(cs.getMinColour());
151 maxColour.setBackground(cs.getMaxColour());
152 minColour.setForeground(cs.getMinColour());
153 maxColour.setForeground(cs.getMaxColour());
154 colourFromLabel.setState(cs.isColourByLabel());
160 } catch (Exception ex)
164 cs.isAboveThreshold() ? 1 : (cs.isBelowThreshold() ? 2 : 0));
168 colourFromLabel.addItemListener(this);
169 slider.addAdjustmentListener(this);
170 slider.addMouseListener(this);
171 owner = (af != null) ? af : fs.frame;
172 frame = new JVDialog(owner, MessageManager.formatMessage(
173 "label.variable_color_for", new String[] { type }), true, 480,
175 frame.setMainPanel(this);
177 frame.setVisible(true);
186 frame.setVisible(false);
190 public FeatureColourChooser()
195 } catch (Exception ex)
197 ex.printStackTrace();
201 private void jbInit() throws Exception
203 Label minLabel = new Label(
204 MessageManager.getString("label.min_value") + COLON);
205 Label maxLabel = new Label(
206 MessageManager.getString("label.max_value") + COLON);
207 minLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
208 maxLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
209 // minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
210 // minColour.setLabel("Min Colour");
212 minColour.setBounds(0, 0, 40, 27);
213 maxColour.setBounds(0, 0, 40, 27);
214 minColour.addMouseListener(this);
216 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
217 maxColour.addMouseListener(this);
219 thresholdIsMin.addItemListener(this);
221 this.setLayout(new GridLayout(4, 1));
222 Panel jPanel1 = new Panel();
223 jPanel1.setLayout(new FlowLayout());
224 Panel jPanel2 = new Panel();
225 jPanel2.setLayout(new FlowLayout());
226 Panel jPanel3 = new Panel();
227 jPanel3.setLayout(new GridLayout(1, 1));
228 Panel jPanel4 = new Panel();
229 jPanel4.setLayout(new FlowLayout());
230 jPanel1.setBackground(Color.white);
231 jPanel2.setBackground(Color.white);
232 jPanel4.setBackground(Color.white);
233 threshold.addItemListener(this);
234 threshold.addItem(MessageManager
235 .getString("label.threshold_feature_no_threshold"));
236 threshold.addItem(MessageManager
237 .getString("label.threshold_feature_above_threshold"));
238 threshold.addItem(MessageManager
239 .getString("label.threshold_feature_below_threshold"));
240 thresholdValue.addActionListener(this);
241 thresholdValue.addFocusListener(new FocusAdapter()
244 public void focusLost(FocusEvent e)
246 thresholdValue_actionPerformed();
249 slider.setBackground(Color.white);
250 slider.setEnabled(false);
251 slider.setSize(new Dimension(93, 21));
252 thresholdValue.setEnabled(false);
253 thresholdValue.setSize(new Dimension(79, 22)); // setBounds(new
254 // Rectangle(248, 2, 79,
256 thresholdValue.setColumns(5);
257 jPanel3.setBackground(Color.white);
259 colourFromLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
261 .setLabel(MessageManager.getString("label.colour_by_label"));
262 colourFromLabel.setSize(new Dimension(139, 22));
263 // threshold.setBounds(new Rectangle(11, 3, 139, 22));
264 thresholdIsMin.setBackground(Color.white);
266 .setLabel(MessageManager.getString("label.threshold_minmax"));
267 thresholdIsMin.setSize(new Dimension(135, 23));
268 // thresholdIsMin.setBounds(new Rectangle(328, 3, 135, 23));
269 jPanel1.add(minLabel);
270 jPanel1.add(minColour);
271 jPanel1.add(maxLabel);
272 jPanel1.add(maxColour);
273 jPanel1.add(colourFromLabel);
274 jPanel2.add(threshold);
276 jPanel4.add(thresholdValue);
277 jPanel4.add(thresholdIsMin);
278 this.add(jPanel1);// , java.awt.BorderLayout.NORTH);
279 this.add(jPanel2);// , java.awt.BorderLayout.NORTH);
280 this.add(jPanel3);// , java.awt.BorderLayout.CENTER);
281 this.add(jPanel4);// , java.awt.BorderLayout.CENTER);
285 public void actionPerformed(ActionEvent evt)
287 if (evt.getSource() == thresholdValue)
289 thresholdValue_actionPerformed();
291 else if (evt.getSource() == minColour)
293 minColour_actionPerformed(null);
295 else if (evt.getSource() == maxColour)
297 maxColour_actionPerformed(null);
306 * Action on input of a value for colour score threshold
308 protected void thresholdValue_actionPerformed()
312 float f = new Float(thresholdValue.getText()).floatValue();
313 slider.setValue((int) (f * SCALE_FACTOR_1K));
314 adjustmentValueChanged(null);
317 * force repaint of any Overview window or structure
320 } catch (NumberFormatException ex)
326 public void itemStateChanged(ItemEvent evt)
328 maxColour.setEnabled(!colourFromLabel.getState());
329 minColour.setEnabled(!colourFromLabel.getState());
334 * Handler called when the value of the threshold slider changes, either by
335 * user action or programmatically
338 public void adjustmentValueChanged(AdjustmentEvent evt)
342 thresholdValue.setText((slider.getValue() / 1000f) + "");
348 * Responds to a change of colour threshold by computing the absolute value
349 * and refreshing the alignment.
351 protected void valueChanged()
353 threshline.value = slider.getValue() / 1000f;
354 cs.setThreshold(threshline.value);
356 PaintRefresher.Refresh(this, fr.getViewport().getSequenceSetId());
359 public void minColour_actionPerformed(Color newCol)
363 new UserDefinedColours(this, minColour.getBackground(), owner,
365 .getString("label.select_colour_minimum_value"));
369 minColour.setBackground(newCol);
370 minColour.setForeground(newCol);
377 public void maxColour_actionPerformed(Color newCol)
381 new UserDefinedColours(this, maxColour.getBackground(), owner,
383 .getString("label.select_colour_maximum_value"));
387 maxColour.setBackground(newCol);
388 maxColour.setForeground(newCol);
394 void changeColour(boolean updateOverview)
396 // Check if combobox is still adjusting
402 int thresholdOption = AnnotationColourGradient.NO_THRESHOLD;
403 if (threshold.getSelectedIndex() == 1)
405 thresholdOption = AnnotationColourGradient.ABOVE_THRESHOLD;
407 else if (threshold.getSelectedIndex() == 2)
409 thresholdOption = AnnotationColourGradient.BELOW_THRESHOLD;
412 slider.setEnabled(true);
413 thresholdValue.setEnabled(true);
414 FeatureColour acg = new FeatureColour(minColour.getBackground(),
415 maxColour.getBackground(), min, max);
417 acg.setColourByLabel(colourFromLabel.getState());
418 maxColour.setEnabled(!colourFromLabel.getState());
419 minColour.setEnabled(!colourFromLabel.getState());
420 if (thresholdOption == AnnotationColourGradient.NO_THRESHOLD)
422 slider.setEnabled(false);
423 thresholdValue.setEnabled(false);
424 thresholdValue.setText("");
427 if (thresholdOption != AnnotationColourGradient.NO_THRESHOLD)
430 acg.setThreshold(threshline.value);
432 slider.setMinimum((int) (min * SCALE_FACTOR_1K));
433 slider.setMaximum((int) (max * SCALE_FACTOR_1K));
434 slider.setValue((int) (threshline.value * SCALE_FACTOR_1K));
435 thresholdValue.setText(threshline.value + "");
436 slider.setEnabled(true);
437 thresholdValue.setEnabled(true);
441 acg.setAboveThreshold(
442 thresholdOption == AnnotationColourGradient.ABOVE_THRESHOLD);
443 acg.setBelowThreshold(
444 thresholdOption == AnnotationColourGradient.BELOW_THRESHOLD);
446 if (thresholdIsMin.getState()
447 && thresholdOption != AnnotationColourGradient.NO_THRESHOLD)
449 if (thresholdOption == AnnotationColourGradient.ABOVE_THRESHOLD)
451 acg = new FeatureColour(acg, threshline.value, max);
455 acg = new FeatureColour(acg, min, threshline.value);
459 fr.setColour(type, acg);
461 fs.selectionChanged(updateOverview);
466 fr.setColour(type, oldcs);
467 fs.selectionChanged(true);
471 public void mouseClicked(MouseEvent evt)
476 public void mousePressed(MouseEvent evt)
481 public void mouseReleased(MouseEvent evt)
483 if (evt.getSource() == minColour)
485 minColour_actionPerformed(null);
487 else if (evt.getSource() == maxColour)
489 maxColour_actionPerformed(null);
494 // PaintRefresher.Refresh(this, fr.getViewport().getSequenceSetId());
499 public void mouseEntered(MouseEvent evt)
504 public void mouseExited(MouseEvent evt)