2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 import java.awt.event.*;
28 import javax.swing.border.LineBorder;
29 import javax.swing.event.*;
31 import jalview.datamodel.*;
32 import jalview.schemes.*;
33 import jalview.util.MessageManager;
35 import java.awt.Dimension;
37 public class FeatureColourChooser extends JalviewDialog
39 // FeatureSettings fs;
42 private GraduatedColor cs;
48 * @return the last colour setting selected by user - either oldcs (which may
49 * be a java.awt.Color) or the new GraduatedColor
51 public Object getLastColour()
60 Hashtable oldgroupColours;
64 boolean adjusting = false;
72 public FeatureColourChooser(FeatureRenderer frender, String type)
74 this(frender, false, type);
77 public FeatureColourChooser(FeatureRenderer frender, boolean block,
83 initDialogFrame(this, true, block, "Graduated Feature Colour for "
85 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
86 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
89 slider.addChangeListener(new ChangeListener()
91 public void stateChanged(ChangeEvent evt)
95 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
100 slider.addMouseListener(new MouseAdapter()
102 public void mouseReleased(MouseEvent evt)
106 ap.paintAlignment(true);
112 float mm[] = ((float[][]) fr.minmax.get(type))[0];
115 oldcs = fr.featureColours.get(type);
116 if (oldcs instanceof GraduatedColor)
118 if (((GraduatedColor) oldcs).isAutoScale())
121 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
125 cs = new GraduatedColor((GraduatedColor) oldcs);
130 // promote original color to a graduated color
131 Color bl = Color.black;
132 if (oldcs instanceof Color)
136 // original colour becomes the maximum colour
137 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
138 cs.setColourByLabel(false);
140 minColour.setBackground(oldminColour = cs.getMinColor());
141 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
147 } catch (Exception ex)
150 // update the gui from threshold state
151 thresholdIsMin.setSelected(!cs.isAutoScale());
152 colourByLabel.setSelected(cs.isColourByLabel());
153 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
155 // initialise threshold slider and selector
157 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
159 slider.setEnabled(true);
160 thresholdValue.setEnabled(true);
161 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
162 "Threshold", Color.black);
172 public FeatureColourChooser()
177 } catch (Exception ex)
179 ex.printStackTrace();
183 private void jbInit() throws Exception
186 minColour.setFont(JvSwingUtils.getLabelFont());
187 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
188 minColour.setPreferredSize(new Dimension(40, 20));
189 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
190 minColour.addMouseListener(new MouseAdapter()
192 public void mousePressed(MouseEvent e)
194 if (minColour.isEnabled())
196 minColour_actionPerformed();
200 maxColour.setFont(JvSwingUtils.getLabelFont());
201 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
202 maxColour.setPreferredSize(new Dimension(40, 20));
203 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
204 maxColour.addMouseListener(new MouseAdapter()
206 public void mousePressed(MouseEvent e)
208 if (maxColour.isEnabled())
210 maxColour_actionPerformed();
214 maxColour.setBorder(new LineBorder(Color.black));
215 minText.setText(MessageManager.getString("label.min"));
216 minText.setFont(JvSwingUtils.getLabelFont());
217 maxText.setText(MessageManager.getString("label.max"));
218 maxText.setFont(JvSwingUtils.getLabelFont());
219 this.setLayout(borderLayout1);
220 jPanel2.setLayout(flowLayout1);
221 jPanel1.setBackground(Color.white);
222 jPanel2.setBackground(Color.white);
223 threshold.addActionListener(new ActionListener()
225 public void actionPerformed(ActionEvent e)
227 threshold_actionPerformed(e);
230 threshold.setToolTipText(MessageManager.getString("label.threshold_feature_display_by_score"));
231 threshold.addItem(MessageManager.getString("label.threshold_feature_no_thereshold")); // index 0
232 threshold.addItem(MessageManager.getString("label.threshold_feature_above_thereshold")); // index 1
233 threshold.addItem(MessageManager.getString("label.threshold_feature_below_thereshold")); // index 2
234 jPanel3.setLayout(flowLayout2);
235 thresholdValue.addActionListener(new ActionListener()
237 public void actionPerformed(ActionEvent e)
239 thresholdValue_actionPerformed(e);
242 slider.setPaintLabels(false);
243 slider.setPaintTicks(true);
244 slider.setBackground(Color.white);
245 slider.setEnabled(false);
246 slider.setOpaque(false);
247 slider.setPreferredSize(new Dimension(100, 32));
248 slider.setToolTipText(MessageManager.getString("label.adjust_thereshold"));
249 thresholdValue.setEnabled(false);
250 thresholdValue.setColumns(7);
251 jPanel3.setBackground(Color.white);
252 thresholdIsMin.setBackground(Color.white);
253 thresholdIsMin.setText(MessageManager.getString("label.threshold_minmax"));
255 .setToolTipText(MessageManager.getString("label.toggle_absolute_relative_display_threshold"));
256 thresholdIsMin.addActionListener(new ActionListener()
258 public void actionPerformed(ActionEvent actionEvent)
260 thresholdIsMin_actionPerformed(actionEvent);
263 colourByLabel.setBackground(Color.white);
264 colourByLabel.setText(MessageManager.getString("label.colour_by_label"));
266 .setToolTipText(MessageManager.getString("label.display_features_same_type_different_label_using_different_colour"));
267 colourByLabel.addActionListener(new ActionListener()
269 public void actionPerformed(ActionEvent actionEvent)
271 colourByLabel_actionPerformed(actionEvent);
274 colourPanel.setBackground(Color.white);
277 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
278 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
279 colourPanel.add(minText);
280 colourPanel.add(minColour);
281 colourPanel.add(maxText);
282 colourPanel.add(maxColour);
283 this.add(jPanel3, java.awt.BorderLayout.CENTER);
284 jPanel3.add(threshold);
286 jPanel3.add(thresholdValue);
287 jPanel3.add(thresholdIsMin);
288 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
289 this.add(jPanel2, java.awt.BorderLayout.NORTH);
292 JLabel minText = new JLabel();
294 JLabel maxText = new JLabel();
296 JPanel minColour = new JPanel();
298 JPanel maxColour = new JPanel();
300 JPanel colourPanel = new JPanel();
302 JPanel jPanel1 = new JPanel();
304 JPanel jPanel2 = new JPanel();
306 BorderLayout borderLayout1 = new BorderLayout();
308 JComboBox threshold = new JComboBox();
310 FlowLayout flowLayout1 = new FlowLayout();
312 JPanel jPanel3 = new JPanel();
314 FlowLayout flowLayout2 = new FlowLayout();
316 JSlider slider = new JSlider();
318 JTextField thresholdValue = new JTextField(20);
320 // TODO implement GUI for tolower flag
321 // JCheckBox toLower = new JCheckBox();
323 JCheckBox thresholdIsMin = new JCheckBox();
325 JCheckBox colourByLabel = new JCheckBox();
327 private GraphLine threshline;
329 private Color oldmaxColour;
331 private Color oldminColour;
333 public void minColour_actionPerformed()
335 Color col = JColorChooser.showDialog(this,
336 MessageManager.getString("label.select_colour_minimum_value"), minColour.getBackground());
339 minColour.setBackground(col);
340 minColour.setForeground(col);
346 public void maxColour_actionPerformed()
348 Color col = JColorChooser.showDialog(this,
349 MessageManager.getString("label.select_colour_maximum_value"), maxColour.getBackground());
352 maxColour.setBackground(col);
353 maxColour.setForeground(col);
361 // Check if combobox is still adjusting
367 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
368 if (threshold.getSelectedIndex()==1)
370 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
372 else if (threshold.getSelectedIndex()==2)
374 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
377 slider.setEnabled(true);
378 thresholdValue.setEnabled(true);
381 if (cs.isColourByLabel())
383 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
387 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
388 oldmaxColour = maxColour.getBackground(), min, max);
392 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
394 slider.setEnabled(false);
395 thresholdValue.setEnabled(false);
396 thresholdValue.setText("");
397 thresholdIsMin.setEnabled(false);
399 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
400 && threshline == null)
402 // todo visual indication of feature threshold
403 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
404 "Threshold", Color.black);
407 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
410 acg.setThresh(threshline.value);
412 float range = max * 1000f - min * 1000f;
414 slider.setMinimum((int) (min * 1000));
415 slider.setMaximum((int) (max * 1000));
416 slider.setValue((int) (threshline.value * 1000));
417 thresholdValue.setText(threshline.value + "");
418 slider.setMajorTickSpacing((int) (range / 10f));
419 slider.setEnabled(true);
420 thresholdValue.setEnabled(true);
421 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
425 acg.setThreshType(aboveThreshold);
426 if (thresholdIsMin.isSelected()
427 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
429 acg.setAutoScaled(false);
430 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
432 acg = new GraduatedColor(acg, threshline.value, max);
436 acg = new GraduatedColor(acg, min, threshline.value);
441 acg.setAutoScaled(true);
443 acg.setColourByLabel(colourByLabel.isSelected());
444 if (acg.isColourByLabel())
446 maxColour.setEnabled(false);
447 minColour.setEnabled(false);
448 maxColour.setBackground(this.getBackground());
449 maxColour.setForeground(this.getBackground());
450 minColour.setBackground(this.getBackground());
451 minColour.setForeground(this.getBackground());
456 maxColour.setEnabled(true);
457 minColour.setEnabled(true);
458 maxColour.setBackground(oldmaxColour);
459 minColour.setBackground(oldminColour);
460 maxColour.setForeground(oldmaxColour);
461 minColour.setForeground(oldminColour);
463 fr.featureColours.put(type, acg);
465 ap.paintAlignment(false);
468 protected void raiseClosed()
470 if (this.colourEditor != null)
472 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
476 public void okPressed()
481 public void cancelPressed()
488 fr.featureColours.put(type, oldcs);
489 ap.paintAlignment(false);
493 public void thresholdCheck_actionPerformed(ActionEvent e)
498 public void annotations_actionPerformed(ActionEvent e)
503 public void threshold_actionPerformed(ActionEvent e)
508 public void thresholdValue_actionPerformed(ActionEvent e)
512 float f = Float.parseFloat(thresholdValue.getText());
513 slider.setValue((int) (f * 1000));
514 threshline.value = f;
515 } catch (NumberFormatException ex)
520 public void valueChanged()
522 threshline.value = (float) slider.getValue() / 1000f;
523 cs.setThresh(threshline.value);
525 ap.paintAlignment(false);
528 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
533 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
538 ActionListener colourEditor = null;
540 public void addActionListener(ActionListener graduatedColorEditor)
542 if (colourEditor != null)
545 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
547 colourEditor = graduatedColorEditor;