001 /*
002 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import javax.swing.*;
029 import javax.swing.plaf.*;
030 import javax.swing.plaf.basic.*;
031 import javax.swing.text.View;
032
033 import java.awt.event.ActionEvent;
034 import java.awt.event.ActionListener;
035 import java.awt.Component;
036 import java.awt.Container;
037 import java.awt.Dimension;
038 import java.awt.Rectangle;
039 import java.awt.Insets;
040 import java.awt.Color;
041 import java.awt.Graphics;
042 import java.awt.Font;
043 import java.awt.FontMetrics;
044 import java.beans.PropertyChangeEvent;
045 import java.beans.PropertyChangeListener;
046 import sun.swing.plaf.synth.SynthUI;
047
048 /**
049 * Synth's LabelUI.
050 *
051 * @version 1.26, 05/05/07
052 * @author Scott Violet
053 */
054 class SynthLabelUI extends BasicLabelUI implements SynthUI {
055 private SynthStyle style;
056
057 /**
058 * Returns the LabelUI implementation used for the skins look and feel.
059 */
060 public static ComponentUI createUI(JComponent c) {
061 return new SynthLabelUI();
062 }
063
064 protected void installDefaults(JLabel c) {
065 updateStyle(c);
066 }
067
068 void updateStyle(JLabel c) {
069 SynthContext context = getContext(c, ENABLED);
070 style = SynthLookAndFeel.updateStyle(context, this );
071 context.dispose();
072 }
073
074 protected void uninstallDefaults(JLabel c) {
075 SynthContext context = getContext(c, ENABLED);
076
077 style.uninstallDefaults(context);
078 context.dispose();
079 style = null;
080 }
081
082 public SynthContext getContext(JComponent c) {
083 return getContext(c, getComponentState(c));
084 }
085
086 private SynthContext getContext(JComponent c, int state) {
087 return SynthContext.getContext(SynthContext.class, c,
088 SynthLookAndFeel.getRegion(c), style, state);
089 }
090
091 private Region getRegion(JComponent c) {
092 return SynthLookAndFeel.getRegion(c);
093 }
094
095 private int getComponentState(JComponent c) {
096 int state = SynthLookAndFeel.getComponentState(c);
097 if (SynthLookAndFeel.selectedUI == this
098 && state == SynthConstants.ENABLED) {
099 state = SynthLookAndFeel.selectedUIState
100 | SynthConstants.ENABLED;
101 }
102 return state;
103 }
104
105 public int getBaseline(JComponent c, int width, int height) {
106 if (c == null) {
107 throw new NullPointerException("Component must be non-null");
108 }
109 if (width < 0 || height < 0) {
110 throw new IllegalArgumentException(
111 "Width and height must be >= 0");
112 }
113 JLabel label = (JLabel) c;
114 String text = label.getText();
115 if (text == null || "".equals(text)) {
116 return -1;
117 }
118 Insets i = label.getInsets();
119 Rectangle viewRect = new Rectangle();
120 Rectangle textRect = new Rectangle();
121 Rectangle iconRect = new Rectangle();
122 viewRect.x = i.left;
123 viewRect.y = i.top;
124 viewRect.width = width - (i.right + viewRect.x);
125 viewRect.height = height - (i.bottom + viewRect.y);
126
127 // layout the text and icon
128 SynthContext context = getContext(label);
129 FontMetrics fm = context.getComponent().getFontMetrics(
130 context.getStyle().getFont(context));
131 context.getStyle().getGraphicsUtils(context).layoutText(
132 context, fm, label.getText(), label.getIcon(),
133 label.getHorizontalAlignment(),
134 label.getVerticalAlignment(),
135 label.getHorizontalTextPosition(),
136 label.getVerticalTextPosition(), viewRect, iconRect,
137 textRect, label.getIconTextGap());
138 View view = (View) label
139 .getClientProperty(BasicHTML.propertyKey);
140 int baseline;
141 if (view != null) {
142 baseline = BasicHTML.getHTMLBaseline(view, textRect.width,
143 textRect.height);
144 if (baseline >= 0) {
145 baseline += textRect.y;
146 }
147 } else {
148 baseline = textRect.y + fm.getAscent();
149 }
150 context.dispose();
151 return baseline;
152 }
153
154 /**
155 * Notifies this UI delegate that it's time to paint the specified
156 * component. This method is invoked by <code>JComponent</code>
157 * when the specified component is being painted.
158 */
159 public void update(Graphics g, JComponent c) {
160 SynthContext context = getContext(c);
161
162 SynthLookAndFeel.update(context, g);
163 context.getPainter().paintLabelBackground(context, g, 0, 0,
164 c.getWidth(), c.getHeight());
165 paint(context, g);
166 context.dispose();
167 }
168
169 public void paint(Graphics g, JComponent c) {
170 SynthContext context = getContext(c);
171
172 paint(context, g);
173 context.dispose();
174 }
175
176 protected void paint(SynthContext context, Graphics g) {
177 JLabel label = (JLabel) context.getComponent();
178 Icon icon = (label.isEnabled()) ? label.getIcon() : label
179 .getDisabledIcon();
180
181 g.setColor(context.getStyle().getColor(context,
182 ColorType.TEXT_FOREGROUND));
183 g.setFont(style.getFont(context));
184 context.getStyle().getGraphicsUtils(context).paintText(context,
185 g, label.getText(), icon,
186 label.getHorizontalAlignment(),
187 label.getVerticalAlignment(),
188 label.getHorizontalTextPosition(),
189 label.getVerticalTextPosition(),
190 label.getIconTextGap(),
191 label.getDisplayedMnemonicIndex(), 0);
192 }
193
194 public void paintBorder(SynthContext context, Graphics g, int x,
195 int y, int w, int h) {
196 context.getPainter().paintLabelBorder(context, g, x, y, w, h);
197 }
198
199 public Dimension getPreferredSize(JComponent c) {
200 JLabel label = (JLabel) c;
201 Icon icon = (label.isEnabled()) ? label.getIcon() : label
202 .getDisabledIcon();
203 SynthContext context = getContext(c);
204 Dimension size = context.getStyle().getGraphicsUtils(context)
205 .getPreferredSize(context,
206 context.getStyle().getFont(context),
207 label.getText(), icon,
208 label.getHorizontalAlignment(),
209 label.getVerticalAlignment(),
210 label.getHorizontalTextPosition(),
211 label.getVerticalTextPosition(),
212 label.getIconTextGap(),
213 label.getDisplayedMnemonicIndex());
214
215 context.dispose();
216 return size;
217 }
218
219 public Dimension getMinimumSize(JComponent c) {
220 JLabel label = (JLabel) c;
221 Icon icon = (label.isEnabled()) ? label.getIcon() : label
222 .getDisabledIcon();
223 SynthContext context = getContext(c);
224 Dimension size = context.getStyle().getGraphicsUtils(context)
225 .getMinimumSize(context,
226 context.getStyle().getFont(context),
227 label.getText(), icon,
228 label.getHorizontalAlignment(),
229 label.getVerticalAlignment(),
230 label.getHorizontalTextPosition(),
231 label.getVerticalTextPosition(),
232 label.getIconTextGap(),
233 label.getDisplayedMnemonicIndex());
234
235 context.dispose();
236 return size;
237 }
238
239 public Dimension getMaximumSize(JComponent c) {
240 JLabel label = (JLabel) c;
241 Icon icon = (label.isEnabled()) ? label.getIcon() : label
242 .getDisabledIcon();
243 SynthContext context = getContext(c);
244 Dimension size = context.getStyle().getGraphicsUtils(context)
245 .getMaximumSize(context,
246 context.getStyle().getFont(context),
247 label.getText(), icon,
248 label.getHorizontalAlignment(),
249 label.getVerticalAlignment(),
250 label.getHorizontalTextPosition(),
251 label.getVerticalTextPosition(),
252 label.getIconTextGap(),
253 label.getDisplayedMnemonicIndex());
254
255 context.dispose();
256 return size;
257 }
258
259 public void propertyChange(PropertyChangeEvent e) {
260 super .propertyChange(e);
261 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
262 updateStyle((JLabel) e.getSource());
263 }
264 }
265 }
|