001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package examples.withtemplate;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024:
025: import net.ar.webonswing.*;
026: import net.ar.webonswing.remote.*;
027: import net.ar.webonswing.swing.layouts.*;
028:
029: public class TemplateCheckBoxDemo extends JDialog {
030: public static void main(String[] args) {
031: WosFramework.getInstance().init();
032: new TemplateCheckBoxDemo().setVisible(true);
033: }
034:
035: public TemplateCheckBoxDemo(Frame owner, boolean modal) {
036: super (owner, modal);
037: init();
038: }
039:
040: public TemplateCheckBoxDemo() {
041: init();
042: }
043:
044: private void init() {
045: setBounds(0, 0, 400, 400);
046:
047: addWindowListener(new WindowAdapter() {
048: public void windowClosing(WindowEvent e) {
049: System.exit(0);
050: }
051: });
052:
053: setContentPane(getCheckBoxDemoPabel());
054: }
055:
056: public JPanel getCheckBoxDemoPabel() {
057: return new CheckBoxDemoPanel();
058: }
059:
060: public static class CheckBoxDemoPanel extends JPanel {
061: JCheckBox chinButton;
062: JCheckBox glassesButton;
063: JCheckBox hairButton;
064: JCheckBox teethButton;
065:
066: /*
067: * Four accessory choices provide for 16 different
068: * combinations. The image for each combination is
069: * contained in a separate image file whose name indicates
070: * the accessories. The filenames are "geek-XXXX.gif"
071: * where XXXX can be one of the following 16 choices.
072: * The "choices" StringBuffer contains the string that
073: * indicates the current selection and is used to generate
074: * the file name of the image to display.
075:
076: ---- // zero accessories
077:
078: c--- // one accessory
079: -g--
080: --h-
081: ---t
082:
083: cg-- // two accessories
084: c-h-
085: c--t
086: -gh-
087: -g-t
088: --ht
089:
090: -ght // three accessories
091: c-ht
092: cg-t
093: cgh-
094:
095: cght // all accessories
096: */
097:
098: StringBuffer choices;
099: JLabel pictureLabel;
100:
101: public CheckBoxDemoPanel() {
102:
103: // Create the check boxes
104: chinButton = new JCheckBox("Chin");
105: chinButton.setMnemonic(KeyEvent.VK_C);
106: chinButton.setSelected(true);
107: chinButton
108: .setLayout(new TemplateLayout(
109: WosFramework
110: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements.checkBoxPanel.check1")));
111:
112: glassesButton = new JCheckBox("Glasses");
113: glassesButton.setMnemonic(KeyEvent.VK_G);
114: glassesButton.setSelected(true);
115: glassesButton
116: .setLayout(new TemplateLayout(
117: WosFramework
118: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements.checkBoxPanel.check1")));
119:
120: hairButton = new JCheckBox("Hair");
121: hairButton.setMnemonic(KeyEvent.VK_H);
122: hairButton.setSelected(true);
123: hairButton
124: .setLayout(new TemplateLayout(
125: WosFramework
126: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements.checkBoxPanel.check1")));
127:
128: teethButton = new JCheckBox("Teeth");
129: teethButton.setMnemonic(KeyEvent.VK_T);
130: teethButton.setSelected(true);
131: teethButton
132: .setLayout(new TemplateLayout(
133: WosFramework
134: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements.checkBoxPanel.check1")));
135:
136: // Register a listener for the check boxes.
137: CheckBoxListener myListener = new CheckBoxListener();
138: chinButton.addItemListener(myListener);
139: glassesButton.addItemListener(myListener);
140: hairButton.addItemListener(myListener);
141: teethButton.addItemListener(new CheckBoxListener());
142:
143: // Indicates what's on the geek.
144: choices = new StringBuffer("cght");
145:
146: // Set up the picture label
147: pictureLabel = new JLabel();
148: pictureLabel.setIcon(new ImageIcon(
149: "resources/images/geek/geek-" + choices.toString()
150: + ".gif"));
151: pictureLabel.setToolTipText(choices.toString());
152: pictureLabel.setText(choices.toString());
153: pictureLabel
154: .setLayout(new TemplateLayout(
155: WosFramework
156: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements.thePicture")));
157:
158: // Put the check boxes in a column in a panel
159: JPanel checkPanel = new JPanel();
160: checkPanel
161: .setLayout(new TemplateLayout(
162: WosFramework
163: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements.checkBoxPanel")));
164: checkPanel.add(chinButton, "check1");
165: checkPanel.add(glassesButton, "check2");
166: checkPanel.add(hairButton, "check3");
167: checkPanel.add(teethButton, "check4");
168:
169: setLayout(new TemplateLayout(
170: WosFramework
171: .getKeyPositionTemplateForName("CheckBoxDemo.twoElements")));
172:
173: add(checkPanel, "checkBoxPanel");
174: add(pictureLabel, "thePicture");
175:
176: setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
177: }
178:
179: /** Listens to the check boxes. */
180: class CheckBoxListener implements ItemListener, RemoteListener {
181: public void itemStateChanged(ItemEvent e) {
182: int index = 0;
183: char c = '-';
184: Object source = e.getItemSelectable();
185:
186: if (source == chinButton) {
187: index = 0;
188: c = 'c';
189: } else if (source == glassesButton) {
190: index = 1;
191: c = 'g';
192: } else if (source == hairButton) {
193: index = 2;
194: c = 'h';
195: } else if (source == teethButton) {
196: index = 3;
197: c = 't';
198: }
199:
200: if (e.getStateChange() == ItemEvent.DESELECTED)
201: c = '-';
202:
203: choices.setCharAt(index, c);
204: pictureLabel.setIcon(new ImageIcon(
205: "resources/images/geek/geek-"
206: + choices.toString() + ".gif"));
207: pictureLabel.setToolTipText(choices.toString());
208: pictureLabel.setText(choices.toString());
209: }
210:
211: public String getRemoteName() {
212: return "CheckBoxListener";
213: }
214:
215: public Object[] getRemoteParameters() {
216: return new Object[] { pictureLabel };
217: }
218: }
219: }
220: }
|