updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / appletgui / 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.appletgui;
20
21 import java.awt.event.*;
22 import java.awt.*;
23
24 import jalview.schemes.*;
25 import java.util.*;
26 import jalview.datamodel.SequenceGroup;
27
28 public class AnnotationColourChooser extends Panel implements ActionListener,
29     AdjustmentListener, ItemListener
30 {
31   Frame frame;
32   AlignViewport av;
33   AlignmentPanel ap;
34   ColourSchemeI oldcs;
35   Hashtable oldgroupColours;
36   jalview.datamodel.AlignmentAnnotation currentAnnotation;
37   boolean adjusting = false;
38
39   public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
40   {
41     try
42     {
43       jbInit();
44     }
45     catch (Exception ex)
46     {}
47
48     oldcs = av.getGlobalColourScheme();
49     if (av.alignment.getGroups() != null)
50     {
51       oldgroupColours = new Hashtable();
52       Vector allGroups = ap.av.alignment.getGroups();
53       SequenceGroup sg;
54       for (int g = 0; g < allGroups.size(); g++)
55       {
56         sg = (SequenceGroup) allGroups.elementAt(g);
57         oldgroupColours.put(sg, sg.cs);
58       }
59     }
60     this.av = av;
61     this.ap = ap;
62
63     slider.addAdjustmentListener(this);
64
65     if (av.alignment.getAlignmentAnnotation() == null)
66       return;
67
68     if (oldcs instanceof AnnotationColourGradient)
69     {
70       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
71       minColour.setBackground(acg.getMinColour());
72       maxColour.setBackground(acg.getMaxColour());
73     }
74     else
75     {
76       minColour.setBackground(Color.orange);
77       maxColour.setBackground(Color.red);
78     }
79
80     adjusting = true;
81     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
82     {
83       if (av.alignment.getAlignmentAnnotation()[i].graph > 0)
84         annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);
85     }
86
87     threshold.addItem("No Threshold");
88     threshold.addItem("Above Threshold");
89     threshold.addItem("Below Threshold");
90
91     adjusting = false;
92
93     changeColour();
94
95     frame = new Frame();
96     frame.add(this);
97     jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 480, 145);
98     validate();
99   }
100
101   public AnnotationColourChooser()
102   {
103     try
104     {
105       jbInit();
106     }
107     catch (Exception ex)
108     {
109       ex.printStackTrace();
110     }
111   }
112
113   private void jbInit()
114       throws Exception
115   {
116     minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
117     minColour.setLabel("Min Colour");
118     minColour.addActionListener(this);
119
120     maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
121     maxColour.setLabel("Max Colour");
122     maxColour.addActionListener(this);
123
124     ok.setLabel("OK");
125     ok.addActionListener(this);
126
127     cancel.setLabel("Cancel");
128     cancel.addActionListener(this);
129
130     this.setLayout(borderLayout1);
131     jPanel2.setLayout(flowLayout1);
132     annotations.addItemListener(this);
133
134     jPanel1.setBackground(Color.white);
135     jPanel2.setBackground(Color.white);
136     threshold.addItemListener(this);
137     jPanel3.setLayout(null);
138     thresholdValue.addActionListener(this);
139
140     slider.setBackground(Color.white);
141     slider.setEnabled(false);
142     slider.setBounds(new Rectangle(172, 7, 120, 16));
143     thresholdValue.setEnabled(false);
144     thresholdValue.setBounds(new Rectangle(295, 4, 83, 22));
145     thresholdValue.setColumns(10);
146     jPanel3.setBackground(Color.white);
147     currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
148     currentColours.setLabel("Use Original Colours");
149     currentColours.addItemListener(this);
150
151     threshold.setBounds(new Rectangle(19, 4, 141, 22));
152     jPanel1.add(ok);
153     jPanel1.add(cancel);
154     jPanel2.add(annotations);
155     jPanel2.add(currentColours);
156     jPanel2.add(minColour);
157     jPanel2.add(maxColour);
158     jPanel3.add(threshold);
159     jPanel3.add(slider);
160     jPanel3.add(thresholdValue);
161     this.add(jPanel2, java.awt.BorderLayout.NORTH);
162     this.add(jPanel3, java.awt.BorderLayout.CENTER);
163     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
164   }
165
166   Choice annotations = new Choice();
167   Button minColour = new Button();
168   Button maxColour = new Button();
169   Button ok = new Button();
170   Button cancel = new Button();
171   Panel jPanel1 = new Panel();
172   Panel jPanel2 = new Panel();
173   Choice threshold = new Choice();
174   FlowLayout flowLayout1 = new FlowLayout();
175   Panel jPanel3 = new Panel();
176   Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
177   TextField thresholdValue = new TextField(20);
178   Checkbox currentColours = new Checkbox();
179   BorderLayout borderLayout1 = new BorderLayout();
180
181
182   public void actionPerformed(ActionEvent evt)
183   {
184     if(evt.getSource() == thresholdValue)
185     {
186       try
187       {
188         float f = new Float(thresholdValue.getText()).floatValue();
189         slider.setValue( (int) (f * 1000));
190         adjustmentValueChanged(null);
191       }
192       catch (NumberFormatException ex)
193       {}
194     }
195     else if (evt.getSource() == minColour)
196       minColour_actionPerformed(null);
197     else if (evt.getSource() == maxColour)
198       maxColour_actionPerformed(null);
199
200     else if (evt.getSource() == ok)
201     {
202       changeColour();
203       frame.setVisible(false);
204     }
205     else if (evt.getSource() == cancel)
206     {
207       reset();
208       ap.repaint();
209       frame.setVisible(false);
210     }
211
212     else
213       changeColour();
214   }
215
216
217   public void itemStateChanged(ItemEvent evt)
218   {
219     if (evt.getSource() == currentColours)
220     {
221       if (currentColours.getState())
222       {
223         reset();
224       }
225
226       maxColour.setEnabled(!currentColours.getState());
227       minColour.setEnabled(!currentColours.getState());
228
229     }
230
231     changeColour();
232   }
233
234   public void adjustmentValueChanged(AdjustmentEvent evt)
235   {
236     if (!adjusting)
237     {
238       thresholdValue.setText( ( (float) slider.getValue() / 1000f) + "");
239       if (currentColours.getState()
240           && ! (av.getGlobalColourScheme() instanceof AnnotationColourGradient))
241       {
242         changeColour();
243       }
244
245       currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
246       ap.repaint();
247     }
248   }
249
250   public void minColour_actionPerformed(Color newCol)
251   {
252     if (newCol != null)
253     {
254       minColour.setBackground(newCol);
255       minColour.repaint();
256       changeColour();
257     }
258     else
259       new UserDefinedColours(this, "Min Colour",
260                               minColour.getBackground());
261
262   }
263
264   public void maxColour_actionPerformed(Color newCol)
265   {
266     if (newCol != null)
267     {
268       maxColour.setBackground(newCol);
269       maxColour.repaint();
270       changeColour();
271     }
272     else
273       new UserDefinedColours(this, "Max Colour",
274                               maxColour.getBackground());
275   }
276
277
278   void changeColour()
279   {
280     // Check if combobox is still adjusting
281     if (adjusting)
282       return;
283
284     // We removed the non-graph annotations when filling the combobox
285     // so allow for them again here
286     int nograph = 0, graph = -1;
287     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
288     {
289       if (av.alignment.getAlignmentAnnotation()[i].graph == 0)
290         nograph++;
291       else
292         graph++;
293
294       if (graph == annotations.getSelectedIndex())
295         break;
296     }
297
298     currentAnnotation = av.alignment.getAlignmentAnnotation()[graph + nograph];
299
300     int aboveThreshold = -1;
301     if (threshold.getSelectedItem().equals("Above Threshold"))
302       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
303     else if (threshold.getSelectedItem().equals("Below Threshold"))
304       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
305
306     slider.setEnabled(true);
307     thresholdValue.setEnabled(true);
308
309     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
310     {
311       slider.setEnabled(false);
312       thresholdValue.setEnabled(false);
313       thresholdValue.setText("");
314     }
315     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD &&
316              currentAnnotation.threshold == null)
317     {
318       currentAnnotation.setThreshold(new jalview.datamodel.GraphLine
319                                      ( (currentAnnotation.graphMax -
320                                         currentAnnotation.graphMin) / 2f,
321                                       "Threshold",
322                                       Color.black));
323     }
324
325     if(aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
326     {
327       adjusting = true;
328
329       slider.setMinimum( (int) (currentAnnotation.graphMin * 1000));
330       slider.setMaximum( (int) (currentAnnotation.graphMax * 1000));
331       slider.setValue( (int) (currentAnnotation.threshold.value * 1000));
332       thresholdValue.setText(currentAnnotation.threshold.value + "");
333       slider.setEnabled(true);
334       thresholdValue.setEnabled(true);
335       adjusting = false;
336     }
337
338     AnnotationColourGradient acg = null;
339     if (currentColours.getState())
340       acg = new AnnotationColourGradient(
341           currentAnnotation,
342           av.getGlobalColourScheme(), aboveThreshold);
343     else
344       acg =
345           new AnnotationColourGradient(
346               currentAnnotation,
347               minColour.getBackground(),
348               maxColour.getBackground(),
349               aboveThreshold);
350
351     av.setGlobalColourScheme(acg);
352
353     if (av.alignment.getGroups() != null)
354     {
355       Vector allGroups = ap.av.alignment.getGroups();
356       SequenceGroup sg;
357       for (int g = 0; g < allGroups.size(); g++)
358       {
359         sg = (SequenceGroup) allGroups.elementAt(g);
360
361         if (sg.cs == null)
362         {
363           continue;
364         }
365
366         if (currentColours.getState())
367           sg.cs = new AnnotationColourGradient(
368               currentAnnotation,
369               sg.cs, aboveThreshold);
370         else
371           sg.cs = new AnnotationColourGradient(
372               currentAnnotation,
373               minColour.getBackground(),
374               maxColour.getBackground(),
375               aboveThreshold);
376
377       }
378     }
379
380     ap.repaint();
381   }
382
383
384   void reset()
385   {
386     av.setGlobalColourScheme(oldcs);
387     if (av.alignment.getGroups() != null)
388     {
389       Vector allGroups = ap.av.alignment.getGroups();
390       SequenceGroup sg;
391       for (int g = 0; g < allGroups.size(); g++)
392       {
393         sg = (SequenceGroup) allGroups.elementAt(g);
394         sg.cs = (ColourSchemeI)oldgroupColours.get(sg);
395       }
396     }
397     ap.repaint();
398
399   }
400
401
402
403 }