2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.getMinMax().get(type))[0];
115 oldcs = fr.getFeatureColours().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
231 .getString("label.threshold_feature_display_by_score"));
232 threshold.addItem(MessageManager
233 .getString("label.threshold_feature_no_thereshold")); // index 0
234 threshold.addItem(MessageManager
235 .getString("label.threshold_feature_above_thereshold")); // index 1
236 threshold.addItem(MessageManager
237 .getString("label.threshold_feature_below_thereshold")); // index 2
238 jPanel3.setLayout(flowLayout2);
239 thresholdValue.addActionListener(new ActionListener()
241 public void actionPerformed(ActionEvent e)
243 thresholdValue_actionPerformed(e);
246 slider.setPaintLabels(false);
247 slider.setPaintTicks(true);
248 slider.setBackground(Color.white);
249 slider.setEnabled(false);
250 slider.setOpaque(false);
251 slider.setPreferredSize(new Dimension(100, 32));
252 slider.setToolTipText(MessageManager
253 .getString("label.adjust_thereshold"));
254 thresholdValue.setEnabled(false);
255 thresholdValue.setColumns(7);
256 jPanel3.setBackground(Color.white);
257 thresholdIsMin.setBackground(Color.white);
258 thresholdIsMin.setText(MessageManager
259 .getString("label.threshold_minmax"));
260 thresholdIsMin.setToolTipText(MessageManager
261 .getString("label.toggle_absolute_relative_display_threshold"));
262 thresholdIsMin.addActionListener(new ActionListener()
264 public void actionPerformed(ActionEvent actionEvent)
266 thresholdIsMin_actionPerformed(actionEvent);
269 colourByLabel.setBackground(Color.white);
271 .setText(MessageManager.getString("label.colour_by_label"));
273 .setToolTipText(MessageManager
274 .getString("label.display_features_same_type_different_label_using_different_colour"));
275 colourByLabel.addActionListener(new ActionListener()
277 public void actionPerformed(ActionEvent actionEvent)
279 colourByLabel_actionPerformed(actionEvent);
282 colourPanel.setBackground(Color.white);
285 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
286 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
287 colourPanel.add(minText);
288 colourPanel.add(minColour);
289 colourPanel.add(maxText);
290 colourPanel.add(maxColour);
291 this.add(jPanel3, java.awt.BorderLayout.CENTER);
292 jPanel3.add(threshold);
294 jPanel3.add(thresholdValue);
295 jPanel3.add(thresholdIsMin);
296 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
297 this.add(jPanel2, java.awt.BorderLayout.NORTH);
300 JLabel minText = new JLabel();
302 JLabel maxText = new JLabel();
304 JPanel minColour = new JPanel();
306 JPanel maxColour = new JPanel();
308 JPanel colourPanel = new JPanel();
310 JPanel jPanel1 = new JPanel();
312 JPanel jPanel2 = new JPanel();
314 BorderLayout borderLayout1 = new BorderLayout();
316 JComboBox threshold = new JComboBox();
318 FlowLayout flowLayout1 = new FlowLayout();
320 JPanel jPanel3 = new JPanel();
322 FlowLayout flowLayout2 = new FlowLayout();
324 JSlider slider = new JSlider();
326 JTextField thresholdValue = new JTextField(20);
328 // TODO implement GUI for tolower flag
329 // JCheckBox toLower = new JCheckBox();
331 JCheckBox thresholdIsMin = new JCheckBox();
333 JCheckBox colourByLabel = new JCheckBox();
335 private GraphLine threshline;
337 private Color oldmaxColour;
339 private Color oldminColour;
341 public void minColour_actionPerformed()
343 Color col = JColorChooser.showDialog(this,
344 MessageManager.getString("label.select_colour_minimum_value"),
345 minColour.getBackground());
348 minColour.setBackground(col);
349 minColour.setForeground(col);
355 public void maxColour_actionPerformed()
357 Color col = JColorChooser.showDialog(this,
358 MessageManager.getString("label.select_colour_maximum_value"),
359 maxColour.getBackground());
362 maxColour.setBackground(col);
363 maxColour.setForeground(col);
371 // Check if combobox is still adjusting
377 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
378 if (threshold.getSelectedIndex() == 1)
380 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
382 else if (threshold.getSelectedIndex() == 2)
384 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
387 slider.setEnabled(true);
388 thresholdValue.setEnabled(true);
391 if (cs.isColourByLabel())
393 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
397 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
398 oldmaxColour = maxColour.getBackground(), min, max);
402 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
404 slider.setEnabled(false);
405 thresholdValue.setEnabled(false);
406 thresholdValue.setText("");
407 thresholdIsMin.setEnabled(false);
409 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
410 && threshline == null)
412 // todo visual indication of feature threshold
413 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
414 "Threshold", Color.black);
417 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
420 acg.setThresh(threshline.value);
422 float range = max * 1000f - min * 1000f;
424 slider.setMinimum((int) (min * 1000));
425 slider.setMaximum((int) (max * 1000));
426 slider.setValue((int) (threshline.value * 1000));
427 thresholdValue.setText(threshline.value + "");
428 slider.setMajorTickSpacing((int) (range / 10f));
429 slider.setEnabled(true);
430 thresholdValue.setEnabled(true);
431 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
435 acg.setThreshType(aboveThreshold);
436 if (thresholdIsMin.isSelected()
437 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
439 acg.setAutoScaled(false);
440 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
442 acg = new GraduatedColor(acg, threshline.value, max);
446 acg = new GraduatedColor(acg, min, threshline.value);
451 acg.setAutoScaled(true);
453 acg.setColourByLabel(colourByLabel.isSelected());
454 if (acg.isColourByLabel())
456 maxColour.setEnabled(false);
457 minColour.setEnabled(false);
458 maxColour.setBackground(this.getBackground());
459 maxColour.setForeground(this.getBackground());
460 minColour.setBackground(this.getBackground());
461 minColour.setForeground(this.getBackground());
466 maxColour.setEnabled(true);
467 minColour.setEnabled(true);
468 maxColour.setBackground(oldmaxColour);
469 minColour.setBackground(oldminColour);
470 maxColour.setForeground(oldmaxColour);
471 minColour.setForeground(oldminColour);
473 fr.setColour(type, acg);
475 ap.paintAlignment(false);
478 protected void raiseClosed()
480 if (this.colourEditor != null)
482 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
486 public void okPressed()
491 public void cancelPressed()
498 fr.setColour(type, oldcs);
499 ap.paintAlignment(false);
503 public void thresholdCheck_actionPerformed(ActionEvent e)
508 public void annotations_actionPerformed(ActionEvent e)
513 public void threshold_actionPerformed(ActionEvent e)
518 public void thresholdValue_actionPerformed(ActionEvent e)
522 float f = Float.parseFloat(thresholdValue.getText());
523 slider.setValue((int) (f * 1000));
524 threshline.value = f;
525 } catch (NumberFormatException ex)
530 public void valueChanged()
532 threshline.value = (float) slider.getValue() / 1000f;
533 cs.setThresh(threshline.value);
535 ap.paintAlignment(false);
538 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
543 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
548 ActionListener colourEditor = null;
550 public void addActionListener(ActionListener graduatedColorEditor)
552 if (colourEditor != null)
555 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
557 colourEditor = graduatedColorEditor;