1 31 package org.objectweb.proactive.ic2d.gui.data; 32 33 import org.objectweb.proactive.ic2d.data.AbstractDataObject; 34 import org.objectweb.proactive.ic2d.data.ActiveObject; 35 import org.objectweb.proactive.ic2d.event.ActiveObjectListener; 36 37 public class ActiveObjectPanel extends AbstractDataObjectPanel implements ActiveObjectListener { 38 39 private static final String GENERIC_TOOL_TIP_TEXT = "Drag to migrate"; 40 private static final String WAITING_REQUEST_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- WaitingForRequest"; 41 private static final String INACTIVE_TOOL_TIP_TEXT = "Cannot Migrate -- Inactive"; 42 private static final String SERVING_REQUEST_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- ServingRequest"; 43 private static final String WAITING_BY_NECESSITY_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- WaitByNecessity"; 44 private static final String ACTIVE_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- Active, not serving request"; 45 public static final java.awt.Color COLOR_WHEN_ACTIVE = new java.awt.Color (180, 255, 180); 46 public static final java.awt.Color COLOR_WHEN_WAITING_BY_NECESSITY = new java.awt.Color (255, 205, 110); 47 public static final java.awt.Color COLOR_WHEN_SERVING_REQUEST = java.awt.Color.white; 48 public static final java.awt.Color COLOR_WHEN_WAITING_REQUEST = new java.awt.Color (225, 225, 225); 49 public static final java.awt.Color COLOR_WHEN_MIGRATING = java.awt.Color.red; 50 public static final java.awt.Color COLOR_REQUEST_SINGLE = java.awt.Color.green; 51 public static final java.awt.Color COLOR_REQUEST_SEVERAL = java.awt.Color.red; 52 public static final java.awt.Color COLOR_REQUEST_MANY = new java.awt.Color (150, 0, 255); 53 public static final int SHOWN_REQUEST_QUEUE_LENGTH = 5; 54 public static final int NUMBER_OF_REQUESTS_FOR_SEVERAL = 5; 55 public static final int NUMBER_OF_REQUESTS_FOR_MANY = 50; 56 57 private ActiveObject activeObject; 58 private javax.swing.JLabel nameLabel; 59 60 63 private java.awt.dnd.DragSource dragSource; 64 private java.awt.dnd.DragSourceListener dragSourceListener; 65 66 private boolean isGhost; 67 68 72 public ActiveObjectPanel(AbstractDataObjectPanel parentDataObjectPanel, ActiveObject targetActiveObject) { 73 super(parentDataObjectPanel, targetActiveObject.getClassName(), "ActiveObject"); 74 activeObject = targetActiveObject; 75 setBackground(COLOR_WHEN_WAITING_REQUEST); 76 setOpaque(false); 77 nameLabel = new javax.swing.JLabel (activeObject.getName()); 78 setFontSize(defaultFont); 79 add(nameLabel); 80 81 dragSource = java.awt.dnd.DragSource.getDefaultDragSource(); 83 dragSource.createDefaultDragGestureRecognizer( 84 this, 85 java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE, 86 new MyDragGestureListener()); 87 dragSourceListener = new MyDragSourceListener(); 88 89 PanelPopupMenu popup = new PanelPopupMenu("Object " + name); 91 popup.add(new javax.swing.AbstractAction ("Filter class " + name, null) { 92 public void actionPerformed(java.awt.event.ActionEvent e) { 93 boolean b = activeObjectFilter.addClass(activeObject.getClassName()); 94 if (b) { 95 filterChangeParentNotification(activeObject.getClassName()); 96 } 97 } 98 }); 99 addMouseListener(popup.getMenuMouseListener()); 100 setVisibleFromFilter(); 101 } 102 103 107 public ActiveObject getActiveObject() { 108 return activeObject; 109 } 110 111 112 public void paintComponent(java.awt.Graphics g) { 113 super.paintComponent(g); 114 java.awt.Color old = g.getColor(); 115 if (isGhost) 116 g.setColor(COLOR_WHEN_MIGRATING); 117 else 118 g.setColor(getBackground()); 119 g.fillOval(0, 0, getWidth(), getHeight()); 121 122 int length = activeObject.getRequestQueueLength(); 124 int numSingle; 125 int numSeveral; 126 int numMany; 127 numSingle = Math.min(length, SHOWN_REQUEST_QUEUE_LENGTH); 128 length -= numSingle; 129 numSeveral = Math.min((int)Math.ceil(length / (double)NUMBER_OF_REQUESTS_FOR_SEVERAL), 130 SHOWN_REQUEST_QUEUE_LENGTH); 131 length -= numSeveral * NUMBER_OF_REQUESTS_FOR_SEVERAL; 132 numMany = (int)Math.ceil(length / (double)NUMBER_OF_REQUESTS_FOR_MANY); 133 if (numSingle > 0) { 134 int requestQueueX = (getWidth() - 6 * numSingle) / 2; 135 int requestQueueY = 2; 136 g.setColor(COLOR_REQUEST_SINGLE); 137 for (int i = 0; i < numSingle; i ++) 138 g.fillRect(requestQueueX + i*6, requestQueueY, 4, 4); 139 } 140 if (numSeveral > 0) { 141 int requestQueueX = (getWidth() - 6 * (numSeveral+numMany)) / 2; 142 int requestQueueY = getHeight() - 6; 143 g.setColor(COLOR_REQUEST_SEVERAL); 144 for (int i = 0; i < numSeveral; i ++) 145 g.fillRect(requestQueueX + i*6, requestQueueY, 4, 4); 146 } 147 if (numMany > 0) { 148 int requestQueueX = (getWidth() - 6 * (numSeveral+numMany)) / 2 + 6 * numSeveral; 149 int requestQueueY = getHeight() - 6; 150 g.setColor(COLOR_REQUEST_MANY); 151 for (int i = 0; i < numMany; i ++) 152 g.fillRect(requestQueueX + i*6, requestQueueY, 4, 4); 153 } 154 155 g.setColor(old); 156 paintChildren(g); 157 } 158 159 163 public void servingStatusChanged(int v) { 164 if (v == ActiveObject.STATUS_SERVING_REQUEST) { 165 setBackground(COLOR_WHEN_SERVING_REQUEST); 167 setToolTipText(SERVING_REQUEST_TOOL_TIP_TEXT); 168 } else if (v == ActiveObject.STATUS_WAITING_BY_NECESSITY_WHILE_ACTIVE || 169 v == ActiveObject.STATUS_WAITING_BY_NECESSITY_WHILE_SERVING) { 170 setBackground(COLOR_WHEN_WAITING_BY_NECESSITY); 172 setToolTipText(WAITING_BY_NECESSITY_TOOL_TIP_TEXT); 173 } else if (v == ActiveObject.STATUS_WAITING_FOR_REQUEST) { 174 setBackground(COLOR_WHEN_WAITING_REQUEST); 176 setToolTipText(WAITING_REQUEST_TOOL_TIP_TEXT); 177 } else { 178 setBackground(COLOR_WHEN_ACTIVE); 180 setToolTipText(ACTIVE_TOOL_TIP_TEXT); 181 } 182 repaint(); 183 } 184 185 public void requestQueueLengthChanged(int value) { 186 repaint(); 187 } 188 189 193 protected void cancelMigration() { 194 isGhost = false; 195 repaint(); 196 } 197 198 protected AbstractDataObject getAbstractDataObject() { 199 return activeObject; 200 } 201 202 protected void activeObjectAddedToFilter() { 203 setVisibleFromFilter(); 204 } 205 206 protected void activeObjectRemovedFromFilter() { 207 setVisibleFromFilter(); 208 } 209 210 protected void setFontSize(java.awt.Font font) { 211 nameLabel.setFont(font); 212 } 213 214 protected java.awt.Dimension getMinimumSizeInternal() { 215 return null; 216 } 217 218 protected Object [][] getDataObjectInfo() { 219 return new Object [][] { { "Class", name }, { 220 "ID", activeObject.getID() 221 } 222 }; 223 } 224 225 protected ActiveObjectPanel findActiveObjectPanelByActiveObject(ActiveObject activeObject) { 226 if (activeObject == this.activeObject) 227 return this; 228 else 229 return null; 230 } 231 232 protected void addAllActiveObjectsToWatcher() { 233 activeObjectWatcher.addActiveObject(activeObject); 234 } 235 236 protected void removeAllActiveObjectsFromWatcher() { 237 activeObjectWatcher.removeActiveObject(activeObject); 238 } 239 240 244 private void setVisibleFromFilter() { 245 setVisible(!activeObjectFilter.isClassFiltered(activeObject.getClassName())); 246 } 247 248 252 259 private class MyDragGestureListener implements java.awt.dnd.DragGestureListener { 260 266 public void dragGestureRecognized(java.awt.dnd.DragGestureEvent event) { 267 if (isGhost) 269 return; if ((event.getDragAction() & java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE) == 0) 271 return; 272 java.awt.datatransfer.Transferable transferable = new TransferableUniqueID(activeObject.getID()); 275 try { 277 event.startDrag(java.awt.dnd.DragSource.DefaultMoveNoDrop, transferable, dragSourceListener); 279 } catch (java.awt.dnd.InvalidDnDOperationException e) { 280 } 281 } 282 } 283 284 292 private class MyDragSourceListener implements java.awt.dnd.DragSourceListener { 293 296 public void dragDropEnd(java.awt.dnd.DragSourceDropEvent event) { 297 if (!event.getDropSuccess()) 298 return; 299 if (event.getDropAction() == java.awt.dnd.DnDConstants.ACTION_MOVE) { 300 if (controller != null) 301 controller.log("Object " + activeObject.getName() + " migrated."); 302 } else if (event.getDropAction() == java.awt.dnd.DnDConstants.ACTION_COPY) { 303 if (controller != null) 304 controller.log("Object " + activeObject.getName() + " cloned."); 305 } 306 isGhost = true; 307 repaint(); 308 } 309 310 313 public void dragEnter(java.awt.dnd.DragSourceDragEvent event) { 314 java.awt.dnd.DragSourceContext context = event.getDragSourceContext(); 315 int myaction = event.getDropAction(); 317 if ((myaction & java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE) != 0) { 318 context.setCursor(java.awt.dnd.DragSource.DefaultCopyDrop); 319 } else { 320 context.setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop); 321 } 322 } 323 324 327 public void dragOver(java.awt.dnd.DragSourceDragEvent event) { 328 } 329 330 333 public void dragExit(java.awt.dnd.DragSourceEvent event) { 334 java.awt.dnd.DragSourceContext context = event.getDragSourceContext(); 335 context.setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop); 336 } 337 338 343 public void dropActionChanged(java.awt.dnd.DragSourceDragEvent event) { 344 dragEnter(event); 345 } 346 } 348 349 } 350 | Popular Tags |