001
002/* ----------------------------------------------------------------------
003 * 
004 * Copyright (c) 2002-2009 The MITRE Corporation
005 * 
006 * Except as permitted below
007 * ALL RIGHTS RESERVED
008 * 
009 * The MITRE Corporation (MITRE) provides this software to you without
010 * charge to use for your internal purposes only. Any copy you make for
011 * such purposes is authorized provided you reproduce MITRE's copyright
012 * designation and this License in any such copy. You may not give or
013 * sell this software to any other party without the prior written
014 * permission of the MITRE Corporation.
015 * 
016 * The government of the United States of America may make unrestricted
017 * use of this software.
018 * 
019 * This software is the copyright work of MITRE. No ownership or other
020 * proprietary interest in this software is granted you other than what
021 * is granted in this license.
022 * 
023 * Any modification or enhancement of this software must inherit this
024 * license, including its warranty disclaimers. You hereby agree to
025 * provide to MITRE, at no charge, a copy of any such modification or
026 * enhancement without limitation.
027 * 
028 * MITRE IS PROVIDING THE PRODUCT "AS IS" AND MAKES NO WARRANTY, EXPRESS
029 * OR IMPLIED, AS TO THE ACCURACY, CAPABILITY, EFFICIENCY,
030 * MERCHANTABILITY, OR FUNCTIONING OF THIS SOFTWARE AND DOCUMENTATION. IN
031 * NO EVENT WILL MITRE BE LIABLE FOR ANY GENERAL, CONSEQUENTIAL,
032 * INDIRECT, INCIDENTAL, EXEMPLARY OR SPECIAL DAMAGES, EVEN IF MITRE HAS
033 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
034 * 
035 * You accept this software on the condition that you indemnify and hold
036 * harmless MITRE, its Board of Trustees, officers, agents, and
037 * employees, from any and all liability or damages to third parties,
038 * including attorneys' fees, court costs, and other related costs and
039 * expenses, arising out of your use of this software irrespective of the
040 * cause of said liability.
041 * 
042 * The export from the United States or the subsequent reexport of this
043 * software is subject to compliance with United States export control
044 * and munitions control restrictions. You agree that in the event you
045 * seek to export this software you assume full responsibility for
046 * obtaining all necessary export licenses and approvals and for assuring
047 * compliance with applicable reexport restrictions.
048 * 
049 * ----------------------------------------------------------------------
050 * 
051 * NOTICE
052 * 
053 * This software was produced for the U. S. Government
054 * under Contract No. W15P7T-09-C-F600, and is
055 * subject to the Rights in Noncommercial Computer Software
056 * and Noncommercial Computer Software Documentation
057 * Clause 252.227-7014 (JUN 1995).
058 * 
059 * (c) 2009 The MITRE Corporation. All Rights Reserved.
060 * 
061 * ----------------------------------------------------------------------
062 *
063 */
064/*
065 * Copyright (c) 2002-2006 The MITRE Corporation
066 * 
067 * Except as permitted below
068 * ALL RIGHTS RESERVED
069 * 
070 * The MITRE Corporation (MITRE) provides this software to you without
071 * charge to use for your internal purposes only. Any copy you make for
072 * such purposes is authorized provided you reproduce MITRE's copyright
073 * designation and this License in any such copy. You may not give or
074 * sell this software to any other party without the prior written
075 * permission of the MITRE Corporation.
076 * 
077 * The government of the United States of America may make unrestricted
078 * use of this software.
079 * 
080 * This software is the copyright work of MITRE. No ownership or other
081 * proprietary interest in this software is granted you other than what
082 * is granted in this license.
083 * 
084 * Any modification or enhancement of this software must inherit this
085 * license, including its warranty disclaimers. You hereby agree to
086 * provide to MITRE, at no charge, a copy of any such modification or
087 * enhancement without limitation.
088 * 
089 * MITRE IS PROVIDING THE PRODUCT "AS IS" AND MAKES NO WARRANTY, EXPRESS
090 * OR IMPLIED, AS TO THE ACCURACY, CAPABILITY, EFFICIENCY,
091 * MERCHANTABILITY, OR FUNCTIONING OF THIS SOFTWARE AND DOCUMENTATION. IN
092 * NO EVENT WILL MITRE BE LIABLE FOR ANY GENERAL, CONSEQUENTIAL,
093 * INDIRECT, INCIDENTAL, EXEMPLARY OR SPECIAL DAMAGES, EVEN IF MITRE HAS
094 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
095 * 
096 * You accept this software on the condition that you indemnify and hold
097 * harmless MITRE, its Board of Trustees, officers, agents, and
098 * employees, from any and all liability or damages to third parties,
099 * including attorneys' fees, court costs, and other related costs and
100 * expenses, arising out of your use of this software irrespective of the
101 * cause of said liability.
102 * 
103 * The export from the United States or the subsequent reexport of this
104 * software is subject to compliance with United States export control
105 * and munitions control restrictions. You agree that in the event you
106 * seek to export this software you assume full responsibility for
107 * obtaining all necessary export licenses and approvals and for assuring
108 * compliance with applicable reexport restrictions.
109 */
110
111package jmri.util.org.mitre.jawb.swing;
112
113import java.awt.Component;
114import java.awt.Graphics;
115
116import javax.swing.Icon;
117import javax.swing.SwingConstants;
118
119/**
120 * CompositeIcon is an Icon implementation which draws two icons with a
121 * specified relative position.<p>
122 *
123 * <code>LEFT, RIGHT, TOP, BOTTOM</code> specify how icon1 is drawn relative
124 * to icon2<p>
125 *
126 * CENTER: icon1 is drawn first, icon2 is drawn over it and with horizontal
127 * and vertical orientations within the alloted space<p>
128 *
129 * It's useful with VTextIcon when you want an icon with your text: if icon1
130 * is the graphic icon and icon2 is the VTextIcon, you get a similar effect to
131 * a JLabel with a graphic icon and text
132 *
133 * @see <a href="http://www.macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html">http://www.macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html</a>
134 */
135public class CompositeIcon implements Icon, SwingConstants {
136  Icon fIcon1, fIcon2;
137  int fPosition, fHorizontalOrientation, fVerticalOrientation;
138        
139  /**
140   * Create a CompositeIcon from the specified Icons, using the default relative
141   * position (icon1 above icon2) and orientations (centered horizontally and
142   * vertically)
143   * 
144   * @param icon1 icon 1
145   * @param icon2 icon 2
146   */
147  public CompositeIcon(Icon icon1, Icon icon2) {
148    this(icon1, icon2, TOP);
149  }
150        
151  /**
152   * Create a CompositeIcon from the specified Icons, using the specified
153   * relative position and default orientations (centered horizontally and
154   * vertically)
155   * 
156   * @param icon1    icon 1
157   * @param icon2    icon 2
158   * @param position icon position
159   */
160  public CompositeIcon(Icon icon1, Icon icon2, int position) {
161      this(icon1, icon2, position, CENTER, CENTER);
162  }
163        
164  /**
165   * Create a CompositeIcon from the specified Icons, using the specified
166   * relative position and orientations
167   * 
168   * @param icon1                 icon 1
169   * @param icon2                 icon 2
170   * @param position              icon positin
171   * @param horizontalOrientation horizontal orientation
172   * @param verticalOrientation   vertical orientation
173   */
174  public CompositeIcon(Icon icon1, Icon icon2, int position,
175          int horizontalOrientation, int verticalOrientation) {
176    fIcon1 = icon1;
177    fIcon2 = icon2;
178    fPosition = position;
179    fHorizontalOrientation = horizontalOrientation;
180    fVerticalOrientation = verticalOrientation;
181  }
182        
183  /**
184   * Draw the icon at the specified location.  Icon implementations
185   * may use the Component argument to get properties useful for 
186   * painting, e.g. the foreground or background color.
187   */
188  @Override
189public void paintIcon(Component c, Graphics g, int x, int y) {
190    int width = getIconWidth();
191    int height = getIconHeight();
192    if (fPosition == LEFT || fPosition == RIGHT) {
193      Icon leftIcon, rightIcon;
194      if (fPosition == LEFT) {
195        leftIcon = fIcon1;
196        rightIcon = fIcon2;
197      }
198      else {
199        leftIcon = fIcon2;
200        rightIcon = fIcon1;
201      }
202      // "Left" orientation, because we specify the x position
203      paintIcon(c, g, leftIcon, x, y,
204                width, height, LEFT, fVerticalOrientation);
205      paintIcon(c, g, rightIcon, x + leftIcon.getIconWidth(), y,
206                width, height, LEFT, fVerticalOrientation);                  
207    }
208    else if (fPosition == TOP || fPosition == BOTTOM) {
209      Icon topIcon, bottomIcon;
210      if (fPosition == TOP) {
211        topIcon = fIcon1;
212        bottomIcon = fIcon2;
213      }
214      else {
215        topIcon = fIcon2;
216        bottomIcon = fIcon1;
217      }
218      // "Top" orientation, because we specify the y position
219      paintIcon(c, g, topIcon, x, y,
220                width, height, fHorizontalOrientation, TOP);
221      paintIcon(c, g, bottomIcon, x, y + topIcon.getIconHeight(),
222                width, height, fHorizontalOrientation, TOP);                        
223    }
224    else {
225      paintIcon(c, g, fIcon1, x, y, width, height,
226                fHorizontalOrientation, fVerticalOrientation);
227      paintIcon(c, g, fIcon2, x, y, width, height,
228                fHorizontalOrientation, fVerticalOrientation);                     
229    }
230  }
231    
232  /** Paints one icon in the specified rectangle with the given orientations
233 * @param c component
234 * @param g graphic
235 * @param icon icon
236 * @param x x location
237 * @param y y location
238 * @param width width of icon
239 * @param height height of icon
240 * @param horizontalOrientation horizontal orientation
241 * @param verticalOrientation vertical orientation
242   */
243  void paintIcon(Component c, Graphics g, Icon icon, int x, int y,
244                 int width, int height,
245                 int horizontalOrientation, int verticalOrientation) {
246                        
247    int xIcon, yIcon;
248    switch (horizontalOrientation) {
249    case LEFT:
250      xIcon = x;
251      break;
252    case RIGHT: 
253      xIcon = x + width - icon.getIconWidth();
254      break;
255    default:
256      xIcon = x + (width - icon.getIconWidth()) / 2;
257      break;
258    }
259    switch (verticalOrientation) {
260    case TOP:
261      yIcon = y;
262      break;
263    case BOTTOM:
264      yIcon = y + height - icon.getIconHeight();
265      break;
266    default:
267      yIcon = y + (height - icon.getIconHeight()) / 2;
268      break;                          
269    }
270    icon.paintIcon(c, g, xIcon, yIcon);             
271  }
272        
273  /**
274   * Returns the icon's width.
275   *
276   * @return an int specifying the fixed width of the icon.
277   */
278  @Override
279public int getIconWidth() {
280    if (fPosition == LEFT || fPosition == RIGHT)
281      return fIcon1.getIconWidth() + fIcon2.getIconWidth();
282                                
283    return Math.max(fIcon1.getIconWidth(), fIcon2.getIconWidth());
284  }
285        
286  /**
287   * Returns the icon's height.
288   *
289   * @return an int specifying the fixed height of the icon.
290   */
291  @Override
292public int getIconHeight() {
293    if (fPosition == TOP || fPosition == BOTTOM)
294      return fIcon1.getIconHeight() + fIcon2.getIconHeight();
295                                
296    return Math.max(fIcon1.getIconHeight(), fIcon2.getIconHeight());
297  }
298        
299}