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.border.LineBorder;
27 import javax.swing.event.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import java.awt.Dimension;
33 public class FeatureColourChooser extends JPanel
38 // FeatureSettings fs;
42 private GraduatedColor cs;
46 * @return the last colour setting selected by user - either oldcs (which may be a java.awt.Color) or the new GraduatedColor
48 public Object getLastColour() {
55 Hashtable oldgroupColours;
60 boolean adjusting = false;
66 public FeatureColourChooser(FeatureRenderer frender, String type)
68 this(frender,false,type);
70 public FeatureColourChooser(FeatureRenderer frender, boolean block, String type)
75 frame = new JDialog(Desktop.instance,true);
76 frame.setTitle("Graduated Feature Colour for "+type);
77 Rectangle deskr = Desktop.instance.getBounds();
78 frame.setBounds(new Rectangle((int) (deskr.getCenterX()-240),(int) (deskr.getCenterY()-92),480,185));
79 frame.setContentPane(this);
80 //frame.setLayer(JLayeredPane.PALETTE_LAYER);
81 //Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type, 480, 145);
83 slider.addChangeListener(new ChangeListener()
85 public void stateChanged(ChangeEvent evt)
89 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
94 slider.addMouseListener(new MouseAdapter()
96 public void mouseReleased(MouseEvent evt)
98 if (ap!=null) { ap.paintAlignment(true); };
102 float mm[] = ((float[][]) fr.minmax.get(type))[0];
105 oldcs = fr.featureColours.get(type);
106 if (oldcs instanceof GraduatedColor)
108 if (((GraduatedColor)oldcs).isAutoScale())
111 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
113 cs = new GraduatedColor((GraduatedColor) oldcs);
116 // promote original color to a graduated color
117 Color bl = Color.black;
118 if (oldcs instanceof Color)
122 // original colour becomes the maximum colour
123 cs = new GraduatedColor(Color.white,bl,mm[0],mm[1]);
124 cs.setColourByLabel(false);
126 minColour.setBackground(oldminColour=cs.getMinColor());
127 maxColour.setBackground(oldmaxColour=cs.getMaxColor());
133 } catch (Exception ex)
136 // update the gui from threshold state
137 thresholdIsMin.setSelected(!cs.isAutoScale());
138 colourByLabel.setSelected(cs.isColourByLabel());
139 if (cs.getThreshType()!=AnnotationColourGradient.NO_THRESHOLD)
141 // initialise threshold slider and selector
142 threshold.setSelectedIndex(cs.getThreshType()==AnnotationColourGradient.ABOVE_THRESHOLD ? 1 : 2);
143 slider.setEnabled(true);
144 thresholdValue.setEnabled(true);
145 threshline = new jalview.datamodel.GraphLine(
147 "Threshold", Color.black);
156 new Thread(new Runnable() {
169 public FeatureColourChooser()
174 } catch (Exception ex)
176 ex.printStackTrace();
180 private void jbInit() throws Exception
183 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
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(new java.awt.Font("Verdana", Font.PLAIN, 11));
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("Min:");
213 minText.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
214 maxText.setText("Max:");
215 maxText.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
218 ok.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 ok_actionPerformed(e);
225 cancel.setOpaque(false);
226 cancel.setText("Cancel");
227 cancel.addActionListener(new ActionListener()
229 public void actionPerformed(ActionEvent e)
231 cancel_actionPerformed(e);
234 this.setLayout(borderLayout1);
235 jPanel2.setLayout(flowLayout1);
236 jPanel1.setBackground(Color.white);
237 jPanel2.setBackground(Color.white);
238 threshold.addActionListener(new ActionListener()
240 public void actionPerformed(ActionEvent e)
242 threshold_actionPerformed(e);
245 threshold.setToolTipText("Threshold the feature display by score.");
246 threshold.addItem("No Threshold"); // index 0
247 threshold.addItem("Above Threshold"); // index 1
248 threshold.addItem("Below Threshold"); // index 2
249 jPanel3.setLayout(flowLayout2);
250 thresholdValue.addActionListener(new ActionListener()
252 public void actionPerformed(ActionEvent e)
254 thresholdValue_actionPerformed(e);
257 slider.setPaintLabels(false);
258 slider.setPaintTicks(true);
259 slider.setBackground(Color.white);
260 slider.setEnabled(false);
261 slider.setOpaque(false);
262 slider.setPreferredSize(new Dimension(100, 32));
263 slider.setToolTipText("Adjust threshold");
264 thresholdValue.setEnabled(false);
265 thresholdValue.setColumns(7);
266 jPanel3.setBackground(Color.white);
267 thresholdIsMin.setBackground(Color.white);
268 thresholdIsMin.setText("Threshold is Min/Max");
269 thresholdIsMin.setToolTipText("Toggle between absolute and relative display threshold.");
270 thresholdIsMin.addActionListener(new ActionListener()
272 public void actionPerformed(ActionEvent actionEvent)
274 thresholdIsMin_actionPerformed(actionEvent);
277 colourByLabel.setBackground(Color.white);
278 colourByLabel.setText("Colour by Label");
279 colourByLabel.setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
280 colourByLabel.addActionListener(new ActionListener()
282 public void actionPerformed(ActionEvent actionEvent)
284 colourByLabel_actionPerformed(actionEvent);
287 colourPanel.setBackground(Color.white);
290 jPanel2.add(colourByLabel,java.awt.BorderLayout.WEST);
291 jPanel2.add(colourPanel,java.awt.BorderLayout.EAST);
292 colourPanel.add(minText);
293 colourPanel.add(minColour);
294 colourPanel.add(maxText);
295 colourPanel.add(maxColour);
296 this.add(jPanel3, java.awt.BorderLayout.CENTER);
297 jPanel3.add(threshold);
299 jPanel3.add(thresholdValue);
300 jPanel3.add(thresholdIsMin);
301 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
302 this.add(jPanel2, java.awt.BorderLayout.NORTH);
306 JLabel minText = new JLabel();
307 JLabel maxText = new JLabel();
308 JPanel minColour = new JPanel();
310 JPanel maxColour = new JPanel();
312 JButton ok = new JButton();
314 JButton cancel = new JButton();
315 JPanel colourPanel = new JPanel();
316 JPanel jPanel1 = new JPanel();
318 JPanel jPanel2 = new JPanel();
320 BorderLayout borderLayout1 = new BorderLayout();
322 JComboBox threshold = new JComboBox();
324 FlowLayout flowLayout1 = new FlowLayout();
326 JPanel jPanel3 = new JPanel();
328 FlowLayout flowLayout2 = new FlowLayout();
330 JSlider slider = new JSlider();
332 JTextField thresholdValue = new JTextField(20);
333 // TODO implement GUI for tolower flag
334 // JCheckBox toLower = new JCheckBox();
336 JCheckBox thresholdIsMin = new JCheckBox();
337 JCheckBox colourByLabel = new JCheckBox();
339 private GraphLine threshline;
342 private Color oldmaxColour;
345 private Color oldminColour;
347 public void minColour_actionPerformed()
349 Color col = JColorChooser.showDialog(this,
350 "Select Colour for Minimum Value", minColour.getBackground());
353 minColour.setBackground(col);
359 public void maxColour_actionPerformed()
361 Color col = JColorChooser.showDialog(this,
362 "Select Colour for Maximum Value", maxColour.getBackground());
365 maxColour.setBackground(col);
373 // Check if combobox is still adjusting
380 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
381 if (threshold.getSelectedItem().equals("Above Threshold"))
383 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
385 else if (threshold.getSelectedItem().equals("Below Threshold"))
387 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
390 slider.setEnabled(true);
391 thresholdValue.setEnabled(true);
394 if (cs.isColourByLabel())
396 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
398 acg = new GraduatedColor(oldminColour=minColour.getBackground(), 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(
415 "Threshold", Color.black);
418 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
421 acg.setThresh(threshline.value);
423 float range = max * 1000f
426 slider.setMinimum((int) (min * 1000));
427 slider.setMaximum((int) (max * 1000));
428 slider.setValue((int) (threshline.value * 1000));
429 thresholdValue.setText(threshline.value + "");
430 slider.setMajorTickSpacing((int) (range / 10f));
431 slider.setEnabled(true);
432 thresholdValue.setEnabled(true);
433 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
437 acg.setThreshType(aboveThreshold);
438 if (thresholdIsMin.isSelected() && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
440 acg.setAutoScaled(false);
441 if (aboveThreshold==AnnotationColourGradient.ABOVE_THRESHOLD)
443 acg = new GraduatedColor(acg, threshline.value, max);
445 acg = new GraduatedColor(acg, min,threshline.value);
448 acg.setAutoScaled(true);
450 acg.setColourByLabel(colourByLabel.isSelected());
451 if (acg.isColourByLabel())
453 maxColour.setEnabled(false);
454 minColour.setEnabled(false);
455 maxColour.setBackground(this.getBackground());
456 minColour.setBackground(this.getBackground());
458 maxColour.setEnabled(true);
459 minColour.setEnabled(true);
460 maxColour.setBackground(oldmaxColour);
461 minColour.setBackground(oldminColour);
463 fr.featureColours.put(type,acg);
465 ap.paintAlignment(false);
467 private void raiseClosed() {
468 if (this.colourEditor!=null)
470 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
473 public void ok_actionPerformed(ActionEvent e)
480 } catch (Exception ex)
485 public void cancel_actionPerformed(ActionEvent e)
491 // frame.setClosed(true);
493 } catch (Exception ex)
500 fr.featureColours.put(type, oldcs);
501 ap.paintAlignment(false);
505 public void thresholdCheck_actionPerformed(ActionEvent e)
510 public void annotations_actionPerformed(ActionEvent e)
515 public void threshold_actionPerformed(ActionEvent e)
520 public void thresholdValue_actionPerformed(ActionEvent e)
524 float f = Float.parseFloat(thresholdValue.getText());
525 slider.setValue((int) (f * 1000));
526 threshline.value = f;
527 } catch (NumberFormatException ex)
532 public void valueChanged()
534 threshline.value = (float) slider.getValue() / 1000f;
535 cs.setThresh(threshline.value);
537 ap.paintAlignment(false);
540 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
544 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
548 ActionListener colourEditor=null;
549 public void addActionListener(ActionListener graduatedColorEditor)
551 if (colourEditor!=null)
553 System.err.println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
555 colourEditor = graduatedColorEditor;