d25caa4fe3308c0f69a81a3aa5d429cff19f188b
[jalview.git] / src / jalview / appletgui / SliderPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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  */
18 package jalview.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27
28 public class SliderPanel extends Panel implements ActionListener,
29         AdjustmentListener, MouseListener
30 {
31   AlignmentPanel ap;
32
33   boolean forConservation = true;
34
35   ColourSchemeI cs;
36
37   static Frame conservationSlider;
38
39   static Frame PIDSlider;
40
41   public static int setConservationSlider(AlignmentPanel ap,
42           ColourSchemeI cs, String source)
43   {
44     SliderPanel sp = null;
45
46     if (conservationSlider == null)
47     {
48       sp = new SliderPanel(ap, cs.getConservationInc(), true, cs);
49       conservationSlider = new Frame();
50       conservationSlider.add(sp);
51     }
52     else
53     {
54       sp = (SliderPanel) conservationSlider.getComponent(0);
55       sp.cs = cs;
56     }
57
58     conservationSlider.setTitle("Conservation Colour Increment  (" + source
59             + ")");
60     if (ap.av.getAlignment().getGroups() != null)
61     {
62       sp.setAllGroupsCheckEnabled(true);
63     }
64     else
65     {
66       sp.setAllGroupsCheckEnabled(false);
67     }
68
69     return sp.getValue();
70   }
71
72   public static void showConservationSlider()
73   {
74     try
75     {
76       PIDSlider.setVisible(false);
77       PIDSlider = null;
78     } catch (Exception ex)
79     {
80     }
81
82     if (!conservationSlider.isVisible())
83     {
84       jalview.bin.JalviewLite.addFrame(conservationSlider,
85               conservationSlider.getTitle(), 420, 100);
86       conservationSlider.addWindowListener(new WindowAdapter()
87       {
88         public void windowClosing(WindowEvent e)
89         {
90           conservationSlider = null;
91         }
92       });
93
94     }
95
96   }
97
98   public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs,
99           String source)
100   {
101     SliderPanel pid = null;
102     if (PIDSlider == null)
103     {
104       pid = new SliderPanel(ap, 50, false, cs);
105       PIDSlider = new Frame();
106       PIDSlider.add(pid);
107     }
108     else
109     {
110       pid = (SliderPanel) PIDSlider.getComponent(0);
111       pid.cs = cs;
112     }
113     PIDSlider.setTitle("Percentage Identity Threshold (" + source + ")");
114
115     if (ap.av.getAlignment().getGroups() != null)
116     {
117       pid.setAllGroupsCheckEnabled(true);
118     }
119     else
120     {
121       pid.setAllGroupsCheckEnabled(false);
122     }
123
124     return pid.getValue();
125
126   }
127
128   public static void showPIDSlider()
129   {
130     try
131     {
132       conservationSlider.setVisible(false);
133       conservationSlider = null;
134     } catch (Exception ex)
135     {
136     }
137
138     if (!PIDSlider.isVisible())
139     {
140       jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(),
141               420, 100);
142       PIDSlider.addWindowListener(new WindowAdapter()
143       {
144         public void windowClosing(WindowEvent e)
145         {
146           PIDSlider = null;
147         }
148       });
149     }
150
151   }
152
153   public SliderPanel(AlignmentPanel ap, int value, boolean forConserve,
154           ColourSchemeI cs)
155   {
156     try
157     {
158       jbInit();
159     } catch (Exception e)
160     {
161       e.printStackTrace();
162     }
163     this.ap = ap;
164     this.cs = cs;
165     forConservation = forConserve;
166     undoButton.setVisible(false);
167     applyButton.setVisible(false);
168     if (forConservation)
169     {
170       label.setText("Modify conservation visibility");
171       slider.setMinimum(0);
172       slider.setMaximum(50 + slider.getVisibleAmount());
173       slider.setUnitIncrement(1);
174     }
175     else
176     {
177       label.setText("Colour residues above % occurence");
178       slider.setMinimum(0);
179       slider.setMaximum(100 + slider.getVisibleAmount());
180       slider.setBlockIncrement(1);
181     }
182
183     slider.addAdjustmentListener(this);
184     slider.addMouseListener(this);
185
186     slider.setValue(value);
187     valueField.setText(value + "");
188   }
189
190   public void valueChanged(int i)
191   {
192     if (cs == null)
193     {
194       return;
195     }
196
197     ColourSchemeI toChange = null;
198     Iterator<SequenceGroup> allGroups = null;
199
200     if (allGroupsCheck.getState())
201     {
202       allGroups = ap.av.getAlignment().getGroups().listIterator();
203     }
204     else
205     {
206       toChange = cs;
207     }
208
209     do
210     {
211       if (allGroups != null)
212       {
213         toChange = allGroups.next().cs;
214       }
215
216       if (forConservation)
217       {
218         toChange.setConservationInc(i);
219       }
220       else
221       {
222         toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
223       }
224
225     } while (allGroups != null && allGroups.hasNext());
226
227     ap.seqPanel.seqCanvas.repaint();
228
229   }
230
231   public void setAllGroupsCheckEnabled(boolean b)
232   {
233     allGroupsCheck.setEnabled(b);
234   }
235
236   public void actionPerformed(ActionEvent evt)
237   {
238     if (evt.getSource() == applyButton)
239     {
240       applyButton_actionPerformed();
241     }
242     else if (evt.getSource() == undoButton)
243     {
244       undoButton_actionPerformed();
245     }
246     else if (evt.getSource() == valueField)
247     {
248       valueField_actionPerformed();
249     }
250   }
251
252   public void adjustmentValueChanged(AdjustmentEvent evt)
253   {
254     valueField.setText(slider.getValue() + "");
255     valueChanged(slider.getValue());
256   }
257
258   public void valueField_actionPerformed()
259   {
260     try
261     {
262       int i = Integer.parseInt(valueField.getText());
263       slider.setValue(i);
264     } catch (Exception ex)
265     {
266       valueField.setText(slider.getValue() + "");
267     }
268   }
269
270   public void setValue(int value)
271   {
272     slider.setValue(value);
273   }
274
275   public int getValue()
276   {
277     return Integer.parseInt(valueField.getText());
278   }
279
280   // this is used for conservation colours, PID colours and redundancy threshold
281   protected Scrollbar slider = new Scrollbar();
282
283   protected TextField valueField = new TextField();
284
285   protected Label label = new Label();
286
287   Panel jPanel1 = new Panel();
288
289   Panel jPanel2 = new Panel();
290
291   protected Button applyButton = new Button();
292
293   protected Button undoButton = new Button();
294
295   FlowLayout flowLayout1 = new FlowLayout();
296
297   protected Checkbox allGroupsCheck = new Checkbox();
298
299   BorderLayout borderLayout1 = new BorderLayout();
300
301   BorderLayout borderLayout2 = new BorderLayout();
302
303   FlowLayout flowLayout2 = new FlowLayout();
304
305   private void jbInit() throws Exception
306   {
307     this.setLayout(borderLayout2);
308
309     // slider.setMajorTickSpacing(10);
310     // slider.setMinorTickSpacing(1);
311     // slider.setPaintTicks(true);
312     slider.setBackground(Color.white);
313     slider.setFont(new java.awt.Font("Verdana", 0, 11));
314     slider.setOrientation(0);
315     valueField.setFont(new java.awt.Font("Verdana", 0, 11));
316     valueField.setText("      ");
317     valueField.addActionListener(this);
318     label.setFont(new java.awt.Font("Verdana", 0, 11));
319     label.setText("set this label text");
320     jPanel1.setLayout(borderLayout1);
321     jPanel2.setLayout(flowLayout1);
322     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
323     applyButton.setLabel("Apply");
324     applyButton.addActionListener(this);
325     undoButton.setEnabled(false);
326     undoButton.setFont(new java.awt.Font("Verdana", 0, 11));
327     undoButton.setLabel("Undo");
328     undoButton.addActionListener(this);
329     allGroupsCheck.setEnabled(false);
330     allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
331     allGroupsCheck.setLabel("Apply threshold to all groups");
332     allGroupsCheck.setName("Apply to all Groups");
333     this.setBackground(Color.white);
334     this.setForeground(Color.black);
335     jPanel2.add(label, null);
336     jPanel2.add(applyButton, null);
337     jPanel2.add(undoButton, null);
338     jPanel2.add(allGroupsCheck);
339     jPanel1.add(valueField, java.awt.BorderLayout.EAST);
340     jPanel1.add(slider, java.awt.BorderLayout.CENTER);
341     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
342     this.add(jPanel2, java.awt.BorderLayout.CENTER);
343   }
344
345   protected void applyButton_actionPerformed()
346   {
347   }
348
349   protected void undoButton_actionPerformed()
350   {
351   }
352
353   public void mousePressed(MouseEvent evt)
354   {
355   }
356
357   public void mouseReleased(MouseEvent evt)
358   {
359     ap.paintAlignment(true);
360   }
361
362   public void mouseClicked(MouseEvent evt)
363   {
364   }
365
366   public void mouseEntered(MouseEvent evt)
367   {
368   }
369
370   public void mouseExited(MouseEvent evt)
371   {
372   }
373 }