Example usage for javax.swing.tree TreePath getLastPathComponent

List of usage examples for javax.swing.tree TreePath getLastPathComponent

Introduction

In this page you can find the example usage for javax.swing.tree TreePath getLastPathComponent.

Prototype

public Object getLastPathComponent() 

Source Link

Document

Returns the last element of this path.

Usage

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private void handleNewSelectedIndex(int theNewIndex) {
    if (mySelectionHandlingDisabled) {
        return;// w  ww . j ava  2  s. c om
    }
    ourLog.info("New selection index: " + theNewIndex);

    if (myCurrentlyEditing) {
        ourLog.info("Not responding to new selection index because we are marked as editing right now");
        return;
    }

    AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
    TreePath path = layout.getPathForRow(theNewIndex);

    DefaultMutableTreeNode lead = (DefaultMutableTreeNode) path.getLastPathComponent();
    if (lead instanceof TreeNodeSegment) {
        TreeNodeSegment segmentNode = (TreeNodeSegment) lead;
        myMessages.setHighlitedRangeBasedOnSegment(segmentNode.getSegment());
    } else if (lead instanceof TreeNodeGroup) {
        TreeNodeGroup type = (TreeNodeGroup) lead;
        try {
            List<Segment> segments = type.getSegments();
            myMessages.setHighlitedRangeBasedOnSegment(segments.toArray(new Segment[segments.size()]));
        } catch (HL7Exception e) {
            e.printStackTrace();
        }
    } else if (lead instanceof TreeNodeType) {
        TreeNodeType type = (TreeNodeType) lead;
        myMessages.setHighlitedRangeBasedOnField(type.getSegmentAndComponentPath());
    } else {
        myMessages.clearHighlight();
    }

}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

void addChildren() throws InterruptedException, InvocationTargetException {
    if (myMessages != null && myMessages.getRuntimeProfile() != null) {
        myRuntimeProfileValidator = new DefaultValidator();
        myRuntimeProfileValidator.setValidateChildren(false);
    }//w  w w. jav  a2s.  c  o m

    final Set<String> openPaths = getOpenPaths();

    int selectedRow = getSelectedRow();
    final String selectedPath = getPathAtRow(selectedRow);

    if (myMessages != null) {
        try {
            addChildren(myMessages.getMessages(), myTop, "");
        } catch (InterruptedException e) {
            ourLog.info("Interrupted during an update loop, going to schedule another pass");
            myUpdaterThread.scheduleUpdate();
        } catch (InvocationTargetException e) {
            ourLog.error("Failed up update message tree", e);
        }

        myTop.validate();

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                myTreeModel.nodeStructureChanged(myTop);
            }
        });

    }

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mySelectionHandlingDisabled = true;
                ourLog.debug("Open paths are: {}", openPaths);
                if (openPaths.isEmpty() && myShouldOpenDefaultPaths) {
                    ourLog.info("Opening default paths");
                    final AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
                    for (int row = 0; row < layout.getRowCount(); row++) {
                        TreePath path = layout.getPathForRow(row);
                        Object component = path.getLastPathComponent();
                        if (component instanceof TreeNodeMessage || component instanceof TreeNodeUnknown
                                || component instanceof TreeNodeGroup) {
                            expandPath(path);
                        }
                    }
                    myShouldOpenDefaultPaths = false;
                } else {
                    ourLog.info("Opening pre-existing paths: {} and selected path: {}", openPaths,
                            selectedPath);
                    expandPaths(openPaths, selectedPath);
                }
            } finally {
                mySelectionHandlingDisabled = false;
            }
        }
    });
    // if (selectedRow != -1) {
    // getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
    // handleNewSelectedIndex(selectedRow);
    // }

}

From source file:net.sf.jabref.gui.BasePanel.java

