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