KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DiningPhilosophers > explorer > PhilosopherPanel


1 /*===========================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@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): Jerome Moroy.
23 Contributor(s): Christophe Contreras.
24
25 ===========================================================================*/

26
27 package DiningPhilosophers.explorer;
28
29 /** The administration console's imports */
30 import org.objectweb.util.explorer.api.Panel;
31 import org.objectweb.util.explorer.api.TreeView;
32
33 /** The CCM Plugin imports */
34 import org.objectweb.openccm.corba.TheRootPOA;
35
36 /** The java API's imports */
37 import java.awt.Color JavaDoc;
38 import javax.swing.Box JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40 import javax.swing.AbstractAction JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42 import javax.swing.JTextArea JavaDoc;
43
44 /** The CCM imports */
45 import org.omg.PortableServer.POA JavaDoc;
46 import org.omg.Components.Cookie;
47 import org.omg.Components.InvalidConnection;
48
49 /** The dinner demo imports */
50 import DiningPhilosophers.*;
51 import DiningPhilosophers.cif.StatusInfoFactory;
52 import DiningPhilosophers.cif.StatusInfoImpl;
53
54 /**
55  * Panel to observe a Philosopher.
56  *
57  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
58  *
59  * @version 0.1
60  */

61 public class PhilosopherPanel
62   implements Panel
63 {
64
65     // ==================================================================
66
//
67
// Static bloc.
68
//
69
// ==================================================================
70

71     // Executed once at the loading of this class
72
static
73     {
74         // Required to register the StatusInfo valuetype factory to the ORB.
75
DiningPhilosophers.StatusInfoFactoryHelper.register(new StatusInfoFactory());
76     }
77
78     // ==================================================================
79
//
80
// Internal states.
81
//
82
// ==================================================================
83

84     /** The Panel which contains all the information. */
85     protected JPanel JavaDoc compositePanel_ = null;
86
87     /** The PhilosopherPanel. */
88     protected DiningPhilosophers.cif.PhilosopherPanel phPanel_ = null;
89     
90     /** The cookie corresponding to the subscription. */
91     protected Cookie cookie_ = null;
92
93     /** The philosopher. */
94     protected Philosopher philosopher_ = null;
95
96     /** The StatusInfoConsumer. */
97     protected StatusInfoConsumerImpl info_ = null;
98
99     /** The cookie of the Servant. */
100     protected byte[] servantId_ = null;
101
102     // ==================================================================
103
//
104
// Internal Methods.
105
//
106
// ==================================================================
107

108     /**
109      * Recursive method wich puts all the sub components of the given component as transparent.
110      */

111     protected void setOpaque(javax.swing.JComponent JavaDoc comp)
112     {
113         comp.setOpaque(false);
114         java.awt.Component JavaDoc[] subComponents = comp.getComponents();
115         for(int i = 0 ; i < subComponents.length ; i++)
116         {
117             setOpaque((javax.swing.JComponent JavaDoc)subComponents[i]);
118         }
119     }
120
121     // ==================================================================
122
//
123
// Public methods for Panel interface.
124
//
125
// ==================================================================
126

127     /**
128      * Creates the panel for the selected Philosopher
129      */

130     public void selected(TreeView treeView)
131     {
132         
133         // Obtains the selected Philosopher
134
philosopher_ = (Philosopher) treeView.getSelectedObject();
135
136         // Creates the composite panel
137
compositePanel_ = new JPanel JavaDoc();
138         compositePanel_.setBackground(Color.white);
139
140         // Creates the title panel
141
JPanel JavaDoc titlePanel = new JPanel JavaDoc();
142         titlePanel.setBackground(Color.white);
143         titlePanel.add(new javax.swing.JLabel JavaDoc(philosopher_.name() + " panel"));
144
145         // Configures the philosopher panel
146
phPanel_ = new DiningPhilosophers.cif.PhilosopherPanel(new StatusInfoImpl(PhilosopherState.from_int(1), philosopher_.name(), 0, false, false));
147         javax.swing.border.Border JavaDoc border = javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED);
148         phPanel_.setBorder(javax.swing.BorderFactory.createTitledBorder(border, "Observer"));
149         setOpaque(phPanel_);
150         
151         // Configures and feeds the composite panel
152
Box JavaDoc box = Box.createVerticalBox();
153         box.add(Box.createVerticalGlue());
154         box.add(titlePanel);
155         box.add(Box.createVerticalStrut(10));
156         box.add(phPanel_);
157         box.add(Box.createVerticalGlue());
158         compositePanel_.add(box);
159
160         // Creates and activates a StatusInfoConsumer
161
info_ = new StatusInfoConsumerImpl(phPanel_);
162         try
163         {
164             POA poa = TheRootPOA.getRootPOA();
165             servantId_ = poa.activate_object(info_);
166             cookie_ = philosopher_.subscribe_info(StatusInfoConsumerHelper.narrow(poa.id_to_reference(servantId_)));
167         }
168         catch ( java.lang.Exception JavaDoc ex )
169         {
170             System.err.println( "An exception has been intercepted" );
171             ex.printStackTrace();
172         }
173     }
174
175     /**
176      * Returns a panel which contains a title and an observer panel
177      * in order to know the status of the selected philosopher
178      */

179     public Object JavaDoc getPanel()
180     {
181         return compositePanel_;
182     }
183
184     /**
185      * Unregisters the StatusInfoConsumer
186      */

187     public void unselected(TreeView treeView)
188     {
189         try
190         {
191             philosopher_.unsubscribe_info(cookie_);
192         }
193         catch(InvalidConnection e)
194         {
195             System.err.println("InvalidConnection: " + e.getMessage());
196         }
197
198         try
199         {
200             TheRootPOA.getRootPOA().deactivate_object(servantId_);
201         }
202         catch ( java.lang.Exception JavaDoc ex )
203         {
204             System.err.println( "An exception has been intercepted" );
205             ex.printStackTrace();
206         }
207
208     }
209
210 }
211
Popular Tags