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.
23 import jalview.datamodel.GraphLine;
24 import jalview.schemes.AnnotationColourGradient;
25 import jalview.schemes.GraduatedColor;
26 import jalview.util.MessageManager;
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.FlowLayout;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.MouseAdapter;
35 import java.awt.event.MouseEvent;
36 import java.util.Hashtable;
38 import javax.swing.BorderFactory;
39 import javax.swing.JCheckBox;
40 import javax.swing.JColorChooser;
41 import javax.swing.JComboBox;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JSlider;
45 import javax.swing.JTextField;
46 import javax.swing.border.LineBorder;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
50 public class FeatureColourChooser extends JalviewDialog
52 // FeatureSettings fs;
55 private GraduatedColor cs;
61 * @return the last colour setting selected by user - either oldcs (which may
62 * be a java.awt.Color) or the new GraduatedColor
64 public Object getLastColour()
73 Hashtable oldgroupColours;
77 boolean adjusting = false;
85 public FeatureColourChooser(FeatureRenderer frender, String type)
87 this(frender, false, type);
90 public FeatureColourChooser(FeatureRenderer frender, boolean block,
96 String title = MessageManager.formatMessage(
97 "label.graduated_color_for_params", new String[] { type });
98 initDialogFrame(this, true, block, title, 480, 185);
99 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
100 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
103 slider.addChangeListener(new ChangeListener()
106 public void stateChanged(ChangeEvent evt)
110 thresholdValue.setText((slider.getValue() / 1000f) + "");
115 slider.addMouseListener(new MouseAdapter()
118 public void mouseReleased(MouseEvent evt)
122 ap.paintAlignment(true);
128 float mm[] = ((float[][]) fr.getMinMax().get(type))[0];
131 oldcs = fr.getFeatureColours().get(type);
132 if (oldcs instanceof GraduatedColor)
134 if (((GraduatedColor) oldcs).isAutoScale())
137 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
141 cs = new GraduatedColor((GraduatedColor) oldcs);
146 // promote original color to a graduated color
147 Color bl = Color.black;
148 if (oldcs instanceof Color)
152 // original colour becomes the maximum colour
153 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
154 cs.setColourByLabel(false);
156 minColour.setBackground(oldminColour = cs.getMinColor());
157 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
163 } catch (Exception ex)
166 // update the gui from threshold state
167 thresholdIsMin.setSelected(!cs.isAutoScale());
168 colourByLabel.setSelected(cs.isColourByLabel());
169 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
171 // initialise threshold slider and selector
173 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
175 slider.setEnabled(true);
176 thresholdValue.setEnabled(true);
177 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
178 "Threshold", Color.black);
188 public FeatureColourChooser()
193 } catch (Exception ex)
195 ex.printStackTrace();
199 private void jbInit() throws Exception
202 minColour.setFont(JvSwingUtils.getLabelFont());
203 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
204 minColour.setPreferredSize(new Dimension(40, 20));
205 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
206 minColour.addMouseListener(new MouseAdapter()
209 public void mousePressed(MouseEvent e)
211 if (minColour.isEnabled())
213 minColour_actionPerformed();
217 maxColour.setFont(JvSwingUtils.getLabelFont());
218 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
219 maxColour.setPreferredSize(new Dimension(40, 20));
220 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
221 maxColour.addMouseListener(new MouseAdapter()
224 public void mousePressed(MouseEvent e)
226 if (maxColour.isEnabled())
228 maxColour_actionPerformed();
232 maxColour.setBorder(new LineBorder(Color.black));
233 minText.setText(MessageManager.getString("label.min"));
234 minText.setFont(JvSwingUtils.getLabelFont());
235 maxText.setText(MessageManager.getString("label.max"));
236 maxText.setFont(JvSwingUtils.getLabelFont());
237 this.setLayout(borderLayout1);
238 jPanel2.setLayout(flowLayout1);
239 jPanel1.setBackground(Color.white);
240 jPanel2.setBackground(Color.white);
241 threshold.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent e)
246 threshold_actionPerformed(e);
249 threshold.setToolTipText(MessageManager
250 .getString("label.threshold_feature_display_by_score"));
251 threshold.addItem(MessageManager
252 .getString("label.threshold_feature_no_thereshold")); // index 0
253 threshold.addItem(MessageManager
254 .getString("label.threshold_feature_above_thereshold")); // index 1
255 threshold.addItem(MessageManager
256 .getString("label.threshold_feature_below_thereshold")); // index 2
257 jPanel3.setLayout(flowLayout2);
258 thresholdValue.addActionListener(new ActionListener()
261 public void actionPerformed(ActionEvent e)
263 thresholdValue_actionPerformed(e);
266 slider.setPaintLabels(false);
267 slider.setPaintTicks(true);
268 slider.setBackground(Color.white);
269 slider.setEnabled(false);
270 slider.setOpaque(false);
271 slider.setPreferredSize(new Dimension(100, 32));
272 slider.setToolTipText(MessageManager
273 .getString("label.adjust_thereshold"));
274 thresholdValue.setEnabled(false);
275 thresholdValue.setColumns(7);
276 jPanel3.setBackground(Color.white);
277 thresholdIsMin.setBackground(Color.white);
278 thresholdIsMin.setText(MessageManager
279 .getString("label.threshold_minmax"));
280 thresholdIsMin.setToolTipText(MessageManager
281 .getString("label.toggle_absolute_relative_display_threshold"));
282 thresholdIsMin.addActionListener(new ActionListener()
285 public void actionPerformed(ActionEvent actionEvent)
287 thresholdIsMin_actionPerformed(actionEvent);
290 colourByLabel.setBackground(Color.white);
292 .setText(MessageManager.getString("label.colour_by_label"));
294 .setToolTipText(MessageManager
295 .getString("label.display_features_same_type_different_label_using_different_colour"));
296 colourByLabel.addActionListener(new ActionListener()
299 public void actionPerformed(ActionEvent actionEvent)
301 colourByLabel_actionPerformed(actionEvent);
304 colourPanel.setBackground(Color.white);
307 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
308 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
309 colourPanel.add(minText);
310 colourPanel.add(minColour);
311 colourPanel.add(maxText);
312 colourPanel.add(maxColour);
313 this.add(jPanel3, java.awt.BorderLayout.CENTER);
314 jPanel3.add(threshold);
316 jPanel3.add(thresholdValue);
317 jPanel3.add(thresholdIsMin);
318 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
319 this.add(jPanel2, java.awt.BorderLayout.NORTH);
322 JLabel minText = new JLabel();
324 JLabel maxText = new JLabel();
326 JPanel minColour = new JPanel();
328 JPanel maxColour = new JPanel();
330 JPanel colourPanel = new JPanel();
332 JPanel jPanel1 = new JPanel();
334 JPanel jPanel2 = new JPanel();
336 BorderLayout borderLayout1 = new BorderLayout();
338 JComboBox threshold = new JComboBox();
340 FlowLayout flowLayout1 = new FlowLayout();
342 JPanel jPanel3 = new JPanel();
344 FlowLayout flowLayout2 = new FlowLayout();
346 JSlider slider = new JSlider();
348 JTextField thresholdValue = new JTextField(20);
350 // TODO implement GUI for tolower flag
351 // JCheckBox toLower = new JCheckBox();
353 JCheckBox thresholdIsMin = new JCheckBox();
355 JCheckBox colourByLabel = new JCheckBox();
357 private GraphLine threshline;
359 private Color oldmaxColour;
361 private Color oldminColour;
363 public void minColour_actionPerformed()
365 Color col = JColorChooser.showDialog(this,
366 MessageManager.getString("label.select_colour_minimum_value"),
367 minColour.getBackground());
370 minColour.setBackground(col);
371 minColour.setForeground(col);
377 public void maxColour_actionPerformed()
379 Color col = JColorChooser.showDialog(this,
380 MessageManager.getString("label.select_colour_maximum_value"),
381 maxColour.getBackground());
384 maxColour.setBackground(col);
385 maxColour.setForeground(col);
393 // Check if combobox is still adjusting
399 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
400 if (threshold.getSelectedIndex() == 1)
402 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
404 else if (threshold.getSelectedIndex() == 2)
406 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
409 slider.setEnabled(true);
410 thresholdValue.setEnabled(true);
413 if (cs.isColourByLabel())
415 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
419 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
420 oldmaxColour = maxColour.getBackground(), min, max);
424 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
426 slider.setEnabled(false);
427 thresholdValue.setEnabled(false);
428 thresholdValue.setText("");
429 thresholdIsMin.setEnabled(false);
431 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
432 && threshline == null)
434 // todo visual indication of feature threshold
435 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
436 "Threshold", Color.black);
439 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
442 acg.setThresh(threshline.value);
444 float range = max * 1000f - min * 1000f;
446 slider.setMinimum((int) (min * 1000));
447 slider.setMaximum((int) (max * 1000));
448 slider.setValue((int) (threshline.value * 1000));
449 thresholdValue.setText(threshline.value + "");
450 slider.setMajorTickSpacing((int) (range / 10f));
451 slider.setEnabled(true);
452 thresholdValue.setEnabled(true);
453 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
457 acg.setThreshType(aboveThreshold);
458 if (thresholdIsMin.isSelected()
459 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
461 acg.setAutoScaled(false);
462 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
464 acg = new GraduatedColor(acg, threshline.value, max);
468 acg = new GraduatedColor(acg, min, threshline.value);
473 acg.setAutoScaled(true);
475 acg.setColourByLabel(colourByLabel.isSelected());
476 if (acg.isColourByLabel())
478 maxColour.setEnabled(false);
479 minColour.setEnabled(false);
480 maxColour.setBackground(this.getBackground());
481 maxColour.setForeground(this.getBackground());
482 minColour.setBackground(this.getBackground());
483 minColour.setForeground(this.getBackground());
488 maxColour.setEnabled(true);
489 minColour.setEnabled(true);
490 maxColour.setBackground(oldmaxColour);
491 minColour.setBackground(oldminColour);
492 maxColour.setForeground(oldmaxColour);
493 minColour.setForeground(oldminColour);
495 fr.setColour(type, acg);
497 ap.paintAlignment(false);
501 protected void raiseClosed()
503 if (this.colourEditor != null)
505 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
510 public void okPressed()
516 public void cancelPressed()
523 fr.setColour(type, oldcs);
524 ap.paintAlignment(false);
528 public void thresholdCheck_actionPerformed(ActionEvent e)
533 public void annotations_actionPerformed(ActionEvent e)
538 public void threshold_actionPerformed(ActionEvent e)
543 public void thresholdValue_actionPerformed(ActionEvent e)
547 float f = Float.parseFloat(thresholdValue.getText());
548 slider.setValue((int) (f * 1000));
549 threshline.value = f;
550 } catch (NumberFormatException ex)
555 public void valueChanged()
557 threshline.value = slider.getValue() / 1000f;
558 cs.setThresh(threshline.value);
560 ap.paintAlignment(false);
563 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
568 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
573 ActionListener colourEditor = null;
575 public void addActionListener(ActionListener graduatedColorEditor)
577 if (colourEditor != null)
580 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
582 colourEditor = graduatedColorEditor;