Example usage for javax.swing.tree DefaultTreeCellRenderer DefaultTreeCellRenderer

List of usage examples for javax.swing.tree DefaultTreeCellRenderer DefaultTreeCellRenderer

Introduction

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

Prototype

public DefaultTreeCellRenderer() 

Source Link

Document

Creates a DefaultTreeCellRenderer .

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JTree tree = new JTree(buildDemoModel());

    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon");
        private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon");

        @Override/*from w ww  .  j  a va2 s . c  o m*/
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean isLeaf, int row, boolean focused) {
            Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row,
                    focused);
            if (selected)
                setIcon(loadIcon);
            else
                setIcon(saveIcon);
            return c;
        }
    });
    tree.setVisibleRowCount(10);
    frame.add(new JScrollPane(tree));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JTree tree = new JTree(createTreeModel(new File(System.getProperty("user.dir")), true));
    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        @Override/*from ww w  .  j a va2  s.  c  o m*/
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                if (node.getUserObject() instanceof File) {
                    setText(((File) node.getUserObject()).getName());
                }
            }
            return this;
        }
    });
    tree.getSelectionModel().addTreeSelectionListener(e -> {
        Object object = tree.getLastSelectedPathComponent();
        if (object instanceof DefaultMutableTreeNode) {
            Object userObject = ((DefaultMutableTreeNode) object).getUserObject();
            if (userObject instanceof File) {
                File file = (File) userObject;
                System.out.println(
                        "Selected file" + file.getAbsolutePath() + " Is directory? " + file.isDirectory());
            }
        }
    });
    JScrollPane pane = new JScrollPane(tree);
    frame.add(pane);
    frame.setSize(400, 600);
    frame.setVisible(true);
}

From source file:Tree1.java

