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