KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DiningPhilosophers > cif > PhilosopherPdaPanel


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Sylvain Leblanc.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package DiningPhilosophers.cif;
28
29 import DiningPhilosophers.*;
30
31 /**
32  * This is an AWT Panel GUI used to show a philosopher state.
33  * This panel show the philosopher name, the philosopher
34  * forks and the philosopher status.
35  *
36  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
37  * @author <a HREF="mailto:Sylvain.Leblanc@lifl.fr">Sylvain Leblanc</a>
38  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
39  *
40  * @version 2.0
41  */

42
43 public class PhilosopherPdaPanel
44     extends java.awt.Panel JavaDoc
45 {
46
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52

53     /**
54      * The progress bar used to show philosopher's thicks
55      * since last meal.
56      */

57     //protected javax.swing.JProgressBar hungryness_;
58

59     /**
60      * The check box used to show if philosopher has
61      * his right fork.
62      */

63     protected MyCheckBox right_fork_;
64
65     /**
66      * The check box used to show if philosopher has
67      * his left fork.
68      */

69     protected MyCheckBox left_fork_;
70
71     /** The label used to show the philosopher name. */
72     protected java.awt.Label JavaDoc name_;
73
74     /** The label used to show the philosopher status. */
75     protected java.awt.Label JavaDoc state_;
76
77     /** The philosopher possible states as string. */
78     protected String JavaDoc[] stateAsStrings_ = {
79         "EATING", "THINKING", "HUNGRY", "STARVING", "DEAD"
80     };
81
82     // ==================================================================
83
//
84
// Constructors.
85
//
86
// ==================================================================
87

88     /**
89      * Initializes the philosopher panel using a StatusInfo
90      * event.
91      *
92      * @param status The StatusInfo event used to initialize
93      * the philosopher panel.
94      */

95     public PhilosopherPdaPanel(StatusInfo status)
96     {
97         /* this panel has a border layout */
98         super(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT));
99
100
101         java.net.URL JavaDoc url;
102         java.awt.Image JavaDoc my_fork, no_fork;
103
104         /* setting hungryness progress bar in two pannels
105            to center it in the main panel */

106         // hungryness_ = new javax.swing.JProgressBar(0, 40);
107
// hungryness_.setSize(new java.awt.Dimension(150, 30));
108
// hungrynessSubPanel.add(hungryness_);
109
// hungrynessPanel.add(new javax.swing.JLabel(" "), java.awt.BorderLayout.NORTH);
110
// hungrynessPanel.add(hungrynessSubPanel, java.awt.BorderLayout.CENTER);
111

112         /* create and configure the name label */
113         name_ = new java.awt.Label JavaDoc(status.name);
114         name_.setSize(new java.awt.Dimension JavaDoc(100, name_.getHeight()));
115
116         /* create check boxes */
117         right_fork_ = new MyCheckBox();
118         left_fork_ = new MyCheckBox();
119
120         /* create state label */
121         state_ = new java.awt.Label JavaDoc();
122         state_.setSize(new java.awt.Dimension JavaDoc(100, state_.getHeight()));
123
124         /* Setting fork icons (icons must be located in the dinner jar file) */
125         url = PhilosopherPdaPanel.class.getResource("/images/myfork.gif");
126         my_fork = java.awt.Toolkit.getDefaultToolkit().getImage(url);
127         right_fork_.setSelectedIcon(my_fork);
128          left_fork_.setSelectedIcon(my_fork);
129          url = PhilosopherPdaPanel.class.getResource("/images/nofork.gif");
130          no_fork = java.awt.Toolkit.getDefaultToolkit().getImage(url);
131          right_fork_.setIcon(no_fork);
132          left_fork_.setIcon(no_fork);
133         right_fork_.setSelected(false);
134         left_fork_.setSelected(false);
135         right_fork_.setSize(50, 50);
136         left_fork_.setSize(50, 50);
137
138
139         /* add name to left side of the panel */
140          add(name_);
141
142         /* creates a new sub panel for forks and state */
143         java.awt.Panel JavaDoc forks = new java.awt.Panel JavaDoc(new java.awt.FlowLayout JavaDoc());
144         add(right_fork_);
145         add(state_);
146         add(left_fork_);
147         
148         /* adding progress bar panel to the east side of the panel */
149         //add(hungrynessPanel, java.awt.BorderLayout.EAST);
150

151         /* initializes panel values */
152         updatePanel(status);
153         validate();
154
155         /* Configure the panel */
156         setVisible(true);
157     }
158
159     // ==================================================================
160
//
161
// Internal methods.
162
//
163
// ==================================================================
164

165     // ==================================================================
166
//
167
// Public methods.
168
//
169
// ==================================================================
170

171     /**
172      * Update the philosopher's panel using a StatusInfo event.
173      *
174      * @param event The event used to update the panel.
175      */

176     public void updatePanel(StatusInfo event)
177     {
178         /* update the status label */
179         state_.setText(stateAsStrings_[event.state.value()]);
180         /* update the hungryness progress bar */
181         //hungryness_.setForeground(getFG(event.ticks_since_last_meal));
182
//hungryness_.setValue(event.ticks_since_last_meal);
183
/* update forks check boxes */
184         left_fork_.setSelected(event.has_left_fork);
185         right_fork_.setSelected(event.has_right_fork);
186     }
187 }
188
189 /**
190  * An internal check box GUI for PDA (based on AWT).
191  */

192 class MyCheckBox extends java.awt.Panel JavaDoc
193 {
194
195     /** used icons */
196     protected java.awt.Image JavaDoc selectedIcon_, icon_;
197
198     /** check box internal state */
199     protected boolean selected_;
200
201     /**
202      * Default constructor.
203      */

204     public MyCheckBox() {
205         selected_ = false;
206     };
207
208     /**
209      * Set the icon to use when check box has been selected.
210      */

211     public void setSelectedIcon(java.awt.Image JavaDoc icon)
212     {
213         selectedIcon_ = icon;
214     }
215
216     /**
217      * Set the icon to use when check box hasn't been selected.
218      */

219     public void setIcon(java.awt.Image JavaDoc icon)
220     {
221         icon_ = icon;
222     }
223
224     /**
225      * Set selection on the check box.
226      */

227     public void setSelected(boolean b)
228     {
229         selected_ = b;
230         repaint();
231     }
232
233     /**
234      * Overrides java.awt.Component.getPreferredSize
235      * in order to have a nice display of icons.
236      */

237     public java.awt.Dimension JavaDoc getPreferredSize()
238     {
239         return new java.awt.Dimension JavaDoc(50, 50);
240     }
241
242     /**
243      * Overrides the component paint method to draw
244      * icons.
245      */

246      public void paint(java.awt.Graphics JavaDoc g)
247      {
248          if (selected_){
249              g.drawImage(selectedIcon_, 0, 0, this);
250          } else {
251              g.drawImage(icon_, 0, 0, this);
252          }
253      }
254 }
255
Popular Tags