public Tree1() {
    super("Sample Tree [OID]");
    setSize(400, 300);/*from   www . j a v  a2 s  . c  om*/

    Object[] nodes = new Object[5];
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new OidNode(1, "ISO"));
    DefaultMutableTreeNode parent = top;
    nodes[0] = top;

    DefaultMutableTreeNode node = new DefaultMutableTreeNode(new OidNode(0, "standard"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2, "member-body"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(3, "org"));
    parent.add(node);
    parent = node;
    nodes[1] = parent;

    node = new DefaultMutableTreeNode(new OidNode(6, "dod"));
    parent.add(node);
    parent = node;
    nodes[2] = parent;

    node = new DefaultMutableTreeNode(new OidNode(1, "internet"));
    parent.add(node);
    parent = node;
    nodes[3] = parent;

    node = new DefaultMutableTreeNode(new OidNode(1, "directory"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2, "mgmt"));
    parent.add(node);
    nodes[4] = node;
    node.add(new DefaultMutableTreeNode(new OidNode(1, "mib-2")));
    node = new DefaultMutableTreeNode(new OidNode(3, "experimental"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(4, "private"));
    node.add(new DefaultMutableTreeNode(new OidNode(1, "enterprises")));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(5, "security"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(6, "snmpV2"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(7, "mail"));
    parent.add(node);

    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);

    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    renderer.setOpenIcon(new ImageIcon("opened.gif"));
    renderer.setClosedIcon(new ImageIcon("closed.gif"));
    renderer.setLeafIcon(new ImageIcon("leaf.gif"));
    m_tree.setCellRenderer(renderer);

    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(false);
    TreePath path = new TreePath(nodes);
    m_tree.setSelectionPath(path);

    m_tree.addTreeSelectionListener(new OidSelectionListener());

    JScrollPane s = new JScrollPane();
    s.getViewport().add(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

    m_display = new JTextField();
    m_display.setEditable(false);
    getContentPane().add(m_display, BorderLayout.SOUTH);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:com.mirth.connect.client.ui.components.MirthTree.java

public MirthTree(MirthTreeNode root, String prefix, String suffix) {
    this.parent = PlatformUI.MIRTH_FRAME;

    setCellRenderer(new DefaultTreeCellRenderer() {
        @Override//from  ww  w .  ja v a  2s . c  o m
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            setFont(UIConstants.MONOSPACED_FONT);
            putClientProperty("html.disable", Boolean.TRUE);
            return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        }
    });

    mf = new MyFilter();
    ftm = new FilterTreeModel(root, mf);
    this.setModel(ftm);
    dropTarget = new DropTarget(this, this);
    this.prefix = prefix;
    this.suffix = suffix;

    if (prefix != null) {
        if (prefix.equals(MessageTreePanel.MAPPER_PREFIX)) {
            this.supportedDropFlavor = TreeTransferable.MESSAGE_BUILDER_DATA_FLAVOR;
        } else if (prefix.equals(MessageTreePanel.MESSAGE_BUILDER_PREFIX)) {
            this.supportedDropFlavor = TreeTransferable.MAPPER_DATA_FLAVOR;
        }
    }
}

From source file:gov.sandia.umf.platform.ui.jobs.RunPanel.java

public RunPanel() {
    root = new NodeBase();
    model = new DefaultTreeModel(root);
    tree = new JTree(model);
    tree.setRootVisible(false);/*  w  w  w  .  j  av a2 s  .c o m*/
    tree.setShowsRootHandles(true);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

            NodeBase node = (NodeBase) value;
            Icon icon = node.getIcon(expanded); // A node knows whether it should hold other nodes or not, so don't pass leaf to it.
            if (icon == null) {
                if (leaf)
                    icon = getDefaultLeafIcon();
                else if (expanded)
                    icon = getDefaultOpenIcon();
                else
                    icon = getDefaultClosedIcon();
            }
            setIcon(icon);

            return this;
        }
    });

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            NodeBase newNode = (NodeBase) tree.getLastSelectedPathComponent();
            if (newNode == null)
                return;
            if (newNode == displayNode)
                return;

            if (displayThread != null)
                synchronized (displayText) {
                    displayThread.stop = true;
                }
            displayNode = newNode;
            if (displayNode instanceof NodeFile)
                viewFile();
            else if (displayNode instanceof NodeJob)
                viewJob();
        }
    });

    tree.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            int keycode = e.getKeyCode();
            if (keycode == KeyEvent.VK_DELETE || keycode == KeyEvent.VK_BACK_SPACE) {
                delete();
            }
        }
    });

    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            TreePath path = event.getPath(); // TODO: can this ever be null?
            Object o = path.getLastPathComponent();
            if (o instanceof NodeJob)
                ((NodeJob) o).build(tree);
        }

        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
        }
    });

    tree.addTreeExpansionListener(new TreeExpansionListener() {
        public void treeExpanded(TreeExpansionEvent event) {
            Rectangle node = tree.getPathBounds(event.getPath());
            Rectangle visible = treePane.getViewport().getViewRect();
            visible.height -= node.y - visible.y;
            visible.y = node.y;
            tree.repaint(visible);
        }

        public void treeCollapsed(TreeExpansionEvent event) {
            Rectangle node = tree.getPathBounds(event.getPath());
            Rectangle visible = treePane.getViewport().getViewRect();
            visible.height -= node.y - visible.y;
            visible.y = node.y;
            tree.repaint(visible);
        }
    });

    Thread refreshThread = new Thread() {
        public void run() {
            try {
                // Initial load
                synchronized (running) {
                    for (MNode n : AppData.runs)
                        running.add(0, new NodeJob(n)); // This should be efficient on a doubly-linked list.
                    for (NodeJob job : running)
                        root.add(job);
                }
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        model.nodeStructureChanged(root);
                        if (model.getChildCount(root) > 0)
                            tree.setSelectionRow(0);
                    }
                });

                // Periodic refresh to show status of running jobs
                int shortCycles = 100; // Force full scan on first cycle.
                while (true) {
                    NodeBase d = displayNode; // Make local copy (atomic action) to prevent it changing from under us
                    if (d instanceof NodeJob)
                        ((NodeJob) d).monitorProgress(RunPanel.this);
                    if (shortCycles++ < 20) {
                        Thread.sleep(1000);
                        continue;
                    }
                    shortCycles = 0;

                    synchronized (running) {
                        Iterator<NodeJob> i = running.iterator();
                        while (i.hasNext()) {
                            NodeJob job = i.next();
                            if (job != d)
                                job.monitorProgress(RunPanel.this);
                            if (job.complete >= 1)
                                i.remove();
                        }
                    }
                }
            } catch (InterruptedException e) {
            }
        }
    };
    refreshThread.setDaemon(true);
    refreshThread.start();

    displayText = new JTextArea();
    displayText.setEditable(false);

    final JCheckBox chkFixedWidth = new JCheckBox("Fixed-Width Font");
    chkFixedWidth.setFocusable(false);
    chkFixedWidth.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int size = displayText.getFont().getSize();
            if (chkFixedWidth.isSelected()) {
                displayText.setFont(new Font(Font.MONOSPACED, Font.PLAIN, size));
            } else {
                displayText.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, size));
            }
        }
    });

    displayPane.setViewportView(displayText);

    ActionListener graphListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (displayNode instanceof NodeFile) {
                NodeFile nf = (NodeFile) displayNode;
                if (nf.type == NodeFile.Type.Output || nf.type == NodeFile.Type.Result) {
                    String graphType = e.getActionCommand();
                    if (displayPane.getViewport().getView() instanceof ChartPanel
                            && displayGraph.equals(graphType)) {
                        viewFile();
                        displayGraph = "";
                    } else {
                        if (graphType.equals("Graph")) {
                            Plot plot = new Plot(nf.path.getAbsolutePath());
                            if (!plot.columns.isEmpty())
                                displayPane.setViewportView(plot.createGraphPanel());
                        } else // Raster
                        {
                            Raster raster = new Raster(nf.path.getAbsolutePath(), displayPane.getHeight());
                            displayPane.setViewportView(raster.createGraphPanel());
                        }
                        displayGraph = graphType;
                    }
                }
            }
        }
    };

    buttonGraph = new JButton("Graph", ImageUtil.getImage("analysis.gif"));
    buttonGraph.setFocusable(false);
    buttonGraph.addActionListener(graphListener);
    buttonGraph.setActionCommand("Graph");

    buttonRaster = new JButton("Raster", ImageUtil.getImage("prnplot.gif"));
    buttonRaster.setFocusable(false);
    buttonRaster.addActionListener(graphListener);
    buttonRaster.setActionCommand("Raster");

    Lay.BLtg(this, "C",
            Lay.SPL(Lay.BL("C", treePane = Lay.sp(tree)), Lay.BL("N",
                    Lay.FL(chkFixedWidth, Lay.FL(buttonGraph, buttonRaster), "hgap=50"), "C", displayPane),
                    "divpixel=250"));
    setFocusCycleRoot(true);
}

