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