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