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