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