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