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 jalview.util.MessageManager;
32 import java.awt.Dimension;
34 public class FeatureColourChooser extends JalviewDialog
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 initDialogFrame(this, true, block, "Graduated Feature Colour for "
82 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
83 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
86 slider.addChangeListener(new ChangeListener()
88 public void stateChanged(ChangeEvent evt)
92 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
97 slider.addMouseListener(new MouseAdapter()
99 public void mouseReleased(MouseEvent evt)
103 ap.paintAlignment(true);
109 float mm[] = ((float[][]) fr.minmax.get(type))[0];
112 oldcs = fr.featureColours.get(type);
113 if (oldcs instanceof GraduatedColor)
115 if (((GraduatedColor) oldcs).isAutoScale())
118 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
122 cs = new GraduatedColor((GraduatedColor) oldcs);
127 // promote original color to a graduated color
128 Color bl = Color.black;
129 if (oldcs instanceof Color)
133 // original colour becomes the maximum colour
134 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
135 cs.setColourByLabel(false);
137 minColour.setBackground(oldminColour = cs.getMinColor());
138 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
144 } catch (Exception ex)
147 // update the gui from threshold state
148 thresholdIsMin.setSelected(!cs.isAutoScale());
149 colourByLabel.setSelected(cs.isColourByLabel());
150 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
152 // initialise threshold slider and selector
154 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
156 slider.setEnabled(true);
157 thresholdValue.setEnabled(true);
158 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
159 "Threshold", Color.black);
169 public FeatureColourChooser()
174 } catch (Exception ex)
176 ex.printStackTrace();
180 private void jbInit() throws Exception
183 minColour.setFont(JvSwingUtils.getLabelFont());
184 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
185 minColour.setPreferredSize(new Dimension(40, 20));
186 minColour.setToolTipText("Minimum Colour");
187 minColour.addMouseListener(new MouseAdapter()
189 public void mousePressed(MouseEvent e)
191 if (minColour.isEnabled())
193 minColour_actionPerformed();
197 maxColour.setFont(JvSwingUtils.getLabelFont());
198 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
199 maxColour.setPreferredSize(new Dimension(40, 20));
200 maxColour.setToolTipText("Maximum Colour");
201 maxColour.addMouseListener(new MouseAdapter()
203 public void mousePressed(MouseEvent e)
205 if (maxColour.isEnabled())
207 maxColour_actionPerformed();
211 maxColour.setBorder(new LineBorder(Color.black));
212 minText.setText(MessageManager.getString("label.min"));
213 minText.setFont(JvSwingUtils.getLabelFont());
214 maxText.setText(MessageManager.getString("label.max"));
215 maxText.setFont(JvSwingUtils.getLabelFont());
216 this.setLayout(borderLayout1);
217 jPanel2.setLayout(flowLayout1);
218 jPanel1.setBackground(Color.white);
219 jPanel2.setBackground(Color.white);
220 threshold.addActionListener(new ActionListener()
222 public void actionPerformed(ActionEvent e)
224 threshold_actionPerformed(e);
227 threshold.setToolTipText("Threshold the feature display by score.");
228 threshold.addItem("No Threshold"); // index 0
229 threshold.addItem("Above Threshold"); // index 1
230 threshold.addItem("Below Threshold"); // index 2
231 jPanel3.setLayout(flowLayout2);
232 thresholdValue.addActionListener(new ActionListener()
234 public void actionPerformed(ActionEvent e)
236 thresholdValue_actionPerformed(e);
239 slider.setPaintLabels(false);
240 slider.setPaintTicks(true);
241 slider.setBackground(Color.white);
242 slider.setEnabled(false);
243 slider.setOpaque(false);
244 slider.setPreferredSize(new Dimension(100, 32));
245 slider.setToolTipText("Adjust threshold");
246 thresholdValue.setEnabled(false);
247 thresholdValue.setColumns(7);
248 jPanel3.setBackground(Color.white);
249 thresholdIsMin.setBackground(Color.white);
250 thresholdIsMin.setText(MessageManager.getString("label.threshold_minmax"));
252 .setToolTipText("Toggle between absolute and relative display threshold.");
253 thresholdIsMin.addActionListener(new ActionListener()
255 public void actionPerformed(ActionEvent actionEvent)
257 thresholdIsMin_actionPerformed(actionEvent);
260 colourByLabel.setBackground(Color.white);
261 colourByLabel.setText(MessageManager.getString("label.colour_by_label"));
263 .setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
264 colourByLabel.addActionListener(new ActionListener()
266 public void actionPerformed(ActionEvent actionEvent)
268 colourByLabel_actionPerformed(actionEvent);
271 colourPanel.setBackground(Color.white);
274 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
275 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
276 colourPanel.add(minText);
277 colourPanel.add(minColour);
278 colourPanel.add(maxText);
279 colourPanel.add(maxColour);
280 this.add(jPanel3, java.awt.BorderLayout.CENTER);
281 jPanel3.add(threshold);
283 jPanel3.add(thresholdValue);
284 jPanel3.add(thresholdIsMin);
285 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
286 this.add(jPanel2, java.awt.BorderLayout.NORTH);
289 JLabel minText = new JLabel();
291 JLabel maxText = new JLabel();
293 JPanel minColour = new JPanel();
295 JPanel maxColour = new JPanel();
297 JPanel colourPanel = new JPanel();
299 JPanel jPanel1 = new JPanel();
301 JPanel jPanel2 = new JPanel();
303 BorderLayout borderLayout1 = new BorderLayout();
305 JComboBox threshold = new JComboBox();
307 FlowLayout flowLayout1 = new FlowLayout();
309 JPanel jPanel3 = new JPanel();
311 FlowLayout flowLayout2 = new FlowLayout();
313 JSlider slider = new JSlider();
315 JTextField thresholdValue = new JTextField(20);
317 // TODO implement GUI for tolower flag
318 // JCheckBox toLower = new JCheckBox();
320 JCheckBox thresholdIsMin = new JCheckBox();
322 JCheckBox colourByLabel = new JCheckBox();
324 private GraphLine threshline;
326 private Color oldmaxColour;
328 private Color oldminColour;
330 public void minColour_actionPerformed()
332 Color col = JColorChooser.showDialog(this,
333 "Select Colour for Minimum Value", minColour.getBackground());
336 minColour.setBackground(col);
337 minColour.setForeground(col);
343 public void maxColour_actionPerformed()
345 Color col = JColorChooser.showDialog(this,
346 "Select Colour for Maximum Value", maxColour.getBackground());
349 maxColour.setBackground(col);
350 maxColour.setForeground(col);
358 // Check if combobox is still adjusting
364 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
365 if (threshold.getSelectedItem().equals("Above Threshold"))
367 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
369 else if (threshold.getSelectedItem().equals("Below Threshold"))
371 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
374 slider.setEnabled(true);
375 thresholdValue.setEnabled(true);
378 if (cs.isColourByLabel())
380 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
384 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
385 oldmaxColour = maxColour.getBackground(), min, max);
389 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
391 slider.setEnabled(false);
392 thresholdValue.setEnabled(false);
393 thresholdValue.setText("");
394 thresholdIsMin.setEnabled(false);
396 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
397 && threshline == null)
399 // todo visual indication of feature threshold
400 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
401 "Threshold", Color.black);
404 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
407 acg.setThresh(threshline.value);
409 float range = max * 1000f - min * 1000f;
411 slider.setMinimum((int) (min * 1000));
412 slider.setMaximum((int) (max * 1000));
413 slider.setValue((int) (threshline.value * 1000));
414 thresholdValue.setText(threshline.value + "");
415 slider.setMajorTickSpacing((int) (range / 10f));
416 slider.setEnabled(true);
417 thresholdValue.setEnabled(true);
418 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
422 acg.setThreshType(aboveThreshold);
423 if (thresholdIsMin.isSelected()
424 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
426 acg.setAutoScaled(false);
427 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
429 acg = new GraduatedColor(acg, threshline.value, max);
433 acg = new GraduatedColor(acg, min, threshline.value);
438 acg.setAutoScaled(true);
440 acg.setColourByLabel(colourByLabel.isSelected());
441 if (acg.isColourByLabel())
443 maxColour.setEnabled(false);
444 minColour.setEnabled(false);
445 maxColour.setBackground(this.getBackground());
446 maxColour.setForeground(this.getBackground());
447 minColour.setBackground(this.getBackground());
448 minColour.setForeground(this.getBackground());
453 maxColour.setEnabled(true);
454 minColour.setEnabled(true);
455 maxColour.setBackground(oldmaxColour);
456 minColour.setBackground(oldminColour);
457 maxColour.setForeground(oldmaxColour);
458 minColour.setForeground(oldminColour);
460 fr.featureColours.put(type, acg);
462 ap.paintAlignment(false);
465 protected void raiseClosed()
467 if (this.colourEditor != null)
469 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
473 public void okPressed()
478 public void cancelPressed()
485 fr.featureColours.put(type, oldcs);
486 ap.paintAlignment(false);
490 public void thresholdCheck_actionPerformed(ActionEvent e)
495 public void annotations_actionPerformed(ActionEvent e)
500 public void threshold_actionPerformed(ActionEvent e)
505 public void thresholdValue_actionPerformed(ActionEvent e)
509 float f = Float.parseFloat(thresholdValue.getText());
510 slider.setValue((int) (f * 1000));
511 threshline.value = f;
512 } catch (NumberFormatException ex)
517 public void valueChanged()
519 threshline.value = (float) slider.getValue() / 1000f;
520 cs.setThresh(threshline.value);
522 ap.paintAlignment(false);
525 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
530 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
535 ActionListener colourEditor = null;
537 public void addActionListener(ActionListener graduatedColorEditor)
539 if (colourEditor != null)
542 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
544 colourEditor = graduatedColorEditor;