Merge branch 'develop' into features/JAL-2360colourSchemeApplicability
[jalview.git] / src / jalview / appletgui / 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.appletgui;
22
23 import jalview.datamodel.SequenceGroup;
24 import jalview.schemes.ColourSchemeI;
25 import jalview.util.MessageManager;
26
27 import java.awt.BorderLayout;
28 import java.awt.Button;
29 import java.awt.Checkbox;
30 import java.awt.Color;
31 import java.awt.FlowLayout;
32 import java.awt.Frame;
33 import java.awt.Label;
34 import java.awt.Panel;
35 import java.awt.Scrollbar;
36 import java.awt.TextField;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.AdjustmentEvent;
40 import java.awt.event.AdjustmentListener;
41 import java.awt.event.FocusAdapter;
42 import java.awt.event.FocusEvent;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
45 import java.awt.event.WindowAdapter;
46 import java.awt.event.WindowEvent;
47 import java.util.Iterator;
48
49 public class SliderPanel extends Panel implements ActionListener,
50         AdjustmentListener, MouseListener
51 {
52   AlignmentPanel ap;
53
54   boolean forConservation = true;
55
56   ColourSchemeI cs;
57
58   static Frame conservationSlider;
59
60   static Frame PIDSlider;
61
62   public static int setConservationSlider(AlignmentPanel ap,
63           ColourSchemeI cs, String source)
64   {
65     SliderPanel sp = null;
66
67     if (conservationSlider == null)
68     {
69       sp = new SliderPanel(ap, cs.getConservationInc(), true, cs);
70       conservationSlider = new Frame();
71       conservationSlider.add(sp);
72     }
73     else
74     {
75       sp = (SliderPanel) conservationSlider.getComponent(0);
76       sp.cs = cs;
77     }
78
79     conservationSlider
80             .setTitle(MessageManager.formatMessage(
81                     "label.conservation_colour_increment",
82                     new String[] { source }));
83     if (ap.av.getAlignment().getGroups() != null)
84     {
85       sp.setAllGroupsCheckEnabled(true);
86     }
87     else
88     {
89       sp.setAllGroupsCheckEnabled(false);
90     }
91
92     return sp.getValue();
93   }
94
95   public static void showConservationSlider()
96   {
97     try
98     {
99       PIDSlider.setVisible(false);
100       PIDSlider = null;
101     } catch (Exception ex)
102     {
103     }
104
105     if (!conservationSlider.isVisible())
106     {
107       jalview.bin.JalviewLite.addFrame(conservationSlider,
108               conservationSlider.getTitle(), 420, 100);
109       conservationSlider.addWindowListener(new WindowAdapter()
110       {
111         @Override
112         public void windowClosing(WindowEvent e)
113         {
114           conservationSlider = null;
115         }
116       });
117
118     }
119
120   }
121
122   public static int setPIDSliderSource(AlignmentPanel ap, ColourSchemeI cs,
123           String source)
124   {
125     SliderPanel pid = null;
126     if (PIDSlider == null)
127     {
128       pid = new SliderPanel(ap, cs.getThreshold(), false, cs);
129       PIDSlider = new Frame();
130       PIDSlider.add(pid);
131     }
132     else
133     {
134       pid = (SliderPanel) PIDSlider.getComponent(0);
135       pid.cs = cs;
136     }
137     PIDSlider
138             .setTitle(MessageManager.formatMessage(
139                     "label.percentage_identity_threshold",
140                     new String[] { source }));
141
142     if (ap.av.getAlignment().getGroups() != null)
143     {
144       pid.setAllGroupsCheckEnabled(true);
145     }
146     else
147     {
148       pid.setAllGroupsCheckEnabled(false);
149     }
150
151     return pid.getValue();
152
153   }
154
155   public static void showPIDSlider()
156   {
157     try
158     {
159       conservationSlider.setVisible(false);
160       conservationSlider = null;
161     } catch (Exception ex)
162     {
163     }
164
165     if (!PIDSlider.isVisible())
166     {
167       jalview.bin.JalviewLite.addFrame(PIDSlider, PIDSlider.getTitle(),
168               420, 100);
169       PIDSlider.addWindowListener(new WindowAdapter()
170       {
171         @Override
172         public void windowClosing(WindowEvent e)
173         {
174           PIDSlider = null;
175         }
176       });
177     }
178
179   }
180
181   /**
182    * Hides the PID slider panel if it is shown
183    */
184   public static void hidePIDSlider()
185   {
186     if (PIDSlider != null)
187     {
188       PIDSlider.setVisible(false);
189       PIDSlider = null;
190     }
191   }
192
193   /**
194    * Hides the Conservation slider panel if it is shown
195    */
196   public static void hideConservationSlider()
197   {
198     if (conservationSlider != null)
199     {
200       conservationSlider.setVisible(false);
201       conservationSlider = null;
202     }
203   }
204   public SliderPanel(AlignmentPanel ap, int value, boolean forConserve,
205           ColourSchemeI cs)
206   {
207     try
208     {
209       jbInit();
210     } catch (Exception e)
211     {
212       e.printStackTrace();
213     }
214     this.ap = ap;
215     this.cs = cs;
216     forConservation = forConserve;
217     undoButton.setVisible(false);
218     applyButton.setVisible(false);
219     if (forConservation)
220     {
221       label.setText(MessageManager
222               .getString("label.modify_conservation_visibility"));
223       slider.setMinimum(0);
224       slider.setMaximum(50 + slider.getVisibleAmount());
225       slider.setUnitIncrement(1);
226     }
227     else
228     {
229       label.setText(MessageManager
230               .getString("label.colour_residues_above_occurrence"));
231       slider.setMinimum(0);
232       slider.setMaximum(100 + slider.getVisibleAmount());
233       slider.setBlockIncrement(1);
234     }
235
236     slider.addAdjustmentListener(this);
237     slider.addMouseListener(this);
238
239     slider.setValue(value);
240     valueField.setText(value + "");
241   }
242
243   public void valueChanged(int i)
244   {
245     if (cs == null)
246     {
247       return;
248     }
249
250     ColourSchemeI toChange = cs;
251     Iterator<SequenceGroup> allGroups = null;
252
253     if (allGroupsCheck.getState())
254     {
255       allGroups = ap.av.getAlignment().getGroups().listIterator();
256     }
257
258     while (toChange != null)
259     {
260       if (forConservation)
261       {
262         toChange.setConservationInc(i);
263       }
264       else
265       {
266         toChange.setThreshold(i, ap.av.isIgnoreGapsConsensus());
267       }
268       if (allGroups != null && allGroups.hasNext())
269       {
270         while ((toChange = allGroups.next().cs) == null
271                 && allGroups.hasNext())
272         {
273           ;
274         }
275       }
276       else
277       {
278         toChange = null;
279       }
280     }
281
282     ap.seqPanel.seqCanvas.repaint();
283
284   }
285
286   public void setAllGroupsCheckEnabled(boolean b)
287   {
288     allGroupsCheck.setEnabled(b);
289   }
290
291   @Override
292   public void actionPerformed(ActionEvent evt)
293   {
294     if (evt.getSource() == applyButton)
295     {
296       applyButton_actionPerformed();
297     }
298     else if (evt.getSource() == undoButton)
299     {
300       undoButton_actionPerformed();
301     }
302     else if (evt.getSource() == valueField)
303     {
304       valueField_actionPerformed();
305     }
306   }
307
308   @Override
309   public void adjustmentValueChanged(AdjustmentEvent evt)
310   {
311     valueField.setText(slider.getValue() + "");
312     valueChanged(slider.getValue());
313   }
314
315   public void valueField_actionPerformed()
316   {
317     try
318     {
319       int i = Integer.valueOf(valueField.getText());
320       slider.setValue(i);
321     } catch (NumberFormatException ex)
322     {
323       valueField.setText(String.valueOf(slider.getValue()));
324     }
325   }
326
327   public void setValue(int value)
328   {
329     slider.setValue(value);
330   }
331
332   public int getValue()
333   {
334     return Integer.parseInt(valueField.getText());
335   }
336
337   // this is used for conservation colours, PID colours and redundancy threshold
338   protected Scrollbar slider = new Scrollbar();
339
340   protected TextField valueField = new TextField();
341
342   protected Label label = new Label();
343
344   Panel jPanel1 = new Panel();
345
346   Panel jPanel2 = new Panel();
347
348   protected Button applyButton = new Button();
349
350   protected Button undoButton = new Button();
351
352   FlowLayout flowLayout1 = new FlowLayout();
353
354   protected Checkbox allGroupsCheck = new Checkbox();
355
356   BorderLayout borderLayout1 = new BorderLayout();
357
358   BorderLayout borderLayout2 = new BorderLayout();
359
360   FlowLayout flowLayout2 = new FlowLayout();
361
362   private void jbInit() throws Exception
363   {
364     this.setLayout(borderLayout2);
365
366     // slider.setMajorTickSpacing(10);
367     // slider.setMinorTickSpacing(1);
368     // slider.setPaintTicks(true);
369     slider.setBackground(Color.white);
370     slider.setFont(new java.awt.Font("Verdana", 0, 11));
371     slider.setOrientation(0);
372     valueField.setFont(new java.awt.Font("Verdana", 0, 11));
373     valueField.setText("   ");
374     valueField.addActionListener(this);
375     valueField.setColumns(3);
376     valueField.addFocusListener(new FocusAdapter()
377     {
378       @Override
379       public void focusLost(FocusEvent e)
380       {
381         valueField_actionPerformed();
382         valueChanged(slider.getValue());
383       }
384     });
385     
386     label.setFont(new java.awt.Font("Verdana", 0, 11));
387     label.setText(MessageManager.getString("label.set_this_label_text"));
388     jPanel1.setLayout(borderLayout1);
389     jPanel2.setLayout(flowLayout1);
390     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
391     applyButton.setLabel(MessageManager.getString("action.apply"));
392     applyButton.addActionListener(this);
393     undoButton.setEnabled(false);
394     undoButton.setFont(new java.awt.Font("Verdana", 0, 11));
395     undoButton.setLabel(MessageManager.getString("action.undo"));
396     undoButton.addActionListener(this);
397     allGroupsCheck.setEnabled(false);
398     allGroupsCheck.setFont(new java.awt.Font("Verdana", 0, 11));
399     allGroupsCheck.setLabel(MessageManager
400             .getString("action.apply_threshold_all_groups"));
401     allGroupsCheck.setName(MessageManager
402             .getString("action.apply_all_groups"));
403     this.setBackground(Color.white);
404     this.setForeground(Color.black);
405     jPanel2.add(label, null);
406     jPanel2.add(applyButton, null);
407     jPanel2.add(undoButton, null);
408     jPanel2.add(allGroupsCheck);
409     jPanel1.add(valueField, java.awt.BorderLayout.EAST);
410     jPanel1.add(slider, java.awt.BorderLayout.CENTER);
411     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
412     this.add(jPanel2, java.awt.BorderLayout.CENTER);
413   }
414
415   protected void applyButton_actionPerformed()
416   {
417   }
418
419   protected void undoButton_actionPerformed()
420   {
421   }
422
423   @Override
424   public void mousePressed(MouseEvent evt)
425   {
426   }
427
428   @Override
429   public void mouseReleased(MouseEvent evt)
430   {
431     ap.paintAlignment(true);
432   }
433
434   @Override
435   public void mouseClicked(MouseEvent evt)
436   {
437   }
438
439   @Override
440   public void mouseEntered(MouseEvent evt)
441   {
442   }
443
444   @Override
445   public void mouseExited(MouseEvent evt)
446   {
447   }
448 }