Example usage for javax.swing.tree TreeSelectionModel SINGLE_TREE_SELECTION

List of usage examples for javax.swing.tree TreeSelectionModel SINGLE_TREE_SELECTION

Introduction

In this page you can find the example usage for javax.swing.tree TreeSelectionModel SINGLE_TREE_SELECTION.

Prototype

int SINGLE_TREE_SELECTION

To view the source code for javax.swing.tree TreeSelectionModel SINGLE_TREE_SELECTION.

Click Source Link

Document

Selection can only contain one path at a time.

Usage

From source file:tk.tomby.tedit.plugins.explorer.Explorer.java

/**
 * Creates a new Explorer object./*from w  ww .j av  a 2 s  .c o m*/
 */
public Explorer() {
    super("Explorer");

    File rootDir = new File(PreferenceManager.getString("explorer.directory", System.getProperty("user.home")));

    JPanel internalPanel = new JPanel();
    internalPanel.setLayout(new BorderLayout());

    topPanel = new TopPanel(rootDir.getAbsolutePath());
    topPanel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JComboBox combo = (JComboBox) evt.getSource();
            TaskManager.execute(new ReadDirectoryWorker(combo));
        }
    });

    internalPanel.add(BorderLayout.NORTH, topPanel);

    DefaultMutableTreeNode directoryRoot = new DefaultMutableTreeNode(rootDir);

    directoryTreeModel = new ShortedTreeModel(directoryRoot, new ToStringComparator());
    directoryTree = new JTree(directoryTreeModel);
    directoryTree.setCellRenderer(new DirectoryCellRenderer());
    directoryTree.setRootVisible(true);
    directoryTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    directoryTree.setEnabled(true);
    directoryTree.setEditable(false);
    directoryTree.setShowsRootHandles(true);
    directoryTree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath path = evt.getPath();
            TreePath leadPath = evt.getNewLeadSelectionPath();

            if ((path != null) && (leadPath != null)) {
                TaskManager.execute(new RefreshWorker(leadPath, false));
            }
        }
    });
    directoryTree.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            if (evt.getClickCount() == 2) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) directoryTree
                        .getLastSelectedPathComponent();

                if (!node.isRoot()) {
                    File dir = (File) node.getUserObject();

                    topPanel.setDirectory(dir.getAbsolutePath());
                }
            }
        }
    });

    JScrollPane directoryScroll = new JScrollPane(directoryTree);
    directoryScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    directoryScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    directoryScroll.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);

    fileListModel = new ShortedListModel(new ToStringComparator());
    fileList = new JList(fileListModel);
    fileList.setDragEnabled(true);
    fileList.setCellRenderer(new FileCellRenderer());
    fileList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            if (evt.getClickCount() == 2) {
                final int index = fileList.locationToIndex(evt.getPoint());

                TaskManager.execute(new Runnable() {
                    public void run() {
                        File file = (File) fileListModel.getElementAt(index);

                        if (log.isDebugEnabled()) {
                            log.debug(file);
                        }

                        IBuffer buffer = new BufferFactory().createBuffer();
                        buffer.open(file);

                        WorkspaceManager.addBuffer(buffer);
                    }
                });
            }
        }
    });

    JScrollPane filesScroll = new JScrollPane(fileList);
    filesScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    filesScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    filesScroll.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);

    splitPane = new StrippedSplitPane(JSplitPane.VERTICAL_SPLIT, directoryScroll, filesScroll);
    splitPane.setBorder(BorderFactory.createEmptyBorder());
    splitPane.setOneTouchExpandable(false);
    splitPane.setDividerSize(5);
    splitPane.setDividerLocation(PreferenceManager.getInt("explorer.divider", 0));

    List<MutableTreeNode> dirs = openDirectory(rootDir);
    directoryTreeModel.insertAllInto(dirs, directoryRoot);

    List<File> files = getFiles(rootDir);
    fileListModel.addAll(files);

    directoryTree.expandRow(0);

    internalPanel.add(splitPane, BorderLayout.CENTER);

    setContent(internalPanel);
}

From source file:view.WorkspacePanel.java

