2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 JalviewDialog
34 // FeatureSettings fs;
37 private GraduatedColor cs;
43 * @return the last colour setting selected by user - either oldcs (which may
44 * be a java.awt.Color) or the new GraduatedColor
46 public Object getLastColour()
55 Hashtable oldgroupColours;
59 boolean adjusting = false;
67 public FeatureColourChooser(FeatureRenderer frender, String type)
69 this(frender, false, type);
72 public FeatureColourChooser(FeatureRenderer frender, boolean block,
78 initDialogFrame(this,true, block,"Graduated Feature Colour for " + type, 480, 185);
79 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
80 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
83 slider.addChangeListener(new ChangeListener()
85 public void stateChanged(ChangeEvent evt)
89 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
94 slider.addMouseListener(new MouseAdapter()
96 public void mouseReleased(MouseEvent evt)
100 ap.paintAlignment(true);
106 float mm[] = ((float[][]) fr.minmax.get(type))[0];
109 oldcs = fr.featureColours.get(type);
110 if (oldcs instanceof GraduatedColor)
112 if (((GraduatedColor) oldcs).isAutoScale())
115 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
119 cs = new GraduatedColor((GraduatedColor) oldcs);
124 // promote original color to a graduated color
125 Color bl = Color.black;
126 if (oldcs instanceof Color)
130 // original colour becomes the maximum colour
131 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
132 cs.setColourByLabel(false);
134 minColour.setBackground(oldminColour = cs.getMinColor());
135 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
141 } catch (Exception ex)
144 // update the gui from threshold state
145 thresholdIsMin.setSelected(!cs.isAutoScale());
146 colourByLabel.setSelected(cs.isColourByLabel());
147 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
149 // initialise threshold slider and selector
151 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
153 slider.setEnabled(true);
154 thresholdValue.setEnabled(true);
155 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
156 "Threshold", Color.black);
166 public FeatureColourChooser()
171 } catch (Exception ex)
173 ex.printStackTrace();
177 private void jbInit() throws Exception
180 minColour.setFont(JvSwingUtils.getLabelFont());
181 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
182 minColour.setPreferredSize(new Dimension(40, 20));
183 minColour.setToolTipText("Minimum Colour");
184 minColour.addMouseListener(new MouseAdapter()
186 public void mousePressed(MouseEvent e)
188 if (minColour.isEnabled())
190 minColour_actionPerformed();
194 maxColour.setFont(JvSwingUtils.getLabelFont());
195 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
196 maxColour.setPreferredSize(new Dimension(40, 20));
197 maxColour.setToolTipText("Maximum Colour");
198 maxColour.addMouseListener(new MouseAdapter()
200 public void mousePressed(MouseEvent e)
202 if (maxColour.isEnabled())
204 maxColour_actionPerformed();
208 maxColour.setBorder(new LineBorder(Color.black));
209 minText.setText("Min:");
210 minText.setFont(JvSwingUtils.getLabelFont());
211 maxText.setText("Max:");
212 maxText.setFont(JvSwingUtils.getLabelFont());
213 this.setLayout(borderLayout1);
214 jPanel2.setLayout(flowLayout1);
215 jPanel1.setBackground(Color.white);
216 jPanel2.setBackground(Color.white);
217 threshold.addActionListener(new ActionListener()
219 public void actionPerformed(ActionEvent e)
221 threshold_actionPerformed(e);
224 threshold.setToolTipText("Threshold the feature display by score.");
225 threshold.addItem("No Threshold"); // index 0
226 threshold.addItem("Above Threshold"); // index 1
227 threshold.addItem("Below Threshold"); // index 2
228 jPanel3.setLayout(flowLayout2);
229 thresholdValue.addActionListener(new ActionListener()
231 public void actionPerformed(ActionEvent e)
233 thresholdValue_actionPerformed(e);
236 slider.setPaintLabels(false);
237 slider.setPaintTicks(true);
238 slider.setBackground(Color.white);
239 slider.setEnabled(false);
240 slider.setOpaque(false);
241 slider.setPreferredSize(new Dimension(100, 32));
242 slider.setToolTipText("Adjust threshold");
243 thresholdValue.setEnabled(false);
244 thresholdValue.setColumns(7);
245 jPanel3.setBackground(Color.white);
246 thresholdIsMin.setBackground(Color.white);
247 thresholdIsMin.setText("Threshold is Min/Max");
249 .setToolTipText("Toggle between absolute and relative display threshold.");
250 thresholdIsMin.addActionListener(new ActionListener()
252 public void actionPerformed(ActionEvent actionEvent)
254 thresholdIsMin_actionPerformed(actionEvent);
257 colourByLabel.setBackground(Color.white);
258 colourByLabel.setText("Colour by Label");
260 .setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
261 colourByLabel.addActionListener(new ActionListener()
263 public void actionPerformed(ActionEvent actionEvent)
265 colourByLabel_actionPerformed(actionEvent);
268 colourPanel.setBackground(Color.white);
271 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
272 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
273 colourPanel.add(minText);
274 colourPanel.add(minColour);
275 colourPanel.add(maxText);
276 colourPanel.add(maxColour);
277 this.add(jPanel3, java.awt.BorderLayout.CENTER);
278 jPanel3.add(threshold);
280 jPanel3.add(thresholdValue);
281 jPanel3.add(thresholdIsMin);
282 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
283 this.add(jPanel2, java.awt.BorderLayout.NORTH);
286 JLabel minText = new JLabel();
288 JLabel maxText = new JLabel();
290 JPanel minColour = new JPanel();
292 JPanel maxColour = new JPanel();
294 JPanel colourPanel = new JPanel();
296 JPanel jPanel1 = new JPanel();
298 JPanel jPanel2 = new JPanel();
300 BorderLayout borderLayout1 = new BorderLayout();
302 JComboBox threshold = new JComboBox();
304 FlowLayout flowLayout1 = new FlowLayout();
306 JPanel jPanel3 = new JPanel();
308 FlowLayout flowLayout2 = new FlowLayout();
310 JSlider slider = new JSlider();
312 JTextField thresholdValue = new JTextField(20);
314 // TODO implement GUI for tolower flag
315 // JCheckBox toLower = new JCheckBox();
317 JCheckBox thresholdIsMin = new JCheckBox();
319 JCheckBox colourByLabel = new JCheckBox();
321 private GraphLine threshline;
323 private Color oldmaxColour;
325 private Color oldminColour;
327 public void minColour_actionPerformed()
329 Color col = JColorChooser.showDialog(this,
330 "Select Colour for Minimum Value", minColour.getBackground());
333 minColour.setBackground(col);
334 minColour.setForeground(col);
340 public void maxColour_actionPerformed()
342 Color col = JColorChooser.showDialog(this,
343 "Select Colour for Maximum Value", maxColour.getBackground());
346 maxColour.setBackground(col);
347 maxColour.setForeground(col);
355 // Check if combobox is still adjusting
361 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
362 if (threshold.getSelectedItem().equals("Above Threshold"))
364 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
366 else if (threshold.getSelectedItem().equals("Below Threshold"))
368 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
371 slider.setEnabled(true);
372 thresholdValue.setEnabled(true);
375 if (cs.isColourByLabel())
377 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
381 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
382 oldmaxColour = maxColour.getBackground(), min, max);
386 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
388 slider.setEnabled(false);
389 thresholdValue.setEnabled(false);
390 thresholdValue.setText("");
391 thresholdIsMin.setEnabled(false);
393 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
394 && threshline == null)
396 // todo visual indication of feature threshold
397 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
398 "Threshold", Color.black);
401 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
404 acg.setThresh(threshline.value);
406 float range = max * 1000f - min * 1000f;
408 slider.setMinimum((int) (min * 1000));
409 slider.setMaximum((int) (max * 1000));
410 slider.setValue((int) (threshline.value * 1000));
411 thresholdValue.setText(threshline.value + "");
412 slider.setMajorTickSpacing((int) (range / 10f));
413 slider.setEnabled(true);
414 thresholdValue.setEnabled(true);
415 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
419 acg.setThreshType(aboveThreshold);
420 if (thresholdIsMin.isSelected()
421 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
423 acg.setAutoScaled(false);
424 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
426 acg = new GraduatedColor(acg, threshline.value, max);
430 acg = new GraduatedColor(acg, min, threshline.value);
435 acg.setAutoScaled(true);
437 acg.setColourByLabel(colourByLabel.isSelected());
438 if (acg.isColourByLabel())
440 maxColour.setEnabled(false);
441 minColour.setEnabled(false);
442 maxColour.setBackground(this.getBackground());
443 maxColour.setForeground(this.getBackground());
444 minColour.setBackground(this.getBackground());
445 minColour.setForeground(this.getBackground());
450 maxColour.setEnabled(true);
451 minColour.setEnabled(true);
452 maxColour.setBackground(oldmaxColour);
453 minColour.setBackground(oldminColour);
454 maxColour.setForeground(oldmaxColour);
455 minColour.setForeground(oldminColour);
457 fr.featureColours.put(type, acg);
459 ap.paintAlignment(false);
462 protected void raiseClosed()
464 if (this.colourEditor != null)
466 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
470 public void okPressed()
476 public void cancelPressed()
483 fr.featureColours.put(type, oldcs);
484 ap.paintAlignment(false);
488 public void thresholdCheck_actionPerformed(ActionEvent e)
493 public void annotations_actionPerformed(ActionEvent e)
498 public void threshold_actionPerformed(ActionEvent e)
503 public void thresholdValue_actionPerformed(ActionEvent e)
507 float f = Float.parseFloat(thresholdValue.getText());
508 slider.setValue((int) (f * 1000));
509 threshline.value = f;
510 } catch (NumberFormatException ex)
515 public void valueChanged()
517 threshline.value = (float) slider.getValue() / 1000f;
518 cs.setThresh(threshline.value);
520 ap.paintAlignment(false);
523 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
528 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
533 ActionListener colourEditor = null;
535 public void addActionListener(ActionListener graduatedColorEditor)
537 if (colourEditor != null)
540 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
542 colourEditor = graduatedColorEditor;