View Javadoc

1   // %1116523448593:hoplugins.commons.ui%
2   package hoplugins.commons.ui;
3   
4   import javax.swing.Icon;
5   import javax.swing.SwingConstants;
6   
7   import java.awt.Component;
8   import java.awt.Graphics;
9   
10  /***
11   * CompositeIcon is an Icon implementation which draws two icons with a specified relative
12   * position: LEFT, RIGHT, TOP, BOTTOM specify how icon1 is drawn relative to icon2 CENTER: icon1
13   * is drawn first, icon2 is drawn over it  and with horizontal and vertical orientations within
14   * the alloted space  It's useful with VTextIcon when you want an icon with your text:     if
15   * icon1 is the graphic icon and icon2 is the VTextIcon, you get a similar effect     to a JLabel
16   * with a graphic icon and text
17   */
18  public class CompositeIcon implements Icon, SwingConstants {
19      /*** Icon1 */
20      private Icon fIcon1;
21  
22      /*** Icon2 */
23      private Icon fIcon2;
24  
25      /*** Horizontal Orientation */
26      private int fHorizontalOrientation;
27  
28      /*** Position */
29      private int fPosition;
30  
31      /*** Vertical Orientation */
32      private int fVerticalOrientation;
33  
34      /***
35       * Create a CompositeIcon from the specified Icons, using the default relative position (icon1
36       * above icon2) and orientations (centered horizontally and vertically)
37       *
38       * @param icon1
39       * @param icon2
40       */
41      public CompositeIcon(Icon icon1, Icon icon2) {
42          this(icon1, icon2, TOP);
43      }
44  
45      /***
46       * Create a CompositeIcon from the specified Icons, using the specified relative position and
47       * default orientations (centered horizontally and vertically)
48       *
49       * @param icon1
50       * @param icon2
51       * @param position
52       */
53      public CompositeIcon(Icon icon1, Icon icon2, int position) {
54          this(icon1, icon2, position, CENTER, CENTER);
55      }
56  
57      /***
58       * Create a CompositeIcon from the specified Icons, using the specified relative position and
59       * orientations
60       *
61       * @param icon1
62       * @param icon2
63       * @param position
64       * @param horizontalOrientation
65       * @param verticalOrientation
66       */
67      public CompositeIcon(Icon icon1, Icon icon2, int position,
68          int horizontalOrientation, int verticalOrientation) {
69          fIcon1 = icon1;
70          fIcon2 = icon2;
71          fPosition = position;
72          fHorizontalOrientation = horizontalOrientation;
73          fVerticalOrientation = verticalOrientation;
74      }
75  
76      /***
77       * Returns the icon's height.
78       *
79       * @return an int specifying the fixed height of the icon.
80       */
81      public int getIconHeight() {
82          if ((fPosition == TOP) || (fPosition == BOTTOM)) {
83              return fIcon1.getIconHeight() + fIcon2.getIconHeight();
84          }
85  
86          return Math.max(fIcon1.getIconHeight(), fIcon2.getIconHeight());
87      }
88  
89      /***
90       * Returns the icon's width.
91       *
92       * @return an int specifying the fixed width of the icon.
93       */
94      public int getIconWidth() {
95          if ((fPosition == LEFT) || (fPosition == RIGHT)) {
96              return fIcon1.getIconWidth() + fIcon2.getIconWidth();
97          }
98  
99          return Math.max(fIcon1.getIconWidth(), fIcon2.getIconWidth());
100     }
101 
102     /***
103      * Draw the icon at the specified location.  Icon implementations may use the Component
104      * argument to get properties useful for  painting, e.g. the foreground or background color.
105      *
106      * @param c
107      * @param g
108      * @param x
109      * @param y
110      */
111     public void paintIcon(Component c, Graphics g, int x, int y) {
112         int width = getIconWidth();
113         int height = getIconHeight();
114 
115         if ((fPosition == LEFT) || (fPosition == RIGHT)) {
116             Icon leftIcon;
117             Icon rightIcon;
118 
119             if (fPosition == LEFT) {
120                 leftIcon = fIcon1;
121                 rightIcon = fIcon2;
122             }
123             else {
124                 leftIcon = fIcon2;
125                 rightIcon = fIcon1;
126             }
127 
128             // "Left" orientation, because we specify the x position
129             paintIcon(c, g, leftIcon, x, y, width, height, LEFT,
130                 fVerticalOrientation);
131             paintIcon(c, g, rightIcon, x + leftIcon.getIconWidth(), y, width,
132                 height, LEFT, fVerticalOrientation);
133         }
134         else if ((fPosition == TOP) || (fPosition == BOTTOM)) {
135             Icon topIcon;
136             Icon bottomIcon;
137 
138             if (fPosition == TOP) {
139                 topIcon = fIcon1;
140                 bottomIcon = fIcon2;
141             }
142             else {
143                 topIcon = fIcon2;
144                 bottomIcon = fIcon1;
145             }
146 
147             // "Top" orientation, because we specify the y position
148             paintIcon(c, g, topIcon, x, y, width, height,
149                 fHorizontalOrientation, TOP);
150             paintIcon(c, g, bottomIcon, x, y + topIcon.getIconHeight(), width,
151                 height, fHorizontalOrientation, TOP);
152         }
153         else {
154             paintIcon(c, g, fIcon1, x, y, width, height,
155                 fHorizontalOrientation, fVerticalOrientation);
156             paintIcon(c, g, fIcon2, x, y, width, height,
157                 fHorizontalOrientation, fVerticalOrientation);
158         }
159     }
160 
161     /* Paints one icon in the specified rectangle with the given orientations
162      */
163     void paintIcon(Component c, Graphics g, Icon icon, int x, int y, int width,
164         int height, int horizontalOrientation, int verticalOrientation) {
165         int xIcon;
166         int yIcon;
167 
168         switch (horizontalOrientation) {
169         case LEFT:
170             xIcon = x;
171 
172             break;
173 
174         case RIGHT:
175             xIcon = (x + width) - icon.getIconWidth();
176 
177             break;
178 
179         default:
180             xIcon = x + ((width - icon.getIconWidth()) / 2);
181 
182             break;
183         }
184 
185         switch (verticalOrientation) {
186         case TOP:
187             yIcon = y;
188 
189             break;
190 
191         case BOTTOM:
192             yIcon = (y + height) - icon.getIconHeight();
193 
194             break;
195 
196         default:
197             yIcon = y + ((height - icon.getIconHeight()) / 2);
198 
199             break;
200         }
201 
202         icon.paintIcon(c, g, xIcon, yIcon);
203     }
204 }