Java Code Examples for javax.swing.JTree#getCellRenderer()

The following examples show how to use javax.swing.JTree#getCellRenderer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JTreeJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private String getTextForNodeObject(JTree tree, Object lastPathComponent) {
    TreeCellRenderer renderer = tree.getCellRenderer();
    if (renderer == null) {
        return null;
    }
    Component c = renderer.getTreeCellRendererComponent(tree, lastPathComponent, false, false, false, 0, false);
    if (c != null && c instanceof JLabel) {
        return ((JLabel) c).getText();
    }
    return lastPathComponent.toString();
}
 
Example 2
Source File: JTreeNodeJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public static Component getRendererComponent(JTree tree, int row) {
    TreeCellRenderer cellRenderer = tree.getCellRenderer();
    TreePath pathForRow = tree.getPathForRow(row);
    Object lastPathComponent = pathForRow.getLastPathComponent();
    Component rendererComponent = cellRenderer.getTreeCellRendererComponent(tree, lastPathComponent, false, false, false, row,
            false);
    if (rendererComponent == null) {
        return null;
    }
    if (rendererComponent instanceof JComponent) {
        ((JComponent) rendererComponent).putClientProperty("jtree", (JTree) tree);
        ((JComponent) rendererComponent).putClientProperty("row", row);
    }
    return rendererComponent;
}
 
Example 3
Source File: RTree.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private String getTextForNodeObject(JTree tree, Object lastPathComponent) {
    TreeCellRenderer renderer = tree.getCellRenderer();
    if (renderer == null) {
        return null;
    }
    Component c = renderer.getTreeCellRendererComponent(tree, lastPathComponent, false, false, false, 0, false);
    if (c != null && c instanceof JLabel) {
        return ((JLabel) c).getText();
    }
    return lastPathComponent.toString();
}
 
Example 4
Source File: QualifierTable.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public TableCellRenderer getCellRenderer(final int row, final int column) {

    JTree tree = (JTree) super.getCellRenderer(row, column);
    DelegatingRenderer rend = ((DelegatingRenderer) tree.getCellRenderer());

    rend.setLeafIcon(leafIcon);
    rend.setOpenIcon(folderIcon);
    rend.setClosedIcon(folderSheetIcon);

    return (TableCellRenderer) tree;
}
 
Example 5
Source File: BranchTreeTable.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
    if (column == 0) {
        JTree tree = (JTree) super.getCellRenderer(row, column);
        DelegatingRenderer rend = ((DelegatingRenderer) tree
                .getCellRenderer());
        TreePath pathForRow = tree
                .getPathForRow(convertRowIndexToModel(row));
        if (pathForRow == null)
            return super.getCellRenderer(row, column);
        BranchView.Node n = (BranchView.Node) pathForRow
                .getLastPathComponent();
        if (n.branch.getBranchId() == branchView.getActualBranch()) {
            rend.setLeafIcon(branchActual);
            rend.setOpenIcon(branchActual);
            rend.setClosedIcon(branchActual);
        } else {
            if (n.branch.getChildren().size() < 2) {
                rend.setLeafIcon(branch);
                rend.setOpenIcon(branch);
                rend.setClosedIcon(branch);
            } else {
                rend.setLeafIcon(branchTree);
                rend.setOpenIcon(branchTree);
                rend.setClosedIcon(branchTree);
            }
        }
    }
    return super.getCellRenderer(row, column);
}
 
