updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
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.
9 *
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.
14 *
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
18 */
19 package jalview.gui;
20
21 import javax.swing.*;
22 import java.awt.event.*;
23 import java.awt.*;
24
25 import jalview.schemes.*;
26 import javax.swing.event.*;
27 import java.util.*;
28 import jalview.datamodel.SequenceGroup;
29
30 public class AnnotationColourChooser
31     extends JPanel
32 {
33   JInternalFrame frame;
34   AlignViewport av;
35   AlignmentPanel ap;
36   ColourSchemeI oldcs;
37   Hashtable oldgroupColours;
38   jalview.datamodel.AlignmentAnnotation currentAnnotation;
39   boolean adjusting = false;
40
41   public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
42   {
43     oldcs = av.getGlobalColourScheme();
44     if (av.alignment.getGroups() != null)
45     {
46       oldgroupColours = new Hashtable();
47       Vector allGroups = ap.av.alignment.getGroups();
48       SequenceGroup sg;
49       for (int g = 0; g < allGroups.size(); g++)
50       {
51         sg = (SequenceGroup) allGroups.get(g);
52         if(sg.cs!=null)
53           oldgroupColours.put(sg, sg.cs);
54       }
55     }
56     this.av = av;
57     this.ap = ap;
58     frame = new JInternalFrame();
59     frame.setContentPane(this);
60     frame.setLayer(JLayeredPane.PALETTE_LAYER);
61     Desktop.addInternalFrame(frame, "Colour by Annotation", 480, 145);
62
63     try
64     {
65       jbInit();
66     }
67     catch (Exception ex)
68     {}
69
70     slider.addChangeListener(new ChangeListener()
71     {
72       public void stateChanged(ChangeEvent evt)
73       {
74         if(!adjusting)
75         {
76           thresholdValue.setText( ( (float) slider.getValue() / 1000f) + "");
77           valueChanged();
78         }
79       }
80     });
81
82     if (av.alignment.getAlignmentAnnotation() == null)
83       return;
84
85     if (oldcs instanceof AnnotationColourGradient)
86     {
87       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
88       minColour.setBackground(acg.getMinColour());
89       maxColour.setBackground(acg.getMaxColour());
90     }
91     else
92     {
93       minColour.setBackground(Color.orange);
94       maxColour.setBackground(Color.red);
95     }
96
97     adjusting = true;
98     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
99     {
100       if (av.alignment.getAlignmentAnnotation()[i].graph > 0)
101         annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);
102     }
103
104     threshold.addItem("No Threshold");
105     threshold.addItem("Above Threshold");
106     threshold.addItem("Below Threshold");
107
108     adjusting = false;
109
110     changeColour();
111
112   }
113
114   public AnnotationColourChooser()
115   {
116     try
117     {
118       jbInit();
119     }
120     catch (Exception ex)
121     {
122       ex.printStackTrace();
123     }
124   }
125
126   private void jbInit()
127       throws Exception
128   {
129     minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
130     minColour.setToolTipText("");
131     minColour.setMargin(new Insets(2, 2, 2, 2));
132     minColour.setText("Min Colour");
133     minColour.addActionListener(new ActionListener()
134     {
135       public void actionPerformed(ActionEvent e)
136       {
137         minColour_actionPerformed(e);
138       }
139     });
140     maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
141     maxColour.setMargin(new Insets(2, 2, 2, 2));
142     maxColour.setText("Max Colour");
143     maxColour.addActionListener(new ActionListener()
144     {
145       public void actionPerformed(ActionEvent e)
146       {
147         maxColour_actionPerformed(e);
148       }
149     });
150     ok.setOpaque(false);
151     ok.setText("OK");
152     ok.addActionListener(new ActionListener()
153     {
154       public void actionPerformed(ActionEvent e)
155       {
156         ok_actionPerformed(e);
157       }
158     });
159     cancel.setOpaque(false);
160     cancel.setText("Cancel");
161     cancel.addActionListener(new ActionListener()
162     {
163       public void actionPerformed(ActionEvent e)
164       {
165         cancel_actionPerformed(e);
166       }
167     });
168     this.setLayout(borderLayout1);
169     jPanel2.setLayout(flowLayout1);
170     annotations.addActionListener(new ActionListener()
171     {
172       public void actionPerformed(ActionEvent e)
173       {
174         annotations_actionPerformed(e);
175       }
176     });
177     jPanel1.setBackground(Color.white);
178     jPanel2.setBackground(Color.white);
179     threshold.addActionListener(new ActionListener()
180     {
181       public void actionPerformed(ActionEvent e)
182       {
183         threshold_actionPerformed(e);
184       }
185     });
186     jPanel3.setLayout(flowLayout2);
187     thresholdValue.addActionListener(new ActionListener()
188     {
189       public void actionPerformed(ActionEvent e)
190       {
191         thresholdValue_actionPerformed(e);
192       }
193     });
194     slider.setPaintLabels(false);
195     slider.setPaintTicks(true);
196     slider.setBackground(Color.white);
197     slider.setEnabled(false);
198     slider.setOpaque(false);
199     slider.setPreferredSize(new Dimension(150, 32));
200     thresholdValue.setEnabled(false);
201     thresholdValue.setColumns(10);
202     jPanel3.setBackground(Color.white);
203     currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
204     currentColours.setOpaque(false);
205     currentColours.setText("Use Original Colours");
206     currentColours.addActionListener(new ActionListener()
207     {
208       public void actionPerformed(ActionEvent e)
209       {
210         currentColours_actionPerformed(e);
211       }
212     });
213     jPanel1.add(ok);
214     jPanel1.add(cancel);
215     jPanel2.add(annotations);
216     jPanel2.add(currentColours);
217     jPanel2.add(minColour);
218     jPanel2.add(maxColour);
219     this.add(jPanel3, java.awt.BorderLayout.CENTER);
220     jPanel3.add(threshold);
221     jPanel3.add(slider);
222     jPanel3.add(thresholdValue);
223     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
224     this.add(jPanel2, java.awt.BorderLayout.NORTH);
225   }
226
227   JComboBox annotations = new JComboBox();
228   JButton minColour = new JButton();
229   JButton maxColour = new JButton();
230   JButton ok = new JButton();
231   JButton cancel = new JButton();
232   JPanel jPanel1 = new JPanel();
233   JPanel jPanel2 = new JPanel();
234   BorderLayout borderLayout1 = new BorderLayout();
235   JComboBox threshold = new JComboBox();
236   FlowLayout flowLayout1 = new FlowLayout();
237   JPanel jPanel3 = new JPanel();
238   FlowLayout flowLayout2 = new FlowLayout();
239   JSlider slider = new JSlider();
240   JTextField thresholdValue = new JTextField(20);
241   JCheckBox currentColours = new JCheckBox();
242
243   public void minColour_actionPerformed(ActionEvent e)
244   {
245     Color col = JColorChooser.showDialog(this,
246                                          "Select Colour for Minimum Value",
247                                          minColour.getBackground());
248     if (col != null)
249       minColour.setBackground(col);
250     minColour.repaint();
251     changeColour();
252   }
253
254   public void maxColour_actionPerformed(ActionEvent e)
255   {
256     Color col = JColorChooser.showDialog(this,
257                                          "Select Colour for Maximum Value",
258                                          maxColour.getBackground());
259     if (col != null)
260       maxColour.setBackground(col);
261     maxColour.repaint();
262     changeColour();
263   }
264
265   void changeColour()
266   {
267     // Check if combobox is still adjusting
268     if (adjusting)
269       return;
270
271     // We removed the non-graph annotations when filling the combobox
272     // so allow for them again here
273     int nograph = 0, graph = -1;
274     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
275     {
276       if (av.alignment.getAlignmentAnnotation()[i].graph == 0)
277         nograph++;
278       else
279         graph++;
280
281       if (graph == annotations.getSelectedIndex())
282         break;
283     }
284
285     currentAnnotation = av.alignment.getAlignmentAnnotation()[graph + nograph];
286
287     int aboveThreshold = -1;
288     if (threshold.getSelectedItem().equals("Above Threshold"))
289       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
290     else if (threshold.getSelectedItem().equals("Below Threshold"))
291       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
292
293     slider.setEnabled(true);
294     thresholdValue.setEnabled(true);
295
296     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
297     {
298       slider.setEnabled(false);
299       thresholdValue.setEnabled(false);
300       thresholdValue.setText("");
301     }
302     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD &&
303              currentAnnotation.threshold == null)
304     {
305       currentAnnotation.setThreshold(new jalview.datamodel.GraphLine
306                                      ( (currentAnnotation.graphMax -
307                                         currentAnnotation.graphMin) / 2f,
308                                       "Threshold",
309                                       Color.black));
310     }
311
312     if(aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
313     {
314       adjusting = true;
315       float range = currentAnnotation.graphMax * 1000 -
316           currentAnnotation.graphMin * 1000;
317
318       slider.setMinimum( (int) (currentAnnotation.graphMin * 1000));
319       slider.setMaximum( (int) (currentAnnotation.graphMax * 1000));
320       slider.setValue( (int) (currentAnnotation.threshold.value * 1000));
321       thresholdValue.setText(currentAnnotation.threshold.value + "");
322       slider.setMajorTickSpacing( (int) (range / 10f));
323       slider.setEnabled(true);
324       thresholdValue.setEnabled(true);
325       adjusting = false;
326     }
327
328     AnnotationColourGradient acg = null;
329     if (currentColours.isSelected())
330       acg = new AnnotationColourGradient(
331           currentAnnotation,
332           av.getGlobalColourScheme(), aboveThreshold);
333     else
334       acg =
335           new AnnotationColourGradient(
336               currentAnnotation,
337               minColour.getBackground(),
338               maxColour.getBackground(),
339               aboveThreshold);
340
341     av.setGlobalColourScheme(acg);
342
343     if (av.alignment.getGroups() != null)
344     {
345       Vector allGroups = ap.av.alignment.getGroups();
346       SequenceGroup sg;
347       for (int g = 0; g < allGroups.size(); g++)
348       {
349         sg = (SequenceGroup) allGroups.get(g);
350
351         if (sg.cs == null)
352         {
353           continue;
354         }
355
356         if (currentColours.isSelected())
357           sg.cs = new AnnotationColourGradient(
358               currentAnnotation,
359               sg.cs, aboveThreshold);
360         else
361           sg.cs = new AnnotationColourGradient(
362               currentAnnotation,
363               minColour.getBackground(),
364               maxColour.getBackground(),
365               aboveThreshold);
366
367       }
368     }
369
370     ap.repaint();
371   }
372
373   public void ok_actionPerformed(ActionEvent e)
374   {
375     changeColour();
376     try
377     {
378       frame.setClosed(true);
379     }
380     catch (Exception ex)
381     {}
382   }
383
384   public void cancel_actionPerformed(ActionEvent e)
385   {
386     reset();
387     try
388     {
389       frame.setClosed(true);
390     }
391     catch (Exception ex)
392     {}
393   }
394
395
396   void reset()
397   {
398     av.setGlobalColourScheme(oldcs);
399     if (av.alignment.getGroups() != null)
400     {
401       Vector allGroups = ap.av.alignment.getGroups();
402       SequenceGroup sg;
403       for (int g = 0; g < allGroups.size(); g++)
404       {
405         sg = (SequenceGroup) allGroups.get(g);
406         sg.cs = (ColourSchemeI)oldgroupColours.get(sg);
407       }
408     }
409   }
410
411   public void thresholdCheck_actionPerformed(ActionEvent e)
412   {
413     changeColour();
414   }
415
416   public void annotations_actionPerformed(ActionEvent e)
417   {
418     changeColour();
419   }
420
421   public void threshold_actionPerformed(ActionEvent e)
422   {
423     changeColour();
424   }
425
426   public void thresholdValue_actionPerformed(ActionEvent e)
427   {
428     try
429     {
430       float f = Float.parseFloat(thresholdValue.getText());
431       slider.setValue( (int) (f * 1000));
432     }
433     catch (NumberFormatException ex)
434     {}
435   }
436
437   public void valueChanged()
438   {
439     if (currentColours.isSelected()
440         && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
441     {
442       changeColour();
443     }
444
445     currentAnnotation.threshold.value = (float)slider.getValue()/1000f;
446     ap.repaint();
447   }
448
449   public void currentColours_actionPerformed(ActionEvent e)
450   {
451     if(currentColours.isSelected())
452     {
453       reset();
454     }
455
456     maxColour.setEnabled(!currentColours.isSelected());
457     minColour.setEnabled(!currentColours.isSelected());
458
459     changeColour();
460   }
461
462 }