private void createMainTable() {
    database.registerListener(tableModel.getListSynchronizer());
    database.registerListener(SpecialFieldDatabaseChangeListener.getInstance());

    tableFormat = new MainTableFormat(database);
    tableFormat.updateTableFormat();/* ww w  .  j  av a2s . co m*/
    mainTable = new MainTable(tableFormat, tableModel, frame, this);

    selectionListener = new MainTableSelectionListener(this, mainTable);
    mainTable.updateFont();
    mainTable.addSelectionListener(selectionListener);
    mainTable.addMouseListener(selectionListener);
    mainTable.addKeyListener(selectionListener);
    mainTable.addFocusListener(selectionListener);

    // Add the listener that will take care of highlighting groups as the selection changes:
    groupsHighlightListener = listEvent -> {
        HighlightMatchingGroupPreferences highlightMatchingGroupPreferences = new HighlightMatchingGroupPreferences(
                Globals.prefs);
        if (highlightMatchingGroupPreferences.isAny()) {
            getGroupSelector().showMatchingGroups(mainTable.getSelectedEntries(), false);
        } else if (highlightMatchingGroupPreferences.isAll()) {
            getGroupSelector().showMatchingGroups(mainTable.getSelectedEntries(), true);
        } else {
            // no highlight
            getGroupSelector().showMatchingGroups(null, true);
        }
    };
    mainTable.addSelectionListener(groupsHighlightListener);

    mainTable.getActionMap().put(Actions.CUT, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                runCommand(Actions.CUT);
            } catch (Throwable ex) {
                LOGGER.warn("Could not cut", ex);
            }
        }
    });
    mainTable.getActionMap().put(Actions.COPY, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                runCommand(Actions.COPY);
            } catch (Throwable ex) {
                LOGGER.warn("Could not copy", ex);
            }
        }
    });
    mainTable.getActionMap().put(Actions.PASTE, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                runCommand(Actions.PASTE);
            } catch (Throwable ex) {
                LOGGER.warn("Could not paste", ex);
            }
        }
    });

    mainTable.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            final int keyCode = e.getKeyCode();
            final TreePath path = frame.getGroupSelector().getSelectionPath();
            final GroupTreeNodeViewModel node = path == null ? null
                    : (GroupTreeNodeViewModel) path.getLastPathComponent();

            if (e.isControlDown()) {
                switch (keyCode) {
                // The up/down/left/rightkeystrokes are displayed in the
                // GroupSelector's popup menu, so if they are to be changed,
                // edit GroupSelector.java accordingly!
                case KeyEvent.VK_UP:
                    e.consume();
                    if (node != null) {
                        frame.getGroupSelector().moveNodeUp(node, true);
                    }
                    break;
                case KeyEvent.VK_DOWN:
                    e.consume();
                    if (node != null) {
                        frame.getGroupSelector().moveNodeDown(node, true);
                    }
                    break;
                case KeyEvent.VK_LEFT:
                    e.consume();
                    if (node != null) {
                        frame.getGroupSelector().moveNodeLeft(node, true);
                    }
                    break;
                case KeyEvent.VK_RIGHT:
                    e.consume();
                    if (node != null) {
                        frame.getGroupSelector().moveNodeRight(node, true);
                    }
                    break;
                case KeyEvent.VK_PAGE_DOWN:
                    frame.nextTab.actionPerformed(null);
                    e.consume();
                    break;
                case KeyEvent.VK_PAGE_UP:
                    frame.prevTab.actionPerformed(null);
                    e.consume();
                    break;
                default:
                    break;
                }
            } else if (keyCode == KeyEvent.VK_ENTER) {
                e.consume();
                try {
                    runCommand(Actions.EDIT);
                } catch (Throwable ex) {
                    LOGGER.warn("Could not run action based on key press", ex);
                }
            }
        }
    });
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

