JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / appletgui / SliderPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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("Conservation Colour Increment  (" + source
63             + ")");
64     if (ap.av.getAlignment().getGroups() != null)
65     {
66       sp.setAllGroupsCheckEnabled(true);
67     }
68     else
69     {
70       sp.setAllGroupsCheckEnabled(false);
71     }
72
73     return sp.getValue();
74   }
75
76   public static void showConservationSlider()
77   {
78     try
79     {
80       PIDSlider.setVisible(false);
81       PIDSlider = null;
82     } catch (Exception ex)
83     {
84     }
85
86     if (!conservationSlider.isVisible())
87     {
88       jalview.bin.JalviewLite.addFrame(conservationSlider,
89               conservationSlider.getTitle(), 420, 100);
90       conservationSlider.addWindowListener(new WindowAdapter()
91       {
92         public void windowClosing(WindowEvent e)
93         {
94           conservationSlider = null;
95         }
96       });
97
98     }
99
100   }
101
102   public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs,
103           String source)
104   {
105     SliderPanel pid = null;
106     if (PIDSlider == null)
107     {
108       pid = new SliderPanel(ap, 50, false, cs);
109       PIDSlider = new Frame();
110       PIDSlider.add(pid);
111     }
112     else
113     {
114       pid = (SliderPanel) PIDSlider.getComponent(0);
115       pid.cs = cs;
116     }
117     PIDSlider.setTitle("Percentage Identity Threshold (" + source + ")");
118
119     if (ap.av.getAlignment().getGroups() != null)
120     {
121       pid.setAllGroupsCheckEnabled(true);
122     }
123     else
124     {
125       pid.setAllGroupsCheckEnabled(false);
126     }
127
128     return pid.getValue();
129
130   }
131
132   public static void showPIDSlider()
133   {
134     try
135     {
136       conservationSlider.setVisible(false);
137       conservationSlider = null;
138     } catch (Exception ex)
139     {
140     }
141
142     if (!PIDSlider.isVisible())
143     {
144       jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(),
145               420, 100);
146       PIDSlider.addWindowListener(new WindowAdapter()
147       {
148         public void windowClosing(WindowEvent e)
149         {
150           PIDSlider = null;
151         }
152       });
153     }
154
155   }
156
157   public SliderPanel(AlignmentPanel ap, int value, boolean forConserve,
158           ColourSchemeI cs)
159   {
160     try
161     {
162       jbInit();
163     } catch (Exception e)
164     {
165       e.printStackTrace();
166     }
167     this.ap = ap;
168     this.cs = cs;
169     forConservation = forConserve;
170     undoButton.setVisible(false);
171     applyButton.setVisible(false);
172     if (forConservation)
173     {
174       label.setText(MessageManager.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.getString("label.colour_residues_above_occurence"));
182       slider.setMinimum(0);
183       slider.setMaximum(100 + slider.getVisibleAmount());
184       slider.setBlockIncrement(1);
185     }
186
187     slider.addAdjustmentListener(this);
188     slider.addMouseListener(this);
189
190     slider.setValue(value);
191     valueField.setText(value + "");
192   }
193
194   public void valueChanged(int i)
195   {
196     if (cs == null)
197     {
198       return;
199     }
200
201     ColourSchemeI toChange = cs;
202     Iterator<SequenceGroup> allGroups = null;
203
204     if (allGroupsCheck.getState())
205     {
206       allGroups = ap.av.getAlignment().getGroups().listIterator();
207     }
208
209     while (toChange != null)
210     {
211       if (forConservation)
212       {
213         toChange.setConservationInc(i);
214       }
215       else
216       {
217         toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
218       }
219       if (allGroups != null && allGroups.hasNext())
220       {
221         while ((toChange = allGroups.next().cs) == null
222                 && allGroups.hasNext())
223           ;
224       }
225       else
226       {
227         toChange = null;
228       }
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     valueField.setColumns(3);
323     label.setFont(new java.awt.Font("Verdana", 0, 11));
324     label.setText(MessageManager.getString("label.set_this_label_text"));
325     jPanel1.setLayout(borderLayout1);
326     jPanel2.setLayout(flowLayout1);
327     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
328     applyButton.setLabel(MessageManager.getString("action.apply"));
329     applyButton.addActionListener(this);
330     undoButton.setEnabled(false);
331     undoButton.setFont(new java.awt.Font("Verdana", 0, 11));
332     undoButton.setLabel(MessageManager.getString("action.undo"));
333     undoButton.addActionListener(this);
334     allGroupsCheck.setEnabled(false);
335     allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
336     allGroupsCheck.setLabel(MessageManager.getString("action.apply_threshold_all_groups"));
337     allGroupsCheck.setName(MessageManager.getString("action.apply_all_groups"));
338     this.setBackground(Color.white);
339     this.setForeground(Color.black);
340     jPanel2.add(label, null);
341     jPanel2.add(applyButton, null);
342     jPanel2.add(undoButton, null);
343     jPanel2.add(allGroupsCheck);
344     jPanel1.add(valueField, java.awt.BorderLayout.EAST);
345     jPanel1.add(slider, java.awt.BorderLayout.CENTER);
346     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
347     this.add(jPanel2, java.awt.BorderLayout.CENTER);
348   }
349
350   protected void applyButton_actionPerformed()
351   {
352   }
353
354   protected void undoButton_actionPerformed()
355   {
356   }
357
358   public void mousePressed(MouseEvent evt)
359   {
360   }
361
362   public void mouseReleased(MouseEvent evt)
363   {
364     ap.paintAlignment(true);
365   }
366
367   public void mouseClicked(MouseEvent evt)
368   {
369   }
370
371   public void mouseEntered(MouseEvent evt)
372   {
373   }
374
375   public void mouseExited(MouseEvent evt)
376   {
377   }
378 }