update author list in license for (JAL-826)
[jalview.git] / src / jalview / appletgui / SliderPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.alignment.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.alignment.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     Vector allGroups = null;
199     int groupIndex = 0;
200
201     if (allGroupsCheck.getState())
202     {
203       allGroups = ap.av.alignment.getGroups();
204       groupIndex = allGroups.size() - 1;
205     }
206     else
207     {
208       toChange = cs;
209     }
210
211     while (groupIndex > -1)
212     {
213       if (allGroups != null)
214       {
215         toChange = ((SequenceGroup) allGroups.elementAt(groupIndex)).cs;
216       }
217
218       if (forConservation)
219       {
220         toChange.setConservationInc(i);
221       }
222       else
223       {
224         toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
225       }
226
227       groupIndex--;
228     }
229
230     ap.seqPanel.seqCanvas.repaint();
231
232   }
233
234   public void setAllGroupsCheckEnabled(boolean b)
235   {
236     allGroupsCheck.setEnabled(b);
237   }
238
239   public void actionPerformed(ActionEvent evt)
240   {
241     if (evt.getSource() == applyButton)
242     {
243       applyButton_actionPerformed();
244     }
245     else if (evt.getSource() == undoButton)
246     {
247       undoButton_actionPerformed();
248     }
249     else if (evt.getSource() == valueField)
250     {
251       valueField_actionPerformed();
252     }
253   }
254
255   public void adjustmentValueChanged(AdjustmentEvent evt)
256   {
257     valueField.setText(slider.getValue() + "");
258     valueChanged(slider.getValue());
259   }
260
261   public void valueField_actionPerformed()
262   {
263     try
264     {
265       int i = Integer.parseInt(valueField.getText());
266       slider.setValue(i);
267     } catch (Exception ex)
268     {
269       valueField.setText(slider.getValue() + "");
270     }
271   }
272
273   public void setValue(int value)
274   {
275     slider.setValue(value);
276   }
277
278   public int getValue()
279   {
280     return Integer.parseInt(valueField.getText());
281   }
282
283   // this is used for conservation colours, PID colours and redundancy threshold
284   protected Scrollbar slider = new Scrollbar();
285
286   protected TextField valueField = new TextField();
287
288   protected Label label = new Label();
289
290   Panel jPanel1 = new Panel();
291
292   Panel jPanel2 = new Panel();
293
294   protected Button applyButton = new Button();
295
296   protected Button undoButton = new Button();
297
298   FlowLayout flowLayout1 = new FlowLayout();
299
300   protected Checkbox allGroupsCheck = new Checkbox();
301
302   BorderLayout borderLayout1 = new BorderLayout();
303
304   BorderLayout borderLayout2 = new BorderLayout();
305
306   FlowLayout flowLayout2 = new FlowLayout();
307
308   private void jbInit() throws Exception
309   {
310     this.setLayout(borderLayout2);
311
312     // slider.setMajorTickSpacing(10);
313     // slider.setMinorTickSpacing(1);
314     // slider.setPaintTicks(true);
315     slider.setBackground(Color.white);
316     slider.setFont(new java.awt.Font("Verdana", 0, 11));
317     slider.setOrientation(0);
318     valueField.setFont(new java.awt.Font("Verdana", 0, 11));
319     valueField.setText("      ");
320     valueField.addActionListener(this);
321     label.setFont(new java.awt.Font("Verdana", 0, 11));
322     label.setText("set this label text");
323     jPanel1.setLayout(borderLayout1);
324     jPanel2.setLayout(flowLayout1);
325     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
326     applyButton.setLabel("Apply");
327     applyButton.addActionListener(this);
328     undoButton.setEnabled(false);
329     undoButton.setFont(new java.awt.Font("Verdana", 0, 11));
330     undoButton.setLabel("Undo");
331     undoButton.addActionListener(this);
332     allGroupsCheck.setEnabled(false);
333     allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
334     allGroupsCheck.setLabel("Apply threshold to all groups");
335     allGroupsCheck.setName("Apply to all Groups");
336     this.setBackground(Color.white);
337     this.setForeground(Color.black);
338     jPanel2.add(label, null);
339     jPanel2.add(applyButton, null);
340     jPanel2.add(undoButton, null);
341     jPanel2.add(allGroupsCheck);
342     jPanel1.add(valueField, java.awt.BorderLayout.EAST);
343     jPanel1.add(slider, java.awt.BorderLayout.CENTER);
344     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
345     this.add(jPanel2, java.awt.BorderLayout.CENTER);
346   }
347
348   protected void applyButton_actionPerformed()
349   {
350   }
351
352   protected void undoButton_actionPerformed()
353   {
354   }
355
356   public void mousePressed(MouseEvent evt)
357   {
358   }
359
360   public void mouseReleased(MouseEvent evt)
361   {
362     ap.paintAlignment(true);
363   }
364
365   public void mouseClicked(MouseEvent evt)
366   {
367   }
368
369   public void mouseEntered(MouseEvent evt)
370   {
371   }
372
373   public void mouseExited(MouseEvent evt)
374   {
375   }
376 }