use of javax.swing.tree.TreeNode in project JMRI by JMRI.
the class WarrantRoute method showRoute.
/**
* Callback from RouteFinder - exactly one route found
*
* @param destNode destination block
* @param tree possible routes
*/
protected void showRoute(DefaultMutableTreeNode destNode, DefaultTreeModel tree) {
TreeNode[] nodes = tree.getPathToRoot(destNode);
_orders.clear();
for (TreeNode node : nodes) {
_orders.add((BlockOrder) ((DefaultMutableTreeNode) node).getUserObject());
}
_routeModel.fireTableDataChanged();
if (log.isDebugEnabled()) {
log.debug("showRoute: Route has " + _orders.size() + " orders.");
}
}
use of javax.swing.tree.TreeNode in project JMRI by JMRI.
the class CombinedLocoSelTreePane method updateForDecoderTypeID.
/**
* Decoder identify has matched one or more specific types
*/
@Override
void updateForDecoderTypeID(List<DecoderFile> pList) {
// find and select the first item
if (log.isDebugEnabled()) {
StringBuffer buf = new StringBuffer("Identified " + pList.size() + " matches: ");
for (int i = 0; i < pList.size(); i++) {
buf.append(pList.get(i).getModel() + ":");
}
log.debug(buf.toString());
}
if (pList.size() <= 0) {
log.error("Found empty list in updateForDecoderTypeID, should not happen");
return;
}
dTree.clearSelection();
// and issue a warning instruction in the status bar
if (pList.size() > 1) {
dTree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
_statusLabel.setText(Bundle.getMessage("StateMultipleMatch"));
} else {
dTree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
}
// set everybody not identified
Enumeration<DecoderTreeNode> e = dRoot.breadthFirstEnumeration();
while (e.hasMoreElements()) {
// loop over the tree
DecoderTreeNode node = e.nextElement();
node.setIdentified(false);
}
selectedPath = new ArrayList<TreePath>();
// Find decoder nodes in tree and set selected
for (int i = 0; i < pList.size(); i++) {
// loop over selected decoders
e = dRoot.breadthFirstEnumeration();
DecoderFile f = pList.get(i);
String findMfg = f.getMfg();
String findFamily = f.getFamily();
String findModel = f.getModel();
while (e.hasMoreElements()) {
// loop over the tree & find node
DecoderTreeNode node = e.nextElement();
// never match show=NO nodes
if (node.getShowable() == DecoderFile.Showable.NO) {
continue;
}
// convert path to comparison string
TreeNode[] list = node.getPath();
if (list.length == 3) {
// check for match to mfg, model (as family)
if (list[1].toString().equals(findMfg) && list[2].toString().equals(findModel)) {
log.debug("match length 3");
node.setIdentified(true);
dModel.reload();
((DecoderTreeNode) list[1]).setIdentified(true);
((DecoderTreeNode) list[2]).setIdentified(true);
TreePath path = new TreePath(node.getPath());
selectedPath.add(path);
break;
}
} else if (list.length == 4) {
// check for match to mfg, family, model
if (list[1].toString().equals(findMfg) && list[2].toString().equals(findFamily) && list[3].toString().equals(findModel)) {
log.debug("match length 4");
node.setIdentified(true);
dModel.reload();
((DecoderTreeNode) list[1]).setIdentified(true);
((DecoderTreeNode) list[2]).setIdentified(true);
((DecoderTreeNode) list[3]).setIdentified(true);
TreePath path = new TreePath(node.getPath());
selectedPath.add(path);
break;
}
}
}
}
// now select and show paths in tree
for (TreePath path : selectedPath) {
dTree.addSelectionPath(path);
dTree.expandPath(path);
dTree.scrollPathToVisible(path);
}
}
use of javax.swing.tree.TreeNode in project jdk8u_jdk by JetBrains.
the class bug6505523 method createAndShowGUI.
private static void createAndShowGUI() {
final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Problem with NPE under JDK 1.6");
final DefaultMutableTreeNode problematic = new DefaultMutableTreeNode("Expand me and behold a NPE in stderr");
problematic.add(new DefaultMutableTreeNode("some content"));
root.add(new DefaultMutableTreeNode("irrelevant..."));
root.add(problematic);
final DefaultTreeModel model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.setRootVisible(true);
tree.setShowsRootHandles(true);
tree.expandRow(0);
tree.collapseRow(2);
// this is critical - without dragEnabled everything works
tree.setDragEnabled(true);
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
TreeNode parent = problematic.getParent();
if (parent instanceof DefaultMutableTreeNode) {
model.removeNodeFromParent(problematic);
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
});
JFrame frame = new JFrame("JTree Problem");
frame.add(new JScrollPane(tree));
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
use of javax.swing.tree.TreeNode in project jdk8u_jdk by JetBrains.
the class Test4631471 method main.
private static void main() throws Exception {
// the DefaultMutableTreeNode will archive correctly
new Test4631471() {
protected Object getObject() {
return getRoot();
}
}.test(false);
// the DefaultTreeModel will also archive correctly
new Test4631471() {
protected Object getObject() {
return getModel();
}
}.test(false);
// create a new model from the root node
// this simulates the the MetaData ctor:
// registerConstructor("javax.swing.tree.DefaultTreeModel", new String[]{"root"});
new Test4631471() {
protected Object getObject() {
return new DefaultTreeModel((TreeNode) getModel().getRoot());
}
}.test(false);
// the JTree will archive correctly too
new Test4631471() {
protected Object getObject() {
return getTree();
}
}.test(false);
}
use of javax.swing.tree.TreeNode in project processdash by dtuma.
the class PropTreeModel method getNodeForKey.
public TreeNode getNodeForKey(DashHierarchy props, PropertyKey key) {
if (key == null)
return null;
if (rootKey.equals(key))
return (TreeNode) getRoot();
PropertyKey parentKey = key.getParent();
int numChildren = props.getNumChildren(parentKey);
TreeNode parent = getNodeForKey(props, parentKey);
if (parent == null || parent.getChildCount() != numChildren)
return null;
PropertyKey childKey;
for (int i = numChildren; i-- > 0; ) {
childKey = props.getChildKey(parentKey, i);
if (childKey.equals(key))
return parent.getChildAt(i);
}
return null;
}
Aggregations