updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / appletgui / SliderPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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
20 package jalview.appletgui;
21
22 import java.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29
30 public class SliderPanel extends Panel
31     implements ActionListener,
32     AdjustmentListener,
33     MouseListener
34 {
35   AlignmentPanel ap;
36   boolean forConservation = true;
37   ColourSchemeI cs;
38
39   static Frame conservationSlider;
40   static Frame PIDSlider;
41
42   public static int setConservationSlider(AlignmentPanel ap, ColourSchemeI cs,
43                                           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     }
80     catch (Exception ex)
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     }
136     catch (Exception ex)
137     {}
138
139     if (!PIDSlider.isVisible())
140     {
141       jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(), 420,
142                                        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         jbInit();
159     } catch (Exception e) {
160         e.printStackTrace();
161       }
162     this.ap = ap;
163     this.cs = cs;
164     forConservation = forConserve;
165     undoButton.setVisible(false);
166     applyButton.setVisible(false);
167     if (forConservation)
168     {
169       label.setText("Modify conservation visibility");
170       slider.setMinimum(0);
171       slider.setMaximum(50 + slider.getVisibleAmount());
172       slider.setUnitIncrement(1);
173     }
174     else
175     {
176       label.setText("Colour residues above % occurence");
177       slider.setMinimum(0);
178       slider.setMaximum(100 + slider.getVisibleAmount());
179       slider.setBlockIncrement(1);
180     }
181
182     slider.addAdjustmentListener(this);
183     slider.addMouseListener(this);
184
185     slider.setValue(value);
186     valueField.setText(value + "");
187   }
188
189   public void valueChanged(int i)
190   {
191     if (cs == null)
192     {
193       return;
194     }
195
196     ColourSchemeI toChange = null;
197     Vector allGroups = null;
198     int groupIndex = 0;
199
200     if (allGroupsCheck.getState())
201     {
202       allGroups = ap.av.alignment.getGroups();
203       groupIndex = allGroups.size() - 1;
204     }
205     else
206     {
207       toChange = cs;
208     }
209
210     while (groupIndex > -1)
211     {
212       if (allGroups != null)
213       {
214         toChange = ( (SequenceGroup) allGroups.elementAt(groupIndex)).cs;
215       }
216
217       if (forConservation)
218       {
219         toChange.setConservationInc(i);
220       }
221       else
222       {
223         toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
224       }
225
226       groupIndex--;
227     }
228
229     ap.seqPanel.seqCanvas.repaint();
230
231   }
232
233   public void setAllGroupsCheckEnabled(boolean b)
234   {
235     allGroupsCheck.setEnabled(b);
236   }
237
238   public void actionPerformed(ActionEvent evt)
239   {
240     if(evt.getSource()==applyButton)
241       applyButton_actionPerformed();
242     else if(evt.getSource()==undoButton)
243       undoButton_actionPerformed();
244     else if(evt.getSource()==valueField)
245       valueField_actionPerformed();
246   }
247
248   public void adjustmentValueChanged(AdjustmentEvent evt)
249   {
250     valueField.setText(slider.getValue() + "");
251     valueChanged(slider.getValue());
252   }
253
254
255   public void valueField_actionPerformed()
256   {
257     try
258     {
259       int i = Integer.parseInt(valueField.getText());
260       slider.setValue(i);
261     }
262     catch (Exception ex)
263     {
264       valueField.setText(slider.getValue() + "");
265     }
266   }
267
268   public void setValue(int value)
269   {
270     slider.setValue(value);
271   }
272
273   public int getValue()
274   {
275     return Integer.parseInt(valueField.getText());
276   }
277
278   // this is used for conservation colours, PID colours and redundancy threshold
279   protected Scrollbar slider = new Scrollbar();
280   protected TextField valueField = new TextField();
281   protected Label label = new Label();
282   Panel jPanel1 = new Panel();
283   Panel jPanel2 = new Panel();
284   protected Button applyButton = new Button();
285   protected Button undoButton = new Button();
286   FlowLayout flowLayout1 = new FlowLayout();
287   protected Checkbox allGroupsCheck = new Checkbox();
288   BorderLayout borderLayout1 = new BorderLayout();
289   BorderLayout borderLayout2 = new BorderLayout();
290   FlowLayout flowLayout2 = new FlowLayout();
291
292   private void jbInit() throws Exception {
293       this.setLayout(borderLayout2);
294
295       // slider.setMajorTickSpacing(10);
296       //  slider.setMinorTickSpacing(1);
297       //  slider.setPaintTicks(true);
298       slider.setBackground(Color.white);
299       slider.setFont(new java.awt.Font("Verdana", 0, 11));
300       slider.setOrientation(0);
301       valueField.setFont(new java.awt.Font("Verdana", 0, 11));
302       valueField.setText("      ");
303       valueField.addActionListener(this);
304       label.setFont(new java.awt.Font("Verdana", 0, 11));
305       label.setText("set this label text");
306       jPanel1.setLayout(borderLayout1);
307       jPanel2.setLayout(flowLayout1);
308       applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
309       applyButton.setLabel("Apply");
310       applyButton.addActionListener(this);
311       undoButton.setEnabled(false);
312       undoButton.setFont(new java.awt.Font("Verdana", 0, 11));
313       undoButton.setLabel("Undo");
314       undoButton.addActionListener(this);
315       allGroupsCheck.setEnabled(false);
316       allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
317       allGroupsCheck.setLabel("Apply threshold to all groups");
318       allGroupsCheck.setName("Apply to all Groups");
319       this.setBackground(Color.white);
320       this.setForeground(Color.black);
321       jPanel2.add(label, null);
322       jPanel2.add(applyButton, null);
323       jPanel2.add(undoButton, null);
324       jPanel2.add(allGroupsCheck);
325       jPanel1.add(valueField, java.awt.BorderLayout.EAST);
326       jPanel1.add(slider, java.awt.BorderLayout.CENTER);
327       this.add(jPanel1, java.awt.BorderLayout.SOUTH);
328       this.add(jPanel2, java.awt.BorderLayout.CENTER);
329   }
330
331   protected void applyButton_actionPerformed()
332   { }
333
334   protected void undoButton_actionPerformed()
335   { }
336
337   public void mousePressed(MouseEvent evt)
338   { }
339
340   public void mouseReleased(MouseEvent evt)
341   {
342     if (ap.overviewPanel != null)
343       ap.overviewPanel.updateOverviewImage();
344   }
345   public void mouseClicked(MouseEvent evt)
346   {}
347   public void mouseEntered(MouseEvent evt)
348   {}
349   public void mouseExited(MouseEvent evt)
350   {}
351 }