001    /* SynthGraphicsUtils.java -- Wrapper for graphics primitives used in Synth
002       Copyright (C) 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.swing.plaf.synth;
040    
041    import gnu.classpath.NotImplementedException;
042    
043    import java.awt.Component;
044    import java.awt.Dimension;
045    import java.awt.Font;
046    import java.awt.FontMetrics;
047    import java.awt.Graphics;
048    import java.awt.Rectangle;
049    
050    import javax.swing.Icon;
051    import javax.swing.SwingUtilities;
052    
053    /**
054     * Wrapper for graphics primitives used in Synth.
055     *
056     * @author Roman Kennke (kennke@aicas.com)
057     *
058     * @since 1.5
059     */
060    public class SynthGraphicsUtils
061    
062    {
063      /**
064       * Creates a new <code>SynthGraphicsUtils</code> object.
065       */
066      public SynthGraphicsUtils()
067      {
068        // Nothing to do here.
069      }
070    
071      /**
072       * Draws a line from (x1,y1) to (x2,y2).
073       *
074       * @param ctx the synth context, identifies the region
075       * @param paintKey identifies the portion of the component to be painted, may
076       *        be <code>null</code>
077       * @param g the graphics context to use for painting
078       * @param x1 the x coordinate of the start point
079       * @param y1 the y coordinate of the start point
080       * @param x2 the x coordinate of the end point
081       * @param y2 the y coordinate of the end point
082       */
083      public void drawLine(SynthContext ctx, Object paintKey, Graphics g, int x1,
084                           int y1, int x2, int y2)
085      {
086        // TODO: Correct?
087        g.drawLine(x1, y1, x2, y2);
088      }
089    
090      /**
091       * Lays out a label and (if non-null) an icon. The calculated coordinates are
092       * then stored in <code>viewR</code>, <code>iconR</code> and
093       * <code>textR</code>.
094       *   
095       * The alignment and position parameters may be one of the alignment or
096       * position constants defined in {@link javax.swing.SwingConstants}.
097       * 
098       * @param ctx the synth context, identifies the current region
099       * @param fm the font metrics to use to fetch the text measures
100       * @param text the text to lay out, may be <code>null</code>
101       * @param icon the icon to lay out, may be <code>null</code>
102       * @param hAlign the horizontal alignment of the label
103       * @param vAlign the vertical alignment of the label
104       * @param hTextPos the horizontal text position
105       * @param vTextPos the vertical text position
106       * @param viewR the view rectangle (return parameter)
107       * @param iconR the icon rectangle (return parameter)
108       * @param textR the text rectangle (return parameter)
109       * @param iconTextGap the gap between text and label
110       *
111       * @return the label text, may be shortened
112       */
113      public String layoutText(SynthContext ctx, FontMetrics fm, String text,
114                               Icon icon, int hAlign, int vAlign, int hTextPos,
115                               int vTextPos, Rectangle viewR, Rectangle iconR,
116                               Rectangle textR, int iconTextGap)
117      {
118        return SwingUtilities.layoutCompoundLabel(fm, text, icon, vAlign, hAlign,
119                                                  vTextPos, hTextPos, viewR, iconR,
120                                                  textR, iconTextGap);
121      }
122    
123      /**
124       * Returns the width of the string <code>text</code> for the specified font
125       * and font metrics.
126       * 
127       * @param ctx identifies the current region
128       * @param font the font
129       * @param fm the font metrics to use
130       * @param text the text to be measured
131       *
132       * @return the width of the string <code>text</code> for the specified font
133       *         and font metrics
134       */
135      public int computeStringWidth(SynthContext ctx, Font font, FontMetrics fm,
136                                    String text)
137      {
138        return fm.stringWidth(text);
139      }
140    
141      /**
142       * Calculates the minimums size that is needed to render the label with
143       * <code>text</code> and <code>icon</code> correctly.
144       *
145       * @param ctx identifies the current region
146       * @param font the font to use
147       * @param text the label text
148       * @param icon the label icon
149       * @param hAlign the horizontal alignment
150       * @param vAlign the vertical alignment
151       * @param hTextPosition the horizontal text position
152       * @param vTextPosition the vertical text position
153       * @param iconTextGap the gap between icon and text
154       * @param mnemonicIndex index to the mnemonic character within
155       *        <code>text</code>
156       *
157       * @return the minimums size that is needed to render the label with
158       *         <code>text</code> and <code>icon</code> correctly
159       */
160      public Dimension getMinimumSize(SynthContext ctx, Font font, String text,
161                                      Icon icon, int hAlign, int vAlign,
162                                      int hTextPosition,int vTextPosition,
163                                      int iconTextGap,int mnemonicIndex)
164        throws NotImplementedException
165      {
166        // FIXME: Implement this correctly.
167        return new Dimension(0, 0);
168      }
169    
170      /**
171       * Calculates the preferred size that is needed to render the label with
172       * <code>text</code> and <code>icon</code> correctly.
173       *
174       * @param ctx identifies the current region
175       * @param font the font to use
176       * @param text the label text
177       * @param icon the label icon
178       * @param hAlign the horizontal alignment
179       * @param vAlign the vertical alignment
180       * @param hTextPosition the horizontal text position
181       * @param vTextPosition the vertical text position
182       * @param iconTextGap the gap between icon and text
183       * @param mnemonicIndex index to the mnemonic character within
184       *        <code>text</code>
185       *
186       * @return the preferred size that is needed to render the label with
187       *         <code>text</code> and <code>icon</code> correctly
188       */
189      public Dimension getPreferredSize(SynthContext ctx, Font font, String text,
190                                        Icon icon, int hAlign, int vAlign,
191                                        int hTextPosition,int vTextPosition,
192                                        int iconTextGap,int mnemonicIndex)
193        throws NotImplementedException
194      {
195        // FIXME: Implement this correctly.
196        return new Dimension(0, 0);
197      }
198    
199      /**
200       * Calculates the maximum size that is needed to render the label with
201       * <code>text</code> and <code>icon</code> correctly.
202       *
203       * @param ctx identifies the current region
204       * @param font the font to use
205       * @param text the label text
206       * @param icon the label icon
207       * @param hAlign the horizontal alignment
208       * @param vAlign the vertical alignment
209       * @param hTextPosition the horizontal text position
210       * @param vTextPosition the vertical text position
211       * @param iconTextGap the gap between icon and text
212       * @param mnemonicIndex index to the mnemonic character within
213       *        <code>text</code>
214       *
215       * @return the maximum size that is needed to render the label with
216       *         <code>text</code> and <code>icon</code> correctly
217       */
218      public Dimension getMaximumSize(SynthContext ctx, Font font, String text,
219                                      Icon icon, int hAlign, int vAlign,
220                                      int hTextPosition,int vTextPosition,
221                                      int iconTextGap,int mnemonicIndex)
222        throws NotImplementedException
223      {
224        // FIXME: Implement this correctly.
225        return new Dimension(0, 0);
226      }
227    
228      /**
229       * Returns the maximum character height of the font from the component of the
230       * passed in <code>context</code>.
231       *
232       * @param context identifies the current component and region
233       *
234       * @return the maximum character height of the font from the component of the
235       *         passed in <code>context</code>
236       */
237      public int getMaximumCharHeight(SynthContext context)
238      {
239        Component comp = context.getComponent();
240        Font font = comp.getFont();
241        return comp.getFontMetrics(font).getHeight();
242      }
243    
244      /**
245       * Renders the specified <code>text</code> within the <code>bounds</code>.
246       *
247       * @param ctx identifies the component and region
248       * @param g the graphics context for drawing the tetx
249       * @param text the text to be rendered
250       * @param bounds the bounds within which the text should be rendered
251       * @param mnemonicIndex the index of the mnemonic character within
252       *        <code>text</code>
253       */
254      public void paintText(SynthContext ctx, Graphics g, String text,
255                            Rectangle bounds, int mnemonicIndex)
256      {
257        // FIXME: This is very primitive and should be improved to paint the
258        // mnemonic char.
259        g.drawString(text, bounds.x, bounds.y);
260      }
261    
262      /**
263       * Renders the specified <code>text</code> at the specified location.
264       *
265       * @param ctx identifies the component and region
266       * @param g the graphics context for drawing the tetx
267       * @param text the text to be rendered
268       * @param x the X location where the text should be rendered
269       * @param y the Y location where the text should be rendered
270       * @param mnemonicIndex the index of the mnemonic character within
271       *        <code>text</code>
272       */
273      public void paintText(SynthContext ctx, Graphics g, String text,
274                            int x, int y, int mnemonicIndex)
275      {
276        // FIXME: This is very primitive and should be improved to paint the
277        // mnemonic char.
278        g.drawString(text, x, y);
279      }
280    
281      public void paintText(SynthContext ctx, Graphics g, String text, Icon icon,
282                            int hAlign, int vAlign, int hTextPosition,
283                            int vTextPosition, int iconTextGap, int mnemonicIndex,
284                            int textOffset)
285        throws NotImplementedException
286      {
287        // FIXME: Implement this correctly.
288      }
289    }