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