1 7 8 package com.sun.java.swing.plaf.motif; 9 10 import javax.swing.*; 11 12 import javax.swing.plaf.UIResource ; 13 14 import java.awt.Color ; 15 import java.awt.Component ; 16 import java.awt.Dimension ; 17 import java.awt.Graphics ; 18 import java.awt.Polygon ; 19 20 import java.io.Serializable ; 21 22 35 public class MotifIconFactory implements Serializable 36 { 37 private static Icon checkBoxIcon; 38 private static Icon radioButtonIcon; 39 private static Icon menuItemCheckIcon; 40 private static Icon menuItemArrowIcon; 41 private static Icon menuArrowIcon; 42 43 public static Icon getMenuItemCheckIcon() { 44 if (menuItemCheckIcon == null) { 45 menuItemCheckIcon = new MenuItemCheckIcon(); 46 } 47 return menuItemCheckIcon; 48 } 49 50 public static Icon getMenuItemArrowIcon() { 51 if (menuItemArrowIcon == null) { 52 menuItemArrowIcon = new MenuItemArrowIcon(); 53 } 54 return menuItemArrowIcon; 55 } 56 57 public static Icon getMenuArrowIcon() { 58 if (menuArrowIcon == null) { 59 menuArrowIcon = new MenuArrowIcon(); 60 } 61 return menuArrowIcon; 62 } 63 64 public static Icon getCheckBoxIcon() { 65 if (checkBoxIcon == null) { 66 checkBoxIcon = new CheckBoxIcon(); 67 } 68 return checkBoxIcon; 69 } 70 71 public static Icon getRadioButtonIcon() { 72 if (radioButtonIcon == null) { 73 radioButtonIcon = new RadioButtonIcon(); 74 } 75 return radioButtonIcon; 76 } 77 78 private static class CheckBoxIcon implements Icon, UIResource , Serializable { 79 final static int csize = 13; 80 81 private Color control = UIManager.getColor("control"); 82 private Color foreground = UIManager.getColor("CheckBox.foreground"); 83 private Color shadow = UIManager.getColor("controlShadow"); 84 private Color highlight = UIManager.getColor("controlHighlight"); 85 private Color lightShadow = UIManager.getColor("controlLightShadow"); 86 87 public void paintIcon(Component c, Graphics g, int x, int y) { 88 AbstractButton b = (AbstractButton) c; 89 ButtonModel model = b.getModel(); 90 91 boolean flat = false; 92 93 if(b instanceof JCheckBox) { 94 flat = ((JCheckBox)b).isBorderPaintedFlat(); 95 } 96 97 boolean isPressed = model.isPressed(); 98 boolean isArmed = model.isArmed(); 99 boolean isEnabled = model.isEnabled(); 100 boolean isSelected = model.isSelected(); 101 102 boolean checkToggleIn = ((isPressed && 108 !isArmed && 109 isSelected) || 110 (isPressed && 111 isArmed && 112 !isSelected)); 113 boolean uncheckToggleOut = ((isPressed && 114 !isArmed && 115 !isSelected) || 116 (isPressed && 117 isArmed && 118 isSelected)); 119 120 boolean checkIn = (!isPressed && 121 isArmed && 122 isSelected || 123 (!isPressed && 124 !isArmed && 125 isSelected)); 126 127 128 if(flat) { 129 g.setColor(shadow); 130 g.drawRect(x+2,y,csize-1,csize-1); 131 if(uncheckToggleOut || checkToggleIn) { 132 g.setColor(control); 133 g.fillRect(x+3,y+1,csize-2,csize-2); 134 } 135 } 136 137 x += (MotifGraphicsUtils.isLeftToRight(c)) ? 2 : -3; 139 140 if (checkToggleIn) { 141 drawCheckBezel(g,x,y,csize,true,false,false,flat); 143 } else if (uncheckToggleOut) { 144 drawCheckBezel(g,x,y,csize,true,true,false,flat); 146 } else if (checkIn) { 147 drawCheckBezel(g,x,y,csize,false,false,true,flat); 149 } else if(!flat) { 150 drawCheckBezelOut(g,x,y,csize); 152 } 153 } 154 155 public int getIconWidth() { 156 return csize; 157 } 158 159 public int getIconHeight() { 160 return csize; 161 } 162 163 public void drawCheckBezelOut(Graphics g, int x, int y, int csize){ 164 Color controlShadow = UIManager.getColor("controlShadow"); 165 166 int w = csize; 167 int h = csize; 168 Color oldColor = g.getColor(); 169 170 g.translate(x,y); 171 g.setColor(highlight); g.drawLine(0, 0, 0, h-1); 173 g.drawLine(1, 0, w-1, 0); 174 175 g.setColor(shadow); g.drawLine(1, h-1, w-1, h-1); 177 g.drawLine(w-1, h-1, w-1, 1); 178 g.translate(-x,-y); 179 g.setColor(oldColor); 180 } 181 182 public void drawCheckBezel(Graphics g, int x, int y, int csize, 183 boolean shade, boolean out, boolean check, boolean flat) 184 { 185 186 187 Color oldColor = g.getColor(); 188 g.translate(x, y); 189 190 191 if(!flat) { 193 if (out) { 194 g.setColor(control); 195 g.fillRect(1,1,csize-2,csize-2); 196 g.setColor(shadow); 197 } else { 198 g.setColor(lightShadow); 199 g.fillRect(0,0,csize,csize); 200 g.setColor(highlight); 201 } 202 203 g.drawLine(1,csize-1,csize-2,csize-1); 204 if (shade) { 205 g.drawLine(2,csize-2,csize-3,csize-2); 206 g.drawLine(csize-2,2,csize-2 ,csize-1); 207 if (out) { 208 g.setColor(highlight); 209 } else { 210 g.setColor(shadow); 211 } 212 g.drawLine(1,2,1,csize-2); 213 g.drawLine(1,1,csize-3,1); 214 if (out) { 215 g.setColor(shadow); 216 } else { 217 g.setColor(highlight); 218 } 219 } 220 g.drawLine(csize-1,1,csize-1,csize-1); 222 223 if (out) { 225 g.setColor(highlight); 226 } else { 227 g.setColor(shadow); 228 } 229 g.drawLine(0,1,0,csize-1); 230 231 g.drawLine(0,0,csize-1,0); 233 } 234 235 if (check) { 236 g.setColor(foreground); 238 g.drawLine(csize-2,1,csize-2,2); 239 g.drawLine(csize-3,2,csize-3,3); 240 g.drawLine(csize-4,3,csize-4,4); 241 g.drawLine(csize-5,4,csize-5,6); 242 g.drawLine(csize-6,5,csize-6,8); 243 g.drawLine(csize-7,6,csize-7,10); 244 g.drawLine(csize-8,7,csize-8,10); 245 g.drawLine(csize-9,6,csize-9,9); 246 g.drawLine(csize-10,5,csize-10,8); 247 g.drawLine(csize-11,5,csize-11,7); 248 g.drawLine(csize-12,6,csize-12,6); 249 } 250 g.translate(-x, -y); 251 g.setColor(oldColor); 252 } 253 } 255 private static class RadioButtonIcon implements Icon, UIResource , Serializable { 256 private Color dot = UIManager.getColor("activeCaptionBorder"); 257 private Color highlight = UIManager.getColor("controlHighlight"); 258 private Color shadow = UIManager.getColor("controlShadow"); 259 260 public void paintIcon(Component c, Graphics g, int x, int y) { 261 AbstractButton b = (AbstractButton) c; 263 ButtonModel model = b.getModel(); 264 265 int w = getIconWidth(); 266 int h = getIconHeight(); 267 268 x += (MotifGraphicsUtils.isLeftToRight(c))? 2 : -3; 270 271 boolean isPressed = model.isPressed(); 272 boolean isArmed = model.isArmed(); 273 boolean isEnabled = model.isEnabled(); 274 boolean isSelected = model.isSelected(); 275 276 boolean checkIn = ((isPressed && 277 !isArmed && 278 isSelected) || 279 (isPressed && 280 isArmed && 281 !isSelected) 282 || 283 (!isPressed && 284 isArmed && 285 isSelected || 286 (!isPressed && 287 !isArmed && 288 isSelected))); 289 290 if (checkIn){ 291 g.setColor(shadow); 292 g.drawLine(x+5,y+0,x+8,y+0); 293 g.drawLine(x+3,y+1,x+4,y+1); 294 g.drawLine(x+9,y+1,x+9,y+1); 295 g.drawLine(x+2,y+2,x+2,y+2); 296 g.drawLine(x+1,y+3,x+1,y+3); 297 g.drawLine(x,y+4,x,y+9); 298 g.drawLine(x+1,y+10,x+1,y+10); 299 g.drawLine(x+2,y+11,x+2,y+11); 300 g.setColor(highlight); 301 g.drawLine(x+3,y+12,x+4,y+12); 302 g.drawLine(x+5,y+13,x+8,y+13); 303 g.drawLine(x+9,y+12,x+10,y+12); 304 g.drawLine(x+11,y+11,x+11,y+11); 305 g.drawLine(x+12,y+10,x+12,y+10); 306 g.drawLine(x+13,y+9,x+13,y+4); 307 g.drawLine(x+12,y+3,x+12,y+3); 308 g.drawLine(x+11,y+2,x+11,y+2); 309 g.drawLine(x+10,y+1,x+10,y+1); 310 g.setColor(dot); 311 g.fillRect(x+4,y+5,6,4); 312 g.drawLine(x+5,y+4,x+8,y+4); 313 g.drawLine(x+5,y+9,x+8,y+9); 314 } 315 else { 316 g.setColor(highlight); 317 g.drawLine(x+5,y+0,x+8,y+0); 318 g.drawLine(x+3,y+1,x+4,y+1); 319 g.drawLine(x+9,y+1,x+9,y+1); 320 g.drawLine(x+2,y+2,x+2,y+2); 321 g.drawLine(x+1,y+3,x+1,y+3); 322 g.drawLine(x,y+4,x,y+9); 323 g.drawLine(x+1,y+10,x+1,y+10); 324 g.drawLine(x+2,y+11,x+2,y+11); 325 326 g.setColor(shadow); 327 g.drawLine(x+3,y+12,x+4,y+12); 328 g.drawLine(x+5,y+13,x+8,y+13); 329 g.drawLine(x+9,y+12,x+10,y+12); 330 g.drawLine(x+11,y+11,x+11,y+11); 331 g.drawLine(x+12,y+10,x+12,y+10); 332 g.drawLine(x+13,y+9,x+13,y+4); 333 g.drawLine(x+12,y+3,x+12,y+3); 334 g.drawLine(x+11,y+2,x+11,y+2); 335 g.drawLine(x+10,y+1,x+10,y+1); 336 337 } 338 } 339 340 public int getIconWidth() { 341 return 13; 342 } 343 344 public int getIconHeight() { 345 return 13; 346 } 347 } 349 private static class MenuItemCheckIcon implements Icon, UIResource , Serializable 350 { 351 public void paintIcon(Component c,Graphics g, int x, int y) 352 { 353 } 354 public int getIconWidth() { return 0; } 355 public int getIconHeight() { return 0; } 356 } 358 359 private static class MenuItemArrowIcon implements Icon, UIResource , Serializable 360 { 361 public void paintIcon(Component c,Graphics g, int x, int y) 362 { 363 } 364 public int getIconWidth() { return 0; } 365 public int getIconHeight() { return 0; } 366 } 368 private static class MenuArrowIcon implements Icon, UIResource , Serializable 369 { 370 private Color focus = UIManager.getColor("windowBorder"); 371 private Color shadow = UIManager.getColor("controlShadow"); 372 private Color highlight = UIManager.getColor("controlHighlight"); 373 374 public void paintIcon(Component c, Graphics g, int x, int y) { 375 AbstractButton b = (AbstractButton) c; 376 ButtonModel model = b.getModel(); 377 378 int w = getIconWidth(); 381 int h = getIconHeight(); 382 383 Color oldColor = g.getColor(); 384 385 if (model.isSelected()){ 386 if( MotifGraphicsUtils.isLeftToRight(c) ){ 387 g.setColor(shadow); 388 g.fillRect(x+1,y+1,2,h); 389 g.drawLine(x+4,y+2,x+4,y+2); 390 g.drawLine(x+6,y+3,x+6,y+3); 391 g.drawLine(x+8,y+4,x+8,y+5); 392 g.setColor(focus); 393 g.fillRect(x+2,y+2,2,h-2); 394 g.fillRect(x+4,y+3,2,h-4); 395 g.fillRect(x+6,y+4,2,h-6); 396 g.setColor(highlight); 397 g.drawLine(x+2,y+h,x+2,y+h); 398 g.drawLine(x+4,y+h-1,x+4,y+h-1); 399 g.drawLine(x+6,y+h-2,x+6,y+h-2); 400 g.drawLine(x+8,y+h-4,x+8,y+h-3); 401 } else { 402 g.setColor(highlight); 403 g.fillRect(x+7,y+1,2,10); 404 g.drawLine(x+5,y+9,x+5,y+9); 405 g.drawLine(x+3,y+8,x+3,y+8); 406 g.drawLine(x+1,y+6,x+1,y+7); 407 g.setColor(focus); 408 g.fillRect(x+6,y+2,2,8); 409 g.fillRect(x+4,y+3,2,6); 410 g.fillRect(x+2,y+4,2,4); 411 g.setColor(shadow); 412 g.drawLine(x+1,y+4,x+1,y+5); 413 g.drawLine(x+3,y+3,x+3,y+3); 414 g.drawLine(x+5,y+2,x+5,y+2); 415 g.drawLine(x+7,y+1,x+7,y+1); 416 } 417 } else { 418 if( MotifGraphicsUtils.isLeftToRight(c) ){ 419 g.setColor(highlight); 420 g.drawLine(x+1,y+1,x+1,y+h); 421 g.drawLine(x+2,y+1,x+2,y+h-2); 422 g.fillRect(x+3,y+2,2,2); 423 g.fillRect(x+5,y+3,2,2); 424 g.fillRect(x+7,y+4,2,2); 425 g.setColor(shadow); 426 g.drawLine(x+2,y+h-1,x+2,y+h); 427 g.fillRect(x+3,y+h-2,2,2); 428 g.fillRect(x+5,y+h-3,2,2); 429 g.fillRect(x+7,y+h-4,2,2); 430 g.setColor(oldColor); 431 } else { 432 g.setColor(highlight); 433 g.fillRect(x+1,y+4,2,2); 434 g.fillRect(x+3,y+3,2,2); 435 g.fillRect(x+5,y+2,2,2); 436 g.drawLine(x+7,y+1,x+7,y+2); 437 g.setColor(shadow); 438 g.fillRect(x+1,y+h-4,2,2); 439 g.fillRect(x+3,y+h-3,2,2); 440 g.fillRect(x+5,y+h-2,2,2); 441 g.drawLine(x+7,y+3,x+7,y+h); 442 g.drawLine(x+8,y+1,x+8,y+h); 443 g.setColor(oldColor); 444 } 445 } 446 447 } 448 public int getIconWidth() { return 10; } 449 public int getIconHeight() { return 10; } 450 } } 452 | Popular Tags |