What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.xml.tree.children;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.nodes.Children;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.FilterNode;
import org.openide.nodes.NodeMemberEvent;
import org.openide.nodes.NodeReorderEvent;
import org.netbeans.modules.xml.tree.nodes.AbstractDataNode;
import org.netbeans.modules.xml.tax.cookies.TreeDocumentCookie;
import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie;
import org.netbeans.tax.TreeDocumentRoot;
import org.openide.util.WeakListener;
/**
* Children extending NodeListChildren by adding support for wait and
* fatal error node that indicates states when NodeList is not
* available, yet.
*
*
*
* @author Libor Kramolis
* @author Petr Kuzel
* @version 0.1
*/
public class DataObjectChildren extends FilterNode.Children {
private static final String ICON_DIR_BASE = "org/netbeans/modules/xml/tree/resources/"; // NOI18N
private static final String WAIT_ICON_BASE = ICON_DIR_BASE + "waitNode"; // NOI18N
private static final Object WAIT_NODE_KEY = WAIT_ICON_BASE; // can be same as icon base
private static final String ERROR_ICON_BASE = ICON_DIR_BASE + "errorNode"; // NOI18N
private static final Object ERROR_NODE_KEY = ERROR_ICON_BASE;
/** Lazy initialized instance of wait please... node */
private AbstractNode[] waitNode = null;
/** Lazy initialized instance of error node */
private AbstractNode[] errorNode = null;
/** listens at data node changes. */
private final RootNodeListener rnListener;
private PropertyChangeListener weakRNListener;
/** listens at tree changes. */
private final TreeEditorListener teListener;
private PropertyChangeListener weakTEListener;
/** Last cached document cookie. */
private final TreeEditorCookie editor;
//
// init
//
/** */
public DataObjectChildren (TreeEditorCookie editor) {
super (null);
this.editor = editor;
this.teListener = new TreeEditorListener();
this.rnListener = new RootNodeListener();
}
//
// FilterNode.Children
//
/**
*/
protected void finalize () {
if ( canDelegate() ) {
super.finalize();
}
}
/**
*/
public Object clone () {
return new DataObjectChildren (editor);
}
/**
*/
protected void addNotify () {
updateOriginal (getRootNode());
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+++ DataObjectChildren::addNotify: canDelegate = " + canDelegate()); // NOI18N
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+++ ::addNotify: status " + editor.getStatus()); // NOI18N
if ( canDelegate() ) {
super.addNotify();
} else {
setKeys();
}
weakTEListener = WeakListener.propertyChange(teListener, editor);
editor.addPropertyChangeListener (weakTEListener);
Node rootNode = getNode();
weakRNListener = WeakListener.propertyChange(rnListener, rootNode);
rootNode.addPropertyChangeListener (rnListener);
// initialize parsing
editor.prepareDocumentRoot();
}
/**
* Clear itself.
*/
protected void removeNotify () {
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+++ DataObjectChildren::removeNotify"); // NOI18N
if ( canDelegate() ) {
super.removeNotify();
updateOriginal (null);
} else {
setKeys (Collections.EMPTY_SET);
}
if (weakTEListener != null) {
editor.removePropertyChangeListener (weakTEListener);
}
if (weakRNListener != null) {
getNode().removePropertyChangeListener (rnListener);
}
errorNode = null;
waitNode = null;
}
/**
*/
public Node findChild (String name) {
if ( canDelegate() ) {
return super.findChild (name);
} else {
if ( (errorNode != null) &&
(errorNode[0].getName().equals (name)) ) {
return errorNode[0];
} else if ( (waitNode != null) &&
(waitNode[0].getName().equals (name)) ) {
return waitNode[0];
}
}
return null;
}
/**
*/
protected Node[] createNodes (Object key) {
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+++ DataObjectChildren::createNodes: key = " + key); // NOI18N
Node[] nodes = null;
if ( canDelegate() ) {
nodes = super.createNodes (key);
} else {
if (key == WAIT_NODE_KEY) {
nodes = createWaitNode();
} else if (key == ERROR_NODE_KEY) {
nodes = createErrorNode();
} else {
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+++ ::createNodes: key = " + key);//, new RuntimeException ("Unexpected key.")); // NOI18N
nodes = new Node[0];
}
}
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+++ ::createNodes: nodes = " + nodes); // NOI18N
return nodes;
}
/**
*/
public boolean add (Node[] arr) {
if ( canDelegate() ) {
return super.add (arr);
}
return false;
}
/**
*/
public boolean remove (Node[] arr) {
if ( canDelegate() ) {
return super.remove (arr);
}
return false;
}
/**
*/
protected void filterChildrenAdded (NodeMemberEvent ev) {
if ( canDelegate() ) {
super.filterChildrenAdded (ev);
}
}
/**
*/
protected void filterChildrenRemoved (NodeMemberEvent ev) {
if ( canDelegate() ) {
super.filterChildrenRemoved (ev);
}
}
/**
*/
protected void filterChildrenReordered (NodeReorderEvent ev) {
if ( canDelegate() ) {
super.filterChildrenReordered (ev);
}
}
/**
*/
public Node[] getNodes (boolean optimalResult) {
return super.getNodes (optimalResult && canDelegate()); // #27166
}
//
// itself
//
/**
* Recreates all keys and set them.
*/
protected final void refreshKeys () {
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+++ DataObjectChildren::refreshKeys: isInitialized = " + isInitialized()); // NOI18N
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+++ ::refreshKeys: status " + editor.getStatus()); // NOI18N
// if (!!! isInitialized())
// return;
updateOriginal (getRootNode());
if ( canDelegate() ) {
super.removeNotify();
super.addNotify();
} else {
setKeys();
}
}
/**
*/
private void setKeys () {
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+++ DataObjectChildren::setKeys"); // NOI18N
setKeys (createKeys());
}
/**
* Create keys according to tree status.
*/
protected synchronized Collection createKeys () {
final Collection keys = new LinkedList();
final int status = editor.getStatus();
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("DataObjectChildren::createKeys: status " + status); // NOI18N
switch (status) {
case TreeEditorCookie.STATUS_NOT:
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\tsetting null object list..."); // NOI18N
keys.add (WAIT_NODE_KEY);
break;
case TreeEditorCookie.STATUS_ERROR:
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\tsetting null object list..."); // NOI18N
keys.add (ERROR_NODE_KEY);
break;
default:
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nDataObjectChildren::createKeys: status = " + status);//, new RuntimeException ("Unexpected status.")); // NOI18N
}
return keys;
}
/** Create waiting node. */
private final Node[] createWaitNode () {
if (waitNode == null) {
waitNode = new AbstractNode[] { new AbstractNode (Children.LEAF) };
waitNode[0].setName (Util.THIS.getString ("PROP_Wait"));
waitNode[0].setIconBase (WAIT_ICON_BASE);
}
return waitNode;
}
/** Create error node. */
private final Node[] createErrorNode () {
if (errorNode == null) {
errorNode = new AbstractNode[] { new AbstractNode (Children.LEAF) };
errorNode[0].setName (Util.THIS.getString ("PROP_Error"));
errorNode[0].setIconBase (ERROR_ICON_BASE);
}
return errorNode;
}
/**
*/
private Node getRootNode () {
return ((AbstractDataNode)getNode()).getRootNode();
}
/**
*/
private boolean canDelegate () {
return original != null;
}
/**
*/
private boolean updateOriginal (Node newOriginal) {
Node oldOriginal = original;
original = newOriginal;
return oldOriginal != original;
}
//
// TreeEditorListener
//
/**
*
*/
private class TreeEditorListener implements PropertyChangeListener {
/**
*/
public void propertyChange (PropertyChangeEvent pche) {
String property = pche.getPropertyName();
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("DataObjectChildren::TreeEditorListener propertyName = '" + property + "'"); // NOI18N
if (TreeEditorCookie.PROP_STATUS.equals (property)) {
refreshKeys();
}
}
} // end: class TreeEditorListener
//
// RootNodeListener
//
/**
*
*/
private class RootNodeListener implements PropertyChangeListener {
/**
*/
public void propertyChange (PropertyChangeEvent pche) {
String property = pche.getPropertyName();
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("DataObjectChildren::RootNodeListener propertyName = '" + property + "'"); // NOI18N
if (AbstractDataNode.PROP_ROOT_NODE.equals (property)) {
refreshKeys();
}
}
} // end: class RootNodeListener
}
|