1   /*
2    * Copyright (c) 2004 imagero Andrei Kouznetsov. All Rights Reserved.
3    * http://jgui.imagero.com
4    *
5    * This program is free software; you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation; either version 2, or (at your option)
8    * any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18   */
19  
20  package jguitest;
21  
22  import com.imagero.gui.swing.SCheckBoxMenuItem;
23  import com.imagero.gui.swing.SToggleButton;
24  import com.imagero.gui.swing.SelectableAction;
25  
26  import javax.swing.*;
27  import java.awt.*;
28  import java.awt.event.ActionEvent;
29  
30  public class SelectableActionTest extends JApplet {
31  
32      boolean inApplet = false;
33  
34      public static void main(String[] args) {
35          SelectableActionTest sat = new SelectableActionTest();
36          sat.create(false);
37      }
38  
39      public void init() {
40          inApplet = true;
41          create(inApplet);
42      }
43  
44  
45      private void create(boolean inApplet) {
46  
47          String[] fileActionNames = {"one", "two", "three", "four", "fife", "six"};
48          String[] zoomActionNames = {"10", "20", "30", "30", "50", "100"};
49  
50          SelectableAction[] fileActions = new SelectableAction[fileActionNames.length];
51          SelectableAction[] zoomActions = new SelectableAction[zoomActionNames.length];
52  
53          for (int i = 0; i < zoomActions.length; i++) {
54              zoomActions[i] = new EmptyAction(zoomActionNames[i]);
55          }
56  
57          for (int i = 0; i < fileActions.length; i++) {
58              fileActions[i] = new EmptyAction(fileActionNames[i]);
59          }
60  
61          JMenu fileMenu = new JMenu("File");
62          JMenu zoomMenu = new JMenu("Zoom");
63          JMenuBar mb = new JMenuBar();
64  
65          mb.add(fileMenu);
66          mb.add(zoomMenu);
67  
68          for (int i = 0; i < zoomActions.length; i++) {
69              SCheckBoxMenuItem item = new SCheckBoxMenuItem(zoomActions[i]);
70              zoomMenu.add(item);
71          }
72  
73          for (int i = 0; i < fileActions.length; i++) {
74              SCheckBoxMenuItem item = new SCheckBoxMenuItem(fileActions[i]);
75              fileMenu.add(item);
76          }
77  
78          Box fbox = new Box(BoxLayout.X_AXIS);
79          Box fbox2 = new Box(BoxLayout.X_AXIS);
80          Box zbox = new Box(BoxLayout.Y_AXIS);
81          Box zbox2 = new Box(BoxLayout.Y_AXIS);
82  
83          for (int i = 0; i < fileActions.length; i++) {
84              SToggleButton stb = new SToggleButton(fileActions[i]);
85              fbox.add(stb);
86          }
87  
88          for (int i = 0; i < fileActions.length; i++) {
89              SToggleButton stb = new SToggleButton(fileActions[i]);
90              fbox2.add(stb);
91          }
92  
93          for (int i = 0; i < zoomActions.length; i++) {
94              SToggleButton stb = new SToggleButton(zoomActions[i]);
95              zbox.add(stb);
96          }
97  
98          for (int i = 0; i < zoomActions.length; i++) {
99              SToggleButton stb = new SToggleButton(zoomActions[i]);
100             zbox2.add(stb);
101         }
102 
103         JPanel panel = new JPanel(new BorderLayout());
104 
105         panel.add(fbox, BorderLayout.NORTH);
106         panel.add(fbox2, BorderLayout.SOUTH);
107         panel.add(zbox, BorderLayout.EAST);
108         panel.add(zbox2, BorderLayout.WEST);
109         panel.add(new JTextArea("SelectableAction demo:\nIf you select one of buttons \nthen appropriate menu item selected \nand vice versa"));
110 
111 
112 
113         if (!inApplet) {
114             JFrame frame = new JFrame();
115             final Container contentPane = frame.getContentPane();
116             contentPane.add(new JScrollPane(panel));
117 
118             frame.setJMenuBar(mb);
119             frame.pack();
120             frame.show();
121         }
122         else {
123             final Container contentPane = getContentPane();
124             contentPane.add(new JScrollPane(panel));
125             setJMenuBar(mb);
126         }
127     }
128 
129     static class EmptyAction extends SelectableAction {
130         public EmptyAction(String name) {
131             super(name);
132         }
133 
134         public EmptyAction(String name, boolean selected) {
135             super(name, selected);
136         }
137 
138         public void actionPerformed(ActionEvent e) {
139         }
140     }
141 }
142