// NodeTreeRep.java package org.openlcb.swing.networktree; import javax.swing.*; import javax.swing.tree.*; import java.beans.PropertyChangeListener; import java.util.List; import org.openlcb.*; /** * Represent a single node for the tree display *
*
* @author Bob Jacobsen Copyright (C) 2010, 2012
* @version $Revision: 2585 $
*/
public class NodeTreeRep extends DefaultMutableTreeNode {
MimicNodeStore.NodeMemo memo;
MimicNodeStore store;
DefaultTreeModel treeModel;
DefaultMutableTreeNode getThis() { return this; }
DefaultTreeModel getTreeModel() { return treeModel; }
SelectionKeyLoader loader;
public NodeTreeRep(MimicNodeStore.NodeMemo memo, MimicNodeStore store, DefaultTreeModel treeModel, SelectionKeyLoader loader) {
super("Node");
this.memo = memo;
this.store = store;
this.treeModel = treeModel;
this.loader = loader;
}
void initConnections() {
// listen for more info arriving
memo.addPropertyChangeListener(
new PropertyChangeListener(){
public void propertyChange(java.beans.PropertyChangeEvent e) {
if (e.getPropertyName().equals("updateProtocol")) {
updateProtocolIdent((ProtocolIdentification)e.getNewValue());
}
if (e.getPropertyName().equals("updateSimpleNodeIdent")) {
updateSimpleNodeIdent((SimpleNodeIdent)e.getNewValue());
}
if (e.getPropertyName().equals("updateConsumers")) {
getTreeModel().insertNodeInto(newNode("Supported Consumers"), getThis(),
getThis().getChildCount());
}
if (e.getPropertyName().equals("updateProducers")) {
getTreeModel().insertNodeInto(newNode("Supported Producers"), getThis(),
getThis().getChildCount());
}
}
});
// see if protocol info already present
ProtocolIdentification pip = store.getProtocolIdentification(memo.getNodeID());
if (pip != null) updateProtocolIdent(pip); // otherwise, will be notified later
// see if simple ID info already present
SimpleNodeIdent snii = store.getSimpleNodeIdent(memo.getNodeID());
if (snii != null) updateSimpleNodeIdent(snii); // otherwise, will be notified later
}
DefaultMutableTreeNode newNode(String name, SelectionKey key) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
node.setUserObject(key);
return node;
}
DefaultMutableTreeNode newNode(String name) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
node.setUserObject(new SelectionKey(name, memo.getNodeID()));
return node;
}
void updateSimpleNodeIdent(SimpleNodeIdent e) {
if (simpleInfoMfgNode == null) {
if (e.getMfgName().replace(" ","").length()>0) {
simpleInfoMfgNode = newNode("Mfg: "+e.getMfgName());
getTreeModel().insertNodeInto(simpleInfoMfgNode,
getThis(),
getThis().getChildCount());
}
} else {
simpleInfoMfgNode.setUserObject("Mfg: "+e.getMfgName());
}
if (simpleInfoModelNode == null) {
if (e.getModelName().replace(" ","").length()>0) {
simpleInfoModelNode = newNode("Mod: "+e.getModelName());
getTreeModel().insertNodeInto(simpleInfoModelNode,
getThis(),
getThis().getChildCount());
}
} else {
simpleInfoModelNode.setUserObject("Mod: "+e.getModelName());
}
if (simpleInfoHardwareVersionNode == null) {
if (e.getHardwareVersion().replace(" ","").length()>0) {
simpleInfoHardwareVersionNode = newNode("Hardware: "+e.getHardwareVersion());
getTreeModel().insertNodeInto(simpleInfoHardwareVersionNode,
getThis(),
getThis().getChildCount());
}
} else {
simpleInfoHardwareVersionNode.setUserObject("Hardware: "+e.getHardwareVersion());
}
if (simpleInfoSoftwareVersionNode == null) {
if (e.getSoftwareVersion().replace(" ","").length()>0) {
simpleInfoSoftwareVersionNode = newNode("Software: "+e.getSoftwareVersion());
getTreeModel().insertNodeInto(simpleInfoSoftwareVersionNode,
getThis(),
getThis().getChildCount());
}
} else {
simpleInfoSoftwareVersionNode.setUserObject("Software: "+e.getSoftwareVersion());
}
if (simpleInfoUserNameNode == null) {
if (e.getUserName().replace(" ","").length()>0) {
simpleInfoUserNameNode = new DefaultMutableTreeNode("Name: "+e.getUserName());
getTreeModel().insertNodeInto(simpleInfoUserNameNode,
getThis(),
getThis().getChildCount());
}
} else {
simpleInfoUserNameNode.setUserObject("Name: "+e.getUserName());
}
if (simpleInfoUserDescNode == null ) {
if (e.getUserDesc().replace(" ","").length()>0) {
simpleInfoUserDescNode = newNode("Desc: "+e.getUserDesc());
getTreeModel().insertNodeInto(simpleInfoUserDescNode,
getThis(),
getThis().getChildCount());
}
} else {
simpleInfoUserDescNode.setUserObject("Desc: "+e.getUserDesc());
}
}
DefaultMutableTreeNode pipNode;
void updateProtocolIdent(ProtocolIdentification pi) {
if (pi.getValue() != 0) {
if (pipNode == null) {
pipNode = newNode("Supported Protocols");
getTreeModel().insertNodeInto(pipNode, getThis(),
getThis().getChildCount());
}
List