Example 6
Source File: SLDTree.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/** Creates the ui. */
private void createUI() {
    setBorder(new LineBorder(new Color(0, 0, 0)));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    JPanel panelSymbolMarkerTree = new JPanel();
    add(panelSymbolMarkerTree);

    // CHECKSTYLE:OFF
    JScrollPane scrollpane = new JScrollPane();
    // CHECKSTYLE:ON

    rootNode = new DefaultMutableTreeNode("SLD");
    treeModel = new DefaultTreeModel(rootNode);
    symbolTree = new JTree(treeModel);
    symbolTree.setEditable(true);
    symbolTree.setBorder(new LineBorder(Color.black));
    ComponentCellRenderer cellRenderer =
            new ComponentCellRenderer(symbolTree.getCellRenderer());
    symbolTree.setCellRenderer(cellRenderer);
    symbolTree.setCellEditor(new CheckBoxNodeEditor(symbolTree, cellRenderer, this));
    symbolTree.setEditable(true);
    symbolTree.setRowHeight(0);

    // Listen for when the selection changes.
    symbolTree.addTreeSelectionListener(this);
    panelSymbolMarkerTree.setLayout(new BorderLayout(0, 0));

    scrollpane.setViewportView(symbolTree);

    panelSymbolMarkerTree.add(scrollpane);

    // Add the tree tools if they were supplied
    if (treeTools != null) {
        treeTools.configure(this, symbolTree, treeModel, renderList);
        add(treeTools.getButtonPanel());
    } else {
        setPreferredSize(new Dimension(SLDTreeTools.getPanelWidth(), PANEL_HEIGHT));
    }
}
 
Example 7
Source File: MethodsCountPanel.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
private void setup() {
    this.setLayout(new BorderLayout());
    treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(null));
    jTree = new JTree(treeModel);
    jTree.setRootVisible(false);
    jTree.setCellRenderer(new CellRenderer());
    theme.applyTo(jTree);

    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();

    cellRenderer.setFont(new Font("Monospaced", Font.PLAIN, 20));
    jTree.setCellRenderer(cellRenderer);
    jTree.addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            Object selection = jTree.getLastSelectedPathComponent();
            DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode)selection;
            ClassNode node = (ClassNode) defaultMutableTreeNode.getUserObject();
            viewerController.onSelectedMethodCount(node);
        }
    });

    JScrollPane jScrollPane = new JScrollPane(jTree);
    this.setBorder(new EmptyBorder(0,0,0,0));
    this.add(jScrollPane, BorderLayout.CENTER);
    theme.applyTo(jScrollPane);

    jTree.setDragEnabled(true);
    jTree.setTransferHandler(new FileTransferHandler(viewerController));
}
 
Example 8
Source File: CatalogTree_simpleDifferentTests.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testThatCellRendererIsSet() {
    final JTree jTree = new JTree();
    final TreeCellRenderer renderer1 = jTree.getCellRenderer();
    assertNotNull(renderer1);
    assertEquals(true, renderer1 instanceof DefaultTreeCellRenderer);

    CatalogTreeUtils.addCellRenderer(jTree);

    final TreeCellRenderer renderer2 = jTree.getCellRenderer();
    assertNotNull(renderer2);
    assertEquals(true, renderer2 instanceof DefaultTreeCellRenderer);
    assertNotSame(renderer1, renderer2);
}
 
Example 9
Source File: TreeSelectionRenderer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
public static void installFor(JTree tree) {
    TreeSelectionRenderer renderer = (TreeSelectionRenderer) tree.getCellRenderer();
    renderer.install(tree);
}
 
Example 10
Source File: FilesTree.java    From android-classyshark with Apache License 2.0 4 votes vote down vote up
private void configureJTree(final JTree jTree) {
    jTree.setRootVisible(false);
    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
    cellRenderer.setFont(new Font("Monospaced", Font.PLAIN, 20));
    jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    jTree.addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            Object selection = jTree.getLastSelectedPathComponent();
            if (selection == null) return;

            DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) selection;

            if (selection.toString().endsWith(".dex")) {
                FilesTree.this.viewerController.onSelectedClassName(
                        (String) defaultMutableTreeNode.getUserObject());
                return;
            }

            if (selection.toString().endsWith(".jar")) {
                FilesTree.this.viewerController.onSelectedClassName(
                        (String) defaultMutableTreeNode.getUserObject());
                return;
            }

            if (selection.toString().endsWith(".apk")) {
                FilesTree.this.viewerController.onSelectedClassName(
                        (String) defaultMutableTreeNode.getUserObject());
                return;
            }

            if (selection.toString().endsWith(".so")) {
                FilesTree.this.viewerController.onSelectedClassName(
                        (String) defaultMutableTreeNode.getUserObject());
                return;
            }

            if (!defaultMutableTreeNode.isLeaf()) return;

            if (FilesTree.this.viewerController != null) {

                if (defaultMutableTreeNode.getUserObject() instanceof String) {
                    FilesTree.this.viewerController.onSelectedClassName(
                            (String) defaultMutableTreeNode.getUserObject());
                } else {
                    FilesTree.this.viewerController.onSelectedClassName(
                            ((NodeInfo) defaultMutableTreeNode.getUserObject()).fullname);
                }
            }
        }
    });
}
 
