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