1 31 package org.pdfbox.pdfviewer; 32 33 import java.awt.Component ; 34 35 import javax.swing.JTree ; 36 37 import javax.swing.tree.DefaultTreeCellRenderer ; 38 39 import org.pdfbox.cos.COSArray; 40 import org.pdfbox.cos.COSBase; 41 import org.pdfbox.cos.COSDictionary; 42 import org.pdfbox.cos.COSName; 43 import org.pdfbox.cos.COSNull; 44 import org.pdfbox.cos.COSFloat; 45 import org.pdfbox.cos.COSInteger; 46 import org.pdfbox.cos.COSStream; 47 import org.pdfbox.cos.COSString; 48 49 55 public class PDFTreeCellRenderer extends DefaultTreeCellRenderer 56 { 57 60 public Component getTreeCellRendererComponent( 61 JTree tree, 62 Object nodeValue, 63 boolean isSelected, 64 boolean expanded, 65 boolean leaf, 66 int row, 67 boolean componentHasFocus) 68 { 69 nodeValue = convertToTreeObject( nodeValue ); 70 return super.getTreeCellRendererComponent( tree, nodeValue, isSelected, expanded, leaf, 71 row, componentHasFocus ); 72 } 73 74 private Object convertToTreeObject( Object nodeValue ) 75 { 76 if( nodeValue instanceof MapEntry ) 77 { 78 MapEntry entry = (MapEntry)nodeValue; 79 COSName key = (COSName)entry.getKey(); 80 COSBase value = (COSBase)entry.getValue(); 81 nodeValue = key.getName() + ":" + convertToTreeObject( value ); 82 } 83 else if( nodeValue instanceof COSFloat ) 84 { 85 nodeValue = "" + ((COSFloat)nodeValue).floatValue(); 86 } 87 else if( nodeValue instanceof COSInteger ) 88 { 89 nodeValue = "" + ((COSInteger)nodeValue).intValue(); 90 } 91 else if( nodeValue instanceof COSString ) 92 { 93 nodeValue = ((COSString)nodeValue).getString(); 94 } 95 else if( nodeValue instanceof COSName ) 96 { 97 nodeValue = ((COSName)nodeValue).getName(); 98 } 99 else if( nodeValue instanceof ArrayEntry ) 100 { 101 ArrayEntry entry = (ArrayEntry)nodeValue; 102 nodeValue = "[" + entry.getIndex() + "]" + convertToTreeObject( entry.getValue() ); 103 } 104 else if( nodeValue instanceof COSNull ) 105 { 106 nodeValue = "null"; 107 } 108 else if( nodeValue instanceof COSDictionary ) 109 { 110 COSDictionary dict = (COSDictionary)nodeValue; 111 if( nodeValue instanceof COSStream ) 112 { 113 nodeValue = "Stream"; 114 } 115 else 116 { 117 nodeValue = "Dictionary"; 118 } 119 120 COSName type = (COSName)dict.getDictionaryObject( COSName.TYPE ); 121 if( type != null ) 122 { 123 nodeValue = nodeValue + "(" + type.getName(); 124 COSName subType = (COSName)dict.getDictionaryObject( COSName.SUBTYPE ); 125 if( subType != null ) 126 { 127 nodeValue = nodeValue + ":" + subType.getName(); 128 } 129 130 nodeValue = nodeValue + ")"; 131 } 132 } 133 else if( nodeValue instanceof COSArray ) 134 { 135 nodeValue="Array"; 136 } 137 else if( nodeValue instanceof COSString ) 138 { 139 nodeValue = ((COSString)nodeValue).getString(); 140 } 141 return nodeValue; 142 143 } 144 } | Popular Tags |