001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.Dimension;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026: import java.awt.Rectangle;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029:
030: import javax.swing.Icon;
031: import javax.swing.JComponent;
032: import javax.swing.JLabel;
033: import javax.swing.LookAndFeel;
034: import javax.swing.SwingUtilities;
035: import javax.swing.plaf.ComponentUI;
036: import javax.swing.plaf.LabelUI;
037:
038: import org.apache.harmony.x.swing.Utilities;
039:
040: public class BasicLabelUI extends LabelUI implements
041: PropertyChangeListener {
042: protected static BasicLabelUI labelUI;
043:
044: private final Rectangle viewRect = new Rectangle();
045: private final Rectangle iconRect = new Rectangle();
046: private final Rectangle textRect = new Rectangle();
047:
048: public static ComponentUI createUI(final JComponent c) {
049: if (labelUI == null) {
050: labelUI = new BasicLabelUI();
051: }
052:
053: return labelUI;
054: }
055:
056: public void installUI(final JComponent c) {
057: JLabel label = (JLabel) c;
058:
059: installDefaults(label);
060: installComponents(label);
061: installListeners(label);
062: installKeyboardActions(label);
063: }
064:
065: public void uninstallUI(final JComponent c) {
066: JLabel label = (JLabel) c;
067:
068: uninstallKeyboardActions(label);
069: uninstallListeners(label);
070: uninstallComponents(label);
071: uninstallDefaults(label);
072: }
073:
074: public void paint(final Graphics g, final JComponent c) {
075: JLabel label = (JLabel) c;
076:
077: FontMetrics fm = Utilities.getFontMetrics(label);
078: if (fm == null) {
079: return;
080: }
081: iconRect.setBounds(0, 0, 0, 0);
082: textRect.setBounds(0, 0, 0, 0);
083: SwingUtilities.calculateInnerArea(c, viewRect);
084:
085: Icon icon = label.isEnabled() ? label.getIcon() : label
086: .getDisabledIcon();
087:
088: String clippedText = layoutCL(label, fm, label.getText(), icon,
089: viewRect, iconRect, textRect);
090:
091: int textY = Utilities.getTextY(fm, textRect);
092:
093: if (icon != null) {
094: icon.paintIcon(label, g, iconRect.x, iconRect.y);
095: }
096: if (label.isEnabled()) {
097: paintEnabledText(label, g, clippedText, textRect.x, textY);
098: } else {
099: paintDisabledText(label, g, clippedText, textRect.x, textY);
100: }
101: }
102:
103: public Dimension getPreferredSize(final JComponent c) {
104: JLabel label = (JLabel) c;
105:
106: Icon icon = label.isEnabled() ? label.getIcon() : label
107: .getDisabledIcon();
108:
109: Dimension size = Utilities.getCompoundLabelSize(label, label
110: .getText(), icon, label.getVerticalTextPosition(),
111: label.getHorizontalTextPosition(), label
112: .getIconTextGap());
113:
114: return Utilities.addInsets(size, label.getInsets());
115: }
116:
117: public void propertyChange(final PropertyChangeEvent e) {
118: JLabel label = (JLabel) e.getSource();
119: label.revalidate();
120: label.repaint();
121: }
122:
123: protected String layoutCL(final JLabel label,
124: final FontMetrics fontMetrics, final String text,
125: final Icon icon, final Rectangle viewR,
126: final Rectangle iconR, final Rectangle textR) {
127: return SwingUtilities.layoutCompoundLabel(label, fontMetrics,
128: text, icon, label.getVerticalAlignment(), label
129: .getHorizontalAlignment(), label
130: .getVerticalTextPosition(), label
131: .getHorizontalTextPosition(), viewR, iconR,
132: textR, label.getIconTextGap());
133: }
134:
135: protected void paintEnabledText(final JLabel label,
136: final Graphics g, final String clippedText,
137: final int textX, final int textY) {
138: int underscore = Utilities.getClippedUnderscoreIndex(label
139: .getText(), clippedText, label
140: .getDisplayedMnemonicIndex());
141: Utilities.drawString(g, clippedText, textX, textY, label
142: .getFontMetrics(label.getFont()),
143: label.getForeground(), underscore);
144: }
145:
146: protected void paintDisabledText(final JLabel label,
147: final Graphics g, final String clippedText,
148: final int textX, final int textY) {
149: int underscore = Utilities.getClippedUnderscoreIndex(label
150: .getText(), clippedText, label
151: .getDisplayedMnemonicIndex());
152: Utilities.drawString(g, clippedText, textX, textY, label
153: .getFontMetrics(label.getFont()), label.getBackground()
154: .darker(), underscore);
155: Utilities.drawString(g, clippedText, textX + 1, textY + 1,
156: label.getFontMetrics(label.getFont()), label
157: .getBackground().brighter(), underscore);
158: }
159:
160: protected void installDefaults(final JLabel label) {
161: label.setInheritsPopupMenu(true);
162: LookAndFeel.installColorsAndFont(label, "Label.background",
163: "Label.foreground", "Label.font");
164: LookAndFeel.installBorder(label, "Label.border");
165: LookAndFeel.installProperty(label, "alignmentX", new Float(0));
166: }
167:
168: protected void uninstallDefaults(final JLabel label) {
169: if (label == null) {
170: return;
171: }
172: Utilities.uninstallColorsAndFont(label);
173: LookAndFeel.uninstallBorder(label);
174: }
175:
176: protected void installListeners(final JLabel label) {
177: label.addPropertyChangeListener(this );
178: }
179:
180: protected void uninstallListeners(final JLabel label) {
181: label.removePropertyChangeListener(this );
182: }
183:
184: protected void installComponents(final JLabel label) {
185: }
186:
187: protected void uninstallComponents(final JLabel label) {
188: }
189:
190: protected void installKeyboardActions(final JLabel label) {
191: }
192:
193: protected void uninstallKeyboardActions(final JLabel label) {
194: }
195: }
|