A class to model a PDF document as a tree structure.
Method from org.apache.pdfbox.pdfviewer.PDFTreeModel Detail: |
public void addTreeModelListener(TreeModelListener l) {
//required for interface
}
Adds a listener for the TreeModelEvent
posted after the tree changes. |
public Object getChild(Object parent,
int index) {
Object retval = null;
if( parent instanceof COSArray )
{
ArrayEntry entry = new ArrayEntry();
entry.setIndex( index );
entry.setValue( ((COSArray)parent).getObject( index ) );
retval = entry;
}
else if( parent instanceof COSDictionary )
{
COSDictionary dict = ((COSDictionary)parent);
List< COSName > keys = new ArrayList< COSName >(dict.keySet());
Collections.sort( keys );
Object key = keys.get( index );
Object value = dict.getDictionaryObject( (COSName)key );
MapEntry entry = new MapEntry();
entry.setKey( key );
entry.setValue( value );
retval = entry;
}
else if( parent instanceof MapEntry )
{
retval = getChild( ((MapEntry)parent).getValue(), index );
}
else if( parent instanceof ArrayEntry )
{
retval = getChild( ((ArrayEntry)parent).getValue(), index );
}
else if( parent instanceof COSDocument )
{
retval = ((COSDocument)parent).getObjects().get( index );
}
else if( parent instanceof COSObject )
{
retval = ((COSObject)parent).getObject();
}
else
{
throw new RuntimeException( "Unknown COS type " + parent.getClass().getName() );
}
return retval;
}
Returns the child of parent at index index
in the parent's
child array. parent must be a node previously obtained
from this data source. This should not return null
if index
is a valid index for parent (that is index >= 0 &&
index < getChildCount(parent )). |
public int getChildCount(Object parent) {
int retval = 0;
if( parent instanceof COSArray )
{
retval = ((COSArray)parent).size();
}
else if( parent instanceof COSDictionary )
{
retval = ((COSDictionary)parent).size();
}
else if( parent instanceof MapEntry )
{
retval = getChildCount( ((MapEntry)parent).getValue() );
}
else if( parent instanceof ArrayEntry )
{
retval = getChildCount( ((ArrayEntry)parent).getValue() );
}
else if( parent instanceof COSDocument )
{
retval = ((COSDocument)parent).getObjects().size();
}
else if( parent instanceof COSObject )
{
retval = 1;
}
return retval;
}
Returns the number of children of parent .
Returns 0 if the node
is a leaf or if it has no children. parent must be a node
previously obtained from this data source. |
public int getIndexOfChild(Object parent,
Object child) {
int retval = -1;
if( parent != null && child != null )
{
if( parent instanceof COSArray )
{
COSArray array = (COSArray)parent;
if( child instanceof ArrayEntry )
{
ArrayEntry arrayEntry = (ArrayEntry)child;
retval = arrayEntry.getIndex();
}
else
{
retval = array.indexOf( (COSBase)child );
}
}
else if( parent instanceof COSDictionary )
{
MapEntry entry = (MapEntry)child;
COSDictionary dict = (COSDictionary)parent;
List< COSName > keys = new ArrayList< COSName >(dict.keySet());
Collections.sort( keys );
for( int i=0; retval == -1 && i< keys.size(); i++ )
{
if( keys.get( i ).equals( entry.getKey() ) )
{
retval = i;
}
}
}
else if( parent instanceof MapEntry )
{
retval = getIndexOfChild( ((MapEntry)parent).getValue(), child );
}
else if( parent instanceof ArrayEntry )
{
retval = getIndexOfChild( ((ArrayEntry)parent).getValue(), child );
}
else if( parent instanceof COSDocument )
{
retval = ((COSDocument)parent).getObjects().indexOf( child );
}
else if( parent instanceof COSObject )
{
retval = 0;
}
else
{
throw new RuntimeException( "Unknown COS type " + parent.getClass().getName() );
}
}
return retval;
}
Returns the index of child in parent. If parent
is null or child is null ,
returns -1. |
public Object getRoot() {
return document.getDocument().getTrailer();
}
Returns the root of the tree. Returns null
only if the tree has no nodes. |
public boolean isLeaf(Object node) {
boolean isLeaf = !(node instanceof COSDictionary ||
node instanceof COSArray ||
node instanceof COSDocument ||
node instanceof COSObject ||
(node instanceof MapEntry && !isLeaf(((MapEntry)node).getValue()) ) ||
(node instanceof ArrayEntry && !isLeaf(((ArrayEntry)node).getValue()) ));
return isLeaf;
}
Returns true if node is a leaf.
It is possible for this method to return false
even if node has no children.
A directory in a filesystem, for example,
may contain no files; the node representing
the directory is not a leaf, but it also has no children. |
public void removeTreeModelListener(TreeModelListener l) {
//required for interface
}
Removes a listener previously added with
addTreeModelListener . |
public void setDocument(PDDocument doc) {
document = doc;
}
Set the document to display in the tree. |
public void valueForPathChanged(TreePath path,
Object newValue) {
//required for interface
}
Messaged when the user has altered the value for the item identified
by path to newValue .
If newValue signifies a truly new value
the model should post a treeNodesChanged event. |