KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.UniqueID;
34 import org.objectweb.proactive.ic2d.data.AbstractDataObject;
35 import org.objectweb.proactive.ic2d.data.ActiveObject;
36 import org.objectweb.proactive.ic2d.data.NodeObject;
37 import org.objectweb.proactive.ic2d.data.VMObject;
38 import org.objectweb.proactive.ic2d.event.NodeObjectListener;
39
40 public class NodePanel extends AbstractDataObjectPanel implements NodeObjectListener {
41
42   private static javax.swing.border.Border JavaDoc STANDARD_LINE_BORDER =
43     javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(0, 128, 128));
44   private static javax.swing.border.Border JavaDoc ACCEPT_LINE_BORDER =
45     javax.swing.BorderFactory.createLineBorder(java.awt.Color.green);
46   private static javax.swing.border.Border JavaDoc ACCEPTED_LINE_BORDER =
47     javax.swing.BorderFactory.createLineBorder(java.awt.Color.red);
48   private static javax.swing.border.Border JavaDoc MARGIN_EMPTY_BORDER =
49     javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 3);
50
51   private NodeObject nodeObject;
52   private final javax.swing.border.TitledBorder JavaDoc currentTitledBorder;
53   private final javax.swing.border.Border JavaDoc currentBorder;
54
55   /** enables this component to be a dropTarget */
56   private java.awt.dnd.DropTarget JavaDoc dropTarget;
57
58   //
59
// -- CONSTRUCTORS -----------------------------------------------
60
//
61

62   public NodePanel(AbstractDataObjectPanel parentDataObjectPanel, NodeObject targetNodeObject) {
63     this(parentDataObjectPanel, targetNodeObject, new java.awt.Color JavaDoc(0xd0, 0xd0, 0xe0));
64   }
65
66   public NodePanel(AbstractDataObjectPanel parentDataObjectPanel, NodeObject targetNodeObject, java.awt.Color JavaDoc c) {
67     super(parentDataObjectPanel, targetNodeObject.getName(), "NodeObject");
68     if (targetNodeObject.getProtocol().equals("jini")) {
69       c = java.awt.Color.cyan;
70     }
71     nodeObject = targetNodeObject;
72     // dnd stuff
73
dropTarget =
74       new java.awt.dnd.DropTarget JavaDoc(
75         this,
76         java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE,
77         new MyDropTargetListener(),
78         true);
79     // ui look
80
setBackground(c);
81     setLayout(new java.awt.GridLayout JavaDoc(0, 1, 2, 2));
82     String JavaDoc name = nodeObject.getName();
83     currentTitledBorder = javax.swing.BorderFactory.createTitledBorder(STANDARD_LINE_BORDER, name);
84     currentBorder = javax.swing.BorderFactory.createCompoundBorder(currentTitledBorder, MARGIN_EMPTY_BORDER);
85     setBorder(currentBorder);
86     setMinimumSize(new java.awt.Dimension JavaDoc(100, 20));
87
88     // The popup
89
PanelPopupMenu popup = new PanelPopupMenu("Node " + name);
90     popup.add(new javax.swing.AbstractAction JavaDoc("Look for new ActiveObjects", null) {
91       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
92         ((VMObject) nodeObject.getParent()).sendEventsForAllActiveObjects();
93       }
94     });
95     popup.add(new javax.swing.AbstractAction JavaDoc("Create a new ActiveObject", null) {
96       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
97         doCreateObject();
98       }
99     });
100     popup.addSeparator();
101     popup.add(new javax.swing.AbstractAction JavaDoc("Stop monitoring this node", null) {
102       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
103         nodeObject.destroyObject();
104       }
105     });
106
107     addMouseListener(popup.getMenuMouseListener());
108   }
109
110   //
111
// -- PUBLIC METHODS -----------------------------------------------
112
//
113

114   //
115
// -- implements NodeObjectListener -----------------------------------------------
116
//
117

118   public void activeObjectAdded(ActiveObject activeObject) {
119     ActiveObjectPanel panel = new ActiveObjectPanel(this, activeObject);
120     addChild(activeObject, panel);
121     activeObject.registerListener(panel);
122     showBorder(STANDARD_LINE_BORDER);
123     if (activeObject.isViewedInEventList())
124       activeObjectWatcher.addActiveObject(activeObject);
125   }
126
127   public void activeObjectRemoved(ActiveObject activeObject) {
128     removeChild(activeObject);
129     activeObjectWatcher.removeActiveObject(activeObject);
130   }
131
132   //
133
// -- PROTECTED METHODS -----------------------------------------------
134
//
135

