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