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: package javax.swing.plaf.synth;
019:
020: import java.awt.Color;
021: import java.awt.Dimension;
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.awt.Rectangle;
027: import java.awt.font.FontRenderContext;
028: import java.awt.geom.Rectangle2D;
029:
030: import javax.swing.Icon;
031: import javax.swing.SwingConstants;
032: import javax.swing.SwingUtilities;
033:
034: import org.apache.harmony.x.swing.ButtonCommons;
035: import org.apache.harmony.x.swing.Utilities;
036:
037: public class SynthGraphicsUtils {
038:
039: /**
040: * Calculate string width with given parameters
041: *
042: * @param context
043: * SynthContext to specify component
044: * @param font
045: * Font used to draw the string (if null font calculates from the
046: * SynthContext)
047: * @param metrics
048: * FontMetrics used to draw the string (if null metrics
049: * calculates from the SynthContext)
050: * @param text
051: * String to draw
052: */
053: public int computeStringWidth(SynthContext context, Font font,
054: FontMetrics metrics, String text) {
055: metrics = computeFontMetrics(context, font, metrics);
056:
057: return metrics.stringWidth(text);
058: }
059:
060: /**
061: * Calculate string height from Context
062: *
063: * @param context
064: * SynthContext to specify component
065: */
066: public int getMaximumCharHeight(SynthContext context) {
067: Rectangle2D maxBounds = context.getStyle().getFont(context)
068: .getMaxCharBounds(
069: new FontRenderContext(null, true, true));
070:
071: return (int) Math.ceil(maxBounds.getHeight());
072: }
073:
074: /**
075: * Draws a line
076: */
077: @SuppressWarnings("unused")
078: public void drawLine(SynthContext context, Object paintKey,
079: Graphics g, int x1, int y1, int x2, int y2) {
080: g.setColor(context.getStyle().getColor(context,
081: ColorType.FOREGROUND));
082: g.drawLine(x1, y1, x2, y2);
083: }
084:
085: /**
086: * Calculates the maximum size for the component with given parameters
087: */
088: @SuppressWarnings("unused")
089: public Dimension getPreferredSize(SynthContext ss, Font font,
090: String text, Icon icon, int hAlign, int vAlign,
091: int hTextPosition, int vTextPosition, int iconTextGap,
092: int mnemonicIndex) {
093:
094: final FontMetrics fm = computeFontMetrics(ss, font, null);
095:
096: Dimension size = getCompoundLabelSize(fm, text, icon,
097: vTextPosition, hTextPosition, iconTextGap);
098:
099: return Utilities.addInsets(size, ss.getStyle().getInsets(ss,
100: null));
101: }
102:
103: /**
104: * Calculates the minimum size for the component with given parameters
105: */
106: public Dimension getMinimumSize(SynthContext ss, Font font,
107: String text, Icon icon, int hAlign, int vAlign,
108: int hTextPosition, int vTextPosition, int iconTextGap,
109: int mnemonicIndex) {
110:
111: return getPreferredSize(ss, font, text, icon, hAlign, vAlign,
112: hTextPosition, vTextPosition, iconTextGap,
113: mnemonicIndex);
114: }
115:
116: /**
117: * Calculates the preferred size for the component with given parameters
118: */
119: public Dimension getMaximumSize(SynthContext ss, Font font,
120: String text, Icon icon, int hAlign, int vAlign,
121: int hTextPosition, int vTextPosition, int iconTextGap,
122: int mnemonicIndex) {
123:
124: return getPreferredSize(ss, font, text, icon, hAlign, vAlign,
125: hTextPosition, vTextPosition, iconTextGap,
126: mnemonicIndex);
127: }
128:
129: /**
130: * Layouts text and Icon in the complex JComponent
131: */
132: public String layoutText(SynthContext ss, FontMetrics fm,
133: String text, Icon icon, int hAlign, int vAlign,
134: int hTextPosition, int vTextPosition, Rectangle viewR,
135: Rectangle iconR, Rectangle textR, int iconTextGap) {
136:
137: fm = computeFontMetrics(ss, fm);
138:
139: SwingUtilities.layoutCompoundLabel(fm, text, icon, vAlign,
140: hAlign, vTextPosition, hTextPosition, viewR, iconR,
141: textR, iconTextGap);
142:
143: return text;
144: }
145:
146: public void paintText(SynthContext ss, Graphics g, String text,
147: Icon icon, int hAlign, int vAlign, int hTextPosition,
148: int vTextPosition, int iconTextGap, int mnemonicIndex,
149: int textOffset) {
150:
151: FontMetrics metrics = computeFontMetrics(ss, null);
152:
153: Color color = ss.getStyle().getColor(ss,
154: ColorType.TEXT_FOREGROUND);
155:
156: Rectangle textR = getTextRect(text, metrics);
157: Rectangle iconR = getIconRect(icon);
158:
159: Insets insets = ss.getStyle().getInsets(ss, null);
160: Rectangle viewR = Utilities.subtractInsets(g.getClipBounds(),
161: insets);
162:
163: layoutText(ss, metrics, text, icon, hAlign, vAlign,
164: hTextPosition, vTextPosition, viewR, iconR, textR,
165: iconTextGap);
166:
167: textR.translate(textOffset, textOffset);
168:
169: ButtonCommons.paintText(g, metrics, text, mnemonicIndex, textR,
170: text, color);
171:
172: }
173:
174: public void paintText(SynthContext ss, Graphics g, String text,
175: int x, int y, int mnemonicIndex) {
176:
177: Color color = ss.getStyle().getColor(ss,
178: ColorType.TEXT_FOREGROUND);
179: FontMetrics metrics = computeFontMetrics(ss, null);
180: Rectangle textR = getTextRect(text, metrics);
181: textR.x = x;
182: textR.y = y;
183: ButtonCommons.paintText(g, metrics, text, mnemonicIndex, textR,
184: text, color);
185: }
186:
187: public void paintText(SynthContext ss, Graphics g, String text,
188: Rectangle bounds, int mnemonicIndex) {
189:
190: Color color = ss.getStyle().getColor(ss,
191: ColorType.TEXT_FOREGROUND);
192: FontMetrics metrics = computeFontMetrics(ss, null);
193: ButtonCommons.paintText(g, metrics, text, mnemonicIndex,
194: bounds, text, color);
195: }
196:
197: /**
198: * This method calculates Font metrics from context
199: */
200: private FontMetrics computeFontMetrics(SynthContext context,
201: Font font, FontMetrics metrics) {
202:
203: if (metrics == null) {
204:
205: if (font == null) {
206: font = context.getStyle().getFont(context);
207: }
208:
209: metrics = context.getComponent().getFontMetrics(font);
210: }
211:
212: return metrics;
213: }
214:
215: /**
216: * This method calculates Font metrics from context
217: */
218: private FontMetrics computeFontMetrics(SynthContext context,
219: FontMetrics metrics) {
220:
221: if (metrics == null) {
222:
223: metrics = context.getComponent().getFontMetrics(
224: context.getStyle().getFont(context));
225: }
226:
227: return metrics;
228: }
229:
230: /**
231: * @param text
232: * text to be outlined
233: * @param fm
234: * the FontMetrics of the text
235: * @return outline Rectangle
236: */
237: private Rectangle getTextRect(String text, FontMetrics fm) {
238:
239: if (!Utilities.isEmptyString(text)) {
240: Dimension stringDim = Utilities.getStringSize(text, fm);
241: Dimension size = new Dimension(stringDim.width, Utilities
242: .getTextY(fm, new Rectangle(stringDim)));
243:
244: return new Rectangle(size);
245: }
246:
247: return new Rectangle();
248: }
249:
250: private Dimension getCompoundLabelSize(FontMetrics fm,
251: final String text, final Icon icon,
252: final int verticalTextPosition,
253: final int horizontalTextPosition, final int iconTextGap) {
254:
255: final Dimension result = new Dimension();
256: Rectangle viewR = new Rectangle(Short.MAX_VALUE,
257: Short.MAX_VALUE);
258: Rectangle iconR = new Rectangle();
259: Rectangle textR = new Rectangle();
260:
261: SwingUtilities.layoutCompoundLabel(fm, text, icon,
262: SwingConstants.TOP, SwingConstants.LEFT,
263: verticalTextPosition, horizontalTextPosition, viewR,
264: iconR, textR, iconTextGap);
265:
266: result.width = Math.max(iconR.x + iconR.width, textR.x
267: + textR.width);
268: result.height = Math.max(iconR.y + iconR.height, textR.y
269: + textR.height);
270:
271: return result;
272: }
273:
274: /**
275: * @param icon
276: * Icon to be outlined
277: * @return outline Rectangle
278: */
279: private Rectangle getIconRect(Icon icon) {
280:
281: if (icon != null) {
282: return new Rectangle(icon.getIconWidth(), icon
283: .getIconHeight());
284: }
285:
286: return new Rectangle();
287: }
288:
289: }
|