KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > contrib > poibrowser > DocumentDescriptorRenderer


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.contrib.poibrowser;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import javax.swing.tree.*;
24
25 /**
26  * <p>{@link TreeCellRenderer} for a {@link DocumentDescriptor}. The
27  * renderer is extremly rudimentary since displays only the document's
28  * name, its size and its fist few bytes.</p>
29  *
30  * @author Rainer Klute <a
31  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
32  * @version $Id: DocumentDescriptorRenderer.java,v 1.3 2004/04/09 13:05:08 glens Exp $
33  * @since 2002-02-05
34  */

35 public class DocumentDescriptorRenderer extends DefaultTreeCellRenderer
36 {
37
38     public Component getTreeCellRendererComponent(final JTree tree,
39                                                   final Object JavaDoc value,
40                                                   final boolean selected,
41                                                   final boolean expanded,
42                                                   final boolean leaf,
43                                                   final int row,
44                                                   final boolean hasFocus)
45     {
46         final DocumentDescriptor d = (DocumentDescriptor)
47             ((DefaultMutableTreeNode) value).getUserObject();
48         final JPanel p = new JPanel();
49         final JTextArea text = new JTextArea();
50         text.append(renderAsString(d));
51         text.setFont(new Font("Monospaced", Font.PLAIN, 10));
52         p.add(text);
53         if (selected)
54             Util.invert(text);
55         return p;
56     }
57
58
59     /**
60      * <p>Renders {@link DocumentDescriptor} as a string.</p>
61      */

62     protected String JavaDoc renderAsString(final DocumentDescriptor d)
63     {
64         final StringBuffer JavaDoc b = new StringBuffer JavaDoc();
65         b.append("Name: ");
66         b.append(d.name);
67         b.append(" (");
68         b.append(Codec.hexEncode(d.name));
69         b.append(") \n");
70
71         b.append("Size: ");
72         b.append(d.size);
73         b.append(" bytes\n");
74
75         b.append("First bytes: ");
76         b.append(Codec.hexEncode(d.bytes));
77
78         return b.toString();
79     }
80
81 }
82
Popular Tags