136   protected AbstractDataObject getAbstractDataObject() {
137     return nodeObject;
138   }
139
140   protected ActiveObjectPanel getActiveObjectPanel(ActiveObject activeObject) {
141     return (ActiveObjectPanel) getChild(activeObject);
142   }
143
144   protected Object JavaDoc[][] getDataObjectInfo() {
145     return new Object JavaDoc[][] { { "Name", name }, {
146         "Active objects", new Integer JavaDoc(nodeObject.getChildObjectsCount())
147         }
148     };
149   }
150
151   protected void setFontSize(java.awt.Font JavaDoc font) {
152     super.setFontSize(font);
153     currentTitledBorder.setTitleFont(font);
154     showBorder(STANDARD_LINE_BORDER);
155   }
156
157   //
158
// -- PRIVATE METHODS -----------------------------------------------
159
//
160

161   private void doCreateObject() {
162       Object JavaDoc result = javax.swing.JOptionPane.showInputDialog(parentFrame, // Component parentComponent,
163
"Please enter the classname of the object to create on node " + nodeObject.getURL(), // Object message,
164
"Creating a new ActiveObject", // String title,
165
javax.swing.JOptionPane.PLAIN_MESSAGE // int messageType,
166
);
167     if (result == null || (!(result instanceof String JavaDoc)))
168       return;
169     nodeObject.createNewRemoteObject((String JavaDoc) result);
170   }
171
172   private void showBorder(javax.swing.border.Border JavaDoc b) {
173     currentTitledBorder.setBorder(b);
174     setBorder(currentBorder);
175     repaint();
176   }
177   
178   private void invokeMigrateTo(final ActiveObject activeObject, final String JavaDoc destURL) {
179     new Thread JavaDoc(new Runnable JavaDoc() {
180         public void run() {
181           boolean b = activeObject.migrateTo(destURL);
182           if (! b) {
183             ActiveObjectPanel activeObjectPanel = getTopLevelParent().findActiveObjectPanelByActiveObject(activeObject);
184             if (activeObjectPanel != null)
185               activeObjectPanel.cancelMigration();
186           }
187         }
188       }).start();
189   }
190
191   //
192
// -- INNER CLASSES -----------------------------------------------
193
//
194

195   /**
196    * MyDropTargetListener
197    * a listener that tracks the state of the operation
198    * @see java.awt.dnd.DropTargetListener
199    * @see java.awt.dnd.DropTarget
200    */

201   private class MyDropTargetListener extends UniqueIDDropTargetListener {
202
203     /**
204      * processes the drop and return false if the drop is rejected or true if the drop is accepted.
205      * The method must not call the rejectDrop as returning false signel that the drop is rejected.
206      * On the other hand it is the responsability of this method to call the acceptDrop and dropComplete
207      * when accepting the drop and returning true
208      */

209     protected boolean processDrop(java.awt.dnd.DropTargetDropEvent JavaDoc event, UniqueID uniqueID) {
210       // check if not the same node
211
if (nodeObject.getActiveObject(uniqueID) != null) {
212         controller.warn("The active object originates from the same VM you're trying to migrate it to !");
213         return false;
214       }
215       ActiveObject activeObject = nodeObject.getTopLevelParent().findActiveObjectById(uniqueID);
216       if (activeObject == null) {
217         controller.warn("Cannot find the ActiveObject id=" + uniqueID + ". It must have been removed.");
218         return false;
219       }
220       if (event.getDropAction() == java.awt.dnd.DnDConstants.ACTION_MOVE) {
221         event.acceptDrop(java.awt.dnd.DnDConstants.ACTION_MOVE);
222         // do migration
223
controller.log("Received object " + activeObject.getName() + " to move to " + nodeObject.getURL());
224         invokeMigrateTo(activeObject, nodeObject.getURL());
225
226         //success = activeObject.migrateTo(nodeObject.getURL());
227
} else if (event.getDropAction() == java.awt.dnd.DnDConstants.ACTION_COPY) {
228         event.acceptDrop(java.awt.dnd.DnDConstants.ACTION_COPY);
229         // do migration clone
230
controller.log("Received object " + activeObject.getName() + " to copy to " + nodeObject.getURL());
231         invokeMigrateTo(activeObject, nodeObject.getURL());
232       }
233       showBorder(STANDARD_LINE_BORDER);
234       event.dropComplete(true);
235       return true;
236     }
237
238     /**
239      * Displays a user feed back to show that the drag is going on
240      */

241     protected void showDragFeedBack() {
242       showBorder(ACCEPT_LINE_BORDER);
243     }
244
245     /**
246      * Displays a user feed back to show that the drop is going on
247      */

248     protected void showDropFeedBack() {
249       showBorder(ACCEPTED_LINE_BORDER);
250     }
251
252     /**
253      * Removes the user feed back that shows the drag
254      */

255     protected void hideDnDFeedBack() {
256       showBorder(STANDARD_LINE_BORDER);
257     }
258
259   } // end inner class MyDropTargetListener
260

261 }
262
Popular Tags