Source code: org/mitre/cvw/ContentsCoordinator.java
1 /*
2 * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3 * All rights reserved.
4 * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5 */
6
7 package org.mitre.cvw;
8
9 import java.awt.*;
10 import java.util.*;
11 import javax.swing.*;
12
13 import java.net.URL;
14 import java.net.MalformedURLException;
15 import java.io.IOException;
16
17 /**
18 * This class coordinates the contents of the room and all folders.
19 * @version 1.1
20 * @author Dee Goepel
21 */
22 public class ContentsCoordinator extends JPanel {
23
24 DetailList ContentsWindow;
25 CVWFolderWindow InventoryWindow;
26 Hashtable contentsWindows = new Hashtable();
27 CVWCoordinator jcvw;
28 String prevObjNums = " ", userLocationString;
29
30 /* 3/25/97 dage - Begin static/class methods
31 * create a private class variable and an accessor method
32 * so that all objects can access the current instance of ContentsCoordinator
33 * need this because all instances of DetailLists need the icon from applet (ContentsCoordinator)
34 */
35 private static ContentsCoordinator contents;
36
37 /** Get an instance of ContentsCoordinator. If one does not exist, it will be created
38 * @return The only instance of ContentsCoordinator
39 */
40 public static ContentsCoordinator getInstance() {
41 if (contents == null) {
42 contents = new ContentsCoordinator();
43 }
44
45 return contents;
46 }
47
48 /**
49 * Returns the carry folder/inventory window of the current user.
50 * @return the carry folder/inventory window
51 */
52 public static CVWFolderWindow getCarryingFolder() {
53 return contents.InventoryWindow;
54 }
55
56 /**
57 * Return the image representing communication to the document server
58 * @return Image
59 */
60 static public Image getDocServIcon() {
61 return CVWCoordinator.getDocServIcon();
62 }
63
64 /*
65 * End of static/class methods
66 */
67
68 /**
69 * Constructor
70 */
71 protected ContentsCoordinator() {
72 super();
73 jcvw = CVWCoordinator.getInstance();
74 init();
75 }
76
77 /**
78 * Initialzes the gui.
79 */
80 public void init() {
81 int maxWidth =600;
82 int maxHeight =600;
83
84 setLayout(new BorderLayout());
85 setSize(maxWidth,maxHeight);
86 }
87
88 /**
89 * Returns the preferred size of this main window.
90 * @return the preferred size of this main window
91 */
92 public Dimension getPreferredSize() {
93 return getMinimumSize();
94 }
95
96
97 /**
98 * Returns the minimum size of this main window.
99 * @return the minimum size of this main window
100 */
101 public Dimension getMinimumSize() {
102 return new Dimension(jcvw.getSize().width, 120);
103 }
104
105 /**
106 * Initializes the window for the inventory/carrying folder of the current user.
107 */
108 public void initCarryFolder() {
109 if (InventoryWindow != null) return;
110 InventoryWindow = new CVWFolderWindow(this);
111 InventoryWindow.setVisible(false);
112 }
113
114
115 /* 1/7/98 dage - noticed this code was identical to objectContents code so remove
116 * and call objectContents with "RoomContents" flag.
117 */
118 /**
119 * Initializes the contents of the current room.
120 * @param objNumstr a space delimited list of object numbers in the room
121 * @param ownerNamestr a space delimited list of owner names of the objects in the room
122 * @see #objectContents
123 */
124 public void initContents(String objNumstr, String ownerNamestr) {
125
126 /* 1/7/98 dage - also check to see if "Document" is a type, if so refresh w/DocServer
127 */
128
129 if (objNumstr.equals(prevObjNums)) {
130 return;
131 }
132
133 prevObjNums = objNumstr;
134 if (jcvw != null)
135 userLocationString = jcvw.roomName;
136
137
138 objectContents("RoomContents", objNumstr, ownerNamestr);
139 }
140
141 /**
142 * Refreshes the room contents panel as well as all open contents windows.
143 */
144 public void doLayout() {
145 super.doLayout();
146 if (ContentsWindow != null)
147 ContentsWindow.doLayouts();
148 }
149
150 /* 1/7/98 dage - also check to see if "Document" is a type, if so refresh w/DocServer
151 * 2/7/98 dage - Changed "SC to xxx" to just "SC", "Doc/xxx" to "xxx" and
152 "Document" to "---"
153 */
154 /**
155 * MCP from server contains list of objects and owner names
156 * @param strContainer one of the following: "Inventory", "RoomContents", or an object number representing the folder.
157 * @param objNumstr a list of object numbers
158 * @param ownerNamestr we dont care about this anymore
159 */
160 public void objectContents(String strContainer, String objNumstr, String ownerNamestr) {
161 int contentsCount;
162 CVWObjNum container = null;
163 CVWFolderWindow folder = null; // if this is a folder
164
165 if (strContainer.equals("Inventory") || strContainer.equals("RoomContents")) {
166 ;
167 } else { // this is a folder
168 container = new CVWObjNum(strContainer);
169 folder = (CVWFolderWindow)(contentsWindows.get(container));
170 }
171
172 //System.err.println("container = "+strContainer + " contents = "+ objNumstr);
173
174 StringTokenizer objs = new StringTokenizer(objNumstr, " ");
175 //StringTokenizer owns = new StringTokenizer(ownerNamestr, " ");
176 contentsCount = objs.countTokens();
177
178 //System.err.println("in objContents" + contentsCount);
179
180 CVWObject contentsObjs[] = new CVWObject[contentsCount];
181
182 /* 11/25/96 dage - changed 50 to 9 and 500 to 200
183 * put a check to see if the contentsObjs was null before getting name, type
184 * 12/22/98 dage - if not in cache ask server for info
185 */
186 String oNum;
187 for (int i=0;i<contentsCount; i++) {
188 oNum = objs.nextToken();
189 contentsObjs[i] = (CVWObject)(jcvw.cache.get(oNum));
190 if (contentsObjs[i] == null) {
191 System.err.println("object "+oNum+" is null");
192 CVWServerComm.sendMCPCmdToServer("#$#cvw-object-info-request", "object: " + oNum);
193 int q=1;
194 while ((contentsObjs[i] == null) && (q<3)) {
195 System.err.println("waiting for non-null contents value, try "+q);
196 try {
197 Thread.sleep(200);
198 } catch (InterruptedException e) { }
199 contentsObjs[i] = (CVWObject)(jcvw.cache.get(oNum));
200 q++;
201 }
202 }
203 }
204
205 //System.err.println("done getting cache objs");
206
207 boolean checkOutCheck = false;
208 if (strContainer.equals("Inventory"))
209 checkOutCheck = true;
210 else if (folder != null && folder.DoT.equals("drop"))
211 checkOutCheck = true;
212
213 refreshDocTypes(contentsObjs, false, checkOutCheck);
214
215 if (strContainer.equals("RoomContents")) {
216 if (ContentsWindow == null) {
217 ContentsWindow = new DetailList(contentsObjs, "take", "null");
218 add("Center", ContentsWindow);
219 ContentsWindow.setDropTargetListener();
220 } else
221 ContentsWindow.updateDetailList(contentsObjs);
222
223 doLayout();
224 ContentsWindow.doLayouts();
225 } else if (strContainer.equals("Inventory")) { //(container == null)
226 if (InventoryWindow == null)
227 initCarryFolder();
228 InventoryWindow.refreshWindow(contentsObjs);
229 InventoryWindow.list.doLayouts();
230 } else { // this is a folder
231 if (folder != null) {
232 folder.refreshWindow(contentsObjs);
233 }
234 //else System.err.println(container + " is not in contentsWindows");
235 }
236 }
237
238 /* 11/25/96 dage - if item is null from server set up the appropriate
239 * values in the arrays
240 * 8/6/97 dage - while poking a while back, realized that the CVWCoordinator was
241 * converting doc ids from String to Integer and then DocServerInterface was
242 * converting from Integer to String so left as String... havent seen any problems
243 */
244
245 /**
246 * FUNCT DESC
247 * @param contentsObjs
248 * @param all
249 * @param checkOutCheck
250 */
251 public void refreshDocTypes(CVWObject contentsObjs[], boolean all, boolean checkOutCheck) {
252 int contentsCount = contentsObjs.length;
253 //Hashtable docBatchJob = new Hashtable();
254 Vector docBatchJob = new Vector(contentsCount);
255 Vector sendingDocids = new Vector(contentsCount);
256
257 for (int i=0;i<contentsCount; i++) {
258 if (contentsObjs[i] != null && contentsObjs[i] instanceof CVWDocument) {
259 CVWDocument theDoc = (CVWDocument)contentsObjs[i];
260
261 // if null, put it in the batch to request from the docserver
262 if (all || (theDoc.docType == null)) {
263 docBatchJob.addElement(theDoc);
264 if (! theDoc.docID.equals("000"))
265 sendingDocids.addElement(theDoc.docID);
266 }
267 }
268 }
269 //System.err.println("starting batch");
270
271 // request the doctypes from the docserver
272 if (docBatchJob.isEmpty())
273 return;
274 CVWDocument.getDocTypes(docBatchJob, sendingDocids, checkOutCheck);
275 //System.err.println("done doc batch");
276 }
277
278 /**
279 * Updates the room name in the gui, assumes that the user just moved into a different
280 * room and therefore closes all folders from previous room.
281 * <br> MCP receive cvw-env-changed
282 * @param roomName the new room name
283 */
284 public void updateContentsHeading(String roomName) {
285 ContentsWindow.updateHeading(" Contents of \""+roomName+"\"");
286 closeAllRoomFolders();
287 }
288
289 /* 6/3/98 dage - created so as to make the dialogs modal to the
290 * appropriate folder window
291 */
292 /**
293 * Returns the CVWFolderWindow associated with the object number.
294 * @param obj the object number of the CVWFolder desired
295 * @return an instance ot CVWFolderWindow
296 */
297 public CVWFolderWindow getOpenFolder(CVWObjNum obj) {
298 return (CVWFolderWindow)(contentsWindows.get(obj));
299 }
300
301 /**
302 * Opens a folder.
303 *
304 * @param obj the CVWObject to be opened
305 * @param fullpath the full path of the object includes either room name or Carrying Folder
306 * @param perms "1" means writeable, currently all folders are writable
307 */
308 public void openFolder(CVWObject object, String fullpath, String perms) {
309 CVWFolderWindow currentFolder;
310
311 CVWObjNum container = object.objNum;
312
313 currentFolder = (CVWFolderWindow)(contentsWindows.get(container));
314 if (currentFolder != null) {
315 //postEvent(new Event(currentFolder, Event.WINDOW_DEICONIFY, currentFolder));
316 //System.err.println("## just deiconified "+container+" using: target = "+currentFolder);
317 currentFolder.setVisible(true);
318 } else {
319 String title = object.name;
320 String type = "take";
321 if (fullpath.startsWith("Carrying Folder"))
322 type = "drop";
323 currentFolder = new CVWFolderWindow(this, title, container, true,
324 "Contents of \""+fullpath+"\"", type);
325 currentFolder.setVisible(true);
326 contentsWindows.put(container, currentFolder);
327 //System.err.println("put "+container.strValue() + " in contentsWindows");
328 }
329 }
330
331 /**
332 * Opens the user's Carrying folder.
333 */
334 public void openCarryFolder() {
335 if (InventoryWindow == null)
336 initCarryFolder();
337 InventoryWindow.setVisible(true);
338 }
339
340 /*
341 * Closes a folder given an object number.
342 * @param objNum the object number of the folder to close
343 */
344 public void closeFolder(CVWObjNum objNum) {
345 CVWFolderWindow folder = (CVWFolderWindow)contentsWindows.get(objNum);
346 if (folder == null) return; //folder wasnt open
347 folder.hide();
348 contentsWindows.remove(objNum);
349 folder.dispose();
350 }
351
352 /**
353 * Closes all folders.
354 */
355 public void closeAllFolders() {
356 if (InventoryWindow != null) {
357 InventoryWindow.setVisible(false);
358 InventoryWindow.dispose();
359 InventoryWindow = null;
360 }
361
362 if (contentsWindows.isEmpty())
363 return;
364
365 Enumeration windows = contentsWindows.keys();
366 while (windows.hasMoreElements()) {
367 Object key = windows.nextElement();
368 CVWFolderWindow currentFolder = (CVWFolderWindow)(contentsWindows.get(key));
369 currentFolder.setVisible(false);
370 contentsWindows.remove(key);
371 currentFolder.dispose();
372 }
373 }
374
375 /**
376 * Closes all folders in the current room.
377 */
378 public void closeAllRoomFolders() {
379 if (contentsWindows.isEmpty())
380 return;
381
382 Enumeration windows = contentsWindows.keys();
383 while (windows.hasMoreElements()) {
384 Object key = windows.nextElement();
385 CVWFolderWindow currentFolder = (CVWFolderWindow)(contentsWindows.get(key));
386 if (currentFolder.fullpath.indexOf("Carrying Folder") < 0) {
387 currentFolder.setVisible(false);
388 contentsWindows.remove(key);
389 currentFolder.dispose();
390 }
391 }
392 }
393
394
395 /* 3/25/97 dage - need to check for null because this might be called before any
396 * vars are initialized.
397 */
398 /**
399 * Shows the doc server communication icon in all open windows, and room contents.
400 * @param b whether to show or hide the icon
401 */
402 public void showDocServIcon(boolean b) {
403 //System.err.println("in show doc serv icon in contents coord " + b);
404
405 if (ContentsWindow != null)
406 ContentsWindow.showDocServIcon(b);
407 if (InventoryWindow != null)
408 InventoryWindow.showDocServIcon(b);
409
410 if (contentsWindows == null || contentsWindows.isEmpty())
411 return;
412
413 Enumeration windows = contentsWindows.elements();
414 while (windows.hasMoreElements()) {
415 CVWFolderWindow currentFolder = (CVWFolderWindow)windows.nextElement();
416 currentFolder.showDocServIcon(b);
417 }
418 }
419
420 /* 2/14/97 dage - added method to change the cursor.
421 * 7/16/97 dage - this is part of jcvw frame, so just tell all folders
422 * to change their cursor.
423 */
424 /**
425 * Toggles the cursor between normal and wait while document server communication
426 * is happening.
427 * @param b <code>true</code> show the WAIT cursor
428 */
429 public void waitCursor(boolean b) {
430
431 if (InventoryWindow != null)
432 InventoryWindow.waitCursor(b);
433
434 if (contentsWindows == null || contentsWindows.isEmpty())
435 return;
436
437 Enumeration windows = contentsWindows.elements();
438 while (windows.hasMoreElements()) {
439 CVWFolderWindow currentFolder = (CVWFolderWindow)windows.nextElement();
440 currentFolder.waitCursor(b);
441 }
442 }
443 }
444