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.setTitle("Graduated Feature Colour for "+type);
76 Rectangle deskr = Desktop.instance.getBounds();
77 frame.setBounds(new Rectangle((int) (deskr.getCenterX()-240),(int) (deskr.getCenterY()-92),480,185));
78 frame.setContentPane(this);
79 //frame.setLayer(JLayeredPane.PALETTE_LAYER);
80 //Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type, 480, 145);
82 slider.addChangeListener(new ChangeListener()
84 public void stateChanged(ChangeEvent evt)
88 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
93 slider.addMouseListener(new MouseAdapter()
95 public void mouseReleased(MouseEvent evt)
97 if (ap!=null) { ap.paintAlignment(true); };
101 float mm[] = ((float[][]) fr.minmax.get(type))[0];
104 oldcs = fr.featureColours.get(type);
105 if (oldcs instanceof GraduatedColor)
107 if (((GraduatedColor)oldcs).isAutoScale())
110 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
112 cs = new GraduatedColor((GraduatedColor) oldcs);
115 // promote original color to a graduated color
116 Color bl = Color.black;
117 if (oldcs instanceof Color)
121 // original colour becomes the maximum colour
122 cs = new GraduatedColor(Color.white,bl,mm[0],mm[1]);
123 cs.setColourByLabel(false);
125 minColour.setBackground(oldminColour=cs.getMinColor());
126 maxColour.setBackground(oldmaxColour=cs.getMaxColor());
132 } catch (Exception ex)
135 // update the gui from threshold state
136 thresholdIsMin.setSelected(!cs.isAutoScale());
137 colourByLabel.setSelected(cs.isColourByLabel());
138 if (cs.getThreshType()!=AnnotationColourGradient.NO_THRESHOLD)
140 // initialise threshold slider and selector
141 threshold.setSelectedIndex(cs.getThreshType()==AnnotationColourGradient.ABOVE_THRESHOLD ? 1 : 2);
142 slider.setEnabled(true);
143 thresholdValue.setEnabled(true);
144 threshline = new jalview.datamodel.GraphLine(
146 "Threshold", Color.black);
155 new Thread(new Runnable() {
168 public FeatureColourChooser()
173 } catch (Exception ex)
175 ex.printStackTrace();
179 private void jbInit() throws Exception
182 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
183 minColour.setBorder(BorderFactory.createEtchedBorder());
184 minColour.setPreferredSize(new Dimension(40, 20));
185 minColour.setToolTipText("Minimum Colour");
186 minColour.addMouseListener(new MouseAdapter()
188 public void mousePressed(MouseEvent e)
190 if (minColour.isEnabled())
192 minColour_actionPerformed();
196 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
197 maxColour.setBorder(BorderFactory.createEtchedBorder());
198 maxColour.setPreferredSize(new Dimension(40, 20));
199 maxColour.setToolTipText("Maximum Colour");
200 maxColour.addMouseListener(new MouseAdapter()
202 public void mousePressed(MouseEvent e)
204 if (maxColour.isEnabled())
206 maxColour_actionPerformed();
212 ok.addActionListener(new ActionListener()
214 public void actionPerformed(ActionEvent e)
216 ok_actionPerformed(e);
219 cancel.setOpaque(false);
220 cancel.setText("Cancel");
221 cancel.addActionListener(new ActionListener()
223 public void actionPerformed(ActionEvent e)
225 cancel_actionPerformed(e);
228 this.setLayout(borderLayout1);
229 jPanel2.setLayout(flowLayout1);
230 jPanel1.setBackground(Color.white);
231 jPanel2.setBackground(Color.white);
232 threshold.addActionListener(new ActionListener()
234 public void actionPerformed(ActionEvent e)
236 threshold_actionPerformed(e);
239 threshold.setToolTipText("Threshold the feature display by score.");
240 threshold.addItem("No Threshold"); // index 0
241 threshold.addItem("Above Threshold"); // index 1
242 threshold.addItem("Below Threshold"); // index 2
243 jPanel3.setLayout(flowLayout2);
244 thresholdValue.addActionListener(new ActionListener()
246 public void actionPerformed(ActionEvent e)
248 thresholdValue_actionPerformed(e);
251 slider.setPaintLabels(false);
252 slider.setPaintTicks(true);
253 slider.setBackground(Color.white);
254 slider.setEnabled(false);
255 slider.setOpaque(false);
256 slider.setPreferredSize(new Dimension(100, 32));
257 slider.setToolTipText("Adjust threshold");
258 thresholdValue.setEnabled(false);
259 thresholdValue.setColumns(7);
260 jPanel3.setBackground(Color.white);
261 thresholdIsMin.setBackground(Color.white);
262 thresholdIsMin.setText("Threshold is Min/Max");
263 thresholdIsMin.setToolTipText("Toggle between absolute and relative display threshold.");
264 thresholdIsMin.addActionListener(new ActionListener()
266 public void actionPerformed(ActionEvent actionEvent)
268 thresholdIsMin_actionPerformed(actionEvent);
271 colourByLabel.setBackground(Color.white);
272 colourByLabel.setText("Colour by Label");
273 colourByLabel.setToolTipText("Display features of the same type with a different label using a different colour. (e.g. domain features)");
274 colourByLabel.addActionListener(new ActionListener()
276 public void actionPerformed(ActionEvent actionEvent)
278 colourByLabel_actionPerformed(actionEvent);
283 jPanel2.add(colourByLabel,java.awt.BorderLayout.WEST);
284 jPanel2.add(colourPanel,java.awt.BorderLayout.EAST);
285 colourPanel.add(minColour);
286 colourPanel.add(maxColour);
287 this.add(jPanel3, java.awt.BorderLayout.CENTER);
288 jPanel3.add(threshold);
290 jPanel3.add(thresholdValue);
291 jPanel3.add(thresholdIsMin);
292 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
293 this.add(jPanel2, java.awt.BorderLayout.NORTH);
297 JPanel minColour = new JPanel();
299 JPanel maxColour = new JPanel();
301 JButton ok = new JButton();
303 JButton cancel = new JButton();
304 JPanel colourPanel = new JPanel();
305 JPanel jPanel1 = new JPanel();
307 JPanel jPanel2 = new JPanel();
309 BorderLayout borderLayout1 = new BorderLayout();
311 JComboBox threshold = new JComboBox();
313 FlowLayout flowLayout1 = new FlowLayout();
315 JPanel jPanel3 = new JPanel();
317 FlowLayout flowLayout2 = new FlowLayout();
319 JSlider slider = new JSlider();
321 JTextField thresholdValue = new JTextField(20);
322 // TODO implement GUI for tolower flag
323 // JCheckBox toLower = new JCheckBox();
325 JCheckBox thresholdIsMin = new JCheckBox();
326 JCheckBox colourByLabel = new JCheckBox();
328 private GraphLine threshline;
331 private Color oldmaxColour;
334 private Color oldminColour;
336 public void minColour_actionPerformed()
338 Color col = JColorChooser.showDialog(this,
339 "Select Colour for Minimum Value", minColour.getBackground());
342 minColour.setBackground(col);
348 public void maxColour_actionPerformed()
350 Color col = JColorChooser.showDialog(this,
351 "Select Colour for Maximum Value", maxColour.getBackground());
354 maxColour.setBackground(col);
362 // Check if combobox is still adjusting
369 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
370 if (threshold.getSelectedItem().equals("Above Threshold"))
372 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
374 else if (threshold.getSelectedItem().equals("Below Threshold"))
376 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
379 slider.setEnabled(true);
380 thresholdValue.setEnabled(true);
383 if (cs.isColourByLabel())
385 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
387 acg = new GraduatedColor(oldminColour=minColour.getBackground(), oldmaxColour=maxColour.getBackground(), min, max);
391 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
393 slider.setEnabled(false);
394 thresholdValue.setEnabled(false);
395 thresholdValue.setText("");
397 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
398 && threshline == null)
400 // todo visual indication of feature threshold
401 threshline = new jalview.datamodel.GraphLine(
403 "Threshold", Color.black);
406 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
409 acg.setThresh(threshline.value);
411 float range = max * 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);
424 acg.setThreshType(aboveThreshold);
425 if (thresholdIsMin.isSelected() && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
427 acg.setAutoScaled(false);
428 if (aboveThreshold==AnnotationColourGradient.ABOVE_THRESHOLD)
430 acg = new GraduatedColor(acg, threshline.value, max);
432 acg = new GraduatedColor(acg, min,threshline.value);
435 acg.setAutoScaled(true);
437 acg.setColourByLabel(colourByLabel.isSelected());
438 if (acg.isColourByLabel())
440 maxColour.setEnabled(false);
441 minColour.setEnabled(false);
442 maxColour.setBackground(this.getBackground());
443 minColour.setBackground(this.getBackground());
445 maxColour.setEnabled(true);
446 minColour.setEnabled(true);
447 maxColour.setBackground(oldmaxColour);
448 minColour.setBackground(oldminColour);
450 fr.featureColours.put(type,acg);
452 ap.paintAlignment(false);
454 private void raiseClosed() {
455 if (this.colourEditor!=null)
457 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
460 public void ok_actionPerformed(ActionEvent e)
467 } catch (Exception ex)
472 public void cancel_actionPerformed(ActionEvent e)
478 // frame.setClosed(true);
480 } catch (Exception ex)
487 fr.featureColours.put(type, oldcs);
488 ap.paintAlignment(false);
492 public void thresholdCheck_actionPerformed(ActionEvent e)
497 public void annotations_actionPerformed(ActionEvent e)
502 public void threshold_actionPerformed(ActionEvent e)
507 public void thresholdValue_actionPerformed(ActionEvent e)
511 float f = Float.parseFloat(thresholdValue.getText());
512 slider.setValue((int) (f * 1000));
513 threshline.value = f;
514 } catch (NumberFormatException ex)
519 public void valueChanged()
521 threshline.value = (float) slider.getValue() / 1000f;
522 cs.setThresh(threshline.value);
524 ap.paintAlignment(false);
527 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
531 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
535 ActionListener colourEditor=null;
536 public void addActionListener(ActionListener graduatedColorEditor)
538 if (colourEditor!=null)
540 System.err.println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
542 colourEditor = graduatedColorEditor;