/** * Sets the Icon to use for the handle of a collapsed node. * * Note: this will only succeed if the current ui delegate is * a BasicTreeUI otherwise it will do nothing. * * PENDING JW: incomplete api (no getter) and not a bound property. * * @param collapsedIcon the Icon to use for the handle of a collapsed node. */ public void setCollapsedIcon(Icon collapsedIcon) { if (getUI() instanceof BasicTreeUI) { ((BasicTreeUI) getUI()).setCollapsedIcon(collapsedIcon); } }
/** * Sets the Icon to use for the handle of an expanded node.<p> * * Note: this will only succeed if the current ui delegate is * a BasicTreeUI otherwise it will do nothing.<p> * * PENDING JW: incomplete api (no getter) and not a bound property. * * @param expandedIcon the Icon to use for the handle of an expanded node. */ public void setExpandedIcon(Icon expandedIcon) { if (getUI() instanceof BasicTreeUI) { ((BasicTreeUI) getUI()).setExpandedIcon(expandedIcon); } }
private static class MyJTree extends JTree { ... public void invalidateNodeBoundsViaSideEffect() { if (ui instanceof BasicTreeUI) { BasicTreeUI basicTreeUI = (BasicTreeUI) ui; basicTreeUI.setLeftChildIndent(basicTreeUI.getLeftChildIndent()); }} public void invalidateNodeBoundsViaRefection() { if (ui instanceof BasicTreeUI) { try { Field field = BasicTreeUI.class.getDeclaredField("treeState"); field.setAccessible(true); AbstractLayoutCache treeState = (AbstractLayoutCache) field.get(ui); if (treeState != null) { treeState.invalidateSizes(); } } catch (Exception e) { } }}}
@Override public void setUI(TreeUI ui) { super.setUI(ui); if (ui instanceof BasicTreeUI) { BasicTreeUI bui = (BasicTreeUI)ui; treeHandleWidth = bui.getLeftChildIndent() + bui.getRightChildIndent(); } else { treeHandleWidth = -1; } }
private void setTreeUIVariables() { if (tree.getUI() instanceof BasicTreeUI) { BasicTreeUI treeUI = (BasicTreeUI) tree.getUI(); treeSignExtent = treeUI.getExpandedIcon().getIconWidth() / 2; treeSignRightMargin = treeUI.getRightChildIndent(); } }
tree.setUI(tUI); try { tUI.setCollapsedIcon(new ImageIcon(new URL("https://i.stack.imgur.com/nKJFv.png"))); tUI.setExpandedIcon(new ImageIcon(new URL("https://i.stack.imgur.com/NJvcp.png"))); tUI.setRolloverIcon(new ImageIcon(new URL("https://i.stack.imgur.com/jN6uX.png"))); } catch(MalformedURLException e) { int middleXOfKnob; if (isLeftToRight(t)) { middleXOfKnob = bounds.x - getRightChildIndent() + 1; } else { middleXOfKnob = bounds.x + bounds.width + getRightChildIndent() - 1; Icon expandedIcon = getExpandedIcon(); if(expandedIcon != null) drawCentered(tree, g, expandedIcon, middleXOfKnob, middleYOfKnob ); } else if(isLocationInExpandControl(path, uH.getXPos(), uH.getYPos()) && !isExpanded && rolloverEnabled) { if(row == uH.getRow()) { if(rolloverIcon != null) drawCentered(tree, g, rolloverIcon, middleXOfKnob, middleYOfKnob); } else { Icon collapsedIcon = getCollapsedIcon(); if(collapsedIcon != null) drawCentered(tree, g, collapsedIcon, middleXOfKnob, middleYOfKnob); Icon collapsedIcon = getCollapsedIcon(); if(collapsedIcon != null) drawCentered(tree, g, collapsedIcon, middleXOfKnob, middleYOfKnob);
public void constructTree(DefaultTreeModel model) { this.setModel(model); this.setShowsRootHandles(true); this.setRootVisible(false); DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); ImageIcon plus = new ImageIcon(getClass().getResource("/icon/plusTree.png")); //$NON-NLS-1$ ImageIcon minus = new ImageIcon(getClass().getResource("/icon/minusTree.png")); //$NON-NLS-1$ ComponentUI ui = this.getUI(); if (ui instanceof BasicTreeUI) { ((BasicTreeUI) ui).setExpandedIcon(minus); ((BasicTreeUI) ui).setCollapsedIcon(plus); } renderer.setOpenIcon(null); renderer.setClosedIcon(null); renderer.setLeafIcon(null); this.setCellRenderer(renderer); }
private boolean isLocationInExpandControl( TreePath path, Point location ) { if( tree.getModel().isLeaf( path.getLastPathComponent() ) ) return false; Rectangle r = tree.getPathBounds(path); int boxWidth = 8; Insets i = tree.getInsets(); int indent = 0; if( tree.getUI() instanceof BasicTreeUI ) { BasicTreeUI ui = (BasicTreeUI)tree.getUI(); if( null != ui.getExpandedIcon() ) boxWidth = ui.getExpandedIcon().getIconWidth(); indent = ui.getLeftChildIndent(); } int boxX; if( tree.getComponentOrientation().isLeftToRight() ) { boxX = r.x - positionX - indent - boxWidth; } else { boxX = r.x - positionX + indent + r.width; } return location.getX() >= boxX && location.getX() <= (boxX + boxWidth); } }
m_objTree = new JTree(m_objModelTree); m_objTree.setCellRenderer(new QCTreeCellRenderer()); ((BasicTreeUI)m_objTree.getUI()).setLeftChildIndent(20); m_objTree.setRowHeight(35);
/** * @inheritDoc */ @Override protected void installListeners() { super.installListeners(); tree.addPropertyChangeListener(this); }
public void dragDropEnd(java.awt.dnd.DragSourceDropEvent evt) { // Clear all of the stuff we've highlighted if (dragPaths != null) { javax.swing.plaf.basic.BasicTreeUI myUI = (javax.swing.plaf.basic.BasicTreeUI) getUI(); for (int i = 0; i < dragPaths.length; i++) { javax.swing.tree.TreePath thePath = dragPaths[i]; if (thePath != null) { java.awt.Rectangle pb = myUI.getPathBounds(this, thePath); if (pb != null) repaint(pb); } } } dragwidgs = null; dragPaths = null; }
@Override protected void installDefaults() { super.installDefaults(); if (SubstanceCoreUtilities.toDrawWatermark(this.tree)) this.tree.setOpaque(false); if (this.tree.getSelectionPaths() != null) { for (TreePath selectionPath : this.tree.getSelectionPaths()) { TreePathId pathId = new TreePathId(selectionPath); selectedPaths.put(pathId, selectionPath.getLastPathComponent()); } } setExpandedIcon(new IconUIResource(SubstanceIconFactory.getTreeIcon( this.tree, false))); setCollapsedIcon(new IconUIResource(SubstanceIconFactory.getTreeIcon( this.tree, true))); // instead of computing the cell renderer insets on // every cell rendering, compute it once and expose to the // SubstanceDefaultTreeCellRenderer this.cellRendererInsets = SubstanceSizeUtils .getTreeCellRendererInsets(SubstanceSizeUtils .getComponentFontSize(tree)); }
/** * If a negative number is returned, then all events that occur in the * leading margin will be forwarded to the tree and consumed. * * @return the width of the tree handle if it can be determined, else -1 */ protected int getTreeHandleWidth() { if (renderer.getUI() instanceof BasicTreeUI) { BasicTreeUI ui = (BasicTreeUI) renderer.getUI(); return ui.getLeftChildIndent() + ui.getRightChildIndent(); } else { return -1; } }
treeUI.setCollapsedIcon(ResourceUtil.getImageIcon("info/aduna/swing/config/collapsed.png")); treeUI.setExpandedIcon(ResourceUtil.getImageIcon("info/aduna/swing/config/expanded.png"));
@Override protected void installListeners() { super.installListeners(); this.substancePropertyChangeListener = new PropertyChangeListener() { @Override
@Override public Rectangle getPathBounds(JTree tree, TreePath path) { Rectangle result = super.getPathBounds(tree, path); if (result != null) { if (!tree.getComponentOrientation().isLeftToRight()) { int delta = result.x - tree.getInsets().left; result.x -= delta; result.width += delta; } } return result; }
@Override protected void installDefaults() { super.installDefaults(); if (SubstanceCoreUtilities.toDrawWatermark(this.tree)) this.tree.setOpaque(false); if (this.tree.getSelectionPaths() != null) { for (TreePath selectionPath : this.tree.getSelectionPaths()) { TreePathId pathId = new TreePathId(selectionPath); selectedPaths.put(pathId, selectionPath.getLastPathComponent()); } } setExpandedIcon(new IconUIResource(SubstanceIconFactory.getTreeIcon( this.tree, false))); setCollapsedIcon(new IconUIResource(SubstanceIconFactory.getTreeIcon( this.tree, true))); // instead of computing the cell renderer insets on // every cell rendering, compute it once and expose to the // SubstanceDefaultTreeCellRenderer this.cellRendererInsets = SubstanceSizeUtils .getTreeCellRendererInsets(SubstanceSizeUtils .getComponentFontSize(tree)); }
/** * Invalidates cell size caching in the ui delegate. May do nothing if there's no * safe (i.e. without reflection) way to message the delegate. <p> * * This implementation calls BasicTreeUI setLeftChildIndent with the old indent if available. * Beware: clearing the cache is an undocumented implementation side-effect of the * method. Revisit if we ever should have a custom ui delegate. * * */ public void invalidateCellSizeCache() { if (getUI() instanceof BasicTreeUI) { BasicTreeUI ui = (BasicTreeUI) getUI(); ui.setLeftChildIndent(ui.getLeftChildIndent()); } }
/** * If a negative number is returned, then all events that occur in the * leading margin will be forwarded to the tree and consumed. * * @return the width of the tree handle if it can be determined, else -1 */ protected int getTreeHandleWidth() { if (renderer.getUI() instanceof BasicTreeUI) { BasicTreeUI ui = (BasicTreeUI) renderer.getUI(); return ui.getLeftChildIndent() + ui.getRightChildIndent(); } else { return -1; } }
BufferedImage collapsedIcon = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); collapsedIconPainter.paint(collapsedIcon.createGraphics(), null, size, size); basicUI.setCollapsedIcon(new ImageIcon(collapsedIcon)); basicUI.setExpandedIcon(new ImageIcon(expandedIcon));