Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
CompoundIcon.java
1 
16 package net.squiz.matrix.assetmap;
17 
18 
19 import java.awt.*;
20 import javax.swing.*;
21 
28 public class CompoundIcon implements Icon, SwingConstants {
29 
31  protected static final int[]
32  VALID_X = {LEFT, RIGHT, CENTER};
33 
35  protected static final int[]
36  VALID_Y = {TOP, BOTTOM, CENTER};
37 
39  protected Icon mainIcon, decorator;
40 
42  protected int yAlignment = BOTTOM;
43 
45  protected int xAlignment = LEFT;
46 
51  public CompoundIcon() {
52 
53  }
54 
63  public CompoundIcon(
64  Icon mainIcon,
65  Icon decorator,
66  int xAlignment,
67  int yAlignment) {
68  if (decorator.getIconWidth() > mainIcon.getIconWidth()) {
69  throw new IllegalArgumentException(
70  "decorator icon is wider than main icon");
71  }
72  if (decorator.getIconHeight() > mainIcon.getIconHeight()) {
73  throw new IllegalArgumentException(
74  "decorator icon is higher than main icon");
75  }
76  if (!isLegalValue(xAlignment, VALID_X)) {
77  throw new IllegalArgumentException(
78  "xAlignment must be LEFT, RIGHT or CENTER");
79  }
80  if (!isLegalValue(yAlignment, VALID_Y)) {
81  throw new IllegalArgumentException(
82  "yAlignment must be TOP, BOTTOM or CENTER");
83  }
84 
85  this.mainIcon = mainIcon;
86  this.decorator = decorator;
87  this.xAlignment = xAlignment;
88  this.yAlignment = yAlignment;
89  }
90 
98  public boolean isLegalValue(int value, int[] legal) {
99  for (int i = 0; i < legal.length; i++) {
100  if (value == legal[i])
101  return true;
102  }
103  return false;
104  }
105 
111  public Icon getDisabledIcon() {
112  Image grayImage = GrayFilter.createDisabledImage(((ImageIcon) mainIcon).getImage());
113  return new ImageIcon(grayImage);
114  }
115 
122  public int getIconWidth() {
123  return mainIcon.getIconWidth();
124  }
125 
131  public int getIconHeight() {
132  return mainIcon.getIconHeight();
133  }
134 
143  public void paintIcon(Component c, Graphics g, int x, int y) {
144  mainIcon.paintIcon(c, g, x, y);
145  int w = getIconWidth();
146  int h = getIconHeight();
147 
148  if (xAlignment == CENTER) {
149  x += (w - decorator.getIconWidth()) / 2;
150  }
151  if (xAlignment == RIGHT) {
152  x += (w - decorator.getIconWidth());
153  }
154  if (yAlignment == CENTER) {
155  y += (h - decorator.getIconHeight()) / 2;
156  }
157  if (yAlignment == BOTTOM) {
158  y += (h - decorator.getIconHeight());
159  }
160  decorator.paintIcon(c, g, x, y);
161  }
162 }
163