01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: 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: package org.apache.poi.contrib.poibrowser;
19:
20: import java.awt.*;
21: import javax.swing.*;
22: import javax.swing.tree.*;
23:
24: /**
25: * <p>{@link TreeCellRenderer} for a {@link DocumentDescriptor}. The
26: * renderer is extremly rudimentary since displays only the document's
27: * name, its size and its fist few bytes.</p>
28: *
29: * @author Rainer Klute <a
30: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
31: * @version $Id: DocumentDescriptorRenderer.java 489730 2006-12-22 19:18:16Z bayard $
32: * @since 2002-02-05
33: */
34: public class DocumentDescriptorRenderer extends DefaultTreeCellRenderer {
35:
36: public Component getTreeCellRendererComponent(final JTree tree,
37: final Object value, final boolean selected,
38: final boolean expanded, final boolean leaf, final int row,
39: final boolean hasFocus) {
40: final DocumentDescriptor d = (DocumentDescriptor) ((DefaultMutableTreeNode) value)
41: .getUserObject();
42: final JPanel p = new JPanel();
43: final JTextArea text = new JTextArea();
44: text.append(renderAsString(d));
45: text.setFont(new Font("Monospaced", Font.PLAIN, 10));
46: p.add(text);
47: if (selected)
48: Util.invert(text);
49: return p;
50: }
51:
52: /**
53: * <p>Renders {@link DocumentDescriptor} as a string.</p>
54: */
55: protected String renderAsString(final DocumentDescriptor d) {
56: final StringBuffer b = new StringBuffer();
57: b.append("Name: ");
58: b.append(d.name);
59: b.append(" (");
60: b.append(Codec.hexEncode(d.name));
61: b.append(") \n");
62:
63: b.append("Size: ");
64: b.append(d.size);
65: b.append(" bytes\n");
66:
67: b.append("First bytes: ");
68: b.append(Codec.hexEncode(d.bytes));
69:
70: return b.toString();
71: }
72:
73: }
|