001: // FrameBrowser.java
002: // $Id: FrameBrowser.java,v 1.5 2000/08/16 21:37:27 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadm.editors;
007:
008: import java.awt.Component;
009: import java.awt.Container;
010: import java.awt.Cursor;
011: import java.awt.Dimension;
012: import java.awt.Frame;
013: import java.awt.Image;
014: import java.awt.Toolkit;
015: import java.awt.Window;
016:
017: import java.io.PrintStream;
018:
019: import java.net.URL;
020:
021: import org.w3c.jigadm.PropertyManager;
022: import org.w3c.jigadm.RemoteResourceWrapper;
023:
024: import org.w3c.jigadm.gui.ServerBrowser;
025:
026: import org.w3c.jigsaw.admin.RemoteAccessException;
027: import org.w3c.jigsaw.admin.RemoteResource;
028:
029: import org.w3c.tools.widgets.MessagePopup;
030: import org.w3c.tools.widgets.NodeHandler;
031: import org.w3c.tools.widgets.TreeBrowser;
032: import org.w3c.tools.widgets.TreeNode;
033:
034: public class FrameBrowser extends TreeBrowser implements NodeHandler {
035:
036: class Expander extends Thread {
037: TreeBrowser browser;
038: TreeNode nd;
039:
040: public void run() {
041: if (getLock()) {
042: notifyExpander(browser, nd);
043: unlock();
044: }
045: }
046:
047: Expander(TreeBrowser browser, TreeNode nd) {
048: this .browser = browser;
049: this .nd = nd;
050: }
051: }
052:
053: public static final boolean debug = false;
054: Image frameopened = null;
055: Image frameicon = null;
056: ServerBrowser serverBrowser = null;
057: FrameTreeListener tl = null;
058: TreeNode lastn = null;
059: RemoteResourceWrapper rootResource;
060: boolean locked;
061:
062: protected void errorPopup(String name, Exception ex) {
063: (new MessagePopup(name + " : " + ex.getMessage())).show();
064: }
065:
066: private Dimension preferredSize = null;
067:
068: public Dimension getPreferredSize() {
069: if (preferredSize == null) {
070: preferredSize = new Dimension(120, 100);
071: }
072: return preferredSize;
073: }
074:
075: private final Frame getFrame(Component c) {
076: while (!(c instanceof Frame)) {
077: c = c.getParent();
078: if (c == null)
079: return null;
080: }
081: return (Frame) c;
082: }
083:
084: public void setCursor(int cursor) {
085: getFrame(serverBrowser).setCursor(new Cursor(cursor));
086: Toolkit.getDefaultToolkit().sync();
087: }
088:
089: /**
090: * gets a lock to avoid adding node while removing other nodes
091: * it sets also the Cursor to WAIT_CURSOR
092: */
093:
094: protected synchronized boolean getLock() {
095: if (locked)
096: return false;
097: setCursor(Frame.WAIT_CURSOR);
098: locked = true;
099: return true;
100: }
101:
102: /**
103: * release the lock and sets the Cursor to the default
104: */
105:
106: protected synchronized void unlock() {
107: locked = false;
108: setCursor(Frame.DEFAULT_CURSOR);
109: }
110:
111: /**
112: * give the Root Resource of the browser
113: */
114:
115: public RemoteResourceWrapper getRootWrapper() {
116: return rootResource;
117: }
118:
119: public void renameNode(RemoteResourceWrapper rw, String label) {
120: TreeNode tn = getNode(rw);
121: // if it is a visible node, change the label and repaint
122: if (tn != null) {
123: try {
124: if (rw != rootResource)
125: label = getFrameName(rw.getResource(), label);
126: } catch (RemoteAccessException ex) {
127: //nothing
128: }
129: tn.setLabel(label);
130: repaint();
131: }
132: }
133:
134: public void removeNode(RemoteResourceWrapper rw) {
135: if (getLock()) {
136: if (getNode(rw) != null) {
137: removeBranch(getNode(rw));
138: tl.nodeRemoved(rw);
139: }
140: unlock();
141: repaint();
142: }
143: }
144:
145: public void insertNode(RemoteResourceWrapper father,
146: RemoteResourceWrapper son) {
147: RemoteResource newFrame = son.getResource();
148: String name = null;
149: try {
150: name = getFrameName(newFrame);
151: } catch (RemoteAccessException ex) {
152: // fancy thing
153: errorPopup("RemoteAccessException", ex);
154: }
155: insertNode(father, son, name);
156: }
157:
158: public void insertNode(RemoteResourceWrapper father,
159: RemoteResourceWrapper son, String name) {
160: TreeNode fatherNode;
161: boolean ic = false;
162:
163: if (father == null)
164: System.out.println("Error null father");
165: fatherNode = getNode(father);
166: if (fatherNode.getChildren() == TreeNode.NOCHILD)
167: return;
168:
169: if (fatherNode == null)
170: return; // this should never happen, but...
171: try {
172: ic = son.getResource().isContainer();
173: } catch (RemoteAccessException ex) {
174: // fancy thing
175: errorPopup("RemoteAccessException", ex);
176: }
177: insert(fatherNode, son, this , name, frameicon);
178: repaint();
179: }
180:
181: protected RemoteResourceWrapper getResources(
182: RemoteResourceWrapper rw, String name) {
183: RemoteResource resource = null;
184: if (rw != null) {
185: try {
186: resource = rw.getResource().loadResource(name);
187: } catch (RemoteAccessException ex) {
188: errorPopup("RemoteAccessException", ex);
189: ex.printStackTrace();
190: }
191: }
192: return new RemoteResourceWrapper(rw, resource, serverBrowser);
193: }
194:
195: private final Image getImage(String name) {
196: Image img;
197: img = Toolkit.getDefaultToolkit().getImage(name);
198: return img;
199: }
200:
201: public FrameBrowser(FrameTreeListener tl, RemoteResourceWrapper rrw) {
202: boolean authorized = false;
203: RemoteResource rr = null;
204: PropertyManager pm = PropertyManager.getPropertyManager();
205: serverBrowser = rrw.getBrowser();
206: this .tl = tl;
207:
208: locked = false;
209: frameicon = getImage(pm.getIconLocation("frame"));
210: frameopened = getImage(pm.getIconLocation("frameopened"));
211:
212: rootResource = rrw;
213:
214: String name = null;
215: while (!authorized) {
216: try {
217: authorized = true;
218: rr = rrw.getResource();
219: name = (String) rr.getValue("identifier");
220: } catch (RemoteAccessException ex) {
221: if (ex.getMessage().equals("Unauthorized")) {
222: authorized = false;
223: } else {
224: errorPopup("Can't read resource identifier", ex);
225: }
226: } finally {
227: if (!authorized) {
228: serverBrowser.popupDialog("admin");
229: }
230: }
231: }
232: if (name != null)
233: initialize(rootResource, name, this , frameicon);
234: }
235:
236: public void notifySelect(TreeBrowser browser, TreeNode nd) {
237: if (tl != null) {
238: tl
239: .editedChanged(this , (RemoteResourceWrapper) nd
240: .getItem());
241: }
242: browser.unselect(lastn);
243: browser.select(nd);
244: browser.repaint();
245: lastn = nd;
246: }
247:
248: /**
249: * Handles Select notifications.
250: *
251: * we simply select the node and redraw the browser.
252: */
253: public void notifyExecute(TreeBrowser browser, TreeNode node) {
254: if (tl != null) {
255: tl.focusChanged((RemoteResourceWrapper) node.getItem());
256: }
257: if (!node.equals(lastn)) {
258: browser.unselect(lastn);
259: lastn = null;
260: }
261: browser.repaint();
262: }
263:
264: public void notifyExpand(TreeBrowser browser, TreeNode nd) {
265: (new Expander(browser, nd)).start();
266: }
267:
268: public boolean isDirectory(TreeBrowser browser, TreeNode nd) {
269: //FIXME ??
270: return true;
271: }
272:
273: /**
274: * Get the pretty ResourceFrame name
275: * @param frame The ResourceFrame
276: * @param name The ResourceFrame name
277: * @exception RemoteAccessException if a remote access error occurs.
278: */
279: public String getFrameName(RemoteResource frame, String name)
280: throws RemoteAccessException {
281: String className = frame.getClassHierarchy()[0];
282: String shortName = className.substring(className
283: .lastIndexOf('.') + 1);
284: return shortName.concat(" (").concat(name).concat(")");
285: }
286:
287: /**
288: * Get the pretty ResourceFrame name
289: * @param frame The ResourceFrame
290: * @exception RemoteAccessException if a remote access error occurs.
291: */
292: public String getFrameName(RemoteResource frame)
293: throws RemoteAccessException {
294: String className = frame.getClassHierarchy()[0];
295: String shortName = className.substring(className
296: .lastIndexOf('.') + 1);
297: String frameName = (String) frame.getValue("identifier");
298: return shortName.concat(" (").concat(frameName).concat(")");
299: }
300:
301: /**
302: * Handles Expand notifications
303: *
304: * if the node is a directory, we list its content and insert the
305: * directories and files in the browser.
306: */
307: public void notifyExpander(TreeBrowser browser, TreeNode nd) {
308: boolean authorized = true;
309: if (tl != null) {
310: tl.focusChanged((RemoteResourceWrapper) nd.getItem());
311: }
312: RemoteResourceWrapper rrw = null;
313: RemoteResource rr = null;
314: rrw = (RemoteResourceWrapper) nd.getItem();
315: if (rrw == null)
316: return;
317: rr = rrw.getResource();
318: setCursor(Frame.WAIT_CURSOR);
319: try {
320: RemoteResource frames[] = rr.getFrames();
321: if (frames != null) {
322: if (debug)
323: System.out.println("Found " + frames.length
324: + " identifiers");
325: for (int i = 0; i < frames.length; i++) {
326: RemoteResourceWrapper frw = new RemoteResourceWrapper(
327: rrw, frames[i], serverBrowser);
328: browser.insert(nd, frw, this ,
329: getFrameName(frames[i]), frameicon);
330: }
331: }
332: } catch (RemoteAccessException ex) {
333: if (ex.getMessage().equals("Unauthorized")) {
334: authorized = false;
335: } else {
336: errorPopup("RemoteAccessException", ex);
337: ex.printStackTrace();
338: }
339: } finally {
340: if (!authorized) {
341: serverBrowser.popupDialog("admin");
342: }
343: }
344: setCursor(Frame.DEFAULT_CURSOR);
345: nd.setIcon(frameopened);
346: browser.repaint();
347: }
348:
349: /**
350: * Handles Collapse notifications
351: *
352: * we simply collapse the given node and repaint the browser.
353: */
354: public void notifyCollapse(TreeBrowser browser, TreeNode node) {
355: if (getLock()) {
356: if (tl != null) {
357: tl.focusChanged((RemoteResourceWrapper) node.getItem());
358: }
359: browser.collapse(node);
360: if (!node.equals(lastn)) {
361: browser.unselect(lastn);
362: lastn = null;
363: }
364: unlock();
365: browser.repaint();
366: node.setIcon(frameicon);
367: }
368: }
369:
370: }
|