下面列出了怎么用java.util.prefs.NodeChangeEvent的API类实例代码及写法,或者点击链接到github查看源代码。
private void fireNodeEvents(List<EventBag<NodeChangeListener, NodeChangeEvent>> events) {
if (noEvents) {
return;
}
for(EventBag<NodeChangeListener, NodeChangeEvent> bag : events) {
for(NodeChangeEvent event : bag.getEvents()) {
for(NodeChangeListener l : bag.getListeners()) {
try {
if ((event instanceof NodeChangeEventExt) && ((NodeChangeEventExt) event).isRemovalEvent()) {
l.childRemoved(event);
} else {
l.childAdded(event);
}
} catch (Throwable t) {
LOG.log(Level.WARNING, null, t);
}
}
}
}
}
protected void notifyNCL(NodeChangeEvent evt, boolean bAdded) {
synchronized(nclSet) {
for(Iterator<NodeChangeListener> it = nclSet.iterator(); it.hasNext(); ) {
NodeChangeListener ncl = it.next();
try {
if(bAdded) {
ncl.childAdded(evt);
} else {
ncl.childRemoved(evt);
}
} catch (Exception e) {
Activator.log.warn("Failed to notify " + ncl);
}
}
}
}
/**
* Mount the specified preference node as child to this node.
*
* @param prefs Preferences node to mount
* @param name child name to mount the specified node as.
* This will override any previous child with the same
* name.
*/
public void mount(Preferences prefs, String name) {
synchronized(mounts) {
int ix;
if(-1 != (ix = name.indexOf("/"))) {
String n1 = name.substring(0, ix);
String n2 = name.substring(ix+1);
// this cast is OK since childSpi always return MountedPreferences
((MountedPreferences)node(n1)).mount(prefs, n2);
NodeChangeEvent evt = new NodeChangeEvent(this, prefs);
notifyNCL(evt, true);
} else {
mounts.put(name, prefs);
}
}
}
@Override
public void sync() throws BackingStoreException {
ArrayList<EventBag<PreferenceChangeListener, PreferenceChangeEvent>> prefEvents = new ArrayList<EventBag<PreferenceChangeListener, PreferenceChangeEvent>>();
ArrayList<EventBag<NodeChangeListener, NodeChangeEvent>> nodeEvents = new ArrayList<EventBag<NodeChangeListener, NodeChangeEvent>>();
synchronized (tree.treeLock()) {
_sync(prefEvents, nodeEvents);
}
fireNodeEvents(nodeEvents);
firePrefEvents(prefEvents);
}
/**
* Unmount a child node from this node.
*
* @param name name of node to unmount. If the node does not
* exists, does nothing.
*/
public void unmount(String name) {
synchronized(mounts) {
Preferences prefs = mounts.get(name);
mounts.remove(name);
if(prefs != null) {
NodeChangeEvent evt = new NodeChangeEvent(this, prefs);
notifyNCL(evt, false);
}
}
}
private ProxyPreferencesImpl node(String pathName, boolean create, List<EventBag<NodeChangeListener, NodeChangeEvent>> events) {
if (pathName.length() > 0 && pathName.charAt(0) == '/') { //NOI18N
// absolute path, if this is not the root then find the root
// and pass the call to it
if (parent != null) {
Preferences root = this;
while (root.parent() != null) {
root = root.parent();
}
return ((ProxyPreferencesImpl) root).node(pathName, create, events);
} else {
// this is the root, change the pathName to a relative path and proceed
pathName = pathName.substring(1);
}
}
if (pathName.length() > 0) {
String childName;
String pathFromChild;
int idx = pathName.indexOf('/'); //NOI18N
if (idx != -1) {
childName = pathName.substring(0, idx);
pathFromChild = pathName.substring(idx + 1);
} else {
childName = pathName;
pathFromChild = null;
}
ProxyPreferencesImpl child = children.get(childName);
if (child == null) {
if (removedChildren.contains(childName) && !create) {
// this child has been removed
return null;
}
Preferences childDelegate = null;
try {
if (delegate != null && delegate.nodeExists(childName)) {
childDelegate = delegate.node(childName);
}
} catch (BackingStoreException bse) {
// ignore
}
if (childDelegate != null || create) {
child = tree.get(this, childName, childDelegate);
children.put(childName, child);
removedChildren.remove(childName);
// fire event if we really created the new child node
if (childDelegate == null) {
EventBag<NodeChangeListener, NodeChangeEvent> bag = new EventBag<NodeChangeListener, NodeChangeEvent>();
bag.addListeners(nodeListeners);
bag.addEvent(new NodeChangeEventExt(this, child, false));
events.add(bag);
}
} else {
// childDelegate == null && !create
return null;
}
} else {
assert !child.removed;
}
return pathFromChild != null ? child.node(pathFromChild, create, events) : child;
} else {
return this;
}
}
public void childAdded(NodeChangeEvent evt) {
try { callback.call(); } catch (Exception e) { /* ignore */ }
}
public void childRemoved(NodeChangeEvent evt) {
try { callback.call(); } catch (Exception e) { /* ignore */ }
}
public void childAdded(NodeChangeEvent evt) {
try { callback.call(); } catch (Exception e) { /* ignore */ }
}
public void childRemoved(NodeChangeEvent evt) {
try { callback.call(); } catch (Exception e) { /* ignore */ }
}
public void childAdded(NodeChangeEvent evt) {
try { callback.call(); } catch (Exception e) { /* ignore */ }
}
public void childRemoved(NodeChangeEvent evt) {
try { callback.call(); } catch (Exception e) { /* ignore */ }
}