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: /**
19: * @author Anton Avtamonov
20: * @version $Revision$
21: */package javax.swing.plaf.basic;
22:
23: import java.awt.Component;
24: import java.awt.Dimension;
25:
26: import javax.swing.BorderFactory;
27: import javax.swing.JLabel;
28: import javax.swing.JList;
29: import javax.swing.ListCellRenderer;
30: import javax.swing.border.Border;
31:
32: import org.apache.harmony.x.swing.Utilities;
33:
34: public class BasicComboBoxRenderer extends JLabel implements
35: ListCellRenderer {
36:
37: public static class UIResource extends BasicComboBoxRenderer
38: implements javax.swing.plaf.UIResource {
39: }
40:
41: protected static Border noFocusBorder = BorderFactory
42: .createEmptyBorder(1, 1, 1, 1);
43:
44: public BasicComboBoxRenderer() {
45: setBorder(noFocusBorder);
46: setHorizontalAlignment(JLabel.LEADING);
47: }
48:
49: public Component getListCellRendererComponent(final JList list,
50: final Object value, final int index,
51: final boolean isSelected, final boolean cellHasFocus) {
52: setText(value != null ? value.toString() : null);
53: if (isSelected) {
54: setForeground(list.getSelectionForeground());
55: setBackground(list.getSelectionBackground());
56: } else {
57: setForeground(list.getForeground());
58: setBackground(list.getBackground());
59: }
60:
61: setOpaque(true);
62: setFont(list.getFont());
63: setBorder(noFocusBorder);
64: setComponentOrientation(list.getComponentOrientation());
65:
66: return this ;
67: }
68:
69: public Dimension getPreferredSize() {
70: Dimension result;
71: if (Utilities.isEmptyString(getText())) {
72: setText(" ");
73: result = super .getPreferredSize();
74: setText("");
75: } else {
76: result = super.getPreferredSize();
77: }
78:
79: return result;
80: }
81: }
|