1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Color ; 34 import java.awt.Component ; 35 import java.awt.Graphics ; 36 import java.io.Serializable ; 37 38 import javax.swing.ButtonModel ; 39 import javax.swing.Icon ; 40 import javax.swing.JCheckBox ; 41 import javax.swing.JComponent ; 42 import javax.swing.JMenuItem ; 43 import javax.swing.plaf.UIResource ; 44 import javax.swing.plaf.metal.MetalLookAndFeel ; 45 46 59 60 final class PlasticIconFactory { 61 62 63 private static void drawCheck(Graphics g, int x, int y) { 65 g.translate(x, y); 66 g.drawLine(3, 5, 3, 5); 67 g.fillRect(3, 6, 2, 2); 68 g.drawLine(4, 8, 9, 3); 69 g.drawLine(5, 8, 9, 4); 70 g.drawLine(5, 9, 9, 5); 71 g.translate(-x, -y); 72 } 73 74 75 private static class CheckBoxIcon implements Icon , UIResource , Serializable { 76 77 private static final int SIZE = 13; 78 79 public int getIconWidth() { return SIZE; } 80 public int getIconHeight() { return SIZE; } 81 82 public void paintIcon(Component c, Graphics g, int x, int y) { 83 JCheckBox cb = (JCheckBox ) c; 84 ButtonModel model = cb.getModel(); 85 86 if (model.isEnabled()) { 87 if (cb.isBorderPaintedFlat()) { 88 g.setColor(PlasticLookAndFeel.getControlDarkShadow()); 89 g.drawRect(x, y, SIZE - 2, SIZE - 2); 90 g.setColor(PlasticLookAndFeel.getControlHighlight()); 92 g.fillRect(x+1, y+1, SIZE-3, SIZE-3); 93 } else if (model.isPressed() && model.isArmed()) { 94 g.setColor(MetalLookAndFeel.getControlShadow()); 95 g.fillRect(x, y, SIZE - 1, SIZE - 1); 96 PlasticUtils.drawPressed3DBorder(g, x, y, SIZE, SIZE); 97 } else { 98 PlasticUtils.drawFlush3DBorder(g, x, y, SIZE, SIZE); 99 } 100 g.setColor(MetalLookAndFeel.getControlInfo()); 101 } else { 102 g.setColor(MetalLookAndFeel.getControlShadow()); 103 g.drawRect(x, y, SIZE - 2, SIZE - 2); 104 } 105 106 if (model.isSelected()) { 107 drawCheck(g, x, y); 108 } 109 } 110 111 } 112 113 114 private static class CheckBoxMenuItemIcon implements Icon , UIResource , Serializable { 115 116 private static final int SIZE = 13; 117 118 public int getIconWidth() { return SIZE; } 119 public int getIconHeight() { return SIZE; } 120 121 public void paintIcon(Component c, Graphics g, int x, int y) { 122 JMenuItem b = (JMenuItem ) c; 123 if (b.isSelected()) { 124 drawCheck(g, x, y + 1); 125 } 126 } 127 } 128 129 130 private static class RadioButtonMenuItemIcon implements Icon , UIResource , Serializable { 131 132 private static final int SIZE = 13; 133 134 public int getIconWidth() { return SIZE; } 135 public int getIconHeight() { return SIZE; } 136 137 public void paintIcon(Component c, Graphics g, int x, int y) { 138 JMenuItem b = (JMenuItem ) c; 139 if (b.isSelected()) { 140 drawDot(g, x, y); 141 } 142 } 143 144 private void drawDot(Graphics g, int x, int y) { 145 g.translate(x, y); 146 g.drawLine(5, 4, 8, 4); 147 g.fillRect(4, 5, 6, 4); 148 g.drawLine(5, 9, 8, 9); 149 g.translate(-x, -y); 150 } 151 } 152 153 154 private static class MenuArrowIcon implements Icon , UIResource , Serializable { 155 156 private static final int WIDTH = 4; 157 private static final int HEIGHT = 8; 158 159 public void paintIcon( Component c, Graphics g, int x, int y ) { 160 JMenuItem b = (JMenuItem ) c; 161 162 g.translate( x, y ); 163 if( PlasticUtils.isLeftToRight(b) ) { 164 g.drawLine( 0, 0, 0, 7 ); 165 g.drawLine( 1, 1, 1, 6 ); 166 g.drawLine( 2, 2, 2, 5 ); 167 g.drawLine( 3, 3, 3, 4 ); 168 } else { 169 g.drawLine( 4, 0, 4, 7 ); 170 g.drawLine( 3, 1, 3, 6 ); 171 g.drawLine( 2, 2, 2, 5 ); 172 g.drawLine( 1, 3, 1, 4 ); 173 } 174 g.translate( -x, -y ); 175 } 176 177 public int getIconWidth() { return WIDTH; } 178 public int getIconHeight() { return HEIGHT; } 179 180 } 181 182 183 186 private static class ExpandedTreeIcon implements Icon , Serializable { 187 188 protected static final int SIZE = 9; 189 protected static final int HALF_SIZE = 4; 190 191 public void paintIcon(Component c, Graphics g, int x, int y) { 192 Color backgroundColor = c.getBackground(); 193 194 g.setColor(backgroundColor != null ? backgroundColor : Color.WHITE); 195 g.fillRect(x, y, SIZE - 1, SIZE - 1); 196 g.setColor(Color.GRAY); 197 g.drawRect(x, y, SIZE - 1, SIZE - 1); 198 g.setColor(Color.BLACK); 199 g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE); 200 } 201 202 public int getIconWidth() { return SIZE; } 203 public int getIconHeight() { return SIZE; } 204 } 205 206 207 210 private static class CollapsedTreeIcon extends ExpandedTreeIcon { 211 public void paintIcon(Component c, Graphics g, int x, int y) { 212 super.paintIcon(c, g, x, y); 213 g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3)); 214 } 215 } 216 217 218 221 private static class ComboBoxButtonIcon implements Icon , Serializable { 222 223 public void paintIcon(Component c, Graphics g, int x, int y) { 224 JComponent component = (JComponent )c; 225 int iconWidth = getIconWidth(); 226 227 g.translate(x, y); 228 229 g.setColor( component.isEnabled() 230 ? MetalLookAndFeel.getControlInfo() 231 : MetalLookAndFeel.getControlShadow() ); 232 g.drawLine( 0, 0, iconWidth - 1, 0 ); 233 g.drawLine( 1, 1, 1 + (iconWidth - 3), 1 ); 234 g.drawLine( 2, 2, 2 + (iconWidth - 5), 2 ); 235 g.drawLine( 3, 3, 3 + (iconWidth - 7), 3 ); 236 237 250 g.translate( -x, -y ); 251 } 252 253 public int getIconWidth() { return 8; } 254 public int getIconHeight() { return 4; } 255 } 256 257 258 260 private static Icon checkBoxIcon; 261 private static Icon checkBoxMenuItemIcon; 262 private static Icon radioButtonMenuItemIcon; 263 private static Icon menuArrowIcon; 264 private static Icon expandedTreeIcon; 265 private static Icon collapsedTreeIcon; 266 267 268 271 static Icon getCheckBoxIcon() { 272 if (checkBoxIcon == null) { 273 checkBoxIcon = new CheckBoxIcon(); 274 } 275 return checkBoxIcon; 276 } 277 278 279 282 static Icon getCheckBoxMenuItemIcon() { 283 if (checkBoxMenuItemIcon == null) { 284 checkBoxMenuItemIcon = new CheckBoxMenuItemIcon(); 285 } 286 return checkBoxMenuItemIcon; 287 } 288 289 290 293 static Icon getRadioButtonMenuItemIcon() { 294 if (radioButtonMenuItemIcon == null) { 295 radioButtonMenuItemIcon = new RadioButtonMenuItemIcon(); 296 } 297 return radioButtonMenuItemIcon; 298 } 299 300 301 304 static Icon getMenuArrowIcon() { 305 if (menuArrowIcon == null) { 306 menuArrowIcon = new MenuArrowIcon(); 307 } 308 return menuArrowIcon; 309 } 310 311 312 315 static Icon getExpandedTreeIcon() { 316 if (expandedTreeIcon == null) { 317 expandedTreeIcon = new ExpandedTreeIcon(); 318 } 319 return expandedTreeIcon; 320 } 321 322 325 static Icon getCollapsedTreeIcon() { 326 if (collapsedTreeIcon == null) { 327 collapsedTreeIcon = new CollapsedTreeIcon(); 328 } 329 return collapsedTreeIcon; 330 } 331 332 335 static Icon getComboBoxButtonIcon() { 336 return new ComboBoxButtonIcon(); 337 } 338 339 340 } | Popular Tags |