KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdfviewer > PDFTreeCellRenderer


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdfviewer;
32
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.JTree JavaDoc;
36
37 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
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 /**
50  * A class to render tree cells for the pdfviewer.
51  *
52  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
53  * @version $Revision: 1.6 $
54  */

55 public class PDFTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc
56 {
57     /**
58      * {@inheritDoc}
59      */

60     public Component JavaDoc getTreeCellRendererComponent(
61         JTree JavaDoc tree,
62         Object JavaDoc 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 JavaDoc convertToTreeObject( Object JavaDoc 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