public String getCurrentSelectedId() {
    int selectedRow = templateTreeTable.getSelectedRow();
    if (selectedRow >= 0) {
        TreePath selectedPath = templateTreeTable.getPathForRow(selectedRow);
        if (selectedPath != null) {
            return (String) ((TreeTableNode) selectedPath.getLastPathComponent())
                    .getValueAt(TEMPLATE_ID_COLUMN);
        }//from  w  w w  .j  a v a  2 s  . co  m
    }

    return null;
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

private void updateCurrentNode() {
    if (currentSelectedRow >= 0 && updateCurrentNode.get()) {
        TreePath selectedPath = templateTreeTable.getPathForRow(currentSelectedRow);
        if (selectedPath != null) {
            TreeTableNode selectedNode = (TreeTableNode) selectedPath.getLastPathComponent();

            if (selectedNode instanceof CodeTemplateLibraryTreeTableNode) {
                CodeTemplateLibraryTreeTableNode libraryNode = (CodeTemplateLibraryTreeTableNode) selectedNode;
                updateLibraryNode(libraryNode);

                if (templateTreeTable.getTreeTableModel().getRoot() != fullModel.getRoot()) {
                    updateLibraryNode((CodeTemplateLibraryTreeTableNode) findFullNode(libraryNode));
                }/*from   w  ww  .ja v a2s .c  o m*/
            } else {
                CodeTemplateTreeTableNode codeTemplateNode = (CodeTemplateTreeTableNode) selectedNode;
                updateCodeTemplateNode(codeTemplateNode);

                if (templateTreeTable.getTreeTableModel().getRoot() != fullModel.getRoot()) {
                    CodeTemplateTreeTableNode fullCodeTemplateNode = (CodeTemplateTreeTableNode) findFullNode(
                            codeTemplateNode);
                    fullCodeTemplateNode.setValueAt(codeTemplateNode.getValueAt(TEMPLATE_NAME_COLUMN),
                            TEMPLATE_NAME_COLUMN);
                    updateCodeTemplateNode(fullCodeTemplateNode);
                }
            }
        }
    }
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

public void doExportCodeTemplate() {
    stopTableEditing();/*from w ww. ja v  a  2s  . co  m*/
    updateCurrentNode();

    if (changesHaveBeenMade() && !promptSave(true)) {
        return;
    }

    TreePath selectedPath = templateTreeTable.getTreeSelectionModel().getSelectionPath();
    if (selectedPath != null) {
        CodeTemplate codeTemplate = ((CodeTemplateTreeTableNode) selectedPath.getLastPathComponent())
                .getCodeTemplate();
        ObjectXMLSerializer serializer = ObjectXMLSerializer.getInstance();
        try {
            String codeTemplateXML = serializer.serialize(codeTemplate);
            parent.exportFile(codeTemplateXML, codeTemplate.getName() + ".xml", "XML", "Code template export");
        } catch (Throwable t) {
            parent.alertThrowable(parent, t, "Unable to export: " + t.getMessage());
        }
    }
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

private void libraryComboBoxActionPerformed() {
    if (!libraryComboBoxAdjusting.get()) {
        setSaveEnabled(true);/*from  w  ww  .ja va  2  s .  co  m*/

        TreePath selectedPath = templateTreeTable.getTreeSelectionModel().getSelectionPath();
        if (selectedPath != null) {
            TreeTableNode selectedNode = (TreeTableNode) selectedPath.getLastPathComponent();
            if (selectedNode instanceof CodeTemplateTreeTableNode) {
                CodeTemplateTreeTableNode codeTemplateNode = (CodeTemplateTreeTableNode) selectedNode;
                CodeTemplateTreeTableModel model = (CodeTemplateTreeTableModel) templateTreeTable
                        .getTreeTableModel();
                CodeTemplateLibraryTreeTableNode currentParent = (CodeTemplateLibraryTreeTableNode) codeTemplateNode
                        .getParent();

                String libraryName = (String) templateLibraryComboBox.getSelectedItem();

                CodeTemplateLibraryTreeTableNode libraryNode = null;
                for (Enumeration<? extends MutableTreeTableNode> libraries = ((MutableTreeTableNode) model
                        .getRoot()).children(); libraries.hasMoreElements();) {
                    CodeTemplateLibraryTreeTableNode library = (CodeTemplateLibraryTreeTableNode) libraries
                            .nextElement();
                    if (library.getLibrary().getName().equals(libraryName)) {
                        libraryNode = library;
                        break;
                    }
                }

                CodeTemplateLibraryTreeTableNode fullLibraryNode = null;
                for (Enumeration<? extends MutableTreeTableNode> libraries = ((MutableTreeTableNode) fullModel
                        .getRoot()).children(); libraries.hasMoreElements();) {
                    CodeTemplateLibraryTreeTableNode library = (CodeTemplateLibraryTreeTableNode) libraries
                            .nextElement();
                    if (library.getLibrary().getName().equals(libraryName)) {
                        fullLibraryNode = library;
                        break;
                    }
                }

                if (!currentParent.getLibraryId().equals(fullLibraryNode.getLibraryId())) {
                    updateCurrentNode();
                    updateCurrentNode.set(false);
                    model.removeNodeFromParent(codeTemplateNode);

                    if (libraryNode != null) {
                        model.insertNodeInto(codeTemplateNode, libraryNode);
                        selectTemplatePath(new TreePath(model.getPathToRoot(codeTemplateNode)));
                    } else {
                        selectTemplatePath(new TreePath(model.getPathToRoot(model.getRoot())));
                    }

                    if (model.getRoot() != fullModel.getRoot()) {
                        AbstractSortableTreeTableNode fullCodeTemplateNode = findFullNode(codeTemplateNode);
                        fullModel.removeNodeFromParent(fullCodeTemplateNode);
                        fullModel.insertNodeInto(fullCodeTemplateNode, fullLibraryNode);
                    }
                    updateCurrentNode.set(true);
                }
            }
        }
    }
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

public void doExportLibrary() {
    stopTableEditing();//from  ww w . j a v a  2s. co  m
    updateCurrentNode();

    if (changesHaveBeenMade() && !promptSave(true)) {
        return;
    }

    TreePath selectedPath = templateTreeTable.getTreeSelectionModel().getSelectionPath();
    if (selectedPath != null) {
        CodeTemplateLibrary library = getLibraryWithTemplates(
                (CodeTemplateLibraryTreeTableNode) selectedPath.getLastPathComponent());
        ObjectXMLSerializer serializer = ObjectXMLSerializer.getInstance();
        try {
            String libraryXml = serializer.serialize(library);
            parent.exportFile(libraryXml, library.getName() + ".xml", "XML", "Code template library export");
        } catch (Throwable t) {
            parent.alertThrowable(parent, t, "Unable to export: " + t.getMessage());
        }
    }
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

private void deleteSelectedNode(boolean codeTemplate) {
    stopTableEditing();/*from w w  w  .j av a  2 s  .  c o m*/
    setSaveEnabled(true);
    TreePath selectedPath = templateTreeTable.getTreeSelectionModel().getSelectionPath();

    if (selectedPath != null) {
        CodeTemplateTreeTableModel model = (CodeTemplateTreeTableModel) templateTreeTable.getTreeTableModel();
        MutableTreeTableNode selectedNode = (MutableTreeTableNode) selectedPath.getLastPathComponent();
        MutableTreeTableNode parent = (MutableTreeTableNode) selectedNode.getParent();
        int selectedNodeIndex = parent.getIndex(selectedNode);
        MutableTreeTableNode newSelectedNode = null;

        if (!codeTemplate && selectedNode.getChildCount() > 0) {
            if (!this.parent.alertOkCancel(this.parent, "The selected library contains "
                    + selectedNode.getChildCount()
                    + " code templates. If you delete the library, the code templates will be deleted as well. Are you sure you wish to continue?")) {
                return;
            }

            for (MutableTreeTableNode codeTemplateNode : Collections.list(selectedNode.children())) {
                model.removeNodeFromParent(codeTemplateNode);

                if (model.getRoot() != fullModel.getRoot()) {
                    AbstractSortableTreeTableNode fullCodeTemplateNode = findFullNode(
                            (AbstractSortableTreeTableNode) codeTemplateNode);
                    fullModel.removeNodeFromParent(fullCodeTemplateNode);
                }
            }
        }

        updateCurrentNode.set(false);
        selectedNode = (MutableTreeTableNode) selectedPath.getLastPathComponent();
        model.removeNodeFromParent(selectedNode);

        if (model.getRoot() != fullModel.getRoot()) {
            fullModel.removeNodeFromParent(findFullNode((AbstractSortableTreeTableNode) selectedNode));
        }

        if (selectedNodeIndex < parent.getChildCount()) {
            newSelectedNode = (MutableTreeTableNode) parent.getChildAt(selectedNodeIndex);
        } else if (parent.getChildCount() > 0) {
            newSelectedNode = (MutableTreeTableNode) parent.getChildAt(parent.getChildCount() - 1);
        } else if (codeTemplate) {
            newSelectedNode = parent;
        }

        if (newSelectedNode != null) {
            final TreePath newSelectedPath = new TreePath(
                    ((CodeTemplateTreeTableModel) templateTreeTable.getTreeTableModel())
                            .getPathToRoot(newSelectedNode));
            selectTemplatePath(newSelectedPath);

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    selectTemplatePath(newSelectedPath);
                    updateCurrentNode.set(true);
                }
            });
        } else {
            switchSplitPaneComponent(blankPanel);
            updateCurrentNode.set(true);
        }

        updateFilterNotification();
    }
}

From source file:de.juwimm.cms.util.Communication.java

/**
 * delete in case of multiselect//from www  .java 2s.co  m
 * @param entriesPath
 * @return
 */
public ArrayList<TreePath> removeViewComponents(TreePath[] entriesPath) {
    boolean localVal;
    ArrayList<TreePath> deleted = new ArrayList<TreePath>();

    //      Arrays.<ViewComponentValue> sort(listofVC.toArray(arraysViewComponents), new Comparator<ViewComponentValue>() {
    //         public int compare(ViewComponentValue o1, ViewComponentValue o2) {
    //            if ((o1 != null) && (o2 != null)) { return (int) (Integer.valueOf(o2.getViewLevel()) - Integer.valueOf(o1.getViewLevel()));
    //
    //            }
    //            return 0;
    //         }
    //
    //      });
    try {
        for (TreePath treePath : entriesPath) {
            localVal = true;
            PageNode local = (PageNode) treePath.getLastPathComponent();
            byte onlineState = local.getOnline();
            int intViewComponentId = local.getViewComponent().getViewComponentId();
            try {
                ViewIdAndInfoTextValue[] str = getAllChildrenNamesWithUnit(intViewComponentId);
                String units = "";
                if (str != null && str.length > 0) {
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < str.length; i++) {
                        sb.append(str[i].getInfoText().trim()).append("\n");
                    }
                    units = sb.toString();
                }
                if (!units.equalsIgnoreCase("")) {
                    if (!isUserInRole(UserRights.SITE_ROOT)) {
                        // dazwischen, damit sparen wir uns das zweite...
                        String msg = Messages.getString("comm.removevc.containsunitsandcannotremove", units);
                        JOptionPane.showMessageDialog(UIConstants.getMainFrame(), msg,
                                rb.getString("dialog.title"), JOptionPane.ERROR_MESSAGE);
                        localVal = false;
                    }
                    units = Messages.getString("comm.removevc.header_units", units);
                }
                if (localVal) {
                    if (onlineState == Constants.ONLINE_STATUS_UNDEF
                            || onlineState == Constants.ONLINE_STATUS_OFFLINE) {
                        try {
                            getClientService().removeViewComponent(Integer.valueOf(intViewComponentId), true);
                        } catch (Exception e) {
                        }
                    }
                }
            } catch (Exception exe) {
                log.error("Error removing vc", exe);
            }
            if (localVal) {
                try {
                    checkOutPages.remove(new Integer(getViewComponent(intViewComponentId).getReference()));
                } catch (Exception exe) {
                }
                UIConstants.setStatusInfo(rb.getString("comm.removevc.statusinfo"));
            }
            if (localVal)
                deleted.add(treePath);

        }
    } catch (Exception e) {
        log.error("Error at multiple delete");
    }
    return deleted;
}