d389b9a39f13498859c1fdd7d7a43daf0e9dd567
[jalview.git] / src / jalview / appletgui / FeatureColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28 import java.awt.Rectangle;
29
30 public class FeatureColourChooser extends Panel implements
31         ActionListener, AdjustmentListener, ItemListener, MouseListener
32 {
33   Frame frame;
34
35   FeatureRenderer fr;
36   FeatureSettings fs;
37   AlignmentPanel ap;
38
39   GraduatedColor cs;
40   Object oldcs;
41
42   Hashtable oldgroupColours;
43
44
45   boolean adjusting = false;
46   private float min,max;
47   String type=null;
48   
49   public FeatureColourChooser(FeatureSettings fsettings, String type)
50   {
51     this.fs = fsettings;
52     this.type = type;
53     fr = fsettings.fr;
54     ap = fsettings.ap;
55     float mm[] = ((float[][]) fr.minmax.get(type))[0];
56     min = mm[0];
57     max = mm[1];
58     oldcs = fr.featureColours.get(type);
59     if (oldcs instanceof GraduatedColor)
60     {
61       cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
62     } else {
63       // promote original color to a graduated color
64       Color bl = Color.black;
65       if (oldcs instanceof Color)
66       {
67         bl = (Color) oldcs;
68       }
69       // original colour becomes the maximum colour
70       cs = new GraduatedColor(Color.white,bl,mm[0],mm[1]);
71     }
72     minColour.setBackground(cs.getMinColor());
73     maxColour.setBackground(cs.getMaxColor());
74     adjusting = true;
75
76     try
77     {
78       jbInit();
79     } catch (Exception ex)
80     {
81     }
82     // To HERE!
83     adjusting = false;
84     changeColour();
85     slider.addAdjustmentListener(this);
86     slider.addMouseListener(this);
87     frame = new Frame();
88     frame.add(this);
89     jalview.bin.JalviewLite.addFrame(frame, "Graduated Feature Colour for "+type, 480,
90             145);
91     validate();
92   }
93
94   public FeatureColourChooser()
95   {
96     try
97     {
98       jbInit();
99     } catch (Exception ex)
100     {
101       ex.printStackTrace();
102     }
103   }
104
105   private void jbInit() throws Exception
106   {
107     minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
108     minColour.setLabel("Min Colour");
109     minColour.addActionListener(this);
110
111     maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
112     maxColour.setLabel("Max Colour");
113     maxColour.addActionListener(this);
114
115     thresholdIsMin.addItemListener(this);
116     ok.setLabel("OK");
117     ok.addActionListener(this);
118
119     cancel.setLabel("Cancel");
120     cancel.addActionListener(this);
121
122     this.setLayout(borderLayout1);
123     jPanel2.setLayout(flowLayout1);
124
125     jPanel1.setBackground(Color.white);
126     jPanel2.setBackground(Color.white);
127     threshold.addItemListener(this);
128     threshold.addItem("No Threshold");
129     threshold.addItem("Above Threshold");
130     threshold.addItem("Below Threshold");
131     jPanel3.setLayout(null);
132     thresholdValue.addActionListener(this);
133
134     slider.setBackground(Color.white);
135     slider.setEnabled(false);
136     slider.setBounds(new Rectangle(153, 3, 93, 21));
137     thresholdValue.setEnabled(false);
138     thresholdValue.setBounds(new Rectangle(248, 2, 79, 22));
139     thresholdValue.setColumns(5);
140     jPanel3.setBackground(Color.white);
141     //currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
142     //currentColours.setLabel("Use Original Colours");
143     //currentColours.addItemListener(this);
144
145     threshold.setBounds(new Rectangle(11, 3, 139, 22));
146     thresholdIsMin.setBackground(Color.white);
147     thresholdIsMin.setLabel("Threshold is min/max");
148     thresholdIsMin.setBounds(new Rectangle(328, 3, 135, 23));
149     jPanel1.add(ok);
150     jPanel1.add(cancel);
151     //jPanel2.add(currentColours);
152     jPanel2.add(minColour);
153     jPanel2.add(maxColour);
154     jPanel3.add(threshold);
155     jPanel3.add(slider);
156     jPanel3.add(thresholdValue);
157     jPanel3.add(thresholdIsMin);
158     this.add(jPanel2, java.awt.BorderLayout.NORTH);
159     this.add(jPanel3, java.awt.BorderLayout.CENTER);
160     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
161   }
162
163   Button minColour = new Button();
164
165   Button maxColour = new Button();
166
167   Button ok = new Button();
168
169   Button cancel = new Button();
170
171   Panel jPanel1 = new Panel();
172
173   Panel jPanel2 = new Panel();
174
175   Choice threshold = new Choice();
176
177   FlowLayout flowLayout1 = new FlowLayout();
178
179   Panel jPanel3 = new Panel();
180
181   Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
182
183   TextField thresholdValue = new TextField(20);
184
185
186   BorderLayout borderLayout1 = new BorderLayout();
187
188   Checkbox thresholdIsMin = new Checkbox();
189
190   private GraphLine threshline;
191
192   public void actionPerformed(ActionEvent evt)
193   {
194     if (evt.getSource() == thresholdValue)
195     {
196       try
197       {
198         float f = new Float(thresholdValue.getText()).floatValue();
199         slider.setValue((int) (f * 1000));
200         adjustmentValueChanged(null);
201       } catch (NumberFormatException ex)
202       {
203       }
204     }
205     else if (evt.getSource() == minColour)
206     {
207       minColour_actionPerformed(null);
208     }
209     else if (evt.getSource() == maxColour)
210     {
211       maxColour_actionPerformed(null);
212     }
213
214     else if (evt.getSource() == ok)
215     {
216       changeColour();
217       frame.setVisible(false);
218     }
219     else if (evt.getSource() == cancel)
220     {
221       reset();
222       ap.paintAlignment(true);
223       frame.setVisible(false);
224     }
225
226     else
227     {
228       changeColour();
229     }
230   }
231
232   public void itemStateChanged(ItemEvent evt)
233   {
234
235     changeColour();
236   }
237
238   public void adjustmentValueChanged(AdjustmentEvent evt)
239   {
240     if (!adjusting)
241     {
242       thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
243       valueChanged();
244     }
245   }
246   protected void valueChanged() {
247     changeColour();
248     threshline.value = (float) slider.getValue() / 1000f;
249     ap.paintAlignment(false);
250   }
251   public void minColour_actionPerformed(Color newCol)
252   {
253     if (newCol != null)
254     {
255       minColour.setBackground(newCol);
256       minColour.repaint();
257       changeColour();
258     }
259     else
260     {
261       new UserDefinedColours(this, "Select Colour for Minimum Value", minColour.getBackground());
262     }
263
264   }
265
266   public void maxColour_actionPerformed(Color newCol)
267   {
268     if (newCol != null)
269     {
270       maxColour.setBackground(newCol);
271       maxColour.repaint();
272       changeColour();
273     }
274     else
275     {
276       new UserDefinedColours(this, "Select Colour for Maximum Value", maxColour.getBackground());
277     }
278   }
279
280   void changeColour()
281   {
282     // Check if combobox is still adjusting
283     if (adjusting)
284     {
285       return;
286     }
287
288     int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
289     if (threshold.getSelectedItem().equals("Above Threshold"))
290     {
291       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
292     }
293     else if (threshold.getSelectedItem().equals("Below Threshold"))
294     {
295       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
296     }
297
298     slider.setEnabled(true);
299     thresholdValue.setEnabled(true);
300     GraduatedColor acg = new GraduatedColor(minColour.getBackground(), maxColour.getBackground(), min, max);
301
302     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
303     {
304       slider.setEnabled(false);
305       thresholdValue.setEnabled(false);
306       thresholdValue.setText("");
307     }
308     
309     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
310             && threshline == null)
311     {
312       // todo visual indication of feature threshold
313       threshline = new jalview.datamodel.GraphLine(
314                       (max - min) / 2f,
315                       "Threshold", Color.black);
316     }
317
318     if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
319     {
320       adjusting = true;
321       acg.setThresh(threshline.value);
322
323       float range = max * 1000f
324               - min * 1000f;
325
326       slider.setMinimum((int) (min * 1000));
327       slider.setMaximum((int) (max * 1000));
328       slider.setValue((int) (threshline.value * 1000));
329       thresholdValue.setText(threshline.value + "");
330       slider.setEnabled(true);
331       thresholdValue.setEnabled(true);
332       adjusting = false;
333     }
334
335     acg.setThreshType(aboveThreshold);
336     if (thresholdIsMin.getState() && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
337     {
338       if (aboveThreshold==AnnotationColourGradient.ABOVE_THRESHOLD)
339       { 
340         acg = new GraduatedColor(acg, threshline.value, max);
341       } else { 
342         acg = new GraduatedColor(acg, min,threshline.value);
343       }
344     }
345     
346     fr.featureColours.put(type,acg);
347     cs = acg;
348     ap.paintAlignment(false);
349   }
350
351   void reset()
352   {
353     fr.featureColours.put(type, oldcs);
354     ap.paintAlignment(true);
355
356   }
357
358   public void mouseClicked(MouseEvent evt)
359   {
360   }
361
362   public void mousePressed(MouseEvent evt)
363   {
364   }
365
366   public void mouseReleased(MouseEvent evt)
367   {
368     ap.paintAlignment(true);
369   }
370
371   public void mouseEntered(MouseEvent evt)
372   {
373   }
374
375   public void mouseExited(MouseEvent evt)
376   {
377   }
378
379 }