KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DiningPhilosophers > cif > PhilosopherPanel


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 a Panel GUI used to show a philosopher state.
33  * This panel show the philosopher name, the philosopher
34  * forks, the philosopher status and the philospher thicks
35  * since last meal.
36  *
37  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
38  * @author <a HREF="mailto:Sylvain.Leblanc@lifl.fr">Sylvain Leblanc</a>
39  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
40  *
41  * @version 2.0
42  */

43
44 public class PhilosopherPanel
45     extends javax.swing.JPanel JavaDoc
46 {
47
48     // ==================================================================
49
//
50
// Internal state.
51
//
52
// ==================================================================
53

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

58     protected javax.swing.JProgressBar JavaDoc hungryness_;
59
60     /**
61      * The check box used to show if philosopher has
62      * his right fork.
63      */

64     protected javax.swing.JCheckBox JavaDoc right_fork_;
65
66     /**
67      * The check box used to show if philosopher has
68      * his left fork.
69      */

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

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

96     public PhilosopherPanel(StatusInfo status)
97     {
98         /* this panel has a border layout */
99         super(new java.awt.BorderLayout JavaDoc());
100
101
102         java.net.URL JavaDoc url;
103         javax.swing.JPanel JavaDoc hungrynessPanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
104         javax.swing.JPanel JavaDoc hungrynessSubPanel = new javax.swing.JPanel JavaDoc(new java.awt.FlowLayout JavaDoc());
105
106         /* Creating the used font */
107         java.awt.Font JavaDoc font = getFont();
108         font = font.deriveFont((float)16.0);
109
110         /* setting hungryness progress bar in two pannels
111            to center it in the main panel */

112         hungryness_ = new javax.swing.JProgressBar JavaDoc(0, 40);
113         hungryness_.setSize(new java.awt.Dimension JavaDoc(150, 30));
114         hungrynessSubPanel.add(hungryness_);
115         hungrynessPanel.add(new javax.swing.JLabel JavaDoc(" "), java.awt.BorderLayout.NORTH);
116         hungrynessPanel.add(hungrynessSubPanel, java.awt.BorderLayout.CENTER);
117
118         /* create and configure the name label */
119         name_ = new javax.swing.JLabel JavaDoc(status.name);
120         name_.setPreferredSize(new java.awt.Dimension JavaDoc(100, name_.getHeight()));
121         name_.setFont(font);
122
123         /* create check boxes */
124         right_fork_ = new javax.swing.JCheckBox JavaDoc();
125         left_fork_ = new javax.swing.JCheckBox JavaDoc();
126
127         /* create state label */
128         state_ = new javax.swing.JLabel JavaDoc();
129         state_.setPreferredSize(new java.awt.Dimension JavaDoc(100, state_.getHeight()));
130         state_.setFont(font);
131
132         /* Setting fork icons (icons must be located in the dinner jar file) */
133         url = PhilosopherPanel.class.getResource("/images/myfork.gif");
134         right_fork_.setSelectedIcon(new javax.swing.ImageIcon JavaDoc(url));
135         left_fork_.setSelectedIcon(new javax.swing.ImageIcon JavaDoc(url));
136         url = PhilosopherPanel.class.getResource("/images/nofork.gif");
137         right_fork_.setIcon(new javax.swing.ImageIcon JavaDoc(url));
138         left_fork_.setIcon(new javax.swing.ImageIcon JavaDoc(url));
139
140         /* add name to left side of the panel */
141         add(name_, java.awt.BorderLayout.WEST);
142
143         /* creates a new sub panel for forks and state */
144         javax.swing.JPanel JavaDoc forks = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
145         forks.add(right_fork_, java.awt.BorderLayout.EAST);
146         forks.add(state_, java.awt.BorderLayout.CENTER);
147         forks.add(left_fork_, java.awt.BorderLayout.WEST);
148         
149         /* adding fork's panel in the center of the panel */
150         add(forks, java.awt.BorderLayout.CENTER);
151         /* adding progress bar panel to the east side of the panel */
152         add(hungrynessPanel, java.awt.BorderLayout.EAST);
153
154         /* initializes panel values */
155         updatePanel(status);
156
157         /* Configure the panel */
158         setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
159         setVisible(true);
160     }
161
162     // ==================================================================
163
//
164
// Internal methods.
165
//
166
// ==================================================================
167

168     /**
169      * Returns the color to use for the progress bar
170      * using the philosopher's thicks since last meal.
171      *
172      * @param thicks The philosopher's thicks since last meal.
173      */

174     protected java.awt.Color JavaDoc getFG(int thicks)
175     {
176         if (thicks < 3) return java.awt.Color.green;
177         if (thicks < 10) return java.awt.Color.yellow;
178         if (thicks < 40) return java.awt.Color.orange;
179         return java.awt.Color.red;
180     }
181
182     // ==================================================================
183
//
184
// Public methods.
185
//
186
// ==================================================================
187

188     /**
189      * Update the philosopher's panel using a StatusInfo event.
190      *
191      * @param event The event used to update the panel.
192      */

193     public void updatePanel(StatusInfo event)
194     {
195         /* update the status label */
196         state_.setText(stateAsStrings_[event.state.value()]);
197         /* update the hungryness progress bar */
198         hungryness_.setForeground(getFG(event.ticks_since_last_meal));
199         hungryness_.setValue(event.ticks_since_last_meal);
200         /* update forks check boxes */
201         left_fork_.setSelected(event.has_left_fork);
202         right_fork_.setSelected(event.has_right_fork);
203     }
204 }
205
206
Popular Tags