2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
23 import java.awt.event.*;
25 import javax.swing.border.LineBorder;
26 import javax.swing.event.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import java.awt.Dimension;
32 public class FeatureColourChooser extends JPanel
36 // FeatureSettings fs;
39 private GraduatedColor cs;
45 * @return the last colour setting selected by user - either oldcs (which may
46 * be a java.awt.Color) or the new GraduatedColor
48 public Object getLastColour()
57 Hashtable oldgroupColours;
61 boolean adjusting = false;
69 public FeatureColourChooser(FeatureRenderer frender, String type)
71 this(frender, false, type);
74 public FeatureColourChooser(FeatureRenderer frender, boolean block,
80 frame = new JDialog(Desktop.instance, true);
81 frame.setTitle("Graduated Feature Colour for " + type);
82 Rectangle deskr = Desktop.instance.getBounds();
83 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
84 (int) (deskr.getCenterY() - 92), 480, 185));
85 frame.setContentPane(this);
86 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
87 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
90 slider.addChangeListener(new ChangeListener()
92 public void stateChanged(ChangeEvent evt)
96 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
101 slider.addMouseListener(new MouseAdapter()
103 public void mouseReleased(MouseEvent evt)
107 ap.paintAlignment(true);
113 float mm[] = ((float[][]) fr.minmax.get(type))[0];
116 oldcs = fr.featureColours.get(type);
117 if (oldcs instanceof GraduatedColor)
119 if (((GraduatedColor) oldcs).isAutoScale())
122 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
126 cs = new GraduatedColor((GraduatedColor) oldcs);
131 // promote original color to a graduated color
132 Color bl = Color.black;
133 if (oldcs instanceof Color)
137 // original colour becomes the maximum colour
138 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
139 cs.setColourByLabel(false);
141 minColour.setBackground(oldminColour = cs.getMinColor());
142 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
148 } catch (Exception ex)
151 // update the gui from threshold state
152 thresholdIsMin.setSelected(!cs.isAutoScale());
153 colourByLabel.setSelected(cs.isColourByLabel());
154 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
156 // initialise threshold slider and selector
158 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
160 slider.setEnabled(true);
161 thresholdValue.setEnabled(true);
162 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
163 "Threshold", Color.black);
172 new Thread(new Runnable()
188 public FeatureColourChooser()
193 } catch (Exception ex)
195 ex.printStackTrace();
199 private void jbInit() throws Exception
202 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
203 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
204 minColour.setPreferredSize(new Dimension(40, 20));
205 minColour.setToolTipText("Minimum Colour");
206 minColour.addMouseListener(new MouseAdapter()
208 public void mousePressed(MouseEvent e)
210 if (minColour.isEnabled())
212 minColour_actionPerformed();
216 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
217 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
218 maxColour.setPreferredSize(new Dimension(40, 20));
219 maxColour.setToolTipText("Maximum Colour");
220 maxColour.addMouseListener(new MouseAdapter()
222 public void mousePressed(MouseEvent e)
224 if (maxColour.isEnabled())
226 maxColour_actionPerformed();
230 maxColour.setBorder(new LineBorder(Color.black));
231 minText.setText("Min:");
232 minText.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
233 maxText.setText("Max:");
234 maxText.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
237 ok.addActionListener(new ActionListener()
239 public void actionPerformed(ActionEvent e)
241 ok_actionPerformed(e);
244 cancel.setOpaque(false);
245 cancel.setText("Cancel");
246 cancel.addActionListener(new ActionListener()
248 public void actionPerformed(ActionEvent e)
250 cancel_actionPerformed(e);
253 this.setLayout(borderLayout1);
254 jPanel2.setLayout(flowLayout1);
255 jPanel1.setBackground(Color.white);
256 jPanel2.setBackground(Color.white);
257 threshold.addActionListener(new ActionListener()
259 public void actionPerformed(ActionEvent e)
261 threshold_actionPerformed(e);
264 threshold.setToolTipText("Threshold the feature display by score.");
265 threshold.addItem("No Threshold"); // index 0
266 threshold.addItem("Above Threshold"); // index 1
267 threshold.addItem("Below Threshold"); // index 2
268 jPanel3.setLayout(flowLayout2);
269 thresholdValue.addActionListener(new ActionListener()
271 public void actionPerformed(ActionEvent e)
273 thresholdValue_actionPerformed(e);
276 slider.setPaintLabels(false);
277 slider.setPaintTicks(true);
278 slider.setBackground(Color.white);
279 slider.setEnabled(false);
280 slider.setOpaque(false);
281 slider.setPreferredSize(new Dimension(100, 32));
282 slider.setToolTipText("Adjust threshold");
283 thresholdValue.setEnabled(false);
284 thresholdValue.setColumns(7);
285 jPanel3.setBackground(Color.white);
286 thresholdIsMin.setBackground(Color.white);
287 thresholdIsMin.setText("Threshold is Min/Max");
289 .setToolTipText("Toggle between absolute and relative display threshold.");
290 thresholdIsMin.addActionListener(new ActionListener()
292 public void actionPerformed(ActionEvent actionEvent)
294 thresholdIsMin_actionPerformed(actionEvent);
297 colourByLabel.setBackground(Color.white);
298 colourByLabel.setText("Colour by Label");
300 .setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
301 colourByLabel.addActionListener(new ActionListener()
303 public void actionPerformed(ActionEvent actionEvent)
305 colourByLabel_actionPerformed(actionEvent);
308 colourPanel.setBackground(Color.white);
311 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
312 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
313 colourPanel.add(minText);
314 colourPanel.add(minColour);
315 colourPanel.add(maxText);
316 colourPanel.add(maxColour);
317 this.add(jPanel3, java.awt.BorderLayout.CENTER);
318 jPanel3.add(threshold);
320 jPanel3.add(thresholdValue);
321 jPanel3.add(thresholdIsMin);
322 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
323 this.add(jPanel2, java.awt.BorderLayout.NORTH);
326 JLabel minText = new JLabel();
328 JLabel maxText = new JLabel();
330 JPanel minColour = new JPanel();
332 JPanel maxColour = new JPanel();
334 JButton ok = new JButton();
336 JButton cancel = new JButton();
338 JPanel colourPanel = new JPanel();
340 JPanel jPanel1 = new JPanel();
342 JPanel jPanel2 = new JPanel();
344 BorderLayout borderLayout1 = new BorderLayout();
346 JComboBox threshold = new JComboBox();
348 FlowLayout flowLayout1 = new FlowLayout();
350 JPanel jPanel3 = new JPanel();
352 FlowLayout flowLayout2 = new FlowLayout();
354 JSlider slider = new JSlider();
356 JTextField thresholdValue = new JTextField(20);
358 // TODO implement GUI for tolower flag
359 // JCheckBox toLower = new JCheckBox();
361 JCheckBox thresholdIsMin = new JCheckBox();
363 JCheckBox colourByLabel = new JCheckBox();
365 private GraphLine threshline;
367 private Color oldmaxColour;
369 private Color oldminColour;
371 public void minColour_actionPerformed()
373 Color col = JColorChooser.showDialog(this,
374 "Select Colour for Minimum Value", minColour.getBackground());
377 minColour.setBackground(col);
378 minColour.setForeground(col);
384 public void maxColour_actionPerformed()
386 Color col = JColorChooser.showDialog(this,
387 "Select Colour for Maximum Value", maxColour.getBackground());
390 maxColour.setBackground(col);
391 maxColour.setForeground(col);
399 // Check if combobox is still adjusting
405 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
406 if (threshold.getSelectedItem().equals("Above Threshold"))
408 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
410 else if (threshold.getSelectedItem().equals("Below Threshold"))
412 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
415 slider.setEnabled(true);
416 thresholdValue.setEnabled(true);
419 if (cs.isColourByLabel())
421 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
425 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
426 oldmaxColour = maxColour.getBackground(), min, max);
430 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
432 slider.setEnabled(false);
433 thresholdValue.setEnabled(false);
434 thresholdValue.setText("");
435 thresholdIsMin.setEnabled(false);
437 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
438 && threshline == null)
440 // todo visual indication of feature threshold
441 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
442 "Threshold", Color.black);
445 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
448 acg.setThresh(threshline.value);
450 float range = max * 1000f - min * 1000f;
452 slider.setMinimum((int) (min * 1000));
453 slider.setMaximum((int) (max * 1000));
454 slider.setValue((int) (threshline.value * 1000));
455 thresholdValue.setText(threshline.value + "");
456 slider.setMajorTickSpacing((int) (range / 10f));
457 slider.setEnabled(true);
458 thresholdValue.setEnabled(true);
459 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
463 acg.setThreshType(aboveThreshold);
464 if (thresholdIsMin.isSelected()
465 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
467 acg.setAutoScaled(false);
468 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
470 acg = new GraduatedColor(acg, threshline.value, max);
474 acg = new GraduatedColor(acg, min, threshline.value);
479 acg.setAutoScaled(true);
481 acg.setColourByLabel(colourByLabel.isSelected());
482 if (acg.isColourByLabel())
484 maxColour.setEnabled(false);
485 minColour.setEnabled(false);
486 maxColour.setBackground(this.getBackground());
487 maxColour.setForeground(this.getBackground());
488 minColour.setBackground(this.getBackground());
489 minColour.setForeground(this.getBackground());
494 maxColour.setEnabled(true);
495 minColour.setEnabled(true);
496 maxColour.setBackground(oldmaxColour);
497 minColour.setBackground(oldminColour);
498 maxColour.setForeground(oldmaxColour);
499 minColour.setForeground(oldminColour);
501 fr.featureColours.put(type, acg);
503 ap.paintAlignment(false);
506 private void raiseClosed()
508 if (this.colourEditor != null)
510 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
514 public void ok_actionPerformed(ActionEvent e)
521 } catch (Exception ex)
526 public void cancel_actionPerformed(ActionEvent e)
532 // frame.setClosed(true);
534 } catch (Exception ex)
541 fr.featureColours.put(type, oldcs);
542 ap.paintAlignment(false);
546 public void thresholdCheck_actionPerformed(ActionEvent e)
551 public void annotations_actionPerformed(ActionEvent e)
556 public void threshold_actionPerformed(ActionEvent e)
561 public void thresholdValue_actionPerformed(ActionEvent e)
565 float f = Float.parseFloat(thresholdValue.getText());
566 slider.setValue((int) (f * 1000));
567 threshline.value = f;
568 } catch (NumberFormatException ex)
573 public void valueChanged()
575 threshline.value = (float) slider.getValue() / 1000f;
576 cs.setThresh(threshline.value);
578 ap.paintAlignment(false);
581 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
586 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
591 ActionListener colourEditor = null;
593 public void addActionListener(ActionListener graduatedColorEditor)
595 if (colourEditor != null)
598 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
600 colourEditor = graduatedColorEditor;