From source file:OAT.ui.util.UiUtil.java

public static void clearTreeIcon(JTree tree) {
    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    renderer.setOpenIcon(null);/*  w  w  w. j ava2  s. c  o  m*/
    renderer.setClosedIcon(null);
    //renderer.setLeafIcon(null);
    tree.setCellRenderer(renderer);
}

From source file:TreeIconDemo.java

public TreeIconDemo() {
    super(new GridLayout(1, 0));

    // Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/*from   ww w .j a  v a  2 s  .  c o m*/

    // Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Set the icon for leaf nodes.
    ImageIcon leafIcon = createImageIcon("images/middle.gif");
    if (leafIcon != null) {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setLeafIcon(leafIcon);
        tree.setCellRenderer(renderer);
    } else {
        System.err.println("Leaf icon missing; using default.");
    }

    // Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    // Create the scroll pane and add the tree to it.
    JScrollPane treeView = new JScrollPane(tree);

    // Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    // Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); // XXX: ignored in some releases
    // of Swing. bug 4101306
    // workaround for bug 4101306:
    // treeView.setPreferredSize(new Dimension(100, 100));

    splitPane.setPreferredSize(new Dimension(500, 300));

    // Add the split pane to this panel.
    add(splitPane);
}

From source file:components.TreeIconDemo.java

public TreeIconDemo() {
    super(new GridLayout(1, 0));

    //Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/*from  ww  w.  j ava2  s. c om*/

    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Set the icon for leaf nodes.
    ImageIcon leafIcon = createImageIcon("images/middle.gif");
    if (leafIcon != null) {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setLeafIcon(leafIcon);
        tree.setCellRenderer(renderer);
    } else {
        System.err.println("Leaf icon missing; using default.");
    }

    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);

    //Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); //XXX: ignored in some releases
                                       //of Swing. bug 4101306
                                       //workaround for bug 4101306:
                                       //treeView.setPreferredSize(new Dimension(100, 100)); 

    splitPane.setPreferredSize(new Dimension(500, 300));

    //Add the split pane to this panel.
    add(splitPane);
}

From source file:uk.co.markfrimston.tasktree.Main.java

