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