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