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