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