JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / 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.gui;
20
21 import java.util.*;
22
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26
27 import jalview.datamodel.*;
28 import jalview.jbgui.*;
29 import jalview.schemes.*;
30
31 /**
32  * DOCUMENT ME!
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class SliderPanel extends GSliderPanel
38 {
39   static JInternalFrame conservationSlider;
40
41   static JInternalFrame PIDSlider;
42
43   AlignmentPanel ap;
44
45   boolean forConservation = true;
46
47   ColourSchemeI cs;
48
49   /**
50    * Creates a new SliderPanel object.
51    * 
52    * @param ap
53    *          DOCUMENT ME!
54    * @param value
55    *          DOCUMENT ME!
56    * @param forConserve
57    *          DOCUMENT ME!
58    * @param cs
59    *          DOCUMENT ME!
60    */
61   public SliderPanel(final AlignmentPanel ap, int value,
62           boolean forConserve, ColourSchemeI cs)
63   {
64     this.ap = ap;
65     this.cs = cs;
66     forConservation = forConserve;
67     undoButton.setVisible(false);
68     applyButton.setVisible(false);
69
70     if (forConservation)
71     {
72       label.setText("Enter value to increase conservation visibility");
73       slider.setMinimum(0);
74       slider.setMaximum(100);
75     }
76     else
77     {
78       label.setText("Enter % identity above which to colour residues");
79       slider.setMinimum(0);
80       slider.setMaximum(100);
81     }
82
83     slider.addChangeListener(new ChangeListener()
84     {
85       public void stateChanged(ChangeEvent evt)
86       {
87         valueField.setText(slider.getValue() + "");
88         valueChanged(slider.getValue());
89       }
90     });
91
92     slider.addMouseListener(new MouseAdapter()
93     {
94       public void mouseReleased(MouseEvent evt)
95       {
96         ap.paintAlignment(true);
97       }
98     });
99
100     slider.setValue(value);
101     valueField.setText(value + "");
102   }
103
104   /**
105    * DOCUMENT ME!
106    * 
107    * @param ap
108    *          DOCUMENT ME!
109    * @param cs
110    *          DOCUMENT ME!
111    * @param source
112    *          DOCUMENT ME!
113    * 
114    * @return DOCUMENT ME!
115    */
116   public static int setConservationSlider(AlignmentPanel ap,
117           ColourSchemeI cs, String source)
118   {
119     SliderPanel sp = null;
120
121     if (conservationSlider == null)
122     {
123       sp = new SliderPanel(ap, cs.getConservationInc(), true, cs);
124       conservationSlider = new JInternalFrame();
125       conservationSlider.setContentPane(sp);
126       conservationSlider.setLayer(JLayeredPane.PALETTE_LAYER);
127     }
128     else
129     {
130       sp = (SliderPanel) conservationSlider.getContentPane();
131       sp.cs = cs;
132     }
133
134     conservationSlider.setTitle("Conservation Colour Increment  (" + source
135             + ")");
136
137     if (ap.av.getAlignment().getGroups() != null)
138     {
139       sp.setAllGroupsCheckEnabled(true);
140     }
141     else
142     {
143       sp.setAllGroupsCheckEnabled(false);
144     }
145
146     return sp.getValue();
147   }
148
149   /**
150    * DOCUMENT ME!
151    */
152   public static void showConservationSlider()
153   {
154     try
155     {
156       PIDSlider.setClosed(true);
157       PIDSlider = null;
158     } catch (Exception ex)
159     {
160     }
161
162     if (!conservationSlider.isVisible())
163     {
164       Desktop.addInternalFrame(conservationSlider,
165               conservationSlider.getTitle(), 420, 90, false);
166       conservationSlider
167               .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
168               {
169                 public void internalFrameClosed(
170                         javax.swing.event.InternalFrameEvent e)
171                 {
172                   conservationSlider = null;
173                 }
174               });
175       conservationSlider.setLayer(JLayeredPane.PALETTE_LAYER);
176     }
177   }
178
179   /**
180    * DOCUMENT ME!
181    * 
182    * @param ap
183    *          DOCUMENT ME!
184    * @param cs
185    *          DOCUMENT ME!
186    * @param source
187    *          DOCUMENT ME!
188    * 
189    * @return DOCUMENT ME!
190    */
191   public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs,
192           String source)
193   {
194     SliderPanel pid = null;
195
196     int threshold = cs.getThreshold();
197
198     if (PIDSlider == null)
199     {
200       pid = new SliderPanel(ap, threshold, false, cs);
201       PIDSlider = new JInternalFrame();
202       PIDSlider.setContentPane(pid);
203       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
204     }
205     else
206     {
207       pid = (SliderPanel) PIDSlider.getContentPane();
208       pid.cs = cs;
209     }
210
211     PIDSlider.setTitle("Percentage Identity Threshold (" + source + ")");
212
213     if (ap.av.getAlignment().getGroups() != null)
214     {
215       pid.setAllGroupsCheckEnabled(true);
216     }
217     else
218     {
219       pid.setAllGroupsCheckEnabled(false);
220     }
221
222     return pid.getValue();
223   }
224
225   /**
226    * DOCUMENT ME!
227    */
228   public static void showPIDSlider()
229   {
230     try
231     {
232       conservationSlider.setClosed(true);
233       conservationSlider = null;
234     } catch (Exception ex)
235     {
236     }
237
238     if (!PIDSlider.isVisible())
239     {
240       Desktop.addInternalFrame(PIDSlider, PIDSlider.getTitle(), 420, 90,
241               false);
242       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
243       PIDSlider
244               .addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
245               {
246                 public void internalFrameClosed(
247                         javax.swing.event.InternalFrameEvent e)
248                 {
249                   PIDSlider = null;
250                 }
251               });
252       PIDSlider.setLayer(JLayeredPane.PALETTE_LAYER);
253     }
254   }
255
256   /**
257    * DOCUMENT ME!
258    * 
259    * @param i
260    *          DOCUMENT ME!
261    */
262   public void valueChanged(int i)
263   {
264     if (cs == null)
265     {
266       return;
267     }
268
269     ColourSchemeI toChange = null;
270     List<SequenceGroup> allGroups = null;
271     int groupIndex = 0;
272
273     if (allGroupsCheck.isSelected())
274     {
275       allGroups = ap.av.getAlignment().getGroups();
276       groupIndex = allGroups.size() - 1;
277     }
278     else
279     {
280       toChange = cs;
281     }
282
283     while (groupIndex > -1)
284     {
285       if (allGroups != null)
286       {
287         toChange = ((SequenceGroup) allGroups.get(groupIndex)).cs;
288
289         if (toChange == null)
290         {
291           groupIndex--;
292
293           continue;
294         }
295       }
296
297       if (forConservation)
298       {
299         if (toChange.conservationApplied())
300         {
301           toChange.setConservationInc(i);
302         }
303       }
304       else
305       {
306         toChange.setThreshold(i, ap.av.getIgnoreGapsConsensus());
307       }
308
309       groupIndex--;
310     }
311
312     ap.seqPanel.seqCanvas.repaint();
313   }
314
315   /**
316    * DOCUMENT ME!
317    * 
318    * @param b
319    *          DOCUMENT ME!
320    */
321   public void setAllGroupsCheckEnabled(boolean b)
322   {
323     allGroupsCheck.setEnabled(b);
324   }
325
326   /**
327    * DOCUMENT ME!
328    * 
329    * @param e
330    *          DOCUMENT ME!
331    */
332   public void valueField_actionPerformed(ActionEvent e)
333   {
334     try
335     {
336       int i = Integer.parseInt(valueField.getText());
337       slider.setValue(i);
338     } catch (NumberFormatException ex)
339     {
340       valueField.setText(slider.getValue() + "");
341     }
342   }
343
344   /**
345    * DOCUMENT ME!
346    * 
347    * @param value
348    *          DOCUMENT ME!
349    */
350   public void setValue(int value)
351   {
352     slider.setValue(value);
353   }
354
355   /**
356    * DOCUMENT ME!
357    * 
358    * @return DOCUMENT ME!
359    */
360   public int getValue()
361   {
362     return Integer.parseInt(valueField.getText());
363   }
364
365   public void slider_mouseReleased(MouseEvent e)
366   {
367     if (ap.overviewPanel != null)
368     {
369       ap.overviewPanel.updateOverviewImage();
370     }
371   }
372
373 }