2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
26 import javax.swing.event.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import java.awt.Dimension;
32 public class FeatureColourChooser extends JPanel
37 // FeatureSettings fs;
41 private GraduatedColor cs;
45 * @return the last colour setting selected by user - either oldcs (which may be a java.awt.Color) or the new GraduatedColor
47 public Object getLastColour() {
54 Hashtable oldgroupColours;
59 boolean adjusting = false;
65 public FeatureColourChooser(FeatureRenderer frender, String type)
67 this(frender,false,type);
69 public FeatureColourChooser(FeatureRenderer frender, boolean block, String type)
74 frame = new JDialog(Desktop.instance,true);
75 frame.setSize(480,185);
76 frame.setContentPane(this);
77 //frame.setLayer(JLayeredPane.PALETTE_LAYER);
78 //Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type, 480, 145);
80 slider.addChangeListener(new ChangeListener()
82 public void stateChanged(ChangeEvent evt)
86 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
91 slider.addMouseListener(new MouseAdapter()
93 public void mouseReleased(MouseEvent evt)
95 if (ap!=null) { ap.paintAlignment(true); };
99 float mm[] = ((float[][]) fr.minmax.get(type))[0];
102 oldcs = fr.featureColours.get(type);
103 if (oldcs instanceof GraduatedColor)
105 if (((GraduatedColor)oldcs).isAutoScale())
108 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
110 cs = new GraduatedColor((GraduatedColor) oldcs);
113 // promote original color to a graduated color
114 Color bl = Color.black;
115 if (oldcs instanceof Color)
119 // original colour becomes the maximum colour
120 cs = new GraduatedColor(Color.white,bl,mm[0],mm[1]);
121 cs.setColourByLabel(false);
123 minColour.setBackground(oldminColour=cs.getMinColor());
124 maxColour.setBackground(oldmaxColour=cs.getMaxColor());
130 } catch (Exception ex)
133 // update the gui from threshold state
134 thresholdIsMin.setSelected(!cs.isAutoScale());
135 colourByLabel.setSelected(cs.isColourByLabel());
136 if (cs.getThreshType()!=AnnotationColourGradient.NO_THRESHOLD)
138 // initialise threshold slider and selector
139 threshold.setSelectedIndex(cs.getThreshType()==AnnotationColourGradient.ABOVE_THRESHOLD ? 1 : 2);
140 slider.setEnabled(true);
141 thresholdValue.setEnabled(true);
142 threshline = new jalview.datamodel.GraphLine(
144 "Threshold", Color.black);
153 new Thread(new Runnable() {
166 public FeatureColourChooser()
171 } catch (Exception ex)
173 ex.printStackTrace();
177 private void jbInit() throws Exception
180 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
181 minColour.setBorder(BorderFactory.createEtchedBorder());
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(new java.awt.Font("Verdana", Font.PLAIN, 11));
195 maxColour.setBorder(BorderFactory.createEtchedBorder());
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();
210 ok.addActionListener(new ActionListener()
212 public void actionPerformed(ActionEvent e)
214 ok_actionPerformed(e);
217 cancel.setOpaque(false);
218 cancel.setText("Cancel");
219 cancel.addActionListener(new ActionListener()
221 public void actionPerformed(ActionEvent e)
223 cancel_actionPerformed(e);
226 this.setLayout(borderLayout1);
227 jPanel2.setLayout(flowLayout1);
228 jPanel1.setBackground(Color.white);
229 jPanel2.setBackground(Color.white);
230 threshold.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
234 threshold_actionPerformed(e);
237 threshold.setToolTipText("Threshold the feature display by score.");
238 threshold.addItem("No Threshold"); // index 0
239 threshold.addItem("Above Threshold"); // index 1
240 threshold.addItem("Below Threshold"); // index 2
241 jPanel3.setLayout(flowLayout2);
242 thresholdValue.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent e)
246 thresholdValue_actionPerformed(e);
249 slider.setPaintLabels(false);
250 slider.setPaintTicks(true);
251 slider.setBackground(Color.white);
252 slider.setEnabled(false);
253 slider.setOpaque(false);
254 slider.setPreferredSize(new Dimension(100, 32));
255 slider.setToolTipText("Adjust threshold");
256 thresholdValue.setEnabled(false);
257 thresholdValue.setColumns(7);
258 jPanel3.setBackground(Color.white);
259 thresholdIsMin.setBackground(Color.white);
260 thresholdIsMin.setText("Threshold is Min/Max");
261 thresholdIsMin.setToolTipText("Toggle between absolute and relative display threshold.");
262 thresholdIsMin.addActionListener(new ActionListener()
264 public void actionPerformed(ActionEvent actionEvent)
266 thresholdIsMin_actionPerformed(actionEvent);
269 colourByLabel.setBackground(Color.white);
270 colourByLabel.setText("Colour by Label");
271 colourByLabel.setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
272 colourByLabel.addActionListener(new ActionListener()
274 public void actionPerformed(ActionEvent actionEvent)
276 colourByLabel_actionPerformed(actionEvent);
281 jPanel2.add(colourByLabel,java.awt.BorderLayout.WEST);
282 jPanel2.add(colourPanel,java.awt.BorderLayout.EAST);
283 colourPanel.add(minColour);
284 colourPanel.add(maxColour);
285 this.add(jPanel3, java.awt.BorderLayout.CENTER);
286 jPanel3.add(threshold);
288 jPanel3.add(thresholdValue);
289 jPanel3.add(thresholdIsMin);
290 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
291 this.add(jPanel2, java.awt.BorderLayout.NORTH);
295 JPanel minColour = new JPanel();
297 JPanel maxColour = new JPanel();
299 JButton ok = new JButton();
301 JButton cancel = new JButton();
302 JPanel colourPanel = new JPanel();
303 JPanel jPanel1 = new JPanel();
305 JPanel jPanel2 = new JPanel();
307 BorderLayout borderLayout1 = new BorderLayout();
309 JComboBox threshold = new JComboBox();
311 FlowLayout flowLayout1 = new FlowLayout();
313 JPanel jPanel3 = new JPanel();
315 FlowLayout flowLayout2 = new FlowLayout();
317 JSlider slider = new JSlider();
319 JTextField thresholdValue = new JTextField(20);
320 // TODO implement GUI for tolower flag
321 // JCheckBox toLower = new JCheckBox();
323 JCheckBox thresholdIsMin = new JCheckBox();
324 JCheckBox colourByLabel = new JCheckBox();
326 private GraphLine threshline;
329 private Color oldmaxColour;
332 private Color oldminColour;
334 public void minColour_actionPerformed()
336 Color col = JColorChooser.showDialog(this,
337 "Select Colour for Minimum Value", minColour.getBackground());
340 minColour.setBackground(col);
346 public void maxColour_actionPerformed()
348 Color col = JColorChooser.showDialog(this,
349 "Select Colour for Maximum Value", maxColour.getBackground());
352 maxColour.setBackground(col);
360 // Check if combobox is still adjusting
367 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
368 if (threshold.getSelectedItem().equals("Above Threshold"))
370 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
372 else if (threshold.getSelectedItem().equals("Below Threshold"))
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);
385 acg = new GraduatedColor(oldminColour=minColour.getBackground(), oldmaxColour=maxColour.getBackground(), min, max);
389 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
391 slider.setEnabled(false);
392 thresholdValue.setEnabled(false);
393 thresholdValue.setText("");
395 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
396 && threshline == null)
398 // todo visual indication of feature threshold
399 threshline = new jalview.datamodel.GraphLine(
401 "Threshold", Color.black);
404 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
407 acg.setThresh(threshline.value);
409 float range = max * 1000f
412 slider.setMinimum((int) (min * 1000));
413 slider.setMaximum((int) (max * 1000));
414 slider.setValue((int) (threshline.value * 1000));
415 thresholdValue.setText(threshline.value + "");
416 slider.setMajorTickSpacing((int) (range / 10f));
417 slider.setEnabled(true);
418 thresholdValue.setEnabled(true);
422 acg.setThreshType(aboveThreshold);
423 if (thresholdIsMin.isSelected() && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
425 acg.setAutoScaled(false);
426 if (aboveThreshold==AnnotationColourGradient.ABOVE_THRESHOLD)
428 acg = new GraduatedColor(acg, threshline.value, max);
430 acg = new GraduatedColor(acg, min,threshline.value);
433 acg.setAutoScaled(true);
435 acg.setColourByLabel(colourByLabel.isSelected());
436 if (acg.isColourByLabel())
438 maxColour.setEnabled(false);
439 minColour.setEnabled(false);
440 maxColour.setBackground(this.getBackground());
441 minColour.setBackground(this.getBackground());
443 maxColour.setEnabled(true);
444 minColour.setEnabled(true);
445 maxColour.setBackground(oldmaxColour);
446 minColour.setBackground(oldminColour);
448 fr.featureColours.put(type,acg);
450 ap.paintAlignment(false);
452 private void raiseClosed() {
453 if (this.colourEditor!=null)
455 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
458 public void ok_actionPerformed(ActionEvent e)
465 } catch (Exception ex)
470 public void cancel_actionPerformed(ActionEvent e)
476 // frame.setClosed(true);
478 } catch (Exception ex)
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)
529 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
533 ActionListener colourEditor=null;
534 public void addActionListener(ActionListener graduatedColorEditor)
536 if (colourEditor!=null)
538 System.err.println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
540 colourEditor = graduatedColorEditor;