public Main(TaskTree taskTree) {
    super();//  ww w  . j  a  v  a2 s  . c  o  m

    this.taskTree = taskTree;

    this.setTitle("Task Tree");
    this.setSize(new Dimension(300, 500));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel quickInPanel = new JPanel(new BorderLayout());
    this.quickIn = new JTextArea();
    this.quickIn.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                String newText = quickIn.getText().trim();
                if (newText != null && newText.length() > 0) {
                    addTask(Main.this.taskTree.getRoot(), 0, newText, true);
                    try {
                        Main.this.taskTree.changesMade();
                    } catch (Exception e) {
                        error(e.getMessage());
                    }
                }
                quickIn.setText("");
            }
        }
    });
    this.quickIn.setPreferredSize(new Dimension(300, 75));
    this.quickIn.setBorder(BorderFactory.createTitledBorder("Quick Input"));
    quickInPanel.add(this.quickIn, BorderLayout.CENTER);
    this.syncButton = new JButton("Sync");
    this.syncButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new SyncThread(Main.this).start();
        }
    });
    quickInPanel.add(this.syncButton, BorderLayout.EAST);
    this.getContentPane().add(quickInPanel, BorderLayout.NORTH);

    this.tree = new JTree(taskTree.getTreeModel());
    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            Object newVal = htmlFilter(String.valueOf(node.getUserObject()));
            if (node.getChildCount() > 0 && !tree.isExpanded(new TreePath(node.getPath()))) {
                DefaultMutableTreeNode firstLeaf = (DefaultMutableTreeNode) node.getFirstLeaf();
                newVal = htmlFilter(String.valueOf(node.getUserObject()))
                        + " <span style='color:silver;font-style:italic'>" + "("
                        + String.valueOf(firstLeaf.getUserObject()) + ")</span>";
            }
            newVal = "<html>" + newVal + "</html>";

            return super.getTreeCellRendererComponent(tree, newVal, selected, expanded, leaf, row, hasFocus);
        }
    };
    ImageIcon bulletIcon = new ImageIcon(Main.class.getResource("bullet.gif"));
    renderer.setLeafIcon(bulletIcon);
    renderer.setOpenIcon(bulletIcon);
    renderer.setClosedIcon(bulletIcon);
    renderer.setBorder(BorderFactory.createEmptyBorder(4, 0, 4, 0));
    this.tree.setCellRenderer(renderer);
    this.tree.setRootVisible(false);
    this.tree.setShowsRootHandles(true);
    this.tree.addMouseListener(new MouseAdapter() {
        protected void doSelectRow(MouseEvent arg0) {
            int row = tree.getRowForLocation(arg0.getX(), arg0.getY());
            if (row != -1) {
                tree.setSelectionRow(row);
                if (arg0.isPopupTrigger()) {
                    popup.show(tree, arg0.getX(), arg0.getY());
                }
            }
        }

        public void mousePressed(MouseEvent arg0) {
            doSelectRow(arg0);
        }

        public void mouseReleased(MouseEvent arg0) {
            doSelectRow(arg0);
        }
    });
    JScrollPane treeScroll = new JScrollPane(tree);
    treeScroll.setBorder(BorderFactory.createTitledBorder("Task List"));
    this.getContentPane().add(treeScroll, BorderLayout.CENTER);

    this.popup = new JPopupMenu();
    JMenuItem addBefore = new JMenuItem("Add Before");
    addBefore.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = parent.getIndex(selected);
            promptAndInsert(parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(addBefore);
    JMenuItem addAfter = new JMenuItem("Add After");
    addAfter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = parent.getIndex(selected) + 1;
            promptAndInsert(parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(addAfter);
    JMenuItem addNested = new JMenuItem("Add Nested");
    addNested.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            int pos = selected.getChildCount();
            promptAndInsert(selected, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                ex.getMessage();
            }
        }
    });
    this.popup.add(addNested);
    this.popup.add(new JSeparator());
    JMenuItem moveTop = new JMenuItem("Move to Top");
    moveTop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            moveTask(selected, parent, 0);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveTop);
    JMenuItem moveUp = new JMenuItem("Move Up");
    moveUp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = Math.max(parent.getIndex(selected) - 1, 0);
            moveTask(selected, parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveUp);
    JMenuItem moveDown = new JMenuItem("Move Down");
    moveDown.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            int pos = Math.min(parent.getIndex(selected) + 1, parent.getChildCount() - 1);
            moveTask(selected, parent, pos);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveDown);
    JMenuItem moveBottom = new JMenuItem("Move to Bottom");
    moveBottom.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent();
            moveTask(selected, parent, parent.getChildCount() - 1);
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(moveBottom);
    this.popup.add(new JSeparator());
    JMenuItem rename = new JMenuItem("Edit");
    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selected = getSelectedNode();
            String newText = prompt((String) selected.getUserObject());
            if (newText != null && newText.length() > 0) {
                selected.setUserObject(newText);
                Main.this.taskTree.getTreeModel().reload(selected);
                try {
                    Main.this.taskTree.changesMade();
                } catch (Exception ex) {
                    error(ex.getMessage());
                }
            }
        }
    });
    this.popup.add(rename);
    JMenuItem delete = new JMenuItem("Delete");
    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            promptAndRemove(getSelectedNode());
            try {
                Main.this.taskTree.changesMade();
            } catch (Exception ex) {
                error(ex.getMessage());
            }
        }
    });
    this.popup.add(delete);

    this.setVisible(true);

    loadConfig();
    load();

    syncButton.setVisible(this.taskTree.hasSyncCapability());
}

