KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > Faces


1 /*
2  * $Id: Faces.java,v 1.5 2005/06/03 13:17:17 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.border.SEmptyBorder;
18
19 import java.awt.event.ActionEvent JavaDoc;
20 import java.awt.event.ActionListener JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Random JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
27  * @version $Revision: 1.5 $
28  */

29 public class Faces
30         extends WingSetPane {
31     static final ClassLoader JavaDoc cl = WingSet.class.getClassLoader();
32     static final SIcon sel =
33             new SURLIcon("../icons/RadioButtonSelectedIcon.gif");
34     static final SIcon nsel =
35             new SURLIcon("../icons/RadioButtonIcon.gif");
36     static final SIcon pressed =
37             new SURLIcon("../icons/RadioButtonPressedIcon.gif");
38     static final SIcon rollsel =
39             new SURLIcon("../icons/RadioButtonRolloverSelectedIcon.gif");
40     static final SIcon rollnsel =
41             new SURLIcon("../icons/RadioButtonRolloverIcon.gif");
42
43     static final Face henner = new Face("Henner");
44     static final Face armin = new Face("Armin");
45     static final Face holger = new Face("Holger");
46
47     static final Random JavaDoc random = new Random JavaDoc();
48
49     static final int maxFaces = 10;
50
51     ArrayList JavaDoc faces;
52
53     SEmptyBorder nameBorder;
54
55     SGridLayout layout;
56
57     SPanel facePanel;
58
59     SButtonGroup hairGroup;
60     SButtonGroup eyeGroup;
61     SButtonGroup mouthGroup;
62
63     public SComponent createExample() {
64         SPanel panel = new SPanel();
65         panel.add(createSwitcher());
66         return panel;
67     }
68
69     public SComponent createSwitcher() {
70         nameBorder = new SEmptyBorder(10, 10, 10, 10);
71         faces = new ArrayList JavaDoc();
72         layout = new SGridLayout(4, faces.size() + 1);
73         facePanel = new SPanel(layout);
74
75         final SLabel hair = new SLabel();
76         hair.setImageAbsBottom(true);
77         final SLabel eye = new SLabel();
78         eye.setImageAbsBottom(true);
79         final SLabel mouth = new SLabel();
80         mouth.setImageAbsBottom(true);
81
82         SForm shuffleForm = new SForm();
83         SButton shuffleButton = new SButton("Shuffle");
84         shuffleButton.addActionListener(new ActionListener JavaDoc() {
85             public void actionPerformed(ActionEvent JavaDoc e) {
86                 shuffle();
87             }
88         });
89         shuffleForm.add(shuffleButton);
90
91         facePanel.add(shuffleForm);
92         facePanel.add(hair);
93         facePanel.add(eye);
94         facePanel.add(mouth);
95
96         hairGroup = new SButtonGroup();
97         hairGroup.addActionListener(new ActionListener JavaDoc() {
98             public void actionPerformed(ActionEvent JavaDoc e) {
99                 int index = Integer.parseInt(e.getActionCommand());
100                 hair.setIcon(getFace(index).hair);
101             }
102         });
103
104         eyeGroup = new SButtonGroup();
105         eyeGroup.addActionListener(new ActionListener JavaDoc() {
106             public void actionPerformed(ActionEvent JavaDoc e) {
107                 int index = Integer.parseInt(e.getActionCommand());
108                 eye.setIcon(getFace(index).eyes);
109             }
110         });
111
112         mouthGroup = new SButtonGroup();
113         mouthGroup.addActionListener(new ActionListener JavaDoc() {
114             public void actionPerformed(ActionEvent JavaDoc e) {
115                 int index = Integer.parseInt(e.getActionCommand());
116                 mouth.setIcon(getFace(index).mouth);
117             }
118         });
119
120         addFace(henner);
121         addFace(armin);
122         addFace(holger);
123
124         shuffle();
125
126         return facePanel;
127     }
128
129     protected void shuffle() {
130         shuffle(hairGroup);
131         shuffle(eyeGroup);
132         shuffle(mouthGroup);
133     }
134
135     protected void shuffle(SButtonGroup g) {
136         int selIndex = getRandomFaceIndex();
137         for (Iterator JavaDoc iter = g.iterator(); iter.hasNext();) {
138             if (selIndex == 0) {
139                 g.setSelected((SRadioButton) iter.next(), true);
140                 return;
141             }
142             iter.next();
143             selIndex--;
144         }
145     }
146
147     protected Face getFace(int index) {
148         return (Face) faces.get(index);
149     }
150
151     public void addFace(Face f) {
152         if (faces.size() > maxFaces)
153             return;
154
155         layout.setColumns(faces.size() + 2);
156
157         SButton name = new SButton(f.name);
158         name.setBorder(nameBorder);
159         facePanel.add(name, faces.size() + 0 * (faces.size() + 2));
160
161         final int faceNumber = faces.size();
162         // hair
163
final SRadioButton hair = new SRadioButton();
164         decorateButton(hair);
165         hair.setActionCommand("" + faceNumber);
166         hairGroup.add(hair);
167         facePanel.add(hair, faces.size() + 1 * (faces.size() + 2));
168
169         // eye
170
final SRadioButton eye = new SRadioButton();
171         decorateButton(eye);
172         eye.setActionCommand("" + faceNumber);
173         eyeGroup.add(eye);
174         facePanel.add(eye, faces.size() + 2 * (faces.size() + 2));
175         
176         // mouth
177
final SRadioButton mouth = new SRadioButton();
178         decorateButton(mouth);
179         mouth.setActionCommand("" + faceNumber);
180         mouthGroup.add(mouth);
181         facePanel.add(mouth, faces.size() + 3 * (faces.size() + 2));
182
183         faces.add(f);
184         
185         /*
186          * click on the name selects all buttons belonging to
187          * that Face.
188          */

189         name.addActionListener(new ActionListener JavaDoc() {
190             public void actionPerformed(ActionEvent JavaDoc e) {
191                 hairGroup.setSelected(hair, true);
192                 eyeGroup.setSelected(eye, true);
193                 mouthGroup.setSelected(mouth, true);
194             }
195         });
196
197     }
198
199     void decorateButton(SRadioButton b) {
200         b.setIcon(nsel);
201         b.setSelectedIcon(sel);
202         b.setRolloverIcon(rollnsel);
203         b.setRolloverSelectedIcon(rollsel);
204         b.setPressedIcon(pressed);
205         b.setHorizontalAlignment(SConstants.CENTER);
206         b.setVerticalAlignment(SConstants.CENTER);
207     }
208
209     int getRandomFaceIndex() {
210         synchronized (random) {
211             return random.nextInt(faces.size());
212         }
213     }
214
215
216     static class Face {
217         SIcon hair;
218         SIcon eyes;
219         SIcon mouth;
220         String JavaDoc name;
221
222         Face() {
223         }
224
225         Face(String JavaDoc name) {
226             hair = new SURLIcon("../icons/" + name + "_hair.jpeg");
227             eyes = new SURLIcon("../icons/" + name + "_eyes.jpeg");
228             mouth = new SURLIcon("../icons/" + name + "_mouth.jpeg");
229
230             this.name = name;
231         }
232
233         public String JavaDoc toString() {
234             return name;
235         }
236     }
237 }
238
239
240
Popular Tags