JAL-1503 update version in GPL header
[jalview.git] / src / jalview / appletgui / SliderPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 import jalview.util.MessageManager;
29
30 public class SliderPanel extends Panel implements ActionListener,
31         AdjustmentListener, MouseListener
32 {
33   AlignmentPanel ap;
34
35   boolean forConservation = true;
36
37   ColourSchemeI cs;
38
39   static Frame conservationSlider;
40
41   static Frame PIDSlider;
42
43   public static int setConservationSlider(AlignmentPanel ap,
44           ColourSchemeI cs, String source)
45   {
46     SliderPanel sp = null;
47
48     if (conservationSlider == null)
49     {
50       sp = new SliderPanel(ap, cs.getConservationInc(), true, cs);
51       conservationSlider = new Frame();
52       conservationSlider.add(sp);
53     }
54     else
55     {
56       sp = (SliderPanel) conservationSlider.getComponent(0);
57       sp.cs = cs;
58     }
59
60     conservationSlider.setTitle("Conservation Colour Increment  (" + source
61             + ")");
62     if (ap.av.getAlignment().getGroups() != null)
63     {
64       sp.setAllGroupsCheckEnabled(true);
65     }
66     else
67     {
68       sp.setAllGroupsCheckEnabled(false);
69     }
70
71     return sp.getValue();
72   }
73
74   public static void showConservationSlider()
75   {
76     try
77     {
78       PIDSlider.setVisible(false);
79       PIDSlider = null;
80     } catch (Exception ex)
81     {
82     }
83
84     if (!conservationSlider.isVisible())
85     {
86       jalview.bin.JalviewLite.addFrame(conservationSlider,
87               conservationSlider.getTitle(), 420, 100);
88       conservationSlider.addWindowListener(new WindowAdapter()
89       {
90         public void windowClosing(WindowEvent e)
91         {
92           conservationSlider = null;
93         }
94       });
95
96     }
97
98   }
99
100   public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs,
101           String source)
102   {
103     SliderPanel pid = null;
104     if (PIDSlider == null)
105     {
106       pid = new SliderPanel(ap, 50, false, cs);
107       PIDSlider = new Frame();
108       PIDSlider.add(pid);
109     }
110     else
111     {
112       pid = (SliderPanel) PIDSlider.getComponent(0);
113       pid.cs = cs;
114     }
115     PIDSlider.setTitle("Percentage Identity Threshold (" + source + ")");
116
117     if (ap.av.getAlignment().getGroups() != null)
118     {
119       pid.setAllGroupsCheckEnabled(true);
120     }
121     else
122     {
123       pid.setAllGroupsCheckEnabled(false);
124     }
125
126     return pid.getValue();
127
128   }
129
130   public static void showPIDSlider()
131   {
132     try
133     {
134       conservationSlider.setVisible(false);
135       conservationSlider = null;
136     } catch (Exception ex)
137     {
138     }
139
140     if (!PIDSlider.isVisible())
141     {
142       jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(),
143               420, 100);
144       PIDSlider.addWindowListener(new WindowAdapter()
145       {
146         public void windowClosing(WindowEvent e)
147         {
148           PIDSlider = null;
149         }
150       });
151     }
152
153   }
154
155   public SliderPanel(AlignmentPanel ap, int value, boolean forConserve,
156           ColourSchemeI cs)
157   {
158     try
159     {
160       jbInit();
161     } catch (Exception e)
162     {
163       e.printStackTrace();
164     }
165     this.ap = ap;
166     this.cs = cs;
167     forConservation = forConserve;
168     undoButton.setVisible(false);
169     applyButton.setVisible(false);
170     if (forConservation)
171     {
172       label.setText(MessageManager.getString("label.modify_conservation_visibility"));
173       slider.setMinimum(0);
174       slider.setMaximum(50 + slider.getVisibleAmount());
175       slider.setUnitIncrement(1);
176     }
177     else
178     {
179       label.setText(MessageManager.getString("label.colour_residues_above_occurence"));
180       slider.setMinimum(0);
181       slider.setMaximum(100 + slider.getVisibleAmount());
182       slider.setBlockIncrement(1);
183     }
184
185     slider.addAdjustmentListener(this);
186     slider.addMouseListener(this);
187
188     slider.setValue(value);
189     valueField.setText(value + "");
190   }
191
192   public void valueChanged(int i)
193   {
194     if (cs == null)
195     {
196       return;
197     }
198
199     ColourSchemeI toChange = cs;
200     Iterator<SequenceGroup> allGroups = null;
201
202     if (allGroupsCheck.getState())
203     {
204       allGroups = ap.av.getAlignment().getGroups().listIterator();
205     }
206
207     while (toChange != null)
208     {
209       if (forConservation)
210       {
211         toChange.setConservationInc(i);
212       }
213       else
214       {
215         toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
216       }
217       if (allGroups != null && allGroups.hasNext())
218       {
219         while ((toChange = allGroups.next().cs) == null
220                 && allGroups.hasNext())
221           ;
222       }
223       else
224       {
225         toChange = null;
226       }
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     {
242       applyButton_actionPerformed();
243     }
244     else if (evt.getSource() == undoButton)
245     {
246       undoButton_actionPerformed();
247     }
248     else if (evt.getSource() == valueField)
249     {
250       valueField_actionPerformed();
251     }
252   }
253
254   public void adjustmentValueChanged(AdjustmentEvent evt)
255   {
256     valueField.setText(slider.getValue() + "");
257     valueChanged(slider.getValue());
258   }
259
260   public void valueField_actionPerformed()
261   {
262     try
263     {
264       int i = Integer.parseInt(valueField.getText());
265       slider.setValue(i);
266     } catch (Exception ex)
267     {
268       valueField.setText(slider.getValue() + "");
269     }
270   }
271
272   public void setValue(int value)
273   {
274     slider.setValue(value);
275   }
276
277   public int getValue()
278   {
279     return Integer.parseInt(valueField.getText());
280   }
281
282   // this is used for conservation colours, PID colours and redundancy threshold
283   protected Scrollbar slider = new Scrollbar();
284
285   protected TextField valueField = new TextField();
286
287   protected Label label = new Label();
288
289   Panel jPanel1 = new Panel();
290
291   Panel jPanel2 = new Panel();
292
293   protected Button applyButton = new Button();
294
295   protected Button undoButton = new Button();
296
297   FlowLayout flowLayout1 = new FlowLayout();
298
299   protected Checkbox allGroupsCheck = new Checkbox();
300
301   BorderLayout borderLayout1 = new BorderLayout();
302
303   BorderLayout borderLayout2 = new BorderLayout();
304
305   FlowLayout flowLayout2 = new FlowLayout();
306
307   private void jbInit() throws Exception
308   {
309     this.setLayout(borderLayout2);
310
311     // slider.setMajorTickSpacing(10);
312     // slider.setMinorTickSpacing(1);
313     // slider.setPaintTicks(true);
314     slider.setBackground(Color.white);
315     slider.setFont(new java.awt.Font("Verdana", 0, 11));
316     slider.setOrientation(0);
317     valueField.setFont(new java.awt.Font("Verdana", 0, 11));
318     valueField.setText("   ");
319     valueField.addActionListener(this); 
320     valueField.setColumns(3);
321     label.setFont(new java.awt.Font("Verdana", 0, 11));
322     label.setText(MessageManager.getString("label.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(MessageManager.getString("action.apply"));
327     applyButton.addActionListener(this);
328     undoButton.setEnabled(false);
329     undoButton.setFont(new java.awt.Font("Verdana", 0, 11));
330     undoButton.setLabel(MessageManager.getString("action.undo"));
331     undoButton.addActionListener(this);
332     allGroupsCheck.setEnabled(false);
333     allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
334     allGroupsCheck.setLabel(MessageManager.getString("action.apply_threshold_all_groups"));
335     allGroupsCheck.setName(MessageManager.getString("action.apply_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 }