Example 11
Source File: CatalogTree_simpleDifferentTests.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testThatRendererRendersDifferentTypes() {
    final JTree jTree = new JTree();
    CatalogTreeUtils.addCellRenderer(jTree);
    final TreeCellRenderer dapCellRenderer = jTree.getCellRenderer();

    final OpendapLeaf opendapLeaf = new OpendapLeaf("This is A dap Node", new InvDataset(null, "") {
    });
    opendapLeaf.setDapAccess(true);
    final OpendapLeaf fileLeaf = new OpendapLeaf("This is A File Node", new InvDataset(null, "") {
    });
    fileLeaf.setFileAccess(true);
    final Object dapNode = new DefaultMutableTreeNode(opendapLeaf);
    final Object fileNode = new DefaultMutableTreeNode(fileLeaf);
    final Object noDapNode = new DefaultMutableTreeNode("otherNode");

    final Component component = dapCellRenderer.getTreeCellRendererComponent(jTree, noDapNode, false, false, true,
                                                                             0, false);

    assertTrue(component instanceof DefaultTreeCellRenderer);
    final DefaultTreeCellRenderer tcr1 = (DefaultTreeCellRenderer) component;
    assertEquals("otherNode", tcr1.getText());
    assertEquals(true, tcr1.getIcon() != null);

    final Color foreground = tcr1.getForeground();
    final Color background = tcr1.getBackground();
    final Font font = tcr1.getFont();

    final Component component2 = dapCellRenderer.getTreeCellRendererComponent(jTree, dapNode, false, false, true, 0,
                                                                              false);

    assertSame(component, component2);

    assertTrue(component2 instanceof DefaultTreeCellRenderer);
    final DefaultTreeCellRenderer tcr2 = (DefaultTreeCellRenderer) component2;
    assertEquals("This is A dap Node", tcr2.getText());
    assertEquals(true, tcr2.getIcon() != null);
    final Icon icon2 = tcr2.getIcon();
    // todo change the expected icon to a realistic icon
    assertEquals("/DRsProduct16.png", icon2.toString().substring(icon2.toString().lastIndexOf("/")));

    assertEquals(foreground, tcr2.getForeground());
    assertEquals(background, tcr2.getBackground());
    assertEquals(font, tcr2.getFont());


    final Component component3 = dapCellRenderer.getTreeCellRendererComponent(jTree, fileNode, false, false, true,
                                                                              0, false);

    assertSame(component, component3);

    assertTrue(component3 instanceof DefaultTreeCellRenderer);
    final DefaultTreeCellRenderer tcr3 = (DefaultTreeCellRenderer) component3;
    assertEquals("This is A File Node", tcr3.getText());
    assertEquals(true, tcr3.getIcon() instanceof ImageIcon);
    final ImageIcon icon3 = (ImageIcon) tcr3.getIcon();
    // todo change the expected icon to a realistic icon
    assertEquals("/FRsProduct16.png", icon3.getDescription().substring(icon3.getDescription().lastIndexOf("/")));

    assertEquals(foreground, tcr3.getForeground());
    assertEquals(background, tcr3.getBackground());
    assertEquals(font, tcr3.getFont());
}