JAL-4090 JAL-1551 source license
[jalview.git] / utils / test / Test_Applet_Scroll.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 test;
22
23 //web_Ready
24 //web_AppletName= MyTest1
25 //web_Description= A test
26 //web_JavaVersion= http://www.dmitry
27 //web_AppletImage= dddd
28 //web_Category= test
29 //web_Date= $Date$
30 //web_Features= graphics, AWT-to-Swing
31
32 import java.awt.Adjustable;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.AdjustmentEvent;
37 import java.awt.event.AdjustmentListener;
38 import java.awt.event.FocusEvent;
39 import java.awt.event.FocusListener;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
42 import java.awt.event.MouseMotionListener;
43 import java.awt.event.MouseWheelEvent;
44 import java.awt.event.MouseWheelListener;
45 import java.text.DecimalFormat;
46
47 import javax.swing.ButtonGroup;
48 import javax.swing.JApplet;
49 import javax.swing.JComponent;
50 import javax.swing.JLabel;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollBar;
53 import javax.swing.JScrollPane;
54 import javax.swing.JSlider;
55 import javax.swing.JTextField;
56 import javax.swing.JToggleButton;
57 import javax.swing.JViewport;
58 import javax.swing.SwingConstants;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
61 import javax.swing.plaf.ViewportUI;
62
63 public class Test_Applet_Scroll extends JApplet implements ChangeListener {
64
65         static {MouseEvent m;
66                 /**
67                  * @j2sNative
68                  * 
69                  *      thisApplet.__Info.width = 500;
70                  *  thisApplet.__Info.height = 400;
71                  *  thisApplet.__Info.isResizable = true;
72                  */
73         }
74         static DecimalFormat df = new DecimalFormat("0.00");
75
76         boolean preferred = true;
77
78         private JScrollBar hsb;
79
80         private JScrollBar sbar;
81
82         void setSize(JComponent c, int x, int y) {
83                 if (preferred)
84                         c.setPreferredSize(new Dimension(x, y));
85                 else
86                         c.setSize(x, y);
87         }
88
89         @Override
90         public void init() {
91
92                 final JLabel label = new JLabel("hello");
93                 // label.setBounds(0, 60, 200, 60);
94                 setSize(label, 80, 50);
95                 label.setBackground(Color.yellow);
96                 label.setForeground(Color.BLUE);
97                 label.setOpaque(true);
98                 label.setHorizontalAlignment(SwingConstants.RIGHT);
99                 label.setVerticalAlignment(SwingConstants.CENTER);
100
101                 final JTextField tf = new JTextField("12.5", 8);
102                 tf.setBackground(Color.black);
103                 tf.setForeground(Color.yellow);
104                 tf.setOpaque(true);
105                 setSize(tf, 80, 40);
106                 tf.addActionListener(new java.awt.event.ActionListener() {
107                         @Override
108                         public void actionPerformed(ActionEvent event) {
109                                 label.setBackground(Color.white);
110                                 label.setText(tf.getText());
111                                 // repaint();
112                         }
113                 });
114                 tf.addFocusListener(new FocusListener() {
115
116                         @Override
117                         public void focusGained(FocusEvent e) {
118                                 tf.setBackground(Color.BLUE);
119                         }
120
121                         @Override
122                         public void focusLost(FocusEvent e) {
123                                 tf.setBackground(Color.BLACK);
124                         }
125
126                 });
127                 tf.addMouseWheelListener(new MouseWheelListener() {
128                         @Override
129                         public void mouseWheelMoved(MouseWheelEvent e) {
130                                 int n = e.getWheelRotation();
131                                 tf.setText("" + (Float.parseFloat(tf.getText()) + n));
132                                 // e.consume(); not necessary for scrollbars
133                         }
134                 });
135                 final JToggleButton button = new JToggleButton("test");
136                 setSize(button, 80, 20);
137                 button.setBackground(Color.orange);
138                 button.addActionListener(new java.awt.event.ActionListener() {
139                         @Override
140                         public void actionPerformed(ActionEvent event) {
141                                 label.setBackground(button.isSelected() ? Color.green : Color.yellow);
142                                 tf.setBackground(Color.black);
143                                 label.setText("test");
144                                 // repaint();
145                         }
146                 });
147                 final JToggleButton button2 = new JToggleButton("btn2");
148                 button2.addMouseListener(new MouseListener() {
149
150                         @Override
151                         public void mouseClicked(MouseEvent e) {
152                                 
153                                 System.out.println("BTN2 clicked " + e.getClickCount());
154                                 
155                         }
156
157                         @Override
158                         public void mousePressed(MouseEvent e) {
159                                 // TODO Auto-generated method stub
160                                 
161                         }
162
163                         @Override
164                         public void mouseReleased(MouseEvent e) {
165                                 System.out.println("BTN2 released");
166                                 
167                         }
168
169                         @Override
170                         public void mouseEntered(MouseEvent e) {
171                                 System.out.println("in button2");
172                                 // TODO Auto-generated method stub
173                                 
174                         }
175
176                         @Override
177                         public void mouseExited(MouseEvent e) {
178                                 System.out.println("out button2");
179                                 
180                         }
181                         
182                 });
183                 // BasicToggleButtonUI us; just using this to get access to code for
184                 // BasicToggleButtonUI
185                 setSize(button2, 80, 20);
186                 button2.setBackground(Color.orange);
187                 button2.addActionListener(new java.awt.event.ActionListener() {
188                         @Override
189                         public void actionPerformed(ActionEvent event) {
190                                 label.setBackground(button2.isSelected() ? Color.green : Color.yellow);
191                                 tf.setBackground(Color.green);
192                                 label.setText("btn2");
193                                 // repaint();
194                         }
195                 });
196                 
197                 button2.addMouseMotionListener(new MouseMotionListener() {
198
199                         @Override
200                         public void mouseDragged(MouseEvent e) {
201                                 System.out.println("btn2 DRAG " + e);
202                         }
203
204                         @Override
205                         public void mouseMoved(MouseEvent e) {
206                                 // TODO Auto-generated method stub
207                                 
208                         }
209                         
210                 });
211
212                 // the first two buttons act like radio buttons; only one is ever ON
213                 
214                 ButtonGroup bg = new ButtonGroup();
215                 bg.add(button);
216                 bg.add(button2);
217
218                 // the third button is not part of the group
219                 // note that JButtonUI does not need to know anything about the groups
220
221                 final JToggleButton button3 = new JToggleButton("btn3");
222                 // BasicToggleButtonUI us; just using this to get access to code for
223                 // BasicToggleButtonUI
224                 setSize(button3, 80, 20);
225                 button3.setBackground(Color.red);
226                 button3.addActionListener(new java.awt.event.ActionListener() {
227                         @Override
228                         public void actionPerformed(ActionEvent event) {
229                                 label.setBackground(button3.isSelected() ? Color.green : Color.yellow);
230                                 tf.setBackground(Color.black);
231                                 label.setText("btn3");
232                                 // repaint();
233                         }
234                 });
235
236                 JPanel p = new JPanel();
237                 
238                 p.addMouseListener(new MouseListener() {
239
240                         @Override
241                         public void mouseClicked(MouseEvent e) {
242
243                                 System.out.println("PANEL clicked " + e.getClickCount());
244                                 
245
246                         }
247
248                         @Override
249                         public void mousePressed(MouseEvent e) {
250                                 // TODO Auto-generated method stub
251                                 
252                         }
253
254                         @Override
255                         public void mouseReleased(MouseEvent e) {
256                                 // TODO Auto-generated method stub
257                                 
258                         }
259
260                         @Override
261                         public void mouseEntered(MouseEvent e) {
262                                 // TODO Auto-generated method stub
263                                 
264                                 System.out.println("in panel");
265                                 
266                         }
267
268                         @Override
269                         public void mouseExited(MouseEvent e) {
270                                 // TODO Auto-generated method stub
271                                 
272                                 System.out.println("out panel");
273                         }
274                 
275                         
276                 });
277                 
278                 
279                 p.addMouseMotionListener(new MouseMotionListener() {
280
281                         @Override
282                         public void mouseDragged(MouseEvent e) {
283                                 System.out.println("panel DRAG " + e);
284                         }
285
286                         @Override
287                         public void mouseMoved(MouseEvent e) {
288                                 System.out.println("panel Move " + e);
289                                 
290                         }
291                         
292                 });
293
294                 p.setToolTipText("this is the panel");
295                 // p.setLayout(new GridLayout(2, 2, 2, 2));
296                 JScrollPane sp = new JScrollPane();
297                 sp.getViewport().add(p);
298                 getContentPane().add(sp);
299                 sp.getViewport().addChangeListener(this);
300                 hsb = sp.getHorizontalScrollBar();
301                 button2.setToolTipText("this is hsb");
302
303                 mkBar(p, tf, Adjustable.VERTICAL, 20, 200).setToolTipText("this is scrollbar 1");
304                 mkSlider(p, tf, Adjustable.VERTICAL, 20, 200).setToolTipText("this is slider 2");
305
306                 mkSlider(p, tf, Adjustable.VERTICAL, 20, 200).setInverted(true);
307                 p.add(label);
308                 label.setToolTipText("this is label");
309                 p.add(tf);
310                 tf.setToolTipText("this is tf");
311                 p.add(button);
312                 p.add(button2);
313                 p.add(button3);
314                 p.setBackground(Color.blue);
315                 button2.setToolTipText("this is Button 2");
316                 button3.setToolTipText("this is Button 3");
317                 mkBar(p, tf, Adjustable.HORIZONTAL, 100, 20);
318                 mkSlider(p, tf, Adjustable.HORIZONTAL, 100, 20);
319                 mkSlider(p, tf, Adjustable.HORIZONTAL, 100, 20).setInverted(true);
320                 repaint();
321         }
322
323         JScrollBar mkBar(JPanel p, final JTextField tf, int orient, int x, int y) {
324                 final JScrollBar bar = new JScrollBar(orient, 500, 10, 300, 1000);
325                 bar.addAdjustmentListener(new AdjustmentListener() {
326
327                         @Override
328                         public void adjustmentValueChanged(AdjustmentEvent e) {
329                                 tf.setText(df.format(e.getValue() / 100.0));
330                         }
331
332                 });
333                 bar.addMouseWheelListener(new MouseWheelListener() {
334                         @Override
335                         public void mouseWheelMoved(MouseWheelEvent e) {
336                                 int n = e.getWheelRotation();
337                                 bar.setValue(bar.getValue() + n * 5);
338                                 // e.consume(); not necessary for scrollbars
339                         }
340                 });
341                 setSize(bar, x, y);
342                 bar.setBackground(Color.orange);
343                 bar.setForeground(Color.green);
344                 bar.setOpaque(true);
345                 p.add(bar);
346                 sbar = bar;
347                 bar.setVisibleAmount(80);
348                 return bar;
349         }
350
351         JSlider mkSlider(JPanel p, final JTextField tf, int orient, int x, int y) {
352                 final JSlider bar = new JSlider(orient, 300, 1000, 500);
353                 bar.addChangeListener(new ChangeListener() {
354                         @Override
355                         public void stateChanged(ChangeEvent e) {
356                                 tf.setText(df.format(((JSlider) e.getSource()).getValue() / 100.0));
357                         }
358                 });
359                 bar.addMouseWheelListener(new MouseWheelListener() {
360                         @Override
361                         public void mouseWheelMoved(MouseWheelEvent e) {
362                                 int n = e.getWheelRotation();
363                                 bar.setValue(bar.getValue() + n * 5);
364                                 // e.consume(); not necessary for sliders
365                         }
366                 });
367                 setSize(bar, x, y);
368                 bar.setBackground(Color.orange);
369                 bar.setForeground(Color.green);
370                 bar.setOpaque(true);
371                 p.add(bar);
372                 return bar;
373         }
374
375         @Override
376         public void stateChanged(ChangeEvent e) {
377                 // Viewport has scrolled
378 //              JViewport v = (JViewport) e.getSource();
379 //              System.out.println("extent " +v.getExtentSize() + " " + v.getViewPosition());
380 //              if (v.getViewRect().x > 0)
381 //                      System.out.println("view change: " + v.getViewRect());
382 //              System.out.println(v.getWidth() + " " + v.getHeight() + " " + v.getView().getBounds());
383 //              System.out.println(sbar.getValue() + "  "+ sbar.getVisibleAmount() + " " + sbar.getMaximum());
384
385         }
386
387 }