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: * @author Anton Avtamonov
19: * @version $Revision: $
20: */package org.apache.harmony.x.swing;
21:
22: import java.awt.Component;
23: import java.awt.Font;
24:
25: import javax.swing.DefaultListCellRenderer;
26: import javax.swing.JComponent;
27: import javax.swing.JLabel;
28: import javax.swing.JList;
29:
30: public class ExtendedListCellRenderer extends DefaultListCellRenderer {
31: private static final String SUB_ELEMENT_INDENT = " ";
32:
33: public Component getListCellRendererComponent(JList list,
34: Object value, int index, boolean isSelected,
35: boolean cellHasFocus) {
36:
37: if (value instanceof ExtendedListElement) {
38: ExtendedListElement extendedElement = (ExtendedListElement) value;
39: JComponent result = (JComponent) super
40: .getListCellRendererComponent(
41: list,
42: value,
43: index,
44: isSelected && extendedElement.isChoosable(),
45: cellHasFocus
46: && extendedElement.isChoosable());
47: tuneRenderer(result, extendedElement);
48: return result;
49: } else {
50: return super .getListCellRendererComponent(list, value,
51: index, isSelected, cellHasFocus);
52: }
53: }
54:
55: protected void tuneRenderer(final JComponent renderingComponent,
56: final ExtendedListElement element) {
57: Font font = element.getFont();
58: if (font != null) {
59: renderingComponent.setFont(font);
60: }
61: if (element.getIndentationLevel() > 0) {
62: JLabel labelRenderer = (JLabel) renderingComponent;
63: labelRenderer.setText(createIndentation(element
64: .getIndentationLevel())
65: + labelRenderer.getText());
66: }
67: if (!element.isEnabled()) {
68: renderingComponent.setEnabled(false);
69: }
70: renderingComponent.setToolTipText(element.getToolTipText());
71: }
72:
73: private static String createIndentation(final int indentationLevel) {
74: StringBuffer result = new StringBuffer();
75: for (int i = 0; i < indentationLevel; i++) {
76: result.append(SUB_ELEMENT_INDENT);
77: }
78:
79: return result.toString();
80: }
81: }
|