2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 "
80 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
81 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
84 slider.addChangeListener(new ChangeListener()
86 public void stateChanged(ChangeEvent evt)
90 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
95 slider.addMouseListener(new MouseAdapter()
97 public void mouseReleased(MouseEvent evt)
101 ap.paintAlignment(true);
107 float mm[] = ((float[][]) fr.minmax.get(type))[0];
110 oldcs = fr.featureColours.get(type);
111 if (oldcs instanceof GraduatedColor)
113 if (((GraduatedColor) oldcs).isAutoScale())
116 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
120 cs = new GraduatedColor((GraduatedColor) oldcs);
125 // promote original color to a graduated color
126 Color bl = Color.black;
127 if (oldcs instanceof Color)
131 // original colour becomes the maximum colour
132 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
133 cs.setColourByLabel(false);
135 minColour.setBackground(oldminColour = cs.getMinColor());
136 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
142 } catch (Exception ex)
145 // update the gui from threshold state
146 thresholdIsMin.setSelected(!cs.isAutoScale());
147 colourByLabel.setSelected(cs.isColourByLabel());
148 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
150 // initialise threshold slider and selector
152 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
154 slider.setEnabled(true);
155 thresholdValue.setEnabled(true);
156 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
157 "Threshold", Color.black);
167 public FeatureColourChooser()
172 } catch (Exception ex)
174 ex.printStackTrace();
178 private void jbInit() throws Exception
181 minColour.setFont(JvSwingUtils.getLabelFont());
182 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
183 minColour.setPreferredSize(new Dimension(40, 20));
184 minColour.setToolTipText("Minimum Colour");
185 minColour.addMouseListener(new MouseAdapter()
187 public void mousePressed(MouseEvent e)
189 if (minColour.isEnabled())
191 minColour_actionPerformed();
195 maxColour.setFont(JvSwingUtils.getLabelFont());
196 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
197 maxColour.setPreferredSize(new Dimension(40, 20));
198 maxColour.setToolTipText("Maximum Colour");
199 maxColour.addMouseListener(new MouseAdapter()
201 public void mousePressed(MouseEvent e)
203 if (maxColour.isEnabled())
205 maxColour_actionPerformed();
209 maxColour.setBorder(new LineBorder(Color.black));
210 minText.setText("Min:");
211 minText.setFont(JvSwingUtils.getLabelFont());
212 maxText.setText("Max:");
213 maxText.setFont(JvSwingUtils.getLabelFont());
214 this.setLayout(borderLayout1);
215 jPanel2.setLayout(flowLayout1);
216 jPanel1.setBackground(Color.white);
217 jPanel2.setBackground(Color.white);
218 threshold.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 threshold_actionPerformed(e);
225 threshold.setToolTipText("Threshold the feature display by score.");
226 threshold.addItem("No Threshold"); // index 0
227 threshold.addItem("Above Threshold"); // index 1
228 threshold.addItem("Below Threshold"); // index 2
229 jPanel3.setLayout(flowLayout2);
230 thresholdValue.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
234 thresholdValue_actionPerformed(e);
237 slider.setPaintLabels(false);
238 slider.setPaintTicks(true);
239 slider.setBackground(Color.white);
240 slider.setEnabled(false);
241 slider.setOpaque(false);
242 slider.setPreferredSize(new Dimension(100, 32));
243 slider.setToolTipText("Adjust threshold");
244 thresholdValue.setEnabled(false);
245 thresholdValue.setColumns(7);
246 jPanel3.setBackground(Color.white);
247 thresholdIsMin.setBackground(Color.white);
248 thresholdIsMin.setText("Threshold is Min/Max");
250 .setToolTipText("Toggle between absolute and relative display threshold.");
251 thresholdIsMin.addActionListener(new ActionListener()
253 public void actionPerformed(ActionEvent actionEvent)
255 thresholdIsMin_actionPerformed(actionEvent);
258 colourByLabel.setBackground(Color.white);
259 colourByLabel.setText("Colour by Label");
261 .setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
262 colourByLabel.addActionListener(new ActionListener()
264 public void actionPerformed(ActionEvent actionEvent)
266 colourByLabel_actionPerformed(actionEvent);
269 colourPanel.setBackground(Color.white);
272 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
273 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
274 colourPanel.add(minText);
275 colourPanel.add(minColour);
276 colourPanel.add(maxText);
277 colourPanel.add(maxColour);
278 this.add(jPanel3, java.awt.BorderLayout.CENTER);
279 jPanel3.add(threshold);
281 jPanel3.add(thresholdValue);
282 jPanel3.add(thresholdIsMin);
283 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
284 this.add(jPanel2, java.awt.BorderLayout.NORTH);
287 JLabel minText = new JLabel();
289 JLabel maxText = new JLabel();
291 JPanel minColour = new JPanel();
293 JPanel maxColour = new JPanel();
295 JPanel colourPanel = new JPanel();
297 JPanel jPanel1 = new JPanel();
299 JPanel jPanel2 = new JPanel();
301 BorderLayout borderLayout1 = new BorderLayout();
303 JComboBox threshold = new JComboBox();
305 FlowLayout flowLayout1 = new FlowLayout();
307 JPanel jPanel3 = new JPanel();
309 FlowLayout flowLayout2 = new FlowLayout();
311 JSlider slider = new JSlider();
313 JTextField thresholdValue = new JTextField(20);
315 // TODO implement GUI for tolower flag
316 // JCheckBox toLower = new JCheckBox();
318 JCheckBox thresholdIsMin = new JCheckBox();
320 JCheckBox colourByLabel = new JCheckBox();
322 private GraphLine threshline;
324 private Color oldmaxColour;
326 private Color oldminColour;
328 public void minColour_actionPerformed()
330 Color col = JColorChooser.showDialog(this,
331 "Select Colour for Minimum Value", minColour.getBackground());
334 minColour.setBackground(col);
335 minColour.setForeground(col);
341 public void maxColour_actionPerformed()
343 Color col = JColorChooser.showDialog(this,
344 "Select Colour for Maximum Value", maxColour.getBackground());
347 maxColour.setBackground(col);
348 maxColour.setForeground(col);
356 // Check if combobox is still adjusting
362 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
363 if (threshold.getSelectedItem().equals("Above Threshold"))
365 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
367 else if (threshold.getSelectedItem().equals("Below Threshold"))
369 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
372 slider.setEnabled(true);
373 thresholdValue.setEnabled(true);
376 if (cs.isColourByLabel())
378 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
382 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
383 oldmaxColour = maxColour.getBackground(), min, max);
387 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
389 slider.setEnabled(false);
390 thresholdValue.setEnabled(false);
391 thresholdValue.setText("");
392 thresholdIsMin.setEnabled(false);
394 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
395 && threshline == null)
397 // todo visual indication of feature threshold
398 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
399 "Threshold", Color.black);
402 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
405 acg.setThresh(threshline.value);
407 float range = max * 1000f - min * 1000f;
409 slider.setMinimum((int) (min * 1000));
410 slider.setMaximum((int) (max * 1000));
411 slider.setValue((int) (threshline.value * 1000));
412 thresholdValue.setText(threshline.value + "");
413 slider.setMajorTickSpacing((int) (range / 10f));
414 slider.setEnabled(true);
415 thresholdValue.setEnabled(true);
416 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
420 acg.setThreshType(aboveThreshold);
421 if (thresholdIsMin.isSelected()
422 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
424 acg.setAutoScaled(false);
425 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
427 acg = new GraduatedColor(acg, threshline.value, max);
431 acg = new GraduatedColor(acg, min, threshline.value);
436 acg.setAutoScaled(true);
438 acg.setColourByLabel(colourByLabel.isSelected());
439 if (acg.isColourByLabel())
441 maxColour.setEnabled(false);
442 minColour.setEnabled(false);
443 maxColour.setBackground(this.getBackground());
444 maxColour.setForeground(this.getBackground());
445 minColour.setBackground(this.getBackground());
446 minColour.setForeground(this.getBackground());
451 maxColour.setEnabled(true);
452 minColour.setEnabled(true);
453 maxColour.setBackground(oldmaxColour);
454 minColour.setBackground(oldminColour);
455 maxColour.setForeground(oldmaxColour);
456 minColour.setForeground(oldminColour);
458 fr.featureColours.put(type, acg);
460 ap.paintAlignment(false);
463 protected void raiseClosed()
465 if (this.colourEditor != null)
467 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
471 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;