KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActiveObject;
35 import org.objectweb.proactive.ic2d.event.ActiveObjectListener;
36
37 public class ActiveObjectPanel extends AbstractDataObjectPanel implements ActiveObjectListener {
38
39   private static final String JavaDoc GENERIC_TOOL_TIP_TEXT = "Drag to migrate";
40   private static final String JavaDoc WAITING_REQUEST_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- WaitingForRequest";
41   private static final String JavaDoc INACTIVE_TOOL_TIP_TEXT = "Cannot Migrate -- Inactive";
42   private static final String JavaDoc SERVING_REQUEST_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- ServingRequest";
43   private static final String JavaDoc WAITING_BY_NECESSITY_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- WaitByNecessity";
44   private static final String JavaDoc ACTIVE_TOOL_TIP_TEXT = GENERIC_TOOL_TIP_TEXT + " -- Active, not serving request";
45   public static final java.awt.Color JavaDoc COLOR_WHEN_ACTIVE = new java.awt.Color JavaDoc(180, 255, 180);
46   public static final java.awt.Color JavaDoc COLOR_WHEN_WAITING_BY_NECESSITY = new java.awt.Color JavaDoc(255, 205, 110);
47   public static final java.awt.Color JavaDoc COLOR_WHEN_SERVING_REQUEST = java.awt.Color.white;
48   public static final java.awt.Color JavaDoc COLOR_WHEN_WAITING_REQUEST = new java.awt.Color JavaDoc(225, 225, 225);
49   public static final java.awt.Color JavaDoc COLOR_WHEN_MIGRATING = java.awt.Color.red;
50   public static final java.awt.Color JavaDoc COLOR_REQUEST_SINGLE = java.awt.Color.green;
51   public static final java.awt.Color JavaDoc COLOR_REQUEST_SEVERAL = java.awt.Color.red;
52   public static final java.awt.Color JavaDoc COLOR_REQUEST_MANY = new java.awt.Color JavaDoc(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 JavaDoc nameLabel;
59
60   /**
61    * enables this component to be a Drag Source
62    */

63   private java.awt.dnd.DragSource JavaDoc dragSource;
64   private java.awt.dnd.DragSourceListener JavaDoc dragSourceListener;
65
66   private boolean isGhost;
67
68   //
69
// -- CONTRUCTORS -----------------------------------------------
70
//
71

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 JavaDoc(activeObject.getName());
78     setFontSize(defaultFont);
79     add(nameLabel);
80
81     // dnd stuff
82
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     // Popup menu
90
PanelPopupMenu popup = new PanelPopupMenu("Object " + name);
91     popup.add(new javax.swing.AbstractAction JavaDoc("Filter class " + name, null) {
92       public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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   //
104
// -- PUBLIC METHODS -----------------------------------------------
105
//
106

107   public ActiveObject getActiveObject() {
108     return activeObject;
109   }
110
111   /** Redraw the component */
112   public void paintComponent(java.awt.Graphics JavaDoc g) {
113     super.paintComponent(g);
114     java.awt.Color JavaDoc old = g.getColor();
115     if (isGhost)
116       g.setColor(COLOR_WHEN_MIGRATING);
117     else
118       g.setColor(getBackground());
119     // g.fillRoundRect(0,0,getWidth(),getHeight(),20,20);
120
g.fillOval(0, 0, getWidth(), getHeight());
121     
122     // paint request queue information
123
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   //
160
// -- implements ActiveObjectListener -----------------------------------------------
161
//
162

163   public void servingStatusChanged(int v) {
164     if (v == ActiveObject.STATUS_SERVING_REQUEST) {
165       // busy
166
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       // waiting by necessity
171
setBackground(COLOR_WHEN_WAITING_BY_NECESSITY);
172       setToolTipText(WAITING_BY_NECESSITY_TOOL_TIP_TEXT);
173     } else if (v == ActiveObject.STATUS_WAITING_FOR_REQUEST) {
174       // waiting for request
175
setBackground(COLOR_WHEN_WAITING_REQUEST);
176       setToolTipText(WAITING_REQUEST_TOOL_TIP_TEXT);
177     } else {
178       // active
179
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   //
190
// -- PROTECTED METHODS -----------------------------------------------
191
//
192

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 JavaDoc font) {
211     nameLabel.setFont(font);
212   }
213
214   protected java.awt.Dimension JavaDoc getMinimumSizeInternal() {
215     return null;
216   }
217
218   protected Object JavaDoc[][] getDataObjectInfo() {
219     return new Object JavaDoc[][] { { "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   //
241
// -- PRIVATE METHODS -----------------------------------------------
242
//
243

244   private void setVisibleFromFilter() {
245     setVisible(!activeObjectFilter.isClassFiltered(activeObject.getClassName()));
246   }
247   
248   //
249
// -- INNER CLASSES -----------------------------------------------
250
//
251

252   /**
253    * a listener that will start the drag.
254    * has access to top level's dsListener and dragSource
255    * @see java.awt.dnd.DragGestureListener
256    * @see java.awt.dnd.DragSource
257    * @see java.awt.datatransfer.StringSelection
258    */

259   private class MyDragGestureListener implements java.awt.dnd.DragGestureListener JavaDoc {
260     /**
261      * Start the drag if the operation is ok.
262      * uses java.awt.datatransfer.StringSelection to transfer
263      * the label's data
264      * @param e the event object
265      */

266     public void dragGestureRecognized(java.awt.dnd.DragGestureEvent JavaDoc event) {
267       // if the action is ok we go ahead otherwise we punt
268
if (isGhost)
269         return; // cannot migrate if isGhost or if non active
270
if ((event.getDragAction() & java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE) == 0)
271         return;
272       // get the label's text and put it inside a Transferable
273
// Transferable transferable = new StringSelection( DragLabel.this.getText() );
274
java.awt.datatransfer.Transferable JavaDoc transferable = new TransferableUniqueID(activeObject.getID());
275       // now kick off the drag
276
try {
277         // initial cursor, transferrable, dsource listener
278
event.startDrag(java.awt.dnd.DragSource.DefaultMoveNoDrop, transferable, dragSourceListener);
279       } catch (java.awt.dnd.InvalidDnDOperationException JavaDoc e) {
280       }
281     }
282   }
283
284   /**
285    * MyDragSourceListener
286    * a listener that will track the state of the DnD operation
287    *
288    * @see java.awt.dnd.DragSourceListener
289    * @see java.awt.dnd.DragSource
290    * @see java.awt.datatransfer.StringSelection
291    */

292   private class MyDragSourceListener implements java.awt.dnd.DragSourceListener JavaDoc {
293     /**
294      * @param e the event
295      */

296     public void dragDropEnd(java.awt.dnd.DragSourceDropEvent JavaDoc 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     /**
311      * @param e the event
312      */

313     public void dragEnter(java.awt.dnd.DragSourceDragEvent JavaDoc event) {
314       java.awt.dnd.DragSourceContext JavaDoc context = event.getDragSourceContext();
315       //intersection of the users selected action, and the source and target actions
316
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     /**
325      * @param e the event
326      */

327     public void dragOver(java.awt.dnd.DragSourceDragEvent JavaDoc event) {
328     }
329
330     /**
331      * @param e the event
332      */

333     public void dragExit(java.awt.dnd.DragSourceEvent JavaDoc event) {
334       java.awt.dnd.DragSourceContext JavaDoc context = event.getDragSourceContext();
335       context.setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop);
336     }
337
338     /**
339      * for example, press shift during drag to change to
340      * a link action
341      * @param e the event
342      */

343     public void dropActionChanged(java.awt.dnd.DragSourceDragEvent JavaDoc event) {
344       dragEnter(event);
345     }
346   } // end inner class MyDraSourceListener
347

348
349 }
350
Popular Tags