1 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 STANDARD_LINE_BORDER = 43 javax.swing.BorderFactory.createLineBorder(new java.awt.Color (0, 128, 128)); 44 private static javax.swing.border.Border ACCEPT_LINE_BORDER = 45 javax.swing.BorderFactory.createLineBorder(java.awt.Color.green); 46 private static javax.swing.border.Border ACCEPTED_LINE_BORDER = 47 javax.swing.BorderFactory.createLineBorder(java.awt.Color.red); 48 private static javax.swing.border.Border MARGIN_EMPTY_BORDER = 49 javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 3); 50 51 private NodeObject nodeObject; 52 private final javax.swing.border.TitledBorder currentTitledBorder; 53 private final javax.swing.border.Border currentBorder; 54 55 56 private java.awt.dnd.DropTarget dropTarget; 57 58 62 public NodePanel(AbstractDataObjectPanel parentDataObjectPanel, NodeObject targetNodeObject) { 63 this(parentDataObjectPanel, targetNodeObject, new java.awt.Color (0xd0, 0xd0, 0xe0)); 64 } 65 66 public NodePanel(AbstractDataObjectPanel parentDataObjectPanel, NodeObject targetNodeObject, java.awt.Color c) { 67 super(parentDataObjectPanel, targetNodeObject.getName(), "NodeObject"); 68 if (targetNodeObject.getProtocol().equals("jini")) { 69 c = java.awt.Color.cyan; 70 } 71 nodeObject = targetNodeObject; 72 dropTarget = 74 new java.awt.dnd.DropTarget ( 75 this, 76 java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE, 77 new MyDropTargetListener(), 78 true); 79 setBackground(c); 81 setLayout(new java.awt.GridLayout (0, 1, 2, 2)); 82 String 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 (100, 20)); 87 88 PanelPopupMenu popup = new PanelPopupMenu("Node " + name); 90 popup.add(new javax.swing.AbstractAction ("Look for new ActiveObjects", null) { 91 public void actionPerformed(java.awt.event.ActionEvent e) { 92 ((VMObject) nodeObject.getParent()).sendEventsForAllActiveObjects(); 93 } 94 }); 95 popup.add(new javax.swing.AbstractAction ("Create a new ActiveObject", null) { 96 public void actionPerformed(java.awt.event.ActionEvent e) { 97 doCreateObject(); 98 } 99 }); 100 popup.addSeparator(); 101 popup.add(new javax.swing.AbstractAction ("Stop monitoring this node", null) { 102 public void actionPerformed(java.awt.event.ActionEvent e) { 103 nodeObject.destroyObject(); 104 } 105 }); 106 107 addMouseListener(popup.getMenuMouseListener()); 108 } 109 110 114 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 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 [][] getDataObjectInfo() { 145 return new Object [][] { { "Name", name }, { 146 "Active objects", new Integer (nodeObject.getChildObjectsCount()) 147 } 148 }; 149 } 150 151 protected void setFontSize(java.awt.Font font) { 152 super.setFontSize(font); 153 currentTitledBorder.setTitleFont(font); 154 showBorder(STANDARD_LINE_BORDER); 155 } 156 157 161 private void doCreateObject() { 162 Object result = javax.swing.JOptionPane.showInputDialog(parentFrame, "Please enter the classname of the object to create on node " + nodeObject.getURL(), "Creating a new ActiveObject", javax.swing.JOptionPane.PLAIN_MESSAGE ); 167 if (result == null || (!(result instanceof String ))) 168 return; 169 nodeObject.createNewRemoteObject((String ) result); 170 } 171 172 private void showBorder(javax.swing.border.Border b) { 173 currentTitledBorder.setBorder(b); 174 setBorder(currentBorder); 175 repaint(); 176 } 177 178 private void invokeMigrateTo(final ActiveObject activeObject, final String destURL) { 179 new Thread (new Runnable () { 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 195 201 private class MyDropTargetListener extends UniqueIDDropTargetListener { 202 203 209 protected boolean processDrop(java.awt.dnd.DropTargetDropEvent event, UniqueID uniqueID) { 210 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 controller.log("Received object " + activeObject.getName() + " to move to " + nodeObject.getURL()); 224 invokeMigrateTo(activeObject, nodeObject.getURL()); 225 226 } else if (event.getDropAction() == java.awt.dnd.DnDConstants.ACTION_COPY) { 228 event.acceptDrop(java.awt.dnd.DnDConstants.ACTION_COPY); 229 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 241 protected void showDragFeedBack() { 242 showBorder(ACCEPT_LINE_BORDER); 243 } 244 245 248 protected void showDropFeedBack() { 249 showBorder(ACCEPTED_LINE_BORDER); 250 } 251 252 255 protected void hideDnDFeedBack() { 256 showBorder(STANDARD_LINE_BORDER); 257 } 258 259 } 261 } 262 | Popular Tags |