Example usage for javax.swing.tree DefaultMutableTreeNode setUserObject

List of usage examples for javax.swing.tree DefaultMutableTreeNode setUserObject

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode setUserObject.

Prototype

public void setUserObject(Object userObject) 

Source Link

Document

Sets the user object for this node to userObject.

Usage

From source file:edu.ku.brc.af.tasks.subpane.formeditor.ViewSetSelectorPanel.java

protected void buildTreeModel(final ViewDefIFace viewDef) {
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode vdNode = new DefaultMutableTreeNode(viewDef.getName());
    vdNode.setUserObject(viewDef);
    model.setRoot(vdNode);//from w w  w. j a va 2s  .  c o m
    if (viewDef instanceof FormViewDef) {
        FormViewDef fvd = (FormViewDef) viewDef;
        for (FormRowIFace r : fvd.getRows()) {
            FormRow row = (FormRow) r;
            DefaultMutableTreeNode rowNode = new DefaultMutableTreeNode(row.getRowNumber());
            rowNode.setUserObject(row);
            vdNode.add(rowNode);

            for (FormCellIFace c : row.getCells()) {
                FormCell cell = (FormCell) c;
                DefaultMutableTreeNode cellNode = new DefaultMutableTreeNode(cell.toString());
                cellNode.setUserObject(cell);
                rowNode.add(cellNode);
            }
        }
    }
    model.nodeStructureChanged(vdNode);
}

From source file:edu.ku.brc.af.tasks.subpane.formeditor.ViewSetSelectorPanel.java

/**
 * @param viewDefs//from ww w  .j  av  a2s  .c o m
 */
protected void buildTreeModel(Vector<ViewDefIFace> viewDefs) {
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("ViewDefs"); //$NON-NLS-1$
    model.setRoot(root);

    for (ViewDefIFace vd : viewDefs) {
        DefaultMutableTreeNode vdNode = new DefaultMutableTreeNode(vd.getName());
        vdNode.setUserObject(vd);
        if (vd instanceof FormViewDef) {
            root.add(vdNode);
            FormViewDef fvd = (FormViewDef) vd;
            for (FormRowIFace r : fvd.getRows()) {
                FormRow row = (FormRow) r;
                DefaultMutableTreeNode rowNode = new DefaultMutableTreeNode(row.getRowNumber());
                rowNode.setUserObject(row);
                vdNode.add(rowNode);

                for (FormCellIFace c : row.getCells()) {
                    FormCell cell = (FormCell) c;
                    DefaultMutableTreeNode cellNode = new DefaultMutableTreeNode(cell.toString());
                    cellNode.setUserObject(cell);
                    rowNode.add(cellNode);
                }
            }
        }
    }
    model.nodeStructureChanged(root);
}

From source file:edu.ku.brc.af.tasks.subpane.formeditor.ViewSetSelectorPanel.java

/**
 * Adds a new row in the right position (above).
 *//*from   w  w w  .  jav  a  2 s .  co m*/
protected void addRow() {
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

    FormRow newRow = new FormRow();

    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newRow);
    newNode.setUserObject(newRow);

    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getSelectionModel().getSelectionPath()
            .getLastPathComponent();
    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent();
    if (parentNode == null) {
        parentNode = (DefaultMutableTreeNode) model.getRoot();
        selectedNode = null;
    }

    int position;
    if (selectedRow == null || parentNode.getUserObject() instanceof String) {
        formViewDef.getRows().add(newRow);
        position = formViewDef.getRows().size() - 1;

    } else {
        position = formViewDef.getRows().indexOf(selectedRow);
        formViewDef.getRows().insertElementAt(newRow, (byte) position);
    }

    model.insertNodeInto(newNode, parentNode, position);

    renumberRows(formViewDef.getRows());
    updateTreeNodes((DefaultMutableTreeNode) model.getRoot(), model, false);

    Object[] newPath = new Object[2];
    newPath[0] = parentNode;
    newPath[1] = newNode;

    final TreePath newTreePath = new TreePath(newPath);
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            tree.setSelectionPath(newTreePath);
        }
    });

    previewPanel.rebuild(false);
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * //from   ww w.  j a  va2  s. c om
 */
