001: // RemoteResourceWrapperNode.java
002: // $Id: RemoteResourceWrapperNode.java,v 1.13 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import java.awt.Cursor;
009:
010: import javax.swing.JTree;
011: import javax.swing.tree.TreeNode;
012: import javax.swing.tree.TreePath;
013: import javax.swing.tree.MutableTreeNode;
014:
015: import java.util.Enumeration;
016: import java.util.Vector;
017:
018: import org.w3c.jigadmin.RemoteResourceWrapper;
019: import org.w3c.jigadmin.PropertyManager;
020: import org.w3c.jigadmin.gui.Message;
021:
022: import org.w3c.jigsaw.admin.RemoteAccessException;
023:
024: import org.w3c.tools.sorter.Sorter;
025:
026: import org.w3c.util.ArrayEnumeration;
027:
028: /**
029: * The TreeNode for Resources
030: * @version $Revision: 1.13 $
031: * @author Benoît Mahé (bmahe@w3.org)
032: */
033: public class RemoteResourceWrapperNode implements MutableTreeNode,
034: RemoteNode {
035:
036: protected RemoteResourceWrapper rrw = null;
037: protected RemoteResourceWrapperNode parent = null;
038: protected String name = null;
039: protected Vector children = null;
040:
041: /**
042: * Load the children of this node.
043: */
044: protected synchronized void loadChildren() {
045: String names[] = null;
046: RemoteResourceWrapper child = null;
047:
048: children = new Vector();
049:
050: try {
051: names = rrw.getResource().enumerateResourceIdentifiers();
052: } catch (RemoteAccessException ex) {
053: Message.showErrorMessage(rrw, ex);
054: }
055: if (names == null)
056: return;
057: Sorter.sortStringArray(names, true);
058: for (int i = 0; i < names.length; i++) {
059: try {
060: child = rrw.getChildResource(names[i]);
061: } catch (RemoteAccessException ex) {
062: Message.showErrorMessage(rrw, ex, names[i]);
063: }
064: if (child != null) {
065: RemoteResourceWrapperNode node = new RemoteResourceWrapperNode(
066: this , child, names[i]);
067: children.add(node);
068: }
069: }
070: }
071:
072: //RemoteNode part
073: /**
074: * Invoked whenever this node is about to be expanded.
075: */
076: public void nodeWillExpand() {
077: children = null;
078: }
079:
080: /**
081: * Invoked whenever this node is about to be collapsed.
082: */
083: public void nodeWillCollapse() {
084: //children = null;
085: }
086:
087: /**
088: * Get the associated RemoteResourceWrapper.
089: * @return the associated RemoteResourceWrapper
090: */
091: public RemoteResourceWrapper getResourceWrapper() {
092: return rrw;
093: }
094:
095: /**
096: * Load the children if needed.
097: */
098: protected void acquireChildren() {
099: rrw.getServerBrowser().setCursor(Cursor.WAIT_CURSOR);
100: if (children == null)
101: loadChildren();
102: rrw.getServerBrowser().setCursor(Cursor.DEFAULT_CURSOR);
103: }
104:
105: //TreeNode part
106:
107: /**
108: * Returns the child TreeNode at index childIndex.
109: * @param childIndex the index of the child to return
110: * @return a TreeNode instance
111: */
112: public TreeNode getChildAt(int childIndex) {
113: acquireChildren();
114: return (TreeNode) children.elementAt(childIndex);
115: }
116:
117: /**
118: * Returns the number of children TreeNodes the receiver contains.
119: * @return the number of children TreeNodes the receiver contains
120: */
121: public int getChildCount() {
122: acquireChildren();
123: return children.size();
124: }
125:
126: /**
127: * Returns the parent TreeNode of the receiver.
128: * @return a TreeNode
129: */
130: public TreeNode getParent() {
131: return parent;
132: }
133:
134: /**
135: * Returns the index of node in the receivers children. If the receiver
136: * does not contain node, -1 will be returned.
137: * @return an int.
138: */
139: public int getIndex(TreeNode node) {
140: acquireChildren();
141: return children.indexOf(node);
142: }
143:
144: /**
145: * Returns true if the receiver allows children.
146: * @return an int.
147: */
148: public boolean getAllowsChildren() {
149: try {
150: return rrw.getResource().isContainer();
151: } catch (RemoteAccessException ex) {
152: Message.showErrorMessage(rrw, ex);
153: }
154: return false;
155: }
156:
157: /**
158: * Returns true if the receiver is a leaf.
159: * @return a boolean
160: */
161: public boolean isLeaf() {
162: return (!getAllowsChildren());
163: }
164:
165: /**
166: * Returns the children of the reciever as an Enumeration.
167: * @return an Enumeration
168: */
169: public Enumeration children() {
170: acquireChildren();
171: return children.elements();
172: }
173:
174: //MutableTreeNode part
175:
176: /**
177: * Adds child to the receiver at index. child will be messaged
178: * with setParent.
179: * @param child the child to add.
180: * @param index the index of the new child.
181: */
182: public void insert(MutableTreeNode child, int index) {
183: acquireChildren();
184: children.insertElementAt(child, index);
185: }
186:
187: /**
188: * Removes the child at index from the receiver.
189: * @param the index of the child to remove.
190: */
191: public void remove(int index) {
192: acquireChildren();
193: children.remove(index);
194: }
195:
196: /**
197: * Removes node from the receiver. setParent will be messaged on node
198: * @param node the node to remove
199: */
200: public void remove(MutableTreeNode node) {
201: children.remove(node);
202: }
203:
204: /**
205: * Resets the user object of the receiver to object.
206: * @param object the new user object, actually the new identifier.
207: */
208: public void setUserObject(Object object) {
209: if (object instanceof String) {
210: PropertyManager pm = PropertyManager.getPropertyManager();
211: if (pm.isEditable(rrw)) {
212: try {
213: rrw.getResource().setValue("identifier",
214: (String) object);
215: name = (String) object;
216: } catch (RemoteAccessException ex) {
217: Message.showErrorMessage(rrw, ex);
218: }
219: }
220: }
221: }
222:
223: /**
224: * Removes the receiver from its parent.
225: */
226: public void removeFromParent() {
227: if (parent != null)
228: parent.remove(this );
229: }
230:
231: /**
232: * Sets the parent of the receiver to newParent.
233: * @param newParent the new parent.
234: */
235: public void setParent(MutableTreeNode newParent) {
236: this .parent = (RemoteResourceWrapperNode) newParent;
237: }
238:
239: /**
240: * Return the string reoresentation of this node.
241: * @return its name
242: */
243: public String toString() {
244: return name;
245: }
246:
247: /**
248: * Constructor
249: * @param parent The parent node
250: * @param rrw The associated RemoteResourceWrapper
251: * @param name The name of this node
252: */
253: protected RemoteResourceWrapperNode(
254: RemoteResourceWrapperNode parent,
255: RemoteResourceWrapper rrw, String name) {
256: this .parent = parent;
257: this .name = name;
258: this .rrw = rrw;
259:
260: }
261:
262: /**
263: * Constructor
264: * @param rrw The associated RemoteResourceWrapper
265: * @param name The name of this node
266: */
267: protected RemoteResourceWrapperNode(RemoteResourceWrapper rrw,
268: String name) {
269: this.rrw = rrw;
270: this.name = name;
271: }
272:
273: }
|