KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > data > WorldPanel


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.gui.data;
32
33 import org.objectweb.proactive.ic2d.data.AbstractDataObject;
34 import org.objectweb.proactive.ic2d.data.HostObject;
35 import org.objectweb.proactive.ic2d.data.WorldObject;
36 import org.objectweb.proactive.ic2d.event.WorldObjectListener;
37 import org.objectweb.proactive.ic2d.gui.ActiveObjectCommunicationRecorder;
38 import org.objectweb.proactive.ic2d.gui.util.DialogUtils;
39
40 public class WorldPanel extends AbstractDataObjectPanel implements WorldObjectListener, javax.swing.Scrollable JavaDoc {
41
42   private WorldObject worldObject;
43   private ActiveObjectCommunicationRecorder communicationRecorder;
44
45   //
46
// -- CONSTRUCTORS -----------------------------------------------
47
//
48

49   public WorldPanel(AbstractDataObjectPanel dataObjectPanel, WorldObject targetWorldObject, ActiveObjectCommunicationRecorder communicationRecorder) {
50     super(dataObjectPanel, "IC2D", "WorldObject");
51     this.worldObject = targetWorldObject;
52     this.communicationRecorder = communicationRecorder;
53     setBackground(java.awt.Color.white);
54
55     setLayout(new MyFlowLayout(java.awt.FlowLayout.CENTER, 25, 15));
56
57     //
58
// Contextual Menu
59
//
60
PanelPopupMenu popup = new PanelPopupMenu("World Panel");
61     popup.add(new javax.swing.AbstractAction JavaDoc("Monitor a new RMI Host", null) {
62       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
63         DialogUtils.openNewRMIHostDialog(parentFrame, worldObject, controller);
64       }
65     });
66     popup.add(new javax.swing.AbstractAction JavaDoc("Monitor a new RMI Node", null) {
67       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
68         DialogUtils.openNewNodeDialog(parentFrame, worldObject, controller);
69       }
70       });
71     popup.add(new javax.swing.AbstractAction JavaDoc("Monitor all JINI Hosts", null) {
72     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
73       worldObject.addHosts();
74     }
75       });
76     
77     popup.add(new javax.swing.AbstractAction JavaDoc("Monitor a new JINI Hosts", null) {
78     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
79       DialogUtils.openNewJINIHostDialog(parentFrame, worldObject, controller);
80     }
81       });
82     popup.addSeparator();
83     javax.swing.JCheckBoxMenuItem JavaDoc check = new javax.swing.JCheckBoxMenuItem JavaDoc("Manual Layout", false);
84     check.addActionListener(new java.awt.event.ActionListener JavaDoc() {
85     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
86       controller.setAutomaticLayout(! controller.isLayoutAutomatic());
87       revalidate();
88       repaint();
89     }
90       });
91     popup.add(check);
92     addMouseListener(popup.getMenuMouseListener());
93   }
94
95
96   //
97
// -- PUBLIC METHODS -----------------------------------------------
98
//
99

100   public void paint(java.awt.Graphics JavaDoc g) {
101     super.paint(g);
102     if (communicationRecorder.isEnabled()) communicationRecorder.drawAllLinks(g, this.getLocationOnScreen());
103   }
104
105
106   //
107
// -- implements WorldObjectListener -----------------------------------------------
108
//
109

110
111   public void hostObjectAdded(HostObject hostObject) {
112     HostPanel panel = new HostPanel(this, hostObject);
113     addChild(hostObject, panel);
114     hostObject.registerListener(panel);
115   }
116   
117   
118   public void hostObjectRemoved(HostObject hostObject) {
119     removeChild(hostObject);
120   }
121   
122   
123   //
124
// -- implements javax.swing.Scrollable -----------------------------------------------
125
//
126