private void startValidationThread() {
    status = Status.VALIDATING;//from  www . ja  v  a  2s  . c  o m
    svList.clear();
    jtValidation.clearSelection();
    jtValidation.setModel(null);
    showSignatureValidationDetails(null);
    Runnable r = new Runnable() {
        @Override
        public void run() {
            try {
                progressBar.setValue(0);
                jtValidation.setModel(null);
                jtValidation.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
                jtValidation.setRootVisible(false);
                ValidationTreeCellRenderer renderer = new ValidationTreeCellRenderer();
                jtValidation.setCellRenderer(renderer);
                final int numSigs = CCInstance.getInstance()
                        .getNumberOfSignatures(document.getDocumentLocation());
                if (numSigs > 0) {
                    jScrollPane2.setVisible(true);
                    progressBar.setString(Bundle.getBundle().getString("pb.validatingSignature") + " 1 "
                            + Bundle.getBundle().getString("of") + " " + numSigs);
                    progressBar.setMaximum(numSigs);
                    final DefaultMutableTreeNode top = new DefaultMutableTreeNode(null);
                    ValidationListener vl = new ValidationListener() {
                        int numParsed = 1;

                        @Override
                        public void onValidationComplete(SignatureValidation sv) {
                            if (status.equals(Status.VALIDATING)) {
                                sv.setListener(WorkspacePanel.this);
                                svList.add(sv);
                                DefaultMutableTreeNode sig = new DefaultMutableTreeNode(sv);

                                TreeNodeWithState childChanged = null;
                                if (sv.isCertification()) {
                                    if (sv.isValid()) {
                                        if (sv.isChanged() || !sv.isCoversEntireDocument()) {
                                            childChanged = new TreeNodeWithState(
                                                    "<html>" + Bundle.getBundle().getString("tn.1") + "<br>"
                                                            + Bundle.getBundle().getString("tn.2") + "</html>",
                                                    TreeNodeWithState.State.CERTIFIED_WARNING);
                                        } else {
                                            childChanged = new TreeNodeWithState(
                                                    Bundle.getBundle().getString("certifiedOk"),
                                                    TreeNodeWithState.State.CERTIFIED);
                                        }
                                    } else {
                                        childChanged = new TreeNodeWithState(
                                                Bundle.getBundle().getString("changedAfterCertified"),
                                                TreeNodeWithState.State.INVALID);
                                    }
                                } else if (sv.isValid()) {
                                    if (sv.isChanged()) {
                                        childChanged = new TreeNodeWithState(
                                                "<html>" + Bundle.getBundle().getString("tn.3") + "<br>"
                                                        + Bundle.getBundle().getString("tn.4") + "</html>",
                                                TreeNodeWithState.State.VALID_WARNING);
                                    } else {
                                        childChanged = new TreeNodeWithState(
                                                Bundle.getBundle().getString("signedOk"),
                                                TreeNodeWithState.State.VALID);
                                    }
                                } else {
                                    childChanged = new TreeNodeWithState(
                                            Bundle.getBundle().getString("signedChangedOrCorrupted"),
                                            TreeNodeWithState.State.INVALID);
                                }

                                TreeNodeWithState childVerified = null;
                                if (sv.getOcspCertificateStatus().equals(CertificateStatus.OK)
                                        || sv.getCrlCertificateStatus().equals(CertificateStatus.OK)) {
                                    childVerified = new TreeNodeWithState(
                                            Bundle.getBundle().getString("certOK"),
                                            TreeNodeWithState.State.VALID);
                                } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.REVOKED)
                                        || sv.getCrlCertificateStatus().equals(CertificateStatus.REVOKED)) {
                                    childVerified = new TreeNodeWithState(
                                            Bundle.getBundle().getString("certRevoked"),
                                            TreeNodeWithState.State.INVALID);
                                } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.UNCHECKED)
                                        && sv.getCrlCertificateStatus().equals(CertificateStatus.UNCHECKED)) {
                                    childVerified = new TreeNodeWithState(
                                            Bundle.getBundle().getString("certNotVerified"),
                                            TreeNodeWithState.State.WARNING);
                                } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.UNCHAINED)) {
                                    childVerified = new TreeNodeWithState(
                                            Bundle.getBundle().getString("certNotChained"),
                                            TreeNodeWithState.State.WARNING);
                                } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.EXPIRED)) {
                                    childVerified = new TreeNodeWithState(
                                            Bundle.getBundle().getString("certExpired"),
                                            TreeNodeWithState.State.WARNING);
                                } else if (sv.getOcspCertificateStatus()
                                        .equals(CertificateStatus.CHAINED_LOCALLY)) {
                                    childVerified = new TreeNodeWithState(
                                            "<html>" + Bundle.getBundle().getString("tn.5") + "<br>"
                                                    + Bundle.getBundle().getString("tn.6") + "</html>",
                                            TreeNodeWithState.State.VALID_WARNING);
                                }

                                TreeNodeWithState childTimestamp = null;
                                if (sv.isValidTimeStamp()) {
                                    childTimestamp = new TreeNodeWithState(
                                            Bundle.getBundle().getString("validTimestamp"),
                                            TreeNodeWithState.State.VALID);
                                } else {
                                    childTimestamp = new TreeNodeWithState(
                                            Bundle.getBundle().getString("signerDateTime"),
                                            TreeNodeWithState.State.WARNING);
                                }

                                sig.add(childChanged);
                                sig.add(childVerified);
                                sig.add(childTimestamp);
                                top.add(sig);
                                TreeModel tm = new DefaultTreeModel(top);
                                jtValidation.setModel(tm);
                                progressBar.setValue(progressBar.getValue() + 1);
                                numParsed++;
                                progressBar.setString(
                                        Bundle.getBundle().getString("pb.validatingSignature") + " " + numParsed
                                                + " " + Bundle.getBundle().getString("of") + " " + numSigs);
                            } else {
                                CCInstance.getInstance().setValidating(false);
                            }
                        }
                    };
                    CCInstance.getInstance().validatePDF(document.getDocumentLocation(), vl);
                    status = Status.READY;
                    jtValidation.setVisible(true);
                    progressBar.setString(Bundle.getBundle().getString("pb.validationCompleted"));
                    if (rightPanel.isVisible() && jtValidation.getRowCount() > 0) {
                        jtValidation.setSelectionRow(jtValidation.getRowCount() - 1);
                    }
                    imagePanel.setSignatureValidationList(svList);
                    panelValidationResult.setVisible(true);
                    status = Status.READY;
                } else {
                    progressBar.setString(Bundle.getBundle().getString("notSigned"));
                    status = Status.READY;
                    jScrollPane2.setVisible(false);
                }
            } catch (IOException | DocumentException | GeneralSecurityException ex) {
                controller.Logger.getLogger().addEntry(ex);
                status = Status.READY;
                progressBar.setString(Bundle.getBundle().getString("validationError"));
                jScrollPane2.setVisible(false);
            }
        }
    };

    Thread t = new Thread(r);
    t.start();
}