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