Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1)

E13403-04

oracle.javatools.ui
Class TruncatingTreeCellRenderer<T>

java.lang.Object
  extended by oracle.javatools.ui.TruncatingTreeCellRenderer<T>
All Implemented Interfaces:
javax.swing.tree.TreeCellRenderer

public class TruncatingTreeCellRenderer<T>
extends java.lang.Object
implements javax.swing.tree.TreeCellRenderer

An implementation of TreeCellRenderer that can truncate either at the end or the middle if there are more than a specific number of characters in the String.

This is intended to be used in contexts where the tree's width is tied to its preferred width, for example in a flat editor. In particular, this renderer should not be used with trees that are directly in a scrollpane.

Create a subclass of this class, providing an implementation of the methods getIcon() and getText(). The default implementation of getIcon() returns the standard look and feel icon for a folder or leaf. The default implementation of getText() calls String.valueOf on the node value.

Since:
11.0

Nested Class Summary
 class TruncatingTreeCellRenderer.TruncatedLabelComponent
           
 
Constructor Summary
TruncatingTreeCellRenderer()
           
 
Method Summary
protected  javax.swing.Icon getIcon(javax.swing.JTree tree, T value, boolean selected, boolean expanded, boolean leaf, int row)
          Gets the icon to display for an item in the tree.
 int getMaximumCharacters()
          Returns the maximum number of characters that will be displayed before truncation.
protected  java.lang.String getText(javax.swing.JTree tree, T value, boolean selected, boolean expanded, boolean leaf, int row)
          Gets the text to display for an item in the tree.
 java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, java.lang.Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
           
 boolean isTruncateInMiddle()
          Returns whether truncation happens in the middle of text or at the end.
static void main(java.lang.String[] args)
           
 void setMaximumCharacters(int maxChars)
          Sets the maximum number of characters that will be displayed before truncation occurs.
 void setTruncateInMiddle(boolean truncateInMiddle)
          Sets whether truncation happens in the middle of the text, or at the end.
protected static java.lang.String truncate(java.lang.String text, int maxChars, boolean truncateInMiddle)
          Truncates text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TruncatingTreeCellRenderer

public TruncatingTreeCellRenderer()
Method Detail

setMaximumCharacters

public final void setMaximumCharacters(int maxChars)
Sets the maximum number of characters that will be displayed before truncation occurs. The default is 40.

You will have to explicitly repaint() a JTree using this renderer before setting this value has any effect on a visible tree.

Parameters:
maxChars - the maximum number of characters to display before truncation. Must be greater than zero.

getMaximumCharacters

public final int getMaximumCharacters()
Returns the maximum number of characters that will be displayed before truncation. The default is 40.

Returns:
the maximum number of characters before truncation.

setTruncateInMiddle

public final void setTruncateInMiddle(boolean truncateInMiddle)
Sets whether truncation happens in the middle of the text, or at the end.

You will have to expicitly repaint() a JTree using this renderer before setting this value has any effect on a visible tree.

Parameters:
truncateInMiddle - if true, truncation will happen in the middle of the text. Otherwise, truncation will happen at the end.

isTruncateInMiddle

public final boolean isTruncateInMiddle()
Returns whether truncation happens in the middle of text or at the end. The default is truncate in middle (i.e. true).

Returns:
true if truncation happens in the middle, or false if it happens at the end.

getTreeCellRendererComponent

public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree,
                                                       java.lang.Object value,
                                                       boolean selected,
                                                       boolean expanded,
                                                       boolean leaf,
                                                       int row,
                                                       boolean hasFocus)
Specified by:
getTreeCellRendererComponent in interface javax.swing.tree.TreeCellRenderer

getText

protected java.lang.String getText(javax.swing.JTree tree,
                                   T value,
                                   boolean selected,
                                   boolean expanded,
                                   boolean leaf,
                                   int row)
Gets the text to display for an item in the tree.

Parameters:
tree - the tree.
value - the value in this cell.
selected - if true, the tree cell is selected.
expanded - if true, the tree cell is expanded.
leaf - if true, the tree cell is a leaf.
row - the row number of the cell.
Returns:
the text to display. If you return null, an empty string will be displayed.

getIcon

protected javax.swing.Icon getIcon(javax.swing.JTree tree,
                                   T value,
                                   boolean selected,
                                   boolean expanded,
                                   boolean leaf,
                                   int row)
Gets the icon to display for an item in the tree.

Parameters:
tree - the tree.
value - the value in this cell.
selected - if true, the tree cell is selected.
expanded - if true, the tree cell is expanded.
leaf - if true, the tree cell is a leaf.
row - the row number of the cell.
Returns:
the icon to display. If you return null, no icon will be displayed.

truncate

protected static final java.lang.String truncate(java.lang.String text,
                                                 int maxChars,
                                                 boolean truncateInMiddle)
Truncates text.

Parameters:
text - text to truncate. May be null, in which case, this method should return the empty string.
maxChars - the maximum number of regular characters to display in the string.
truncateInMiddle - if true, the string will be truncated in the middle.
Returns:
a truncated string.

main

public static void main(java.lang.String[] args)

Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1)

E13403-04

Copyright © 1997, 2010, Oracle. All rights reserved.