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
43 Hashtable oldgroupColours;
48 boolean adjusting = false;
54 public FeatureColourChooser(FeatureSettings fsettings, String type)
60 frame = new JInternalFrame();
61 frame.setContentPane(this);
62 frame.setLayer(JLayeredPane.PALETTE_LAYER);
63 Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type, 480, 145);
65 slider.addChangeListener(new ChangeListener()
67 public void stateChanged(ChangeEvent evt)
71 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
76 slider.addMouseListener(new MouseAdapter()
78 public void mouseReleased(MouseEvent evt)
80 if (fr.ap!=null) { fr.ap.paintAlignment(true); };
84 float mm[] = ((float[][]) fr.minmax.get(type))[0];
87 oldcs = fr.featureColours.get(type);
88 if (oldcs instanceof GraduatedColor)
90 if (((GraduatedColor)oldcs).isAutoScale())
93 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
95 cs = new GraduatedColor((GraduatedColor) oldcs);
98 // promote original color to a graduated color
99 Color bl = Color.black;
100 if (oldcs instanceof Color)
104 // original colour becomes the maximum colour
105 cs = new GraduatedColor(Color.white,bl,mm[0],mm[1]);
106 cs.setColourByLabel(false);
108 minColour.setBackground(cs.getMinColor());
109 maxColour.setBackground(cs.getMaxColor());
115 } catch (Exception ex)
118 // update the gui from threshold state
119 thresholdIsMin.setSelected(!cs.isAutoScale());
120 colourByLabel.setSelected(cs.isColourByLabel());
121 if (cs.getThreshType()!=AnnotationColourGradient.NO_THRESHOLD)
123 // initialise threshold slider and selector
124 threshold.setSelectedIndex(cs.getThreshType()==AnnotationColourGradient.ABOVE_THRESHOLD ? 1 : 2);
125 slider.setEnabled(true);
126 thresholdValue.setEnabled(true);
127 threshline = new jalview.datamodel.GraphLine(
129 "Threshold", Color.black);
139 public FeatureColourChooser()
144 } catch (Exception ex)
146 ex.printStackTrace();
150 private void jbInit() throws Exception
152 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
153 minColour.setBorder(BorderFactory.createEtchedBorder());
154 minColour.setPreferredSize(new Dimension(40, 20));
155 minColour.setToolTipText("Minimum Colour");
156 minColour.addMouseListener(new MouseAdapter()
158 public void mousePressed(MouseEvent e)
160 if (minColour.isEnabled())
162 minColour_actionPerformed();
166 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
167 maxColour.setBorder(BorderFactory.createEtchedBorder());
168 maxColour.setPreferredSize(new Dimension(40, 20));
169 maxColour.setToolTipText("Maximum Colour");
170 maxColour.addMouseListener(new MouseAdapter()
172 public void mousePressed(MouseEvent e)
174 if (maxColour.isEnabled())
176 maxColour_actionPerformed();
182 ok.addActionListener(new ActionListener()
184 public void actionPerformed(ActionEvent e)
186 ok_actionPerformed(e);
189 cancel.setOpaque(false);
190 cancel.setText("Cancel");
191 cancel.addActionListener(new ActionListener()
193 public void actionPerformed(ActionEvent e)
195 cancel_actionPerformed(e);
198 this.setLayout(borderLayout1);
199 jPanel2.setLayout(flowLayout1);
200 jPanel1.setBackground(Color.white);
201 jPanel2.setBackground(Color.white);
202 threshold.addActionListener(new ActionListener()
204 public void actionPerformed(ActionEvent e)
206 threshold_actionPerformed(e);
209 threshold.addItem("No Threshold"); // index 0
210 threshold.addItem("Above Threshold"); // index 1
211 threshold.addItem("Below Threshold"); // index 2
212 jPanel3.setLayout(flowLayout2);
213 thresholdValue.addActionListener(new ActionListener()
215 public void actionPerformed(ActionEvent e)
217 thresholdValue_actionPerformed(e);
220 slider.setPaintLabels(false);
221 slider.setPaintTicks(true);
222 slider.setBackground(Color.white);
223 slider.setEnabled(false);
224 slider.setOpaque(false);
225 slider.setPreferredSize(new Dimension(100, 32));
226 thresholdValue.setEnabled(false);
227 thresholdValue.setColumns(7);
228 jPanel3.setBackground(Color.white);
229 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
230 currentColours.setOpaque(false);
231 currentColours.setText("Use Original Colours");
232 currentColours.addActionListener(new ActionListener()
234 public void actionPerformed(ActionEvent e)
236 currentColours_actionPerformed(e);
239 thresholdIsMin.setBackground(Color.white);
240 thresholdIsMin.setText("Threshold is Min/Max");
241 thresholdIsMin.addActionListener(new ActionListener()
243 public void actionPerformed(ActionEvent actionEvent)
245 thresholdIsMin_actionPerformed(actionEvent);
248 colourByLabel.setBackground(Color.white);
249 colourByLabel.setText("Colour by Label");
250 colourByLabel.addActionListener(new ActionListener()
252 public void actionPerformed(ActionEvent actionEvent)
254 colourByLabel_actionPerformed(actionEvent);
259 // jPanel2.add(currentColours);
260 jPanel2.add(minColour);
261 jPanel2.add(maxColour);
262 this.add(jPanel3, java.awt.BorderLayout.CENTER);
263 jPanel3.add(threshold);
265 jPanel3.add(thresholdValue);
266 jPanel3.add(thresholdIsMin);
267 jPanel3.add(colourByLabel);
268 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
269 this.add(jPanel2, java.awt.BorderLayout.NORTH);
273 JPanel minColour = new JPanel();
275 JPanel maxColour = new JPanel();
277 JButton ok = new JButton();
279 JButton cancel = new JButton();
281 JPanel jPanel1 = new JPanel();
283 JPanel jPanel2 = new JPanel();
285 BorderLayout borderLayout1 = new BorderLayout();
287 JComboBox threshold = new JComboBox();
289 FlowLayout flowLayout1 = new FlowLayout();
291 JPanel jPanel3 = new JPanel();
293 FlowLayout flowLayout2 = new FlowLayout();
295 JSlider slider = new JSlider();
297 JTextField thresholdValue = new JTextField(20);
298 // TODO refactor to tolower flag
299 JCheckBox currentColours = new JCheckBox();
301 JCheckBox thresholdIsMin = new JCheckBox();
302 JCheckBox colourByLabel = new JCheckBox();
304 private GraphLine threshline;
306 public void minColour_actionPerformed()
308 Color col = JColorChooser.showDialog(this,
309 "Select Colour for Minimum Value", minColour.getBackground());
312 minColour.setBackground(col);
318 public void maxColour_actionPerformed()
320 Color col = JColorChooser.showDialog(this,
321 "Select Colour for Maximum Value", maxColour.getBackground());
324 maxColour.setBackground(col);
332 // Check if combobox is still adjusting
339 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
340 if (threshold.getSelectedItem().equals("Above Threshold"))
342 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
344 else if (threshold.getSelectedItem().equals("Below Threshold"))
346 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
349 slider.setEnabled(true);
350 thresholdValue.setEnabled(true);
351 GraduatedColor acg = new GraduatedColor(minColour.getBackground(), maxColour.getBackground(), min, max);
353 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
355 slider.setEnabled(false);
356 thresholdValue.setEnabled(false);
357 thresholdValue.setText("");
359 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
360 && threshline == null)
362 // todo visual indication of feature threshold
363 threshline = new jalview.datamodel.GraphLine(
365 "Threshold", Color.black);
368 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
371 acg.setThresh(threshline.value);
373 float range = max * 1000f
376 slider.setMinimum((int) (min * 1000));
377 slider.setMaximum((int) (max * 1000));
378 slider.setValue((int) (threshline.value * 1000));
379 thresholdValue.setText(threshline.value + "");
380 slider.setMajorTickSpacing((int) (range / 10f));
381 slider.setEnabled(true);
382 thresholdValue.setEnabled(true);
386 acg.setThreshType(aboveThreshold);
387 if (thresholdIsMin.isSelected() && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
389 acg.setAutoScaled(false);
390 if (aboveThreshold==AnnotationColourGradient.ABOVE_THRESHOLD)
392 acg = new GraduatedColor(acg, threshline.value, max);
394 acg = new GraduatedColor(acg, min,threshline.value);
397 acg.setAutoScaled(true);
399 acg.setColourByLabel(colourByLabel.isSelected());
400 fr.featureColours.put(type,acg);
402 ap.paintAlignment(false);
405 public void ok_actionPerformed(ActionEvent e)
410 frame.setClosed(true);
411 } catch (Exception ex)
416 public void cancel_actionPerformed(ActionEvent e)
421 frame.setClosed(true);
422 } catch (Exception ex)
429 fr.featureColours.put(type, oldcs);
432 public void thresholdCheck_actionPerformed(ActionEvent e)
437 public void annotations_actionPerformed(ActionEvent e)
442 public void threshold_actionPerformed(ActionEvent e)
447 public void thresholdValue_actionPerformed(ActionEvent e)
451 float f = Float.parseFloat(thresholdValue.getText());
452 slider.setValue((int) (f * 1000));
453 threshline.value = f;
454 } catch (NumberFormatException ex)
459 public void valueChanged()
461 threshline.value = (float) slider.getValue() / 1000f;
462 cs.setThresh(threshline.value);
464 ap.paintAlignment(false);
467 public void currentColours_actionPerformed(ActionEvent e)
469 if (currentColours.isSelected())
474 maxColour.setEnabled(!currentColours.isSelected());
475 minColour.setEnabled(!currentColours.isSelected());
480 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
484 public void colourByLabel_actionPerformed(ActionEvent actionEvent)