From source file:com.clough.android.adbv.view.MainFrame.java

public MainFrame(IOManager ioManager, HistoryManager historyManager) {
    this();//from  ww  w .  ja va 2 s  .  c  om
    this.ioManager = ioManager;
    this.historyManager = historyManager;
    ioManager.addConnectionLostListener(new IOManager.ConnectionLostListener() {
        @Override
        public void onDisconnect() {
            showDeviceDisconnectedDialog();
        }
    });

    new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
            try {
                applicationID = MainFrame.this.ioManager.getApplicationID();
                deviceName = MainFrame.this.ioManager.getDeviceName();
                databaseName = MainFrame.this.ioManager.getDatabaseName();
                setTitle(ValueHolder.WINDOW_TITLE + " - (" + deviceName + " - " + applicationID + ")");
            } catch (IOManagerException ex) {
                showDeviceDisconnectedDialog();
            }
            return null;
        }

        @Override
        protected void done() {
            closeProgressDialog();
        }

    }.execute();
    showProgressDialog(true, 0, "Waiting for device/app info");

    refreshDatabase();

    tableInfoTree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) value;
            String nodeString = String.valueOf(dmtn.getUserObject());
            ImageIcon selectedImageIcon = null;
            if (nodeString.equals(databaseName)) {
                selectedImageIcon = ValueHolder.Icons.DATABASE;
            } else {
                l: for (int i = 0; i < tables.length; i++) {
                    String tableName = tables[i];
                    if (tableName.equals(nodeString)) {
                        selectedImageIcon = ValueHolder.Icons.TABLE;
                        break;
                    } else if (i == tables.length - 1) {
                        for (int p = 0; p < tables.length; p++) {
                            for (int j = 0; j < columns[p].length; j++) {
                                String columnName = columns[p][j];
                                if (columnName.equals(nodeString)) {
                                    selectedImageIcon = ValueHolder.Icons.PENCIL;
                                    break l;
                                } else if (j == columns[p].length - 1) {
                                    for (int q = 0; q < tables.length; q++) {
                                        for (int r = 0; r < columns[q].length; r++) {
                                            for (int k = 0; k < columnInfos[q][r].length; k++) {
                                                String columnInfo = columnInfos[q][r][k];
                                                if (columnInfo.equals(nodeString)) {
                                                    switch (k) {
                                                    case 0: {
                                                        selectedImageIcon = ValueHolder.Icons.HASH_TAG;
                                                        break l;
                                                    }
                                                    case 1: {
                                                        selectedImageIcon = ValueHolder.Icons.BLUE;
                                                        break l;
                                                    }
                                                    case 2: {
                                                        selectedImageIcon = ValueHolder.Icons.ORANGE;
                                                        break l;
                                                    }
                                                    case 3: {
                                                        selectedImageIcon = ValueHolder.Icons.GREEN;
                                                        break l;
                                                    }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            setIcon(selectedImageIcon);
            return this;
        }

    });

    tableInfoTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent evt) {
            Object[] data = evt.getPath().getPath();
            selectedTreeNodeValue = String.valueOf(data[data.length - 1]);
        }
    });

    tableInfoTree.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent evt) {
            if (evt.getButton() == MouseEvent.BUTTON3) {
                if (selectedTreeNodeValue.equals(databaseName)) {
                    showTreeNodePopup(evt.getX(), evt.getY(), true);
                } else {
                    for (String table : tables) {
                        if (table.equals(selectedTreeNodeValue)) {
                            showTreeNodePopup(evt.getX(), evt.getY(), false);
                            break;
                        }
                    }
                }
            } else if (evt.getClickCount() >= 2) {
                queryingTextArea.setText(queryingTextArea.getText() + "`" + selectedTreeNodeValue + "`");
            }
        }

    });

    currentHistoryList = historyManager.getHistoryList();
    for (int i = 0; i < currentHistoryList.size(); i++) {
        String[] history = currentHistoryList.get(i);
        historyContainingPanel.add(
                new HistoryItemPanel(i + 1, history[0].equals(applicationID), history[1], queryingTextArea));
    }
    adjustHistoryScrollbar();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                MainFrame.this.historyManager.saveApplicationDb();
            } catch (HistoryManagerException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Application history saving",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }));

}