1 31 package org.pdfbox.pdfviewer; 32 33 41 import javax.swing.tree.TreePath ; 42 import javax.swing.tree.TreeModel ; 43 44 import javax.swing.event.TreeModelListener ; 46 47 import org.pdfbox.cos.COSArray; 48 import org.pdfbox.cos.COSBase; 49 import org.pdfbox.cos.COSDictionary; 50 import org.pdfbox.cos.COSDocument; 51 import org.pdfbox.cos.COSName; 52 import org.pdfbox.cos.COSObject; 53 54 import org.pdfbox.pdmodel.PDDocument; 55 56 import java.util.Collections ; 57 import java.util.List ; 58 59 65 public class PDFTreeModel implements TreeModel 66 { 67 private PDDocument document; 68 69 72 public PDFTreeModel() 73 { 74 } 76 77 82 public PDFTreeModel(PDDocument doc) 83 { 84 setDocument(doc); 85 } 86 87 92 public void setDocument(PDDocument doc) 93 { 94 document = doc; 95 } 96 104 public void addTreeModelListener(TreeModelListener l) 105 { 106 } 108 109 123 public Object getChild(Object parent, int index) 124 { 125 Object retval = null; 126 if( parent instanceof COSArray ) 127 { 128 ArrayEntry entry = new ArrayEntry(); 129 entry.setIndex( index ); 130 entry.setValue( ((COSArray)parent).getObject( index ) ); 131 retval = entry; 132 } 133 else if( parent instanceof COSDictionary ) 134 { 135 COSDictionary dict = ((COSDictionary)parent); 136 List keys = dict.keyList(); 137 Collections.sort( keys ); 138 Object key = keys.get( index ); 139 Object value = dict.getDictionaryObject( (COSName)key ); 140 MapEntry entry = new MapEntry(); 141 entry.setKey( key ); 142 entry.setValue( value ); 143 retval = entry; 144 } 145 else if( parent instanceof MapEntry ) 146 { 147 retval = getChild( ((MapEntry)parent).getValue(), index ); 148 } 149 else if( parent instanceof ArrayEntry ) 150 { 151 retval = getChild( ((ArrayEntry)parent).getValue(), index ); 152 } 153 else if( parent instanceof COSDocument ) 154 { 155 retval = ((COSDocument)parent).getObjects().get( index ); 156 } 157 else if( parent instanceof COSObject ) 158 { 159 retval = ((COSObject)parent).getObject(); 160 } 161 else 162 { 163 throw new RuntimeException ( "Unknown COS type " + parent.getClass().getName() ); 164 } 165 return retval; 166 } 167 168 177 public int getChildCount(Object parent) 178 { 179 int retval = 0; 180 if( parent instanceof COSArray ) 181 { 182 retval = ((COSArray)parent).size(); 183 } 184 else if( parent instanceof COSDictionary ) 185 { 186 retval = ((COSDictionary)parent).size(); 187 } 188 else if( parent instanceof MapEntry ) 189 { 190 retval = getChildCount( ((MapEntry)parent).getValue() ); 191 } 192 else if( parent instanceof ArrayEntry ) 193 { 194 retval = getChildCount( ((ArrayEntry)parent).getValue() ); 195 } 196 else if( parent instanceof COSDocument ) 197 { 198 retval = ((COSDocument)parent).getObjects().size(); 199 } 200 else if( parent instanceof COSObject ) 201 { 202 retval = 1; 203 } 204 return retval; 205 } 206 207 217 public int getIndexOfChild(Object parent, Object child) 218 { 219 int retval = -1; 220 if( parent != null && child != null ) 221 { 222 if( parent instanceof COSArray ) 223 { 224 COSArray array = (COSArray)parent; 225 if( child instanceof ArrayEntry ) 226 { 227 ArrayEntry arrayEntry = (ArrayEntry)child; 228 retval = arrayEntry.getIndex(); 229 } 230 else 231 { 232 retval = array.indexOf( (COSBase)child ); 233 } 234 } 235 else if( parent instanceof COSDictionary ) 236 { 237 MapEntry entry = (MapEntry)child; 238 COSDictionary dict = (COSDictionary)parent; 239 List keys = dict.keyList(); 240 Collections.sort( keys ); 241 for( int i=0; retval == -1 && i<keys.size(); i++ ) 242 { 243 if( keys.get( i ).equals( entry.getKey() ) ) 244 { 245 retval = i; 246 } 247 } 248 } 249 else if( parent instanceof MapEntry ) 250 { 251 retval = getIndexOfChild( ((MapEntry)parent).getValue(), child ); 252 } 253 else if( parent instanceof ArrayEntry ) 254 { 255 retval = getIndexOfChild( ((ArrayEntry)parent).getValue(), child ); 256 } 257 else if( parent instanceof COSDocument ) 258 { 259 retval = ((COSDocument)parent).getObjects().indexOf( child ); 260 } 261 else if( parent instanceof COSObject ) 262 { 263 retval = 0; 264 } 265 else 266 { 267 throw new RuntimeException ( "Unknown COS type " + parent.getClass().getName() ); 268 } 269 } 270 return retval; 271 } 272 273 279 public Object getRoot() 280 { 281 return document.getDocument().getTrailer(); 282 } 283 284 295 public boolean isLeaf(Object node) 296 { 297 boolean isLeaf = !(node instanceof COSDictionary || 298 node instanceof COSArray || 299 node instanceof COSDocument || 300 node instanceof COSObject || 301 (node instanceof MapEntry && !isLeaf(((MapEntry)node).getValue()) ) || 302 (node instanceof ArrayEntry && !isLeaf(((ArrayEntry)node).getValue()) )); 303 return isLeaf; 304 } 305 306 313 314 public void removeTreeModelListener(TreeModelListener l) 315 { 316 } 318 319 328 public void valueForPathChanged(TreePath path, Object newValue) 329 { 330 } 332 } | Popular Tags |