2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.awt.event.*;
26 import javax.swing.border.LineBorder;
27 import javax.swing.event.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import java.awt.Dimension;
33 public class FeatureColourChooser extends JalviewDialog
35 // FeatureSettings fs;
38 private GraduatedColor cs;
44 * @return the last colour setting selected by user - either oldcs (which may
45 * be a java.awt.Color) or the new GraduatedColor
47 public Object getLastColour()
56 Hashtable oldgroupColours;
60 boolean adjusting = false;
68 public FeatureColourChooser(FeatureRenderer frender, String type)
70 this(frender, false, type);
73 public FeatureColourChooser(FeatureRenderer frender, boolean block,
79 initDialogFrame(this, true, block, "Graduated Feature Colour for "
81 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
82 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
85 slider.addChangeListener(new ChangeListener()
87 public void stateChanged(ChangeEvent evt)
91 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
96 slider.addMouseListener(new MouseAdapter()
98 public void mouseReleased(MouseEvent evt)
102 ap.paintAlignment(true);
108 float mm[] = ((float[][]) fr.minmax.get(type))[0];
111 oldcs = fr.featureColours.get(type);
112 if (oldcs instanceof GraduatedColor)
114 if (((GraduatedColor) oldcs).isAutoScale())
117 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
121 cs = new GraduatedColor((GraduatedColor) oldcs);
126 // promote original color to a graduated color
127 Color bl = Color.black;
128 if (oldcs instanceof Color)
132 // original colour becomes the maximum colour
133 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
134 cs.setColourByLabel(false);
136 minColour.setBackground(oldminColour = cs.getMinColor());
137 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
143 } catch (Exception ex)
146 // update the gui from threshold state
147 thresholdIsMin.setSelected(!cs.isAutoScale());
148 colourByLabel.setSelected(cs.isColourByLabel());
149 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
151 // initialise threshold slider and selector
153 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
155 slider.setEnabled(true);
156 thresholdValue.setEnabled(true);
157 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
158 "Threshold", Color.black);
168 public FeatureColourChooser()
173 } catch (Exception ex)
175 ex.printStackTrace();
179 private void jbInit() throws Exception
182 minColour.setFont(JvSwingUtils.getLabelFont());
183 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
184 minColour.setPreferredSize(new Dimension(40, 20));
185 minColour.setToolTipText("Minimum Colour");
186 minColour.addMouseListener(new MouseAdapter()
188 public void mousePressed(MouseEvent e)
190 if (minColour.isEnabled())
192 minColour_actionPerformed();
196 maxColour.setFont(JvSwingUtils.getLabelFont());
197 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
198 maxColour.setPreferredSize(new Dimension(40, 20));
199 maxColour.setToolTipText("Maximum Colour");
200 maxColour.addMouseListener(new MouseAdapter()
202 public void mousePressed(MouseEvent e)
204 if (maxColour.isEnabled())
206 maxColour_actionPerformed();
210 maxColour.setBorder(new LineBorder(Color.black));
211 minText.setText("Min:");
212 minText.setFont(JvSwingUtils.getLabelFont());
213 maxText.setText("Max:");
214 maxText.setFont(JvSwingUtils.getLabelFont());
215 this.setLayout(borderLayout1);
216 jPanel2.setLayout(flowLayout1);
217 jPanel1.setBackground(Color.white);
218 jPanel2.setBackground(Color.white);
219 threshold.addActionListener(new ActionListener()
221 public void actionPerformed(ActionEvent e)
223 threshold_actionPerformed(e);
226 threshold.setToolTipText("Threshold the feature display by score.");
227 threshold.addItem("No Threshold"); // index 0
228 threshold.addItem("Above Threshold"); // index 1
229 threshold.addItem("Below Threshold"); // index 2
230 jPanel3.setLayout(flowLayout2);
231 thresholdValue.addActionListener(new ActionListener()
233 public void actionPerformed(ActionEvent e)
235 thresholdValue_actionPerformed(e);
238 slider.setPaintLabels(false);
239 slider.setPaintTicks(true);
240 slider.setBackground(Color.white);
241 slider.setEnabled(false);
242 slider.setOpaque(false);
243 slider.setPreferredSize(new Dimension(100, 32));
244 slider.setToolTipText("Adjust threshold");
245 thresholdValue.setEnabled(false);
246 thresholdValue.setColumns(7);
247 jPanel3.setBackground(Color.white);
248 thresholdIsMin.setBackground(Color.white);
249 thresholdIsMin.setText("Threshold is Min/Max");
251 .setToolTipText("Toggle between absolute and relative display threshold.");
252 thresholdIsMin.addActionListener(new ActionListener()
254 public void actionPerformed(ActionEvent actionEvent)
256 thresholdIsMin_actionPerformed(actionEvent);
259 colourByLabel.setBackground(Color.white);
260 colourByLabel.setText("Colour by Label");
262 .setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
263 colourByLabel.addActionListener(new ActionListener()
265 public void actionPerformed(ActionEvent actionEvent)
267 colourByLabel_actionPerformed(actionEvent);
270 colourPanel.setBackground(Color.white);
273 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
274 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
275 colourPanel.add(minText);
276 colourPanel.add(minColour);
277 colourPanel.add(maxText);
278 colourPanel.add(maxColour);
279 this.add(jPanel3, java.awt.BorderLayout.CENTER);
280 jPanel3.add(threshold);
282 jPanel3.add(thresholdValue);
283 jPanel3.add(thresholdIsMin);
284 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
285 this.add(jPanel2, java.awt.BorderLayout.NORTH);
288 JLabel minText = new JLabel();
290 JLabel maxText = new JLabel();
292 JPanel minColour = new JPanel();
294 JPanel maxColour = new JPanel();
296 JPanel colourPanel = new JPanel();
298 JPanel jPanel1 = new JPanel();
300 JPanel jPanel2 = new JPanel();
302 BorderLayout borderLayout1 = new BorderLayout();
304 JComboBox threshold = new JComboBox();
306 FlowLayout flowLayout1 = new FlowLayout();
308 JPanel jPanel3 = new JPanel();
310 FlowLayout flowLayout2 = new FlowLayout();
312 JSlider slider = new JSlider();
314 JTextField thresholdValue = new JTextField(20);
316 // TODO implement GUI for tolower flag
317 // JCheckBox toLower = new JCheckBox();
319 JCheckBox thresholdIsMin = new JCheckBox();
321 JCheckBox colourByLabel = new JCheckBox();
323 private GraphLine threshline;
325 private Color oldmaxColour;
327 private Color oldminColour;
329 public void minColour_actionPerformed()
331 Color col = JColorChooser.showDialog(this,
332 "Select Colour for Minimum Value", minColour.getBackground());
335 minColour.setBackground(col);
336 minColour.setForeground(col);
342 public void maxColour_actionPerformed()
344 Color col = JColorChooser.showDialog(this,
345 "Select Colour for Maximum Value", maxColour.getBackground());
348 maxColour.setBackground(col);
349 maxColour.setForeground(col);
357 // Check if combobox is still adjusting
363 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
364 if (threshold.getSelectedItem().equals("Above Threshold"))
366 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
368 else if (threshold.getSelectedItem().equals("Below Threshold"))
370 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
373 slider.setEnabled(true);
374 thresholdValue.setEnabled(true);
377 if (cs.isColourByLabel())
379 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
383 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
384 oldmaxColour = maxColour.getBackground(), min, max);
388 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
390 slider.setEnabled(false);
391 thresholdValue.setEnabled(false);
392 thresholdValue.setText("");
393 thresholdIsMin.setEnabled(false);
395 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
396 && threshline == null)
398 // todo visual indication of feature threshold
399 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
400 "Threshold", Color.black);
403 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
406 acg.setThresh(threshline.value);
408 float range = max * 1000f - min * 1000f;
410 slider.setMinimum((int) (min * 1000));
411 slider.setMaximum((int) (max * 1000));
412 slider.setValue((int) (threshline.value * 1000));
413 thresholdValue.setText(threshline.value + "");
414 slider.setMajorTickSpacing((int) (range / 10f));
415 slider.setEnabled(true);
416 thresholdValue.setEnabled(true);
417 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
421 acg.setThreshType(aboveThreshold);
422 if (thresholdIsMin.isSelected()
423 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
425 acg.setAutoScaled(false);
426 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
428 acg = new GraduatedColor(acg, threshline.value, max);
432 acg = new GraduatedColor(acg, min, threshline.value);
437 acg.setAutoScaled(true);
439 acg.setColourByLabel(colourByLabel.isSelected());
440 if (acg.isColourByLabel())
442 maxColour.setEnabled(false);
443 minColour.setEnabled(false);
444 maxColour.setBackground(this.getBackground());
445 maxColour.setForeground(this.getBackground());
446 minColour.setBackground(this.getBackground());
447 minColour.setForeground(this.getBackground());
452 maxColour.setEnabled(true);
453 minColour.setEnabled(true);
454 maxColour.setBackground(oldmaxColour);
455 minColour.setBackground(oldminColour);
456 maxColour.setForeground(oldmaxColour);
457 minColour.setForeground(oldminColour);
459 fr.featureColours.put(type, acg);
461 ap.paintAlignment(false);
464 protected void raiseClosed()
466 if (this.colourEditor != null)
468 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
472 public void okPressed()
477 public void cancelPressed()
484 fr.featureColours.put(type, oldcs);
485 ap.paintAlignment(false);
489 public void thresholdCheck_actionPerformed(ActionEvent e)
494 public void annotations_actionPerformed(ActionEvent e)
499 public void threshold_actionPerformed(ActionEvent e)
504 public void thresholdValue_actionPerformed(ActionEvent e)
508 float f = Float.parseFloat(thresholdValue.getText());
509 slider.setValue((int) (f * 1000));
510 threshline.value = f;
511 } catch (NumberFormatException ex)
516 public void valueChanged()
518 threshline.value = (float) slider.getValue() / 1000f;
519 cs.setThresh(threshline.value);
521 ap.paintAlignment(false);
524 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
529 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
534 ActionListener colourEditor = null;
536 public void addActionListener(ActionListener graduatedColorEditor)
538 if (colourEditor != null)
541 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
543 colourEditor = graduatedColorEditor;