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.api.FeatureColourI;
24 import jalview.datamodel.GraphLine;
25 import jalview.schemes.FeatureColour;
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;
37 import javax.swing.BorderFactory;
38 import javax.swing.JCheckBox;
39 import javax.swing.JColorChooser;
40 import javax.swing.JComboBox;
41 import javax.swing.JLabel;
42 import javax.swing.JPanel;
43 import javax.swing.JSlider;
44 import javax.swing.JTextField;
45 import javax.swing.border.LineBorder;
46 import javax.swing.event.ChangeEvent;
47 import javax.swing.event.ChangeListener;
49 public class FeatureColourChooser extends JalviewDialog
51 // FeatureSettings fs;
54 private FeatureColourI cs;
56 private FeatureColourI oldcs;
60 * @return the last colour setting selected by user - either oldcs (which may
61 * be a java.awt.Color) or the new GraduatedColor
63 public FeatureColourI getLastColour()
74 boolean adjusting = false;
76 final private float min;
78 final private float max;
80 final private float scaleFactor;
84 public FeatureColourChooser(FeatureRenderer frender, String type)
86 this(frender, false, type);
89 public FeatureColourChooser(FeatureRenderer frender, boolean block,
95 String title = MessageManager.formatMessage(
96 "label.graduated_color_for_params", new String[] { type });
97 initDialogFrame(this, true, block, title, 480, 185);
98 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
99 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
102 slider.addChangeListener(new ChangeListener()
105 public void stateChanged(ChangeEvent evt)
109 thresholdValue.setText((slider.getValue() / scaleFactor) + "");
114 slider.addMouseListener(new MouseAdapter()
117 public void mouseReleased(MouseEvent evt)
121 ap.paintAlignment(true);
127 float mm[] = fr.getMinMax().get(type)[0];
132 * ensure scale factor allows a scaled range with
133 * 10 integer divisions ('ticks'); if we have got here,
134 * we should expect that max != min
136 scaleFactor = (max == min) ? 1f : 100f / (max - min);
138 oldcs = fr.getFeatureColours().get(type);
139 if (!oldcs.isSimpleColour())
141 if (oldcs.isAutoScaled())
144 cs = new FeatureColour((FeatureColour) oldcs, min, max);
148 cs = new FeatureColour((FeatureColour) oldcs);
153 // promote original color to a graduated color
154 Color bl = oldcs.getColour();
159 // original colour becomes the maximum colour
160 cs = new FeatureColour(Color.white, bl, mm[0], mm[1]);
161 cs.setColourByLabel(false);
163 minColour.setBackground(oldminColour = cs.getMinColour());
164 maxColour.setBackground(oldmaxColour = cs.getMaxColour());
170 } catch (Exception ex)
173 // update the gui from threshold state
174 thresholdIsMin.setSelected(!cs.isAutoScaled());
175 colourByLabel.setSelected(cs.isColourByLabel());
176 if (cs.hasThreshold())
178 // initialise threshold slider and selector
179 threshold.setSelectedIndex(cs.isAboveThreshold() ? 1 : 2);
180 slider.setEnabled(true);
181 thresholdValue.setEnabled(true);
182 threshline = new GraphLine((max - min) / 2f, "Threshold", Color.black);
192 private void jbInit() throws Exception
195 minColour.setFont(JvSwingUtils.getLabelFont());
196 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
197 minColour.setPreferredSize(new Dimension(40, 20));
198 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
199 minColour.addMouseListener(new MouseAdapter()
202 public void mousePressed(MouseEvent e)
204 if (minColour.isEnabled())
206 minColour_actionPerformed();
210 maxColour.setFont(JvSwingUtils.getLabelFont());
211 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
212 maxColour.setPreferredSize(new Dimension(40, 20));
213 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
214 maxColour.addMouseListener(new MouseAdapter()
217 public void mousePressed(MouseEvent e)
219 if (maxColour.isEnabled())
221 maxColour_actionPerformed();
225 maxColour.setBorder(new LineBorder(Color.black));
226 minText.setText(MessageManager.getString("label.min"));
227 minText.setFont(JvSwingUtils.getLabelFont());
228 maxText.setText(MessageManager.getString("label.max"));
229 maxText.setFont(JvSwingUtils.getLabelFont());
230 this.setLayout(borderLayout1);
231 jPanel2.setLayout(flowLayout1);
232 jPanel1.setBackground(Color.white);
233 jPanel2.setBackground(Color.white);
234 threshold.addActionListener(new ActionListener()
237 public void actionPerformed(ActionEvent e)
239 threshold_actionPerformed(e);
242 threshold.setToolTipText(MessageManager
243 .getString("label.threshold_feature_display_by_score"));
244 threshold.addItem(MessageManager
245 .getString("label.threshold_feature_no_threshold")); // index 0
246 threshold.addItem(MessageManager
247 .getString("label.threshold_feature_above_threshold")); // index 1
248 threshold.addItem(MessageManager
249 .getString("label.threshold_feature_below_threshold")); // index 2
250 jPanel3.setLayout(flowLayout2);
251 thresholdValue.addActionListener(new ActionListener()
254 public void actionPerformed(ActionEvent e)
256 thresholdValue_actionPerformed(e);
259 slider.setPaintLabels(false);
260 slider.setPaintTicks(true);
261 slider.setBackground(Color.white);
262 slider.setEnabled(false);
263 slider.setOpaque(false);
264 slider.setPreferredSize(new Dimension(100, 32));
265 slider.setToolTipText(MessageManager
266 .getString("label.adjust_threshold"));
267 thresholdValue.setEnabled(false);
268 thresholdValue.setColumns(7);
269 jPanel3.setBackground(Color.white);
270 thresholdIsMin.setBackground(Color.white);
271 thresholdIsMin.setText(MessageManager
272 .getString("label.threshold_minmax"));
273 thresholdIsMin.setToolTipText(MessageManager
274 .getString("label.toggle_absolute_relative_display_threshold"));
275 thresholdIsMin.addActionListener(new ActionListener()
278 public void actionPerformed(ActionEvent actionEvent)
280 thresholdIsMin_actionPerformed(actionEvent);
283 colourByLabel.setBackground(Color.white);
285 .setText(MessageManager.getString("label.colour_by_label"));
287 .setToolTipText(MessageManager
288 .getString("label.display_features_same_type_different_label_using_different_colour"));
289 colourByLabel.addActionListener(new ActionListener()
292 public void actionPerformed(ActionEvent actionEvent)
294 colourByLabel_actionPerformed(actionEvent);
297 colourPanel.setBackground(Color.white);
300 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
301 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
302 colourPanel.add(minText);
303 colourPanel.add(minColour);
304 colourPanel.add(maxText);
305 colourPanel.add(maxColour);
306 this.add(jPanel3, java.awt.BorderLayout.CENTER);
307 jPanel3.add(threshold);
309 jPanel3.add(thresholdValue);
310 jPanel3.add(thresholdIsMin);
311 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
312 this.add(jPanel2, java.awt.BorderLayout.NORTH);
315 JLabel minText = new JLabel();
317 JLabel maxText = new JLabel();
319 JPanel minColour = new JPanel();
321 JPanel maxColour = new JPanel();
323 JPanel colourPanel = new JPanel();
325 JPanel jPanel1 = new JPanel();
327 JPanel jPanel2 = new JPanel();
329 BorderLayout borderLayout1 = new BorderLayout();
331 JComboBox threshold = new JComboBox();
333 FlowLayout flowLayout1 = new FlowLayout();
335 JPanel jPanel3 = new JPanel();
337 FlowLayout flowLayout2 = new FlowLayout();
339 JSlider slider = new JSlider();
341 JTextField thresholdValue = new JTextField(20);
343 // TODO implement GUI for tolower flag
344 // JCheckBox toLower = new JCheckBox();
346 JCheckBox thresholdIsMin = new JCheckBox();
348 JCheckBox colourByLabel = new JCheckBox();
350 private GraphLine threshline;
352 private Color oldmaxColour;
354 private Color oldminColour;
356 public void minColour_actionPerformed()
358 Color col = JColorChooser.showDialog(this,
359 MessageManager.getString("label.select_colour_minimum_value"),
360 minColour.getBackground());
363 minColour.setBackground(col);
364 minColour.setForeground(col);
370 public void maxColour_actionPerformed()
372 Color col = JColorChooser.showDialog(this,
373 MessageManager.getString("label.select_colour_maximum_value"),
374 maxColour.getBackground());
377 maxColour.setBackground(col);
378 maxColour.setForeground(col);
386 // Check if combobox is still adjusting
392 boolean aboveThreshold = false;
393 boolean belowThreshold = false;
394 if (threshold.getSelectedIndex() == 1)
396 aboveThreshold = true;
398 else if (threshold.getSelectedIndex() == 2)
400 belowThreshold = true;
402 boolean hasThreshold = aboveThreshold || belowThreshold;
404 slider.setEnabled(true);
405 thresholdValue.setEnabled(true);
408 if (cs.isColourByLabel())
410 acg = new FeatureColour(oldminColour, oldmaxColour, min, max);
414 acg = new FeatureColour(oldminColour = minColour.getBackground(),
415 oldmaxColour = maxColour.getBackground(), min, max);
421 slider.setEnabled(false);
422 thresholdValue.setEnabled(false);
423 thresholdValue.setText("");
424 thresholdIsMin.setEnabled(false);
426 else if (threshline == null)
428 // todo visual indication of feature threshold
429 threshline = new GraphLine((max - min) / 2f, "Threshold", Color.black);
435 acg.setThreshold(threshline.value);
437 float range = (max - min) * scaleFactor;
439 slider.setMinimum((int) (min * scaleFactor));
440 slider.setMaximum((int) (max * scaleFactor));
441 slider.setValue((int) (threshline.value * scaleFactor));
442 thresholdValue.setText(threshline.value + "");
443 slider.setMajorTickSpacing((int) (range / 10f));
444 slider.setEnabled(true);
445 thresholdValue.setEnabled(true);
446 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
450 acg.setAboveThreshold(aboveThreshold);
451 acg.setBelowThreshold(belowThreshold);
452 if (thresholdIsMin.isSelected() && hasThreshold)
454 acg.setAutoScaled(false);
457 acg = new FeatureColour((FeatureColour) acg, threshline.value, max);
461 acg = new FeatureColour((FeatureColour) acg, min, threshline.value);
466 acg.setAutoScaled(true);
468 acg.setColourByLabel(colourByLabel.isSelected());
469 if (acg.isColourByLabel())
471 maxColour.setEnabled(false);
472 minColour.setEnabled(false);
473 maxColour.setBackground(this.getBackground());
474 maxColour.setForeground(this.getBackground());
475 minColour.setBackground(this.getBackground());
476 minColour.setForeground(this.getBackground());
481 maxColour.setEnabled(true);
482 minColour.setEnabled(true);
483 maxColour.setBackground(oldmaxColour);
484 minColour.setBackground(oldminColour);
485 maxColour.setForeground(oldmaxColour);
486 minColour.setForeground(oldminColour);
488 fr.setColour(type, acg);
490 ap.paintAlignment(false);
494 protected void raiseClosed()
496 if (this.colourEditor != null)
498 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
503 public void okPressed()
509 public void cancelPressed()
516 fr.setColour(type, oldcs);
517 ap.paintAlignment(false);
521 public void thresholdCheck_actionPerformed(ActionEvent e)
526 public void annotations_actionPerformed(ActionEvent e)
531 public void threshold_actionPerformed(ActionEvent e)
536 public void thresholdValue_actionPerformed(ActionEvent e)
540 float f = Float.parseFloat(thresholdValue.getText());
541 slider.setValue((int) (f * scaleFactor));
542 threshline.value = f;
543 } catch (NumberFormatException ex)
548 public void valueChanged()
550 threshline.value = slider.getValue() / scaleFactor;
551 cs.setThreshold(threshline.value);
553 ap.paintAlignment(false);
556 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
561 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
566 ActionListener colourEditor = null;
568 public void addActionListener(ActionListener graduatedColorEditor)
570 if (colourEditor != null)
573 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
575 colourEditor = graduatedColorEditor;