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