127   public java.awt.Dimension JavaDoc getPreferredScrollableViewportSize() {
128     return getPreferredSize();
129   }
130
131   public int getScrollableUnitIncrement(java.awt.Rectangle JavaDoc visibleRect, int orientation, int direction) {
132     return (orientation == javax.swing.SwingConstants.VERTICAL) ? Math.max(4, visibleRect.height / 20) : Math.max(4, visibleRect.width / 20);
133   }
134
135   public int getScrollableBlockIncrement(java.awt.Rectangle JavaDoc visibleRect, int orientation, int direction) {
136     return (orientation == javax.swing.SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width;
137   }
138   
139   public boolean getScrollableTracksViewportWidth() {
140     if (getParent() instanceof javax.swing.JViewport JavaDoc) {
141       return (((javax.swing.JViewport JavaDoc)getParent()).getWidth() > getPreferredSize().width);
142     }
143     return false;
144   }
145
146   public boolean getScrollableTracksViewportHeight() {
147     if (getParent() instanceof javax.swing.JViewport JavaDoc) {
148       return (((javax.swing.JViewport JavaDoc)getParent()).getHeight() > getPreferredSize().height);
149     }
150     return false;
151   }
152
153   //
154
// -- PROTECTED METHODS -----------------------------------------------
155
//
156

157   protected AbstractDataObject getAbstractDataObject() {
158     return worldObject;
159   }
160   
161   
162   protected HostPanel getHostPanel(HostObject hostObject) {
163     return (HostPanel) getChild(hostObject);
164   }
165   
166   
167   protected Object JavaDoc[][] getDataObjectInfo() {
168     return new Object JavaDoc[][] {
169       };
170   }
171
172
173   protected void filterChangeParentNotification(String JavaDoc qname) {
174     activeObjectAddedToFilter();
175     revalidate();
176     repaint();
177   }
178
179
180   //
181
// -- PRIVATE METHODS -----------------------------------------------
182
//
183

184
185   //
186
// -- INNER CLASSES -----------------------------------------------
187
//
188

189   public class MyFlowLayout extends java.awt.FlowLayout JavaDoc {
190     /**
191      * Constructs a new Flow Layout with a centered alignment and a
192      * default 5-unit horizontal and vertical gap.
193      */

194     public MyFlowLayout() {
195       super();
196     }
197
198     /**
199      * Constructs a new Flow Layout with the specified alignment and a
200      * default 5-unit horizontal and vertical gap.
201      * The value of the alignment argument must be one of
202      * <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
203      * or <code>FlowLayout.CENTER</code>.
204      * @param align the alignment value
205      */

206     public MyFlowLayout(int align) {
207       super(align);
208     }
209
210     /**
211      * Creates a new flow layout manager with the indicated alignment
212      * and the indicated horizontal and vertical gaps.
213      * <p>
214      * The value of the alignment argument must be one of
215      * <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
216      * or <code>FlowLayout.CENTER</code>.
217      * @param align the alignment value.
218      * @param hgap the horizontal gap between components.
219      * @param vgap the vertical gap between components.
220      */

221     public MyFlowLayout(int align, int hgap, int vgap) {
222       super(align, hgap, vgap);
223     }
224     
225     /**
226      * Lays out the container. This method lets each component take
227      * its preferred size by reshaping the components in the
228      * target container in order to satisfy the constraints of
229      * this <code>FlowLayout</code> object.
230      * @param target the specified component being laid out.
231      * @see java.awt.Container
232      * @see java.awt.Container#doLayout
233      */

234     public void layoutContainer(java.awt.Container JavaDoc target) {
235       if (controller.isLayoutAutomatic()) {
236         super.layoutContainer(target);
237       } else {
238         synchronized (target.getTreeLock()) {
239           int nmembers = target.getComponentCount();
240           for (int i = 0 ; i < nmembers ; i++) {
241             java.awt.Component JavaDoc m = target.getComponent(i);
242             if (m.isVisible()) {
243               java.awt.Dimension JavaDoc d = m.getPreferredSize();
244               m.setSize(d.width, d.height);
245             }
246           }
247         }
248       }
249     }
250
251
252     /**
253      * Returns the preferred dimensions for this layout given the components
254      * in the specified target container.
255      * @param target the component which needs to be laid out
256      * @return the preferred dimensions to lay out the
257      * subcomponents of the specified container.
258      * @see java.awt.Container
259      * @see #minimumLayoutSize
260      * @see java.awt.Container#getPreferredSize
261      */

262     public java.awt.Dimension JavaDoc preferredLayoutSize(java.awt.Container JavaDoc target) {
263       synchronized (target.getTreeLock()) {
264         int maxX = 0;
265         int maxY = 0;
266         int nmembers = target.getComponentCount();
267         for (int i = 0 ; i < nmembers ; i++) {
268           java.awt.Component JavaDoc m = target.getComponent(i);
269           if (m.isVisible()) {
270             int x = m.getX();
271             int y = m.getY();
272             //if (x < minX) minX = x;
273
//if (y < minY) minY = y;
274
x += m.getWidth();
275             y += m.getHeight();
276             if (x > maxX) maxX = x;
277             if (y > maxY) maxY = y;
278           }
279         }
280         return new java.awt.Dimension JavaDoc(maxX, maxY);
281       }
282     }
283
284     /**
285      * Returns the minimum dimensions needed to layout the components
286      * contained in the specified target container.
287      * @param target the component which needs to be laid out
288      * @return the minimum dimensions to lay out the
289      * subcomponents of the specified container.
290      * @see #preferredLayoutSize
291      * @see java.awt.Container
292      * @see java.awt.Container#doLayout
293      */

294     public java.awt.Dimension JavaDoc minimumLayoutSize(java.awt.Container JavaDoc target) {
295       return preferredLayoutSize(target);
296     }
297   }
298
299
300
301 }
302
Popular Tags