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.*;
114import javax.swing.*;
115
116/**
117 * CompositeIcon is an Icon implementation which draws two icons with a
118 * specified relative position.<p>
119 *
120 * <code>LEFT, RIGHT, TOP, BOTTOM</code> specify how icon1 is drawn relative
121 * to icon2<p>
122 *
123 * CENTER: icon1 is drawn first, icon2 is drawn over it and with horizontal
124 * and vertical orientations within the alloted space<p>
125 *
126 * It's useful with VTextIcon when you want an icon with your text: if icon1
127 * is the graphic icon and icon2 is the VTextIcon, you get a similar effect to
128 * a JLabel with a graphic icon and text
129 *
130 * @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>
131 */
132public class CompositeIcon implements Icon, SwingConstants {
133  Icon fIcon1, fIcon2;
134  int fPosition, fHorizontalOrientation, fVerticalOrientation;
135        
136  /**
137   * Create a CompositeIcon from the specified Icons, using the default
138   * relative position (icon1 above icon2) and orientations (centered
139   * horizontally and vertically) */
140  public CompositeIcon(Icon icon1, Icon icon2) {
141    this(icon1, icon2, TOP);
142  }
143        
144  /**
145   * Create a CompositeIcon from the specified Icons, using the specified
146   * relative position and default orientations (centered horizontally and
147   * vertically) */
148  public CompositeIcon(Icon icon1, Icon icon2, int position) {
149    this(icon1, icon2, position, CENTER, CENTER);
150  }
151        
152  /**
153   * Create a CompositeIcon from the specified Icons, using the specified
154   * relative position and orientations */
155  public CompositeIcon(Icon icon1, Icon icon2, int position,
156                       int horizontalOrientation, int verticalOrientation) {
157    fIcon1 = icon1;
158    fIcon2 = icon2;
159    fPosition = position;
160    fHorizontalOrientation = horizontalOrientation;
161    fVerticalOrientation = verticalOrientation;
162  }
163        
164  /**
165   * Draw the icon at the specified location.  Icon implementations
166   * may use the Component argument to get properties useful for 
167   * painting, e.g. the foreground or background color.
168   */
169  public void paintIcon(Component c, Graphics g, int x, int y) {
170    int width = getIconWidth();
171    int height = getIconHeight();
172    if (fPosition == LEFT || fPosition == RIGHT) {
173      Icon leftIcon, rightIcon;
174      if (fPosition == LEFT) {
175        leftIcon = fIcon1;
176        rightIcon = fIcon2;
177      }
178      else {
179        leftIcon = fIcon2;
180        rightIcon = fIcon1;
181      }
182      // "Left" orientation, because we specify the x position
183      paintIcon(c, g, leftIcon, x, y,
184                width, height, LEFT, fVerticalOrientation);
185      paintIcon(c, g, rightIcon, x + leftIcon.getIconWidth(), y,
186                width, height, LEFT, fVerticalOrientation);                  
187    }
188    else if (fPosition == TOP || fPosition == BOTTOM) {
189      Icon topIcon, bottomIcon;
190      if (fPosition == TOP) {
191        topIcon = fIcon1;
192        bottomIcon = fIcon2;
193      }
194      else {
195        topIcon = fIcon2;
196        bottomIcon = fIcon1;
197      }
198      // "Top" orientation, because we specify the y position
199      paintIcon(c, g, topIcon, x, y,
200                width, height, fHorizontalOrientation, TOP);
201      paintIcon(c, g, bottomIcon, x, y + topIcon.getIconHeight(),
202                width, height, fHorizontalOrientation, TOP);                        
203    }
204    else {
205      paintIcon(c, g, fIcon1, x, y, width, height,
206                fHorizontalOrientation, fVerticalOrientation);
207      paintIcon(c, g, fIcon2, x, y, width, height,
208                fHorizontalOrientation, fVerticalOrientation);                     
209    }
210  }
211    
212  /** Paints one icon in the specified rectangle with the given orientations
213   */
214  void paintIcon(Component c, Graphics g, Icon icon, int x, int y,
215                 int width, int height,
216                 int horizontalOrientation, int verticalOrientation) {
217                        
218    int xIcon, yIcon;
219    switch (horizontalOrientation) {
220    case LEFT:
221      xIcon = x;
222      break;
223    case RIGHT: 
224      xIcon = x + width - icon.getIconWidth();
225      break;
226    default:
227      xIcon = x + (width - icon.getIconWidth()) / 2;
228      break;
229    }
230    switch (verticalOrientation) {
231    case TOP:
232      yIcon = y;
233      break;
234    case BOTTOM:
235      yIcon = y + height - icon.getIconHeight();
236      break;
237    default:
238      yIcon = y + (height - icon.getIconHeight()) / 2;
239      break;                          
240    }
241    icon.paintIcon(c, g, xIcon, yIcon);             
242  }
243        
244  /**
245   * Returns the icon's width.
246   *
247   * @return an int specifying the fixed width of the icon.
248   */
249  public int getIconWidth() {
250    if (fPosition == LEFT || fPosition == RIGHT)
251      return fIcon1.getIconWidth() + fIcon2.getIconWidth();
252                                
253    return Math.max(fIcon1.getIconWidth(), fIcon2.getIconWidth());
254  }
255        
256  /**
257   * Returns the icon's height.
258   *
259   * @return an int specifying the fixed height of the icon.
260   */
261  public int getIconHeight() {
262    if (fPosition == TOP || fPosition == BOTTOM)
263      return fIcon1.getIconHeight() + fIcon2.getIconHeight();
264                                
265    return Math.max(fIcon1.getIconHeight(), fIcon2.getIconHeight());
266  }
267        
268}