Example usage for javax.swing JTree JTree

List of usage examples for javax.swing JTree JTree

Introduction

In this page you can find the example usage for javax.swing JTree JTree.

Prototype

@ConstructorProperties({ "model" })
public JTree(TreeModel newModel) 

Source Link

Document

Returns an instance of JTree which displays the root node -- the tree is created using the specified data model.

Usage

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

/**
 * Creates a new Explorer object.//from   w w  w. j  a  v  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:ubic.basecode.dataStructure.graph.DirectedGraph.java

/**
 * Generate a JTree corresponding to this graph.
 * /*from w w  w .j  ava  2  s.  c  o m*/
 * @param nodeClass The class to be used for TreeNodes. Defaults to DefaultMutableTreeNode.
 * @return javax.swing.JTree
 */
@SuppressWarnings("unchecked")
public JTree treeView(Class<? extends DefaultMutableTreeNode> nodeClass) {
    log.debug("Constructing tree view of graph");
    DirectedGraphNode<K, V> root = getRoot();
    Constructor<DefaultMutableTreeNode> constructor;
    DefaultMutableTreeNode top = null;
    treeView = null;
    try {
        constructor = (Constructor<DefaultMutableTreeNode>) nodeClass
                .getConstructor(new Class[] { root.getClass() });
        top = constructor.newInstance(new Object[] { root });
        log.debug("Starting tree with: " + top.getClass().getName());
        root.mark();
        addJTreeNode(top, root, constructor);
        dtm = new DefaultTreeModel(top);
        treeView = new JTree(dtm);

    } catch (Exception e) {
        log.error(e, e);
    }
    return treeView;
}

From source file:ui.panel.UILicenseAdd.java

