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]);
107 minColour.setBackground(cs.getMinColor());
108 maxColour.setBackground(cs.getMaxColor());
114 } catch (Exception ex)
117 // update the gui from threshold state
118 thresholdIsMin.setSelected(!cs.isAutoScale());
119 if (cs.getThreshType()!=AnnotationColourGradient.NO_THRESHOLD)
121 // initialise threshold slider and selector
122 threshold.setSelectedIndex(cs.getThreshType()==AnnotationColourGradient.ABOVE_THRESHOLD ? 1 : 2);
123 slider.setEnabled(true);
124 thresholdValue.setEnabled(true);
125 threshline = new jalview.datamodel.GraphLine(
127 "Threshold", Color.black);
137 public FeatureColourChooser()
142 } catch (Exception ex)
144 ex.printStackTrace();
148 private void jbInit() throws Exception
150 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
151 minColour.setBorder(BorderFactory.createEtchedBorder());
152 minColour.setPreferredSize(new Dimension(40, 20));
153 minColour.setToolTipText("Minimum Colour");
154 minColour.addMouseListener(new MouseAdapter()
156 public void mousePressed(MouseEvent e)
158 if (minColour.isEnabled())
160 minColour_actionPerformed();
164 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
165 maxColour.setBorder(BorderFactory.createEtchedBorder());
166 maxColour.setPreferredSize(new Dimension(40, 20));
167 maxColour.setToolTipText("Maximum Colour");
168 maxColour.addMouseListener(new MouseAdapter()
170 public void mousePressed(MouseEvent e)
172 if (maxColour.isEnabled())
174 maxColour_actionPerformed();
180 ok.addActionListener(new ActionListener()
182 public void actionPerformed(ActionEvent e)
184 ok_actionPerformed(e);
187 cancel.setOpaque(false);
188 cancel.setText("Cancel");
189 cancel.addActionListener(new ActionListener()
191 public void actionPerformed(ActionEvent e)
193 cancel_actionPerformed(e);
196 this.setLayout(borderLayout1);
197 jPanel2.setLayout(flowLayout1);
198 jPanel1.setBackground(Color.white);
199 jPanel2.setBackground(Color.white);
200 threshold.addActionListener(new ActionListener()
202 public void actionPerformed(ActionEvent e)
204 threshold_actionPerformed(e);
207 threshold.addItem("No Threshold"); // index 0
208 threshold.addItem("Above Threshold"); // index 1
209 threshold.addItem("Below Threshold"); // index 2
210 jPanel3.setLayout(flowLayout2);
211 thresholdValue.addActionListener(new ActionListener()
213 public void actionPerformed(ActionEvent e)
215 thresholdValue_actionPerformed(e);
218 slider.setPaintLabels(false);
219 slider.setPaintTicks(true);
220 slider.setBackground(Color.white);
221 slider.setEnabled(false);
222 slider.setOpaque(false);
223 slider.setPreferredSize(new Dimension(100, 32));
224 thresholdValue.setEnabled(false);
225 thresholdValue.setColumns(7);
226 jPanel3.setBackground(Color.white);
227 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
228 currentColours.setOpaque(false);
229 currentColours.setText("Use Original Colours");
230 currentColours.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
234 currentColours_actionPerformed(e);
237 thresholdIsMin.setBackground(Color.white);
238 thresholdIsMin.setText("Threshold is Min/Max");
239 thresholdIsMin.addActionListener(new ActionListener()
241 public void actionPerformed(ActionEvent actionEvent)
243 thresholdIsMin_actionPerformed(actionEvent);
248 // jPanel2.add(currentColours);
249 jPanel2.add(minColour);
250 jPanel2.add(maxColour);
251 this.add(jPanel3, java.awt.BorderLayout.CENTER);
252 jPanel3.add(threshold);
254 jPanel3.add(thresholdValue);
255 jPanel3.add(thresholdIsMin);
256 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
257 this.add(jPanel2, java.awt.BorderLayout.NORTH);
261 JPanel minColour = new JPanel();
263 JPanel maxColour = new JPanel();
265 JButton ok = new JButton();
267 JButton cancel = new JButton();
269 JPanel jPanel1 = new JPanel();
271 JPanel jPanel2 = new JPanel();
273 BorderLayout borderLayout1 = new BorderLayout();
275 JComboBox threshold = new JComboBox();
277 FlowLayout flowLayout1 = new FlowLayout();
279 JPanel jPanel3 = new JPanel();
281 FlowLayout flowLayout2 = new FlowLayout();
283 JSlider slider = new JSlider();
285 JTextField thresholdValue = new JTextField(20);
286 // TODO refactor to tolower flag
287 JCheckBox currentColours = new JCheckBox();
289 JCheckBox thresholdIsMin = new JCheckBox();
291 private GraphLine threshline;
293 public void minColour_actionPerformed()
295 Color col = JColorChooser.showDialog(this,
296 "Select Colour for Minimum Value", minColour.getBackground());
299 minColour.setBackground(col);
305 public void maxColour_actionPerformed()
307 Color col = JColorChooser.showDialog(this,
308 "Select Colour for Maximum Value", maxColour.getBackground());
311 maxColour.setBackground(col);
319 // Check if combobox is still adjusting
326 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
327 if (threshold.getSelectedItem().equals("Above Threshold"))
329 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
331 else if (threshold.getSelectedItem().equals("Below Threshold"))
333 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
336 slider.setEnabled(true);
337 thresholdValue.setEnabled(true);
338 GraduatedColor acg = new GraduatedColor(minColour.getBackground(), maxColour.getBackground(), min, max);
340 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
342 slider.setEnabled(false);
343 thresholdValue.setEnabled(false);
344 thresholdValue.setText("");
346 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
347 && threshline == null)
349 // todo visual indication of feature threshold
350 threshline = new jalview.datamodel.GraphLine(
352 "Threshold", Color.black);
355 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
358 acg.setThresh(threshline.value);
360 float range = max * 1000f
363 slider.setMinimum((int) (min * 1000));
364 slider.setMaximum((int) (max * 1000));
365 slider.setValue((int) (threshline.value * 1000));
366 thresholdValue.setText(threshline.value + "");
367 slider.setMajorTickSpacing((int) (range / 10f));
368 slider.setEnabled(true);
369 thresholdValue.setEnabled(true);
373 acg.setThreshType(aboveThreshold);
374 if (thresholdIsMin.isSelected() && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
376 acg.setAutoScaled(false);
377 if (aboveThreshold==AnnotationColourGradient.ABOVE_THRESHOLD)
379 acg = new GraduatedColor(acg, threshline.value, max);
381 acg = new GraduatedColor(acg, min,threshline.value);
384 acg.setAutoScaled(true);
387 fr.featureColours.put(type,acg);
389 ap.paintAlignment(false);
392 public void ok_actionPerformed(ActionEvent e)
397 frame.setClosed(true);
398 } catch (Exception ex)
403 public void cancel_actionPerformed(ActionEvent e)
408 frame.setClosed(true);
409 } catch (Exception ex)
416 fr.featureColours.put(type, oldcs);
419 public void thresholdCheck_actionPerformed(ActionEvent e)
424 public void annotations_actionPerformed(ActionEvent e)
429 public void threshold_actionPerformed(ActionEvent e)
434 public void thresholdValue_actionPerformed(ActionEvent e)
438 float f = Float.parseFloat(thresholdValue.getText());
439 slider.setValue((int) (f * 1000));
440 threshline.value = f;
441 } catch (NumberFormatException ex)
446 public void valueChanged()
448 threshline.value = (float) slider.getValue() / 1000f;
449 cs.setThresh(threshline.value);
451 ap.paintAlignment(false);
454 public void currentColours_actionPerformed(ActionEvent e)
456 if (currentColours.isSelected())
461 maxColour.setEnabled(!currentColours.isSelected());
462 minColour.setEnabled(!currentColours.isSelected());
467 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)