private void addColObjAsChild(final DefaultMutableTreeNode parentNodeArg, final CollectionObject colObj) {
    DefaultMutableTreeNode parentNode = parentNodeArg == null ? getSelectedTreeNode() : parentNodeArg;
    if (parentNode != null) {
        if (colObj != null) {
            Container container = (Container) parentNode.getUserObject();

            colObj.setContainerOwner(container);
            container.getCollectionObjectKids().add(colObj);

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode();
            newNode.setUserObject(colObj);
            colObjIdHash.add(colObj.getId());
            int inx = parentNode.getChildCount();
            model.insertNodeInto(newNode, parentNode, inx);
            model.nodesWereInserted(parentNode, new int[] { inx });
            model.nodeChanged(parentNode);
            model.reload();
            tree.restoreTree();

            expandToNode(newNode);
        }
    }
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * @param parentNode/*from   www. ja va2 s .c  o m*/
 * @param container
 * @param colObj
 */
private void loadContainerTree(final DefaultMutableTreeNode parentNode, final Container container,
        final CollectionObject colObj) {
    if (container != null) {
        container.forceLoad();

        int cnt = parentNode.getChildCount();
        Vector<CollectionObject> coKids = new Vector<CollectionObject>(container.getCollectionObjectKids());
        Collections.sort(coKids);
        for (CollectionObject co : coKids) {
            cnt = parentNode.getChildCount();
            DefaultMutableTreeNode node = new DefaultMutableTreeNode();
            node.setUserObject(co);
            colObjIdHash.add(co.getId());
            model.insertNodeInto(node, parentNode, cnt);
            loadContainerTree(node, null, co);
        }

        Vector<Container> cnKids = new Vector<Container>(container.getChildrenList());
        Collections.sort(cnKids);
        for (Container cn : cnKids) {
            cnt = parentNode.getChildCount();
            DefaultMutableTreeNode node = new DefaultMutableTreeNode();
            node.setUserObject(cn);
            containerIdHash.add(cn.getId());
            model.insertNodeInto(node, parentNode, cnt);
            loadContainerTree(node, cn, null);
        }

    } else if (colObj != null) {
        if (colObj.getContainer() != null) {
            loadContainerTree(parentNode, colObj.getContainer(), null);
        }
    } else {
        // error
    }
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * /*from w  ww . j  a  v a2s .co  m*/
 */
private void addContainer(final boolean doSearch) {
    if (checkForChanges()) {
        DefaultMutableTreeNode parentNode = getSelectedTreeNode();
        if (parentNode != null) {
            Container parentContainer = (Container) parentNode.getUserObject();
            Container newContainer = doSearch ? (Container) searchForDataObj(Container.class)
                    : createContainer(parentContainer);
            if (newContainer != null) {
                if (doSearch) {
                    // check here to see if they are already parented.
                    newContainer.setParent(parentContainer);
                    parentContainer.getChildren().add(newContainer);
                    saveObjs(newContainer, parentContainer);
                }

                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode();
                newNode.setUserObject(newContainer);
                if (newContainer.getId() != null) {
                    containerIdHash.add(newContainer.getId());
                }

                int inx = parentNode.getChildCount();
                model.insertNodeInto(newNode, parentNode, inx);
                model.nodesWereInserted(parentNode, new int[] { inx });
                model.nodeChanged(parentNode);
                model.reload();
                tree.restoreTree();

                expandToNode(newNode); // invokedLater
            }
        }
    }
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * //from  www.  j a  v a 2 s . com
 */
public void set(final Container rootCon, final CollectionObject rootCO) {
    this.rootContainer = rootCon;
    this.rootColObj = rootCO;

    colObjIdHash.clear();
    containerIdHash.clear();

    if (rootCon != null) {
        Integer rootParentId = getTopMostParentId(rootCon.getId());
        if (rootParentId != null) {
            this.rootContainer = getContainerFromDB(rootParentId);
            this.rootColObj = rootContainer.getCollectionObject();
        }
    } else if (rootCO == null) {

    }

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
    rootNode.setUserObject(rootContainer != null ? rootContainer : rootColObj);

    if (rootContainer != null) {
        loadContainerTree(rootNode, rootContainer, null);

    } else if (rootColObj != null) {
        loadContainerTree(rootNode, null, rootColObj);
    } else {
        rootNode.setUserObject("New Container");
    }

    model = new DefaultTreeModel(rootNode);
    if (tree == null) {
        tree = new GhostActionableTree(this, model);
        tree.setRowHeight(ROW_HEIGHT);
        tree.setBackground(bgColor);
        tree.setEditable(false);
        tree.setVisibleRowCount(15);
        registerPrintContextMenu(tree);

    } else {
        tree.setModel(model);
    }
    model.reload();

    if (containerPanel != null) {
        containerPanel.getMultiView().setData(rootCon);
    }
}

From source file:dataviewer.DataViewer.java

private void fill_tree() {
    txt_count.setText("");

    DefaultTreeModel tm = (DefaultTreeModel) tr_files.getModel();
    DefaultMutableTreeNode top;
    cur_path = new File(cur_path).getAbsolutePath();
    if (cur_path.charAt(cur_path.length() - 1) == '.') {
        cur_path = cur_path.substring(0, cur_path.length() - 1);
    }/*from  w  w w .j a v a2s.c o m*/
    if (cur_path.charAt(cur_path.length() - 1) == File.separatorChar) {
        cur_path = cur_path.substring(0, cur_path.length() - 1);
    }
    if (tm != null) {
        top = (DefaultMutableTreeNode) tm.getRoot();
        top.removeAllChildren();
        top.setUserObject(cur_path);
    } else {
        top = new DefaultMutableTreeNode(cur_path);
    }

    // get all children nodes and remove them
    // rename the user object
    ArrayList<String> files = this.getAllFileNames(cur_path);
    Collections.sort(files);

    int j = 1;
    int selected_index = -1;
    for (String f : files) {
        String ex = this.getExtension(f);
        boolean is_folder = (new File(cur_path + File.separator + f)).isDirectory();
        if (is_folder || ex.equals(".txt") || ex.equals(".dat") || ex.equals(".csv") || ex.equals(".tsv")) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(f);
            top.add(node);
            if (f.equals(selected_file)) {
                selected_index = j;
            }
            j++;
        }
    }

    //DefaultTreeModel treeModel = new DefaultTreeModel(top);
    //tr_files.setModel(treeModel);
    if (tm == null) {
        tr_files.setModel(new DefaultTreeModel(top));
    } else {
        try {
            tm.reload();
        } catch (Exception e) {
            tm.reload();
        }
    }

    if (selected_index > -1) {
        tr_files.setSelectionRow(selected_index);
    }

    tr_files.grabFocus();
}

From source file:edu.ku.brc.af.tasks.subpane.formeditor.ViewSetSelectorPanel.java

/**
 * @param selectedControl//from  www  .j a  v a  2  s .  c o m
 * @param selectedCell
 */
protected void addControl(final ControlIFace selectedControl,
        @SuppressWarnings("hiding") final FormCell selectedCell) {
    int position = 0;
    if (selectedCell != null) {
        position = selectedRow.getCells().indexOf(selectedCell);
    }

    if (selectedControl instanceof RowControl) {
        addRow();
        return;
    }

    @SuppressWarnings("hiding")
    EditorPropPanel panel = new EditorPropPanel(controlHash, subcontrolHash, getAvailableFieldCells(), false,
            this);
    //panel.setFormViewDef(formViewDef);

    FormCell formCell = null;
    boolean skip = false;
    if (selectedControl instanceof Control) {
        Control control = (Control) selectedControl;

        if (control.getType().equals("label")) //$NON-NLS-1$
        {
            formCell = new FormCellLabel();
            formCell.setIdent(Integer.toString(getNextId()));
        }

        if (formCell != null) {
            formCell.setType(FormCellIFace.CellType.valueOf(control.getType()));

            //System.out.println(formCell.getType() + "  "+ formCell.getClass().getSimpleName());

            panel.loadView(formCell.getType().toString(), selectedViewDef.getClassName());
            panel.setDataIntoUI(formViewDef, formCell, (formViewDef.getRows().size() * 2) - 1,
                    formViewDef.getRowDefItem().getNumItems(), selectedRow.getRowNumber(),
                    (selectedRow.getCells().size() * 2) - 1, formViewDef.getColumnDefItem().getNumItems(),
                    position);
        }

    } else {
        SubControl subControl = (SubControl) selectedControl;

        FormCellField fcf = null;
        if (subControl.getType().equals("combobox")) //$NON-NLS-1$
        {
            fcf = new FormCellField(FormCell.CellType.field, Integer.toString(getNextId()), "", 1, 1); //$NON-NLS-1$
            fcf.setUiType(FormCellFieldIFace.FieldType.combobox);
            setDefaultDspUIType(fcf);
            formCell = fcf;
        }
        //System.out.println("* ["+fcf.getUiType().toString() + "]  "+ fcf.getClass().getSimpleName());
        //SubControl subcontrol = subcontrolHash.get(fcf.getUiType().toString());
        //System.out.println("SC: "+subcontrol);

        panel.loadView(fcf.getUiType().toString(), selectedViewDef.getClassName());
        panel.setDataIntoUI(formViewDef, fcf, (formViewDef.getRows().size() * 2) - 1,
                formViewDef.getRowDefItem().getNumItems(), selectedRow.getRowNumber(),
                (selectedRow.getCells().size() * 2) - 1, formViewDef.getColumnDefItem().getNumItems(),
                position);

    }

    if (!skip && formCell != null) {
        CustomDialog propDlg = new CustomDialog((Frame) UIRegistry.getTopWindow(),
                getResourceString("ViewSetSelectorPanel.CREATE"), true, panel); //$NON-NLS-1$
        propDlg.createUI();
        //panel.getViewDefMultiView().getCurrentView().getValidator().addEnableItem(propDlg.getOkBtn());
        propDlg.pack();
        Rectangle r = propDlg.getBounds();
        r.width += 60;
        r.height += 30;
        propDlg.setBounds(r);
        propDlg.setVisible(true);

        if (!propDlg.isCancelled()) {
            if (selectedControl instanceof Control) {
                panel.getDataFromUI(formCell);
            } else {
                panel.getDataFromUI((FormCellField) formCell);
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(formCell);
            newNode.setUserObject(formCell);

            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) tree.getSelectionModel()
                    .getSelectionPath().getLastPathComponent();
            if (!(parentNode.getUserObject() instanceof FormRow)) {
                parentNode = (DefaultMutableTreeNode) parentNode.getParent();
            }

            TreePath treePath = tree.getSelectionModel().getSelectionPath();
            Object[] path = treePath.getPath();

            Object[] newPath = new Object[path.length + (selectedCell == null ? 1 : 0)];
            for (int i = 0; i < path.length; i++) {
                newPath[i] = path[i];
            }
            newPath[newPath.length - 1] = newNode;

            if (selectedRow.getCells().size() == 0) {
                selectedRow.getCells().add(formCell);
                ((DefaultTreeModel) tree.getModel()).insertNodeInto(newNode, parentNode, 0);

            } else if (position == selectedRow.getCells().size() - 1) {
                //System.out.println("Adding New Cell at position["+(position+1)+"] number of nodes["+nodeParent.getChildCount()+"] rowCells "+selectedRow.getCells().size());
                selectedRow.getCells().add(formCell);
                ((DefaultTreeModel) tree.getModel()).insertNodeInto(newNode, parentNode, position + 1);

            } else {
                //System.out.println("Adding New Cell at position["+position+"] number of nodes["+nodeParent.getChildCount()+"] rowCells "+selectedRow.getCells().size());
                selectedRow.getCells().insertElementAt(formCell, position + 1);
                ((DefaultTreeModel) tree.getModel()).insertNodeInto(newNode, parentNode, position + 1);
            }

            //System.out.println("******* ADDING ["+formCell.getIdent()+"]"); //$NON-NLS-1$ //$NON-NLS-2$

            idHash.put(formCell.getIdent(), true);

            final TreePath newTreePath = new TreePath(newPath);
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    tree.setSelectionPath(newTreePath);
                }
            });

            previewPanel.rebuild(false);
        }
    }

}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * /* ww w  . j a  v  a  2  s . c om*/
 */
private void editColObj() {
    if (checkForChanges()) {
        DefaultMutableTreeNode node = getSelectedTreeNode();
        if (node != null && node.getUserObject() instanceof CollectionObject) {
            CollectionObject co = editColObj((CollectionObject) node.getUserObject());
            if (co != null) {
                node.setUserObject(co);
            }
        }
    }
}