2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
25 import javax.swing.event.*;
27 import jalview.bin.Cache;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import java.awt.Dimension;
32 public class AnnotationColourChooser extends JPanel
42 Hashtable oldgroupColours;
44 jalview.datamodel.AlignmentAnnotation currentAnnotation;
46 boolean adjusting = false;
48 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
50 oldcs = av.getGlobalColourScheme();
51 if (av.alignment.getGroups() != null)
53 oldgroupColours = new Hashtable();
54 Vector allGroups = ap.av.alignment.getGroups();
56 for (int g = 0; g < allGroups.size(); g++)
58 sg = (SequenceGroup) allGroups.get(g);
61 oldgroupColours.put(sg, sg.cs);
67 frame = new JInternalFrame();
68 frame.setContentPane(this);
69 frame.setLayer(JLayeredPane.PALETTE_LAYER);
70 Desktop.addInternalFrame(frame, "Colour by Annotation", 480, 145);
72 slider.addChangeListener(new ChangeListener()
74 public void stateChanged(ChangeEvent evt)
78 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
83 slider.addMouseListener(new MouseAdapter()
85 public void mouseReleased(MouseEvent evt)
87 ap.paintAlignment(true);
91 if (av.alignment.getAlignmentAnnotation() == null)
96 // Always get default shading from preferences.
97 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
98 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
100 if (oldcs instanceof AnnotationColourGradient)
102 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
103 currentColours.setSelected(acg.predefinedColours);
104 if (acg.predefinedColours)
108 minColour.setBackground(acg.getMinColour());
109 maxColour.setBackground(acg.getMaxColour());
114 Vector list = new Vector();
116 for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
118 String label = av.alignment.getAlignmentAnnotation()[i].label;
119 if (!list.contains(label))
120 list.addElement(label);
122 list.addElement(label + "_" + (index++));
125 annotations = new JComboBox(list);
127 threshold.addItem("No Threshold");
128 threshold.addItem("Above Threshold");
129 threshold.addItem("Below Threshold");
131 if (oldcs instanceof AnnotationColourGradient)
133 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
134 annotations.setSelectedItem(acg.getAnnotation());
135 switch (acg.getAboveThreshold()) {
136 case AnnotationColourGradient.NO_THRESHOLD:
137 threshold.setSelectedItem("No Threshold");
139 case AnnotationColourGradient.ABOVE_THRESHOLD:
140 threshold.setSelectedItem("Above Threshold");
142 case AnnotationColourGradient.BELOW_THRESHOLD:
143 threshold.setSelectedItem("Below Threshold");
146 throw new Error("Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
148 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
149 thresholdValue.setText(""+acg.getAnnotationThreshold());
155 } catch (Exception ex)
166 public AnnotationColourChooser()
171 } catch (Exception ex)
173 ex.printStackTrace();
177 private void jbInit() throws Exception
179 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
180 minColour.setBorder(BorderFactory.createEtchedBorder());
181 minColour.setPreferredSize(new Dimension(40, 20));
182 minColour.setToolTipText("Minimum Colour");
183 minColour.addMouseListener(new MouseAdapter()
185 public void mousePressed(MouseEvent e)
187 if (minColour.isEnabled())
189 minColour_actionPerformed();
193 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
194 maxColour.setBorder(BorderFactory.createEtchedBorder());
195 maxColour.setPreferredSize(new Dimension(40, 20));
196 maxColour.setToolTipText("Maximum Colour");
197 maxColour.addMouseListener(new MouseAdapter()
199 public void mousePressed(MouseEvent e)
201 if (maxColour.isEnabled())
203 maxColour_actionPerformed();
209 ok.addActionListener(new ActionListener()
211 public void actionPerformed(ActionEvent e)
213 ok_actionPerformed(e);
216 cancel.setOpaque(false);
217 cancel.setText("Cancel");
218 cancel.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 cancel_actionPerformed(e);
225 this.setLayout(borderLayout1);
226 jPanel2.setLayout(flowLayout1);
227 annotations.addActionListener(new ActionListener()
229 public void actionPerformed(ActionEvent e)
231 annotations_actionPerformed(e);
234 jPanel1.setBackground(Color.white);
235 jPanel2.setBackground(Color.white);
236 threshold.addActionListener(new ActionListener()
238 public void actionPerformed(ActionEvent e)
240 threshold_actionPerformed(e);
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 thresholdValue.setEnabled(false);
258 thresholdValue.setColumns(7);
259 jPanel3.setBackground(Color.white);
260 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
261 currentColours.setOpaque(false);
262 currentColours.setText("Use Original Colours");
263 currentColours.addActionListener(new ActionListener()
265 public void actionPerformed(ActionEvent e)
267 currentColours_actionPerformed(e);
270 thresholdIsMin.setBackground(Color.white);
271 thresholdIsMin.setText("Threshold is Min/Max");
272 thresholdIsMin.addActionListener(new ActionListener()
274 public void actionPerformed(ActionEvent actionEvent)
276 thresholdIsMin_actionPerformed(actionEvent);
281 jPanel2.add(annotations);
282 jPanel2.add(currentColours);
283 jPanel2.add(minColour);
284 jPanel2.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);
294 JComboBox annotations;
296 JPanel minColour = new JPanel();
298 JPanel maxColour = new JPanel();
300 JButton ok = new JButton();
302 JButton cancel = new JButton();
304 JPanel jPanel1 = new JPanel();
306 JPanel jPanel2 = new JPanel();
308 BorderLayout borderLayout1 = new BorderLayout();
310 JComboBox threshold = new JComboBox();
312 FlowLayout flowLayout1 = new FlowLayout();
314 JPanel jPanel3 = new JPanel();
316 FlowLayout flowLayout2 = new FlowLayout();
318 JSlider slider = new JSlider();
320 JTextField thresholdValue = new JTextField(20);
322 JCheckBox currentColours = new JCheckBox();
324 JCheckBox thresholdIsMin = new JCheckBox();
326 public void minColour_actionPerformed()
328 Color col = JColorChooser.showDialog(this,
329 "Select Colour for Minimum Value", minColour.getBackground());
332 minColour.setBackground(col);
338 public void maxColour_actionPerformed()
340 Color col = JColorChooser.showDialog(this,
341 "Select Colour for Maximum Value", maxColour.getBackground());
344 maxColour.setBackground(col);
352 // Check if combobox is still adjusting
358 currentAnnotation = av.alignment.getAlignmentAnnotation()[annotations
359 .getSelectedIndex()];
361 int aboveThreshold = -1;
362 if (threshold.getSelectedItem().equals("Above Threshold"))
364 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
366 else if (threshold.getSelectedItem().equals("Below Threshold"))
368 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
371 slider.setEnabled(true);
372 thresholdValue.setEnabled(true);
374 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
376 slider.setEnabled(false);
377 thresholdValue.setEnabled(false);
378 thresholdValue.setText("");
380 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
381 && currentAnnotation.threshold == null)
384 .setThreshold(new jalview.datamodel.GraphLine(
385 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
386 "Threshold", Color.black));
389 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
392 float range = currentAnnotation.graphMax * 1000
393 - currentAnnotation.graphMin * 1000;
395 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
396 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
397 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
398 thresholdValue.setText(currentAnnotation.threshold.value + "");
399 slider.setMajorTickSpacing((int) (range / 10f));
400 slider.setEnabled(true);
401 thresholdValue.setEnabled(true);
405 AnnotationColourGradient acg = null;
406 if (currentColours.isSelected())
408 acg = new AnnotationColourGradient(currentAnnotation,
409 av.getGlobalColourScheme(), aboveThreshold);
413 acg = new AnnotationColourGradient(currentAnnotation,
414 minColour.getBackground(), maxColour.getBackground(),
418 if (currentAnnotation.graphMin == 0f
419 && currentAnnotation.graphMax == 0f)
421 acg.predefinedColours = true;
424 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
426 av.setGlobalColourScheme(acg);
428 if (av.alignment.getGroups() != null)
430 Vector allGroups = ap.av.alignment.getGroups();
432 for (int g = 0; g < allGroups.size(); g++)
434 sg = (SequenceGroup) allGroups.get(g);
441 if (currentColours.isSelected())
443 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
448 sg.cs = new AnnotationColourGradient(currentAnnotation,
449 minColour.getBackground(), maxColour.getBackground(),
455 // ensure all associated views (overviews, structures, etc) are notified of updated colours.
456 ap.paintAlignment(true);
459 public void ok_actionPerformed(ActionEvent e)
464 frame.setClosed(true);
465 } catch (Exception ex)
470 public void cancel_actionPerformed(ActionEvent e)
475 frame.setClosed(true);
476 } catch (Exception ex)
483 av.setGlobalColourScheme(oldcs);
484 if (av.alignment.getGroups() != null)
486 Vector allGroups = ap.av.alignment.getGroups();
488 for (int g = 0; g < allGroups.size(); g++)
490 sg = (SequenceGroup) allGroups.get(g);
491 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
496 public void thresholdCheck_actionPerformed(ActionEvent e)
501 public void annotations_actionPerformed(ActionEvent e)
506 public void threshold_actionPerformed(ActionEvent e)
511 public void thresholdValue_actionPerformed(ActionEvent e)
515 float f = Float.parseFloat(thresholdValue.getText());
516 slider.setValue((int) (f * 1000));
517 } catch (NumberFormatException ex)
522 public void valueChanged()
524 if (currentColours.isSelected()
525 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
530 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
531 ap.paintAlignment(false);
534 public void currentColours_actionPerformed(ActionEvent e)
536 if (currentColours.isSelected())
541 maxColour.setEnabled(!currentColours.isSelected());
542 minColour.setEnabled(!currentColours.isSelected());
547 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)