2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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.*;
26 import javax.swing.event.*;
28 import net.miginfocom.swing.MigLayout;
30 import jalview.bin.Cache;
31 import jalview.datamodel.*;
32 import jalview.schemes.*;
33 import java.awt.Dimension;
35 public class AnnotationColourChooser extends JPanel
45 Hashtable oldgroupColours;
47 jalview.datamodel.AlignmentAnnotation currentAnnotation;
49 boolean adjusting = false;
51 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
53 oldcs = av.getGlobalColourScheme();
54 if (av.alignment.getGroups() != null)
56 oldgroupColours = new Hashtable();
57 Vector allGroups = ap.av.alignment.getGroups();
59 for (int g = 0; g < allGroups.size(); g++)
61 sg = (SequenceGroup) allGroups.get(g);
64 oldgroupColours.put(sg, sg.cs);
70 frame = new JInternalFrame();
71 frame.setContentPane(this);
72 frame.setLayer(JLayeredPane.PALETTE_LAYER);
73 Desktop.addInternalFrame(frame, "Colour by Annotation", 520, 215);
75 slider.addChangeListener(new ChangeListener()
77 public void stateChanged(ChangeEvent evt)
81 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
86 slider.addMouseListener(new MouseAdapter()
88 public void mouseReleased(MouseEvent evt)
90 ap.paintAlignment(true);
94 if (av.alignment.getAlignmentAnnotation() == null)
99 // Always get default shading from preferences.
102 if (oldcs instanceof AnnotationColourGradient)
104 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
105 currentColours.setSelected(acg.predefinedColours);
106 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 private void setDefaultMinMax()
168 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
169 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
172 public AnnotationColourChooser()
177 } catch (Exception ex)
179 ex.printStackTrace();
183 private void jbInit() throws Exception
185 minColour.setFont(JvSwingUtils.getLabelFont());
186 minColour.setBorder(BorderFactory.createEtchedBorder());
187 minColour.setPreferredSize(new Dimension(40, 20));
188 minColour.setToolTipText("Minimum Colour");
189 minColour.addMouseListener(new MouseAdapter()
191 public void mousePressed(MouseEvent e)
193 if (minColour.isEnabled())
195 minColour_actionPerformed();
199 maxColour.setFont(JvSwingUtils.getLabelFont());
200 maxColour.setBorder(BorderFactory.createEtchedBorder());
201 maxColour.setPreferredSize(new Dimension(40, 20));
202 maxColour.setToolTipText("Maximum Colour");
203 maxColour.addMouseListener(new MouseAdapter()
205 public void mousePressed(MouseEvent e)
207 if (maxColour.isEnabled())
209 maxColour_actionPerformed();
215 ok.addActionListener(new ActionListener()
217 public void actionPerformed(ActionEvent e)
219 ok_actionPerformed(e);
222 cancel.setOpaque(false);
223 cancel.setText("Cancel");
224 cancel.addActionListener(new ActionListener()
226 public void actionPerformed(ActionEvent e)
228 cancel_actionPerformed(e);
231 defColours.setOpaque(false);
232 defColours.setText("Defaults");
233 defColours.setToolTipText("Reset min and max colours to defaults from user preferences.");
234 defColours.addActionListener(new ActionListener()
238 public void actionPerformed(ActionEvent arg0)
240 resetColours_actionPerformed(arg0);
244 annotations.addActionListener(new ActionListener()
246 public void actionPerformed(ActionEvent e)
248 annotations_actionPerformed(e);
251 threshold.addActionListener(new ActionListener()
253 public void actionPerformed(ActionEvent e)
255 threshold_actionPerformed(e);
258 thresholdValue.addActionListener(new ActionListener()
260 public void actionPerformed(ActionEvent e)
262 thresholdValue_actionPerformed(e);
265 slider.setPaintLabels(false);
266 slider.setPaintTicks(true);
267 slider.setBackground(Color.white);
268 slider.setEnabled(false);
269 slider.setOpaque(false);
270 slider.setPreferredSize(new Dimension(100, 32));
271 thresholdValue.setEnabled(false);
272 thresholdValue.setColumns(7);
273 currentColours.setFont(JvSwingUtils.getLabelFont());
274 currentColours.setOpaque(false);
275 currentColours.setText("Use Original Colours");
276 currentColours.addActionListener(new ActionListener()
278 public void actionPerformed(ActionEvent e)
280 currentColours_actionPerformed(e);
283 thresholdIsMin.setBackground(Color.white);
284 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
285 thresholdIsMin.setText("Threshold is Min/Max");
286 thresholdIsMin.addActionListener(new ActionListener()
288 public void actionPerformed(ActionEvent actionEvent)
290 thresholdIsMin_actionPerformed(actionEvent);
293 this.setLayout(borderLayout1);
294 jPanel2.setLayout(new MigLayout("","[left][center][right]","[][][]"));
295 jPanel1.setBackground(Color.white);
296 jPanel2.setBackground(Color.white);
300 jPanel2.add(annotations);
301 jPanel2.add(currentColours);
302 JPanel colpanel = new JPanel(new FlowLayout());
303 colpanel.setBackground(Color.white);
304 colpanel.add(minColour);
305 colpanel.add(maxColour);
306 jPanel2.add(colpanel, "wrap");
308 jPanel2.add(threshold);
309 jPanel2.add(defColours,"skip 1, wrap");
310 jPanel2.add(thresholdIsMin);
311 jPanel2.add(slider, "grow");
312 jPanel2.add(thresholdValue, "grow");
313 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
314 this.add(jPanel2, java.awt.BorderLayout.CENTER);
317 protected void resetColours_actionPerformed(ActionEvent arg0)
323 JComboBox annotations;
325 JPanel minColour = new JPanel();
327 JPanel maxColour = new JPanel();
328 JButton defColours = new JButton();
329 JButton ok = new JButton();
331 JButton cancel = new JButton();
333 JPanel jPanel1 = new JPanel();
334 JPanel jPanel2 = new JPanel();
336 BorderLayout borderLayout1 = new BorderLayout();
338 JComboBox threshold = new JComboBox();
341 JSlider slider = new JSlider();
343 JTextField thresholdValue = new JTextField(20);
345 JCheckBox currentColours = new JCheckBox();
347 JCheckBox thresholdIsMin = new JCheckBox();
349 public void minColour_actionPerformed()
351 Color col = JColorChooser.showDialog(this,
352 "Select Colour for Minimum Value", minColour.getBackground());
355 minColour.setBackground(col);
361 public void maxColour_actionPerformed()
363 Color col = JColorChooser.showDialog(this,
364 "Select Colour for Maximum Value", maxColour.getBackground());
367 maxColour.setBackground(col);
375 // Check if combobox is still adjusting
381 currentAnnotation = av.alignment.getAlignmentAnnotation()[annotations
382 .getSelectedIndex()];
384 int aboveThreshold = -1;
385 if (threshold.getSelectedItem().equals("Above Threshold"))
387 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
389 else if (threshold.getSelectedItem().equals("Below Threshold"))
391 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
394 slider.setEnabled(true);
395 thresholdValue.setEnabled(true);
396 thresholdIsMin.setEnabled(true);
398 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
400 slider.setEnabled(false);
401 thresholdValue.setEnabled(false);
402 thresholdValue.setText("");
403 thresholdIsMin.setEnabled(false);
405 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
406 && currentAnnotation.threshold == null)
409 .setThreshold(new jalview.datamodel.GraphLine(
410 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
411 "Threshold", Color.black));
414 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
417 float range = currentAnnotation.graphMax * 1000
418 - currentAnnotation.graphMin * 1000;
420 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
421 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
422 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
423 thresholdValue.setText(currentAnnotation.threshold.value + "");
424 slider.setMajorTickSpacing((int) (range / 10f));
425 slider.setEnabled(true);
426 thresholdValue.setEnabled(true);
430 AnnotationColourGradient acg = null;
431 if (currentColours.isSelected())
433 acg = new AnnotationColourGradient(currentAnnotation,
434 av.getGlobalColourScheme(), aboveThreshold);
438 acg = new AnnotationColourGradient(currentAnnotation,
439 minColour.getBackground(), maxColour.getBackground(),
443 if (currentAnnotation.graphMin == 0f
444 && currentAnnotation.graphMax == 0f)
446 acg.predefinedColours = true;
449 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
451 av.setGlobalColourScheme(acg);
453 if (av.alignment.getGroups() != null)
455 Vector allGroups = ap.av.alignment.getGroups();
457 for (int g = 0; g < allGroups.size(); g++)
459 sg = (SequenceGroup) allGroups.get(g);
466 if (currentColours.isSelected())
468 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
473 sg.cs = new AnnotationColourGradient(currentAnnotation,
474 minColour.getBackground(), maxColour.getBackground(),
480 // ensure all associated views (overviews, structures, etc) are notified of updated colours.
481 ap.paintAlignment(true);
484 public void ok_actionPerformed(ActionEvent e)
489 frame.setClosed(true);
490 } catch (Exception ex)
495 public void cancel_actionPerformed(ActionEvent e)
498 // ensure all original colouring is propagated to listeners.
499 ap.paintAlignment(true);
502 frame.setClosed(true);
503 } catch (Exception ex)
510 av.setGlobalColourScheme(oldcs);
511 if (av.alignment.getGroups() != null)
513 Vector allGroups = ap.av.alignment.getGroups();
515 for (int g = 0; g < allGroups.size(); g++)
517 sg = (SequenceGroup) allGroups.get(g);
518 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
523 public void thresholdCheck_actionPerformed(ActionEvent e)
528 public void annotations_actionPerformed(ActionEvent e)
533 public void threshold_actionPerformed(ActionEvent e)
538 public void thresholdValue_actionPerformed(ActionEvent e)
542 float f = Float.parseFloat(thresholdValue.getText());
543 slider.setValue((int) (f * 1000));
544 } catch (NumberFormatException ex)
549 public void valueChanged()
551 if (currentColours.isSelected()
552 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
557 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
558 ap.paintAlignment(false);
561 public void currentColours_actionPerformed(ActionEvent e)
563 if (currentColours.isSelected())
568 maxColour.setEnabled(!currentColours.isSelected());
569 minColour.setEnabled(!currentColours.isSelected());
574 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)