-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCompoundIcon.java
More file actions
444 lines (388 loc) · 11.1 KB
/
CompoundIcon.java
File metadata and controls
444 lines (388 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import java.awt.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
/**
* The CompoundIcon will paint two, or more, Icons as a single Icon. The
* Icons are painted in the order in which they are added.
*
* The Icons are layed out on the specified axis:
* <ul>
* <li>X-Axis (horizontally)
* <li>Y-Axis (vertically)
* <li>Z-Axis (stacked)
* </ul>
*
*/
public class CompoundIcon implements Icon
{
public enum Axis
{
X_AXIS,
Y_AXIS,
Z_AXIS;
}
public final static float TOP = 0.0f;
public final static float LEFT = 0.0f;
public final static float CENTER = 0.5f;
public final static float BOTTOM = 1.0f;
public final static float RIGHT = 1.0f;
private Icon[] icons;
private Rectangle[] bounds;
private HashMap<Component, Point> iconOffset = new HashMap<Component, Point>();
private Axis axis;
private int gap;
private float alignmentX = CENTER;
private float alignmentY = CENTER;
/**
* Convenience contructor for creating a CompoundIcon where the
* icons are layed out on on the X-AXIS, the gap is 0 and the
* X/Y alignments will default to CENTER.
*
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Icon... icons)
{
this(Axis.X_AXIS, icons);
}
/**
* Convenience contructor for creating a CompoundIcon where the
* gap is 0 and the X/Y alignments will default to CENTER.
*
* @param axis the axis used to lay out the icons for painting.
* Must be one of the Axis enums: X_AXIS, Y_AXIS, Z_Axis.
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Axis axis, Icon... icons)
{
this(axis, 0, icons);
}
/**
* Convenience contructor for creating a CompoundIcon where the
* X/Y alignments will default to CENTER.
*
* @param axis the axis used to lay out the icons for painting
* Must be one of the Axis enums: X_AXIS, Y_AXIS, Z_Axis.
* @param gap the gap between the icons
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Axis axis, int gap, Icon... icons)
{
this(axis, gap, CENTER, CENTER, icons);
}
/**
* Create a CompoundIcon specifying all the properties.
*
* @param axis the axis used to lay out the icons for painting
* Must be one of the Axis enums: X_AXIS, Y_AXIS, Z_Axis.
* @param gap the gap between the icons
* @param alignmentX the X alignment of the icons. Common values are
* LEFT, CENTER, RIGHT. Can be any value between 0.0 and 1.0
* @param alignmentY the Y alignment of the icons. Common values are
* TOP, CENTER, BOTTOM. Can be any value between 0.0 and 1.0
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Axis axis, int gap, float alignmentX, float alignmentY, Icon... icons)
{
this.axis = axis;
this.gap = gap;
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
for (int i = 0; i < icons.length; i++)
{
if (icons[i] == null)
{
String message = "Icon (" + i + ") cannot be null";
throw new IllegalArgumentException( message );
}
}
this.icons = icons;
determineIconBounds();
}
/**
* Get the Axis along which each icon is painted.
*
* @return the Axis
*/
public Axis getAxis()
{
return axis;
}
/**
* Get the gap between each icon
*
* @return the gap in pixels
*/
public int getGap()
{
return gap;
}
/**
* Get the alignment of the icon on the x-axis
*
* @return the alignment
*/
public float getAlignmentX()
{
return alignmentX;
}
/**
* Get the alignment of the icon on the y-axis
*
* @return the alignment
*/
public float getAlignmentY()
{
return alignmentY;
}
/**
* Get the number of Icons contained in this CompoundIcon.
*
* @return the total number of Icons
*/
public int getIconCount()
{
return icons.length;
}
/**
* Get the Icon at the specified index.
*
* @param index the index of the Icon to be returned
* @return the Icon at the specifed index
* @exception IndexOutOfBoundsException if the index is out of range
*/
public Icon getIcon(int index)
{
return icons[ index ];
}
/**
* Get IconInformation at the specified point for a given component. The
* icon information will include the icon, the bounds of the icon and the
* Point relative to the icon. null is returned when no icon is found at
* the specified point.
*
* @param point A specific point of the component.
* @param component the component which is displaying the CompoundIcon
* @return IconInfo of the Icon at the specifed point or null.
*/
public IconInfo getIconInfoAtPoint(Point point, Component component)
{
// An Icon can be shared by multiple components. Get the offset where
// this Icon was painted.
Point offset = iconOffset.get(component);
if (offset != null)
{
point.x -= offset.x;
point.y -= offset.y;
}
// Start searching Icon bound from the last Icon painted in case
// Icons overlap one another.
for (int i = bounds.length - 1; i >= 0; i--)
{
Rectangle r = bounds[i];
if ( r.contains(point) )
{
Icon icon = icons[i];
// Recursively invoke this method, this time with a null
// component, since the offset has been recalculated.
if (icon instanceof CompoundIcon)
{
CompoundIcon compound = (CompoundIcon)icon;
return compound.getIconInfoAtPoint(new Point(point.x - r.x, point.y - r.y), null);
}
else
{
Point p = new Point(point.x - r.x, point.y - r.y);
return new IconInfo(icon, r, p);
}
}
}
return null;
}
/*
* Get IconInfo for every Icon
*
* @return IconInfo for every Icon.
*
*/
public List<IconInfo> getIconInfo()
{
ArrayList<IconInfo> list = new ArrayList<IconInfo>();
Point offset = new Point(0, 0);
getIconInfo(list, offset);
return list;
}
private void getIconInfo(ArrayList<IconInfo> list, Point offset)
{
for (int i = 0; i < icons.length; i++)
{
Rectangle r = bounds[i];
if (icons[i] instanceof CompoundIcon)
{
CompoundIcon compound = (CompoundIcon)icons[i];
compound.getIconInfo(list, new Point(offset.x + r.x, offset.y + r.y));
}
else
{
r = new Rectangle(r.x + offset.x, r.y + offset.y, r.width, r.height);
list.add( new IconInfo(icons[i], r, null) );
}
}
}
/*
* Helper class to contain information about an Icon contained in the
* CompoundIcon. The bounds will always be returned for the specified
* Icon. The relative point is only returned when a lookup for a single
* Icon was done for a specific point. Otherwise (0, 0) is returned.
*/
static class IconInfo
{
private Icon icon;
private Rectangle bounds;
private Point point;
public IconInfo(Icon icon, Rectangle bounds, Point point)
{
this.icon = icon;
this.bounds = bounds;
this.point = point;
}
public Icon getIcon()
{
return icon;
}
public Rectangle getBounds()
{
return bounds;
}
public Point getPoint()
{
return point;
}
}
//
// Implement the Icon Interface
//
/**
* Gets the width of this icon.
*
* @return the width of the icon in pixels.
*/
@Override
public int getIconWidth()
{
int width = 0;
// Add the width of all Icons while also including the gap
if (axis == Axis.X_AXIS)
{
width += (icons.length - 1) * gap;
for (Icon icon : icons)
width += icon.getIconWidth();
}
else // Just find the maximum width
{
for (Icon icon : icons)
width = Math.max(width, icon.getIconWidth());
}
return width;
}
/**
* Gets the height of this icon.
*
* @return the height of the icon in pixels.
*/
@Override
public int getIconHeight()
{
int height = 0;
// Add the height of all Icons while also including the gap
if (axis == Axis.Y_AXIS)
{
height += (icons.length - 1) * gap;
for (Icon icon : icons)
height += icon.getIconHeight();
}
else // Just find the maximum height
{
for (Icon icon : icons)
height = Math.max(height, icon.getIconHeight());
}
return height;
}
/**
* Paint the icons of this compound icon at the specified location
*
* @param c The component on which the icon is painted
* @param g the graphics context
* @param x the X coordinate of the icon's top-left corner
* @param y the Y coordinate of the icon's top-left corner
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
// An Icon can be used by multiple components. The Icon can be
// painted at a different offset in each of these components.
// Save the offset for each component.
iconOffset.put(c, new Point(x, y));
for (int i = 0; i < icons.length; i++)
{
Icon icon = icons[i];
Rectangle r = bounds[i];
icon.paintIcon(c, g, r.x + x, r.y + y);
}
}
/*
* Determine the bounds of each Icon contained within the CompoundIcon
*/
private void determineIconBounds()
{
bounds = new Rectangle[icons.length];
int index = 0;
if (axis == Axis.X_AXIS)
{
int x = 0;
int height = getIconHeight();
for (Icon icon : icons)
{
int iconY = getOffset(height, icon.getIconHeight(), alignmentY);
bounds[index] = new Rectangle(x, iconY, icon.getIconWidth(), icon.getIconHeight());
x += icon.getIconWidth() + gap;
index++;
}
}
else if (axis == Axis.Y_AXIS)
{
int y = 0;
int width = getIconWidth();
for (Icon icon : icons)
{
int iconX = getOffset(width, icon.getIconWidth(), alignmentX);
bounds[index] = new Rectangle(iconX, y, icon.getIconWidth(), icon.getIconHeight());
y += icon.getIconHeight() + gap;
index++;
}
}
else // must be Z_AXIS
{
int width = getIconWidth();
int height = getIconHeight();
for (Icon icon : icons)
{
int iconX = getOffset(width, icon.getIconWidth(), alignmentX);
int iconY = getOffset(height, icon.getIconHeight(), alignmentY);
bounds[index] = new Rectangle(iconX, iconY, icon.getIconWidth(), icon.getIconHeight());
index++;
}
}
}
/*
* When the icon value is smaller than the maximum value of all icons the
* icon needs to be aligned appropriately. Calculate the offset to be used
* when painting the icon to achieve the proper alignment.
*/
private int getOffset(int maxValue, int iconValue, float alignment)
{
float offset = (maxValue - iconValue) * alignment;
return Math.round(offset);
}
}