1 /* SynthGraphicsUtils.java -- Wrapper for graphics primitives used in Synth
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.swing
.plaf
.synth
;
41 import java
.awt
.Component
;
42 import java
.awt
.Dimension
;
44 import java
.awt
.FontMetrics
;
45 import java
.awt
.Graphics
;
46 import java
.awt
.Rectangle
;
48 import javax
.swing
.Icon
;
49 import javax
.swing
.SwingUtilities
;
52 * Wrapper for graphics primitives used in Synth.
54 * @author Roman Kennke (kennke@aicas.com)
58 public class SynthGraphicsUtils
62 * Creates a new <code>SynthGraphicsUtils</code> object.
64 public SynthGraphicsUtils()
66 // Nothing to do here.
70 * Draws a line from (x1,y1) to (x2,y2).
72 * @param ctx the synth context, identifies the region
73 * @param paintKey identifies the portion of the component to be painted, may
74 * be <code>null</code>
75 * @param g the graphics context to use for painting
76 * @param x1 the x coordinate of the start point
77 * @param y1 the y coordinate of the start point
78 * @param x2 the x coordinate of the end point
79 * @param y2 the y coordinate of the end point
81 public void drawLine(SynthContext ctx
, Object paintKey
, Graphics g
, int x1
,
82 int y1
, int x2
, int y2
)
85 g
.drawLine(x1
, y1
, x2
, y2
);
89 * Lays out a label and (if non-null) an icon. The calculated coordinates are
90 * then stored in <code>viewR</code>, <code>iconR</code> and
93 * The alignment and position parameters may be one of the alignment or
94 * position constants defined in {@link javax.swing.SwingConstants}.
96 * @param ctx the synth context, identifies the current region
97 * @param fm the font metrics to use to fetch the text measures
98 * @param text the text to lay out, may be <code>null</code>
99 * @param icon the icon to lay out, may be <code>null</code>
100 * @param hAlign the horizontal alignment of the label
101 * @param vAlign the vertical alignment of the label
102 * @param hTextPos the horizontal text position
103 * @param vTextPos the vertical text position
104 * @param viewR the view rectangle (return parameter)
105 * @param iconR the icon rectangle (return parameter)
106 * @param textR the text rectangle (return parameter)
107 * @param iconTextGap the gap between text and label
109 * @return the label text, may be shortened
111 public String
layoutText(SynthContext ctx
, FontMetrics fm
, String text
,
112 Icon icon
, int hAlign
, int vAlign
, int hTextPos
,
113 int vTextPos
, Rectangle viewR
, Rectangle iconR
,
114 Rectangle textR
, int iconTextGap
)
116 return SwingUtilities
.layoutCompoundLabel(fm
, text
, icon
, vAlign
, hAlign
,
117 vTextPos
, hTextPos
, viewR
, iconR
,
122 * Returns the width of the string <code>text</code> for the specified font
125 * @param ctx identifies the current region
126 * @param font the font
127 * @param fm the font metrics to use
128 * @param text the text to be measured
130 * @return the width of the string <code>text</code> for the specified font
133 public int computeStringWidth(SynthContext ctx
, Font font
, FontMetrics fm
,
136 return fm
.stringWidth(text
);
140 * Calculates the minimums size that is needed to render the label with
141 * <code>text</code> and <code>icon</code> correctly.
143 * @param ctx identifies the current region
144 * @param font the font to use
145 * @param text the label text
146 * @param icon the label icon
147 * @param hAlign the horizontal alignment
148 * @param vAlign the vertical alignment
149 * @param hTextPosition the horizontal text position
150 * @param vTextPosition the vertical text position
151 * @param iconTextGap the gap between icon and text
152 * @param mnemonicIndex index to the mnemonic character within
155 * @return the minimums size that is needed to render the label with
156 * <code>text</code> and <code>icon</code> correctly
158 public Dimension
getMinimumSize(SynthContext ctx
, Font font
, String text
,
159 Icon icon
, int hAlign
, int vAlign
,
160 int hTextPosition
,int vTextPosition
,
161 int iconTextGap
,int mnemonicIndex
)
163 // FIXME: Implement this correctly.
164 return new Dimension(0, 0);
168 * Calculates the preferred size that is needed to render the label with
169 * <code>text</code> and <code>icon</code> correctly.
171 * @param ctx identifies the current region
172 * @param font the font to use
173 * @param text the label text
174 * @param icon the label icon
175 * @param hAlign the horizontal alignment
176 * @param vAlign the vertical alignment
177 * @param hTextPosition the horizontal text position
178 * @param vTextPosition the vertical text position
179 * @param iconTextGap the gap between icon and text
180 * @param mnemonicIndex index to the mnemonic character within
183 * @return the preferred size that is needed to render the label with
184 * <code>text</code> and <code>icon</code> correctly
186 public Dimension
getPreferredSize(SynthContext ctx
, Font font
, String text
,
187 Icon icon
, int hAlign
, int vAlign
,
188 int hTextPosition
,int vTextPosition
,
189 int iconTextGap
,int mnemonicIndex
)
191 // FIXME: Implement this correctly.
192 return new Dimension(0, 0);
196 * Calculates the maximum size that is needed to render the label with
197 * <code>text</code> and <code>icon</code> correctly.
199 * @param ctx identifies the current region
200 * @param font the font to use
201 * @param text the label text
202 * @param icon the label icon
203 * @param hAlign the horizontal alignment
204 * @param vAlign the vertical alignment
205 * @param hTextPosition the horizontal text position
206 * @param vTextPosition the vertical text position
207 * @param iconTextGap the gap between icon and text
208 * @param mnemonicIndex index to the mnemonic character within
211 * @return the maximum size that is needed to render the label with
212 * <code>text</code> and <code>icon</code> correctly
214 public Dimension
getMaximumSize(SynthContext ctx
, Font font
, String text
,
215 Icon icon
, int hAlign
, int vAlign
,
216 int hTextPosition
,int vTextPosition
,
217 int iconTextGap
,int mnemonicIndex
)
219 // FIXME: Implement this correctly.
220 return new Dimension(0, 0);
224 * Returns the maximum character height of the font from the component of the
225 * passed in <code>context</code>.
227 * @param context identifies the current component and region
229 * @return the maximum character height of the font from the component of the
230 * passed in <code>context</code>
232 public int getMaximumCharHeight(SynthContext context
)
234 Component comp
= context
.getComponent();
235 Font font
= comp
.getFont();
236 return comp
.getFontMetrics(font
).getHeight();
240 * Renders the specified <code>text</code> within the <code>bounds</code>.
242 * @param ctx identifies the component and region
243 * @param g the graphics context for drawing the tetx
244 * @param text the text to be rendered
245 * @param bounds the bounds within which the text should be rendered
246 * @param mnemonicIndex the index of the mnemonic character within
249 public void paintText(SynthContext ctx
, Graphics g
, String text
,
250 Rectangle bounds
, int mnemonicIndex
)
252 // FIXME: This is very primitive and should be improved to paint the
254 g
.drawString(text
, bounds
.x
, bounds
.y
);
258 * Renders the specified <code>text</code> at the specified location.
260 * @param ctx identifies the component and region
261 * @param g the graphics context for drawing the tetx
262 * @param text the text to be rendered
263 * @param x the X location where the text should be rendered
264 * @param y the Y location where the text should be rendered
265 * @param mnemonicIndex the index of the mnemonic character within
268 public void paintText(SynthContext ctx
, Graphics g
, String text
,
269 int x
, int y
, int mnemonicIndex
)
271 // FIXME: This is very primitive and should be improved to paint the
273 g
.drawString(text
, x
, y
);
276 public void paintText(SynthContext ctx
, Graphics g
, String text
, Icon icon
,
277 int hAlign
, int vAlign
, int hTextPosition
,
278 int vTextPosition
, int iconTextGap
, int mnemonicIndex
,
281 // FIXME: Implement this correctly.