Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
CompoundIcon.java
1 
16 package net.squiz.matrix.core;
17 
18 
19 import java.awt.*;
20 import javax.swing.*;
21 import java.util.*;
22 
29 public class CompoundIcon implements Icon, SwingConstants {
30 
32  protected static final int[] VALID_X = {LEFT, RIGHT, CENTER};
33 
35  protected static final int[] VALID_Y = {TOP, BOTTOM, CENTER};
36 
38  protected Icon mainIcon, decorator;
39 
41  protected int yAlignment = BOTTOM;
42 
44  protected int xAlignment = LEFT;
45 
47  private static Map disabledIcons = new HashMap();
48 
57  public CompoundIcon(
58  Icon mainIcon,
59  Icon decorator,
60  int xAlignment,
61  int yAlignment) {
62  if (decorator.getIconWidth() > mainIcon.getIconWidth()) {
63  throw new IllegalArgumentException(
64  "decorator icon is wider than main icon");
65  }
66  if (decorator.getIconHeight() > mainIcon.getIconHeight()) {
67  throw new IllegalArgumentException(
68  "decorator icon is higher than main icon");
69  }
70  if (!isLegalValue(xAlignment, VALID_X)) {
71  throw new IllegalArgumentException(
72  "xAlignment must be LEFT, RIGHT or CENTER");
73  }
74  if (!isLegalValue(yAlignment, VALID_Y)) {
75  throw new IllegalArgumentException(
76  "yAlignment must be TOP, BOTTOM or CENTER");
77  }
78 
79  this.mainIcon = mainIcon;
80  this.decorator = decorator;
81  this.xAlignment = xAlignment;
82  this.yAlignment = yAlignment;
83  }
84 
91  public boolean isLegalValue(int value, int[] legal) {
92  for (int i = 0; i < legal.length; i++) {
93  if (value == legal[i])
94  return true;
95  }
96  return false;
97  }
98 
103  public Icon getDisabledIcon() {
104  if (!disabledIcons.containsKey(mainIcon)) {
105  Image grayImage = GrayFilter.createDisabledImage(((ImageIcon) mainIcon).getImage());
106  disabledIcons.put(mainIcon, grayImage);
107  }
108  return new ImageIcon((Image)disabledIcons.get(mainIcon));
109  }
110 
116  public int getIconWidth() {
117  return mainIcon.getIconWidth();
118  }
119 
124  public int getIconHeight() {
125  return mainIcon.getIconHeight();
126  }
127 
135  public void paintIcon(Component c, Graphics g, int x, int y) {
136  mainIcon.paintIcon(c, g, x, y);
137  int w = getIconWidth();
138  int h = getIconHeight();
139 
140  if (xAlignment == CENTER)
141  x += (w - decorator.getIconWidth()) / 2;
142  if (xAlignment == RIGHT)
143  x += (w - decorator.getIconWidth());
144  if (yAlignment == CENTER)
145  y += (h - decorator.getIconHeight()) / 2;
146  if (yAlignment == BOTTOM)
147  y += (h - decorator.getIconHeight());
148  decorator.paintIcon(c, g, x, y);
149  }
150 }
151