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