private JPanel createPnlFeature() {
    JPanel panel = p.createPanel(Layouts.gridbag);

    GridBagConstraints g = new GridBagConstraints();
    lblSelectFeature = l.createLabel("Select Features", SwingConstants.LEFT);
    g.gridx = 0;//from   w ww .  j  a  va2 s.  c  om
    g.gridy = 0;
    g.gridwidth = 2;
    g.insets = new Insets(5, 5, 5, 5);
    panel.add(lblSelectFeature, g);

    lblServiceApi = l.createLabel("Service APIs Used", SwingConstants.LEFT);
    g.gridx = 4;
    g.gridy = 0;
    g.gridwidth = 2;
    g.insets = new Insets(5, 5, 5, 5);
    panel.add(lblServiceApi, g);
    getFeaturesData();
    DefaultMutableTreeNode root = new MyDataNode("Features", "Features");
    for (String key : Data.featureList.keySet()) {
        try {
            DefaultMutableTreeNode element = new MyDataNode(key, Data.fieldNames.get(key));

            JSONArray features = Data.featureList.get(key);
            ArrayList<DefaultMutableTreeNode> arrayFeatureCheckBox = new ArrayList<DefaultMutableTreeNode>();

            for (int i = 0; i < features.length(); i++) {
                JSONObject feature = features.getJSONObject(i);
                DefaultMutableTreeNode featureElement = new MyDataNode((String) feature.get("name"),
                        Data.fieldNames.get(feature.get("name")));
                element.add(featureElement);
                arrayFeatureCheckBox.add(featureElement);
            }

            root.add(element);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    tree = new JTree(root);
    tree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = tree.getSelectionModel().getSelectionPath();
            model.removeAllElements();
            String item = tree.getSelectionModel().getSelectionPath().getLastPathComponent().toString();
            MyDataNode node = (MyDataNode) tree.getSelectionModel().getSelectionPath().getLastPathComponent();
            String selected = node.getValue();
            servicesList = new HashMap<String, String>();
            try {
                if (selected.equalsIgnoreCase("Features")) {
                    for (String key : Data.featureList.keySet()) {
                        JSONArray featureArray = Data.featureList.get(key);

                        for (int i = 0; i < featureArray.length(); i++) {
                            JSONArray servicesArray = featureArray.getJSONObject(i).getJSONArray("services");

                            for (int x = 0; x < servicesArray.length(); x++) {
                                servicesList.put(Integer.toString(servicesArray.getJSONObject(x).getInt("id")),
                                        servicesArray.getJSONObject(x).getString("name"));
                            }
                        }
                    }
                } else if (path.getPathCount() == 2) {
                    for (String key : Data.featureList.keySet()) {
                        if (key.equals(selected)) {
                            JSONArray featureArray = Data.featureList.get(key);

                            for (int i = 0; i < featureArray.length(); i++) {
                                JSONArray servicesArray = featureArray.getJSONObject(i)
                                        .getJSONArray("services");

                                for (int x = 0; x < servicesArray.length(); x++) {
                                    servicesList.put(
                                            Integer.toString(servicesArray.getJSONObject(x).getInt("id")),
                                            servicesArray.getJSONObject(x).getString("name"));
                                }
                            }
                        } else {
                            continue;
                        }
                    }
                } else {
                    MyDataNode nodePath = (MyDataNode) path.getPathComponent(1);
                    for (String key : Data.featureList.keySet()) {
                        if (key.equals(nodePath.getValue())) {

                            JSONArray featureArray = Data.featureList.get(key);
                            for (int i = 0; i < featureArray.length(); i++) {
                                System.out.println(
                                        featureArray.getJSONObject(i).getString("name") + ":" + selected);
                                if (featureArray.getJSONObject(i).getString("name").equals(selected)) {
                                    JSONArray servicesArray = featureArray.getJSONObject(i)
                                            .getJSONArray("services");
                                    System.out.println(servicesArray);
                                    for (int x = 0; x < servicesArray.length(); x++) {
                                        servicesList.put(
                                                Integer.toString(servicesArray.getJSONObject(x).getInt("id")),
                                                servicesArray.getJSONObject(x).getString("name"));
                                    }
                                } else {
                                    continue;
                                }
                            }
                        } else {
                            continue;
                        }
                    }
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            for (String serviceKey : servicesList.keySet()) {
                model.addElement(servicesList.get(serviceKey));
            }
            listServiceUsed.setModel(model);
        }
    });
    checkTreeManager = new CheckTreeManager(tree);

    spTreeCheckBox = new JScrollPane(tree);
    spTreeCheckBox.setPreferredSize(new Dimension(300, 300));
    g.gridx = 0;
    g.gridy = 1;
    g.gridwidth = 3;
    g.anchor = g.LINE_START;
    g.insets = new Insets(5, 5, 5, 5);
    panel.add(spTreeCheckBox, g);

    model = new DefaultListModel<>();
    model.addElement("");
    listServiceUsed = new JList<String>(model);
    spList = new JScrollPane(listServiceUsed);
    spList.setPreferredSize(new Dimension(300, 300));
    g.gridx = 4;
    g.gridy = 1;
    g.gridwidth = 3;
    g.anchor = g.LINE_START;
    panel.add(spList, g);

    return panel;
}

From source file:ui.panel.UILicenseDetail.java

private JPanel createPnlFeature() {
    JPanel panel = p.createPanel(Layouts.gridbag);

    GridBagConstraints g = new GridBagConstraints();
    g.weightx = 0.25;// w  ww  . j  ava  2 s.  c  o m
    g.weighty = 0.25;
    g.fill = g.BOTH;
    g.anchor = g.CENTER;

    lblSelectFeature = l.createLabel("Features", SwingConstants.LEFT);
    g.gridx = 0;
    g.gridy = 0;
    g.gridwidth = 2;
    g.insets = new Insets(5, 5, 5, 5);
    panel.add(lblSelectFeature, g);

    tree = new JTree(root);
    spTreeCheckBox = new JScrollPane(tree);
    spTreeCheckBox.setPreferredSize(new Dimension(300, 300));
    g.gridx = 0;
    g.gridy = 1;
    g.gridwidth = 3;
    g.insets = new Insets(5, 5, 5, 5);
    panel.add(spTreeCheckBox, g);

    return panel;
}