1 /* 2 * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.java.swing.plaf.motif; 27 28 import javax.swing.*; 29 30 import javax.swing.plaf.UIResource; 31 32 import java.awt.Color; 33 import java.awt.Component; 34 import java.awt.Dimension; 35 import java.awt.Graphics; 36 import java.awt.Polygon; 37 38 import java.io.Serializable; 39 40 /** 41 * Icon factory for the CDE/Motif Look and Feel 42 * <p> 43 * <strong>Warning:</strong> 44 * Serialized objects of this class will not be compatible with 45 * future Swing releases. The current serialization support is appropriate 46 * for short term storage or RMI between applications running the same 47 * version of Swing. A future release of Swing will provide support for 48 * long term persistence. 49 * 50 * 1.20 04/27/99 51 * @author Georges Saab 52 */ 53 public class MotifIconFactory implements Serializable 54 { 55 private static Icon checkBoxIcon; 56 private static Icon radioButtonIcon; 57 private static Icon menuItemCheckIcon; 58 private static Icon menuItemArrowIcon; 59 private static Icon menuArrowIcon; 60 61 public static Icon getMenuItemCheckIcon() { 62 return null; 63 } 64 65 public static Icon getMenuItemArrowIcon() { 66 if (menuItemArrowIcon == null) { 67 menuItemArrowIcon = new MenuItemArrowIcon(); 68 } 69 return menuItemArrowIcon; 70 } 71 72 public static Icon getMenuArrowIcon() { 73 if (menuArrowIcon == null) { 74 menuArrowIcon = new MenuArrowIcon(); 75 } 76 return menuArrowIcon; 77 } 78 79 public static Icon getCheckBoxIcon() { 80 if (checkBoxIcon == null) { 81 checkBoxIcon = new CheckBoxIcon(); 82 } 83 return checkBoxIcon; 84 } 85 86 public static Icon getRadioButtonIcon() { 87 if (radioButtonIcon == null) { 88 radioButtonIcon = new RadioButtonIcon(); 89 } 90 return radioButtonIcon; 91 } 92 93 private static class CheckBoxIcon implements Icon, UIResource, Serializable { 94 final static int csize = 13; 95 96 private Color control = UIManager.getColor("control"); 97 private Color foreground = UIManager.getColor("CheckBox.foreground"); 98 private Color shadow = UIManager.getColor("controlShadow"); 99 private Color highlight = UIManager.getColor("controlHighlight"); 100 private Color lightShadow = UIManager.getColor("controlLightShadow"); 101 102 public void paintIcon(Component c, Graphics g, int x, int y) { 103 AbstractButton b = (AbstractButton) c; 104 ButtonModel model = b.getModel(); 105 106 boolean flat = false; 107 108 if(b instanceof JCheckBox) { 109 flat = ((JCheckBox)b).isBorderPaintedFlat(); 110 } 111 112 boolean isPressed = model.isPressed(); 113 boolean isArmed = model.isArmed(); 114 boolean isEnabled = model.isEnabled(); 115 boolean isSelected = model.isSelected(); 116 117 // There are 4 "looks" to the Motif CheckBox: 118 // drawCheckBezelOut - default unchecked state 119 // drawBezel - when we uncheck in toggled state 120 // drawCheckBezel - when we check in toggle state 121 // drawCheckBezelIn - selected, mouseReleased 122 boolean checkToggleIn = ((isPressed && 123 !isArmed && 124 isSelected) || 125 (isPressed && 126 isArmed && 127 !isSelected)); 128 boolean uncheckToggleOut = ((isPressed && 129 !isArmed && 130 !isSelected) || 131 (isPressed && 132 isArmed && 133 isSelected)); 134 135 boolean checkIn = (!isPressed && 136 isArmed && 137 isSelected || 138 (!isPressed && 139 !isArmed && 140 isSelected)); 141 142 143 if(flat) { 144 g.setColor(shadow); 145 g.drawRect(x+2,y,csize-1,csize-1); 146 if(uncheckToggleOut || checkToggleIn) { 147 g.setColor(control); 148 g.fillRect(x+3,y+1,csize-2,csize-2); 149 } 150 } 151 152 if (checkToggleIn) { 153 // toggled from unchecked to checked 154 drawCheckBezel(g,x,y,csize,true,false,false,flat); 155 } else if (uncheckToggleOut) { 156 // MotifBorderFactory.drawBezel(g,x,y,csize,csize,false,false); 157 drawCheckBezel(g,x,y,csize,true,true,false,flat); 158 } else if (checkIn) { 159 // show checked, unpressed state 160 drawCheckBezel(g,x,y,csize,false,false,true,flat); 161 } else if(!flat) { 162 // show unchecked state 163 drawCheckBezelOut(g,x,y,csize); 164 } 165 } 166 167 public int getIconWidth() { 168 return csize; 169 } 170 171 public int getIconHeight() { 172 return csize; 173 } 174 175 public void drawCheckBezelOut(Graphics g, int x, int y, int csize){ 176 Color controlShadow = UIManager.getColor("controlShadow"); 177 178 int w = csize; 179 int h = csize; 180 Color oldColor = g.getColor(); 181 182 g.translate(x,y); 183 g.setColor(highlight); // inner 3D border 184 g.drawLine(0, 0, 0, h-1); 185 g.drawLine(1, 0, w-1, 0); 186 187 g.setColor(shadow); // black drop shadow __| 188 g.drawLine(1, h-1, w-1, h-1); 189 g.drawLine(w-1, h-1, w-1, 1); 190 g.translate(-x,-y); 191 g.setColor(oldColor); 192 } 193 194 public void drawCheckBezel(Graphics g, int x, int y, int csize, 195 boolean shade, boolean out, boolean check, boolean flat) 196 { 197 198 199 Color oldColor = g.getColor(); 200 g.translate(x, y); 201 202 203 //bottom 204 if(!flat) { 205 if (out) { 206 g.setColor(control); 207 g.fillRect(1,1,csize-2,csize-2); 208 g.setColor(shadow); 209 } else { 210 g.setColor(lightShadow); 211 g.fillRect(0,0,csize,csize); 212 g.setColor(highlight); 213 } 214 215 g.drawLine(1,csize-1,csize-2,csize-1); 216 if (shade) { 217 g.drawLine(2,csize-2,csize-3,csize-2); 218 g.drawLine(csize-2,2,csize-2 ,csize-1); 219 if (out) { 220 g.setColor(highlight); 221 } else { 222 g.setColor(shadow); 223 } 224 g.drawLine(1,2,1,csize-2); 225 g.drawLine(1,1,csize-3,1); 226 if (out) { 227 g.setColor(shadow); 228 } else { 229 g.setColor(highlight); 230 } 231 } 232 //right 233 g.drawLine(csize-1,1,csize-1,csize-1); 234 235 //left 236 if (out) { 237 g.setColor(highlight); 238 } else { 239 g.setColor(shadow); 240 } 241 g.drawLine(0,1,0,csize-1); 242 243 //top 244 g.drawLine(0,0,csize-1,0); 245 } 246 247 if (check) { 248 // draw check 249 g.setColor(foreground); 250 g.drawLine(csize-2,1,csize-2,2); 251 g.drawLine(csize-3,2,csize-3,3); 252 g.drawLine(csize-4,3,csize-4,4); 253 g.drawLine(csize-5,4,csize-5,6); 254 g.drawLine(csize-6,5,csize-6,8); 255 g.drawLine(csize-7,6,csize-7,10); 256 g.drawLine(csize-8,7,csize-8,10); 257 g.drawLine(csize-9,6,csize-9,9); 258 g.drawLine(csize-10,5,csize-10,8); 259 g.drawLine(csize-11,5,csize-11,7); 260 g.drawLine(csize-12,6,csize-12,6); 261 } 262 g.translate(-x, -y); 263 g.setColor(oldColor); 264 } 265 } // end class CheckBoxIcon 266 267 private static class RadioButtonIcon implements Icon, UIResource, Serializable { 268 private Color dot = UIManager.getColor("activeCaptionBorder"); 269 private Color highlight = UIManager.getColor("controlHighlight"); 270 private Color shadow = UIManager.getColor("controlShadow"); 271 272 public void paintIcon(Component c, Graphics g, int x, int y) { 273 // fill interior 274 AbstractButton b = (AbstractButton) c; 275 ButtonModel model = b.getModel(); 276 277 int w = getIconWidth(); 278 int h = getIconHeight(); 279 280 boolean isPressed = model.isPressed(); 281 boolean isArmed = model.isArmed(); 282 boolean isEnabled = model.isEnabled(); 283 boolean isSelected = model.isSelected(); 284 285 boolean checkIn = ((isPressed && 286 !isArmed && 287 isSelected) || 288 (isPressed && 289 isArmed && 290 !isSelected) 291 || 292 (!isPressed && 293 isArmed && 294 isSelected || 295 (!isPressed && 296 !isArmed && 297 isSelected))); 298 299 if (checkIn){ 300 g.setColor(shadow); 301 g.drawLine(x+5,y+0,x+8,y+0); 302 g.drawLine(x+3,y+1,x+4,y+1); 303 g.drawLine(x+9,y+1,x+9,y+1); 304 g.drawLine(x+2,y+2,x+2,y+2); 305 g.drawLine(x+1,y+3,x+1,y+3); 306 g.drawLine(x,y+4,x,y+9); 307 g.drawLine(x+1,y+10,x+1,y+10); 308 g.drawLine(x+2,y+11,x+2,y+11); 309 g.setColor(highlight); 310 g.drawLine(x+3,y+12,x+4,y+12); 311 g.drawLine(x+5,y+13,x+8,y+13); 312 g.drawLine(x+9,y+12,x+10,y+12); 313 g.drawLine(x+11,y+11,x+11,y+11); 314 g.drawLine(x+12,y+10,x+12,y+10); 315 g.drawLine(x+13,y+9,x+13,y+4); 316 g.drawLine(x+12,y+3,x+12,y+3); 317 g.drawLine(x+11,y+2,x+11,y+2); 318 g.drawLine(x+10,y+1,x+10,y+1); 319 g.setColor(dot); 320 g.fillRect(x+4,y+5,6,4); 321 g.drawLine(x+5,y+4,x+8,y+4); 322 g.drawLine(x+5,y+9,x+8,y+9); 323 } 324 else { 325 g.setColor(highlight); 326 g.drawLine(x+5,y+0,x+8,y+0); 327 g.drawLine(x+3,y+1,x+4,y+1); 328 g.drawLine(x+9,y+1,x+9,y+1); 329 g.drawLine(x+2,y+2,x+2,y+2); 330 g.drawLine(x+1,y+3,x+1,y+3); 331 g.drawLine(x,y+4,x,y+9); 332 g.drawLine(x+1,y+10,x+1,y+10); 333 g.drawLine(x+2,y+11,x+2,y+11); 334 335 g.setColor(shadow); 336 g.drawLine(x+3,y+12,x+4,y+12); 337 g.drawLine(x+5,y+13,x+8,y+13); 338 g.drawLine(x+9,y+12,x+10,y+12); 339 g.drawLine(x+11,y+11,x+11,y+11); 340 g.drawLine(x+12,y+10,x+12,y+10); 341 g.drawLine(x+13,y+9,x+13,y+4); 342 g.drawLine(x+12,y+3,x+12,y+3); 343 g.drawLine(x+11,y+2,x+11,y+2); 344 g.drawLine(x+10,y+1,x+10,y+1); 345 346 } 347 } 348 349 public int getIconWidth() { 350 return 14; 351 } 352 353 public int getIconHeight() { 354 return 14; 355 } 356 } // end class RadioButtonIcon 357 358 private static class MenuItemCheckIcon implements Icon, UIResource, Serializable 359 { 360 public void paintIcon(Component c,Graphics g, int x, int y) 361 { 362 } 363 public int getIconWidth() { return 0; } 364 public int getIconHeight() { return 0; } 365 } // end class MenuItemCheckIcon 366 367 368 private static class MenuItemArrowIcon implements Icon, UIResource, Serializable 369 { 370 public void paintIcon(Component c,Graphics g, int x, int y) 371 { 372 } 373 public int getIconWidth() { return 0; } 374 public int getIconHeight() { return 0; } 375 } // end class MenuItemArrowIcon 376 377 private static class MenuArrowIcon implements Icon, UIResource, Serializable 378 { 379 private Color focus = UIManager.getColor("windowBorder"); 380 private Color shadow = UIManager.getColor("controlShadow"); 381 private Color highlight = UIManager.getColor("controlHighlight"); 382 383 public void paintIcon(Component c, Graphics g, int x, int y) { 384 AbstractButton b = (AbstractButton) c; 385 ButtonModel model = b.getModel(); 386 387 // These variables are kind of pointless as the following code 388 // assumes the icon will be 10 x 10 regardless of their value. 389 int w = getIconWidth(); 390 int h = getIconHeight(); 391 392 Color oldColor = g.getColor(); 393 394 if (model.isSelected()){ 395 if( MotifGraphicsUtils.isLeftToRight(c) ){ 396 g.setColor(shadow); 397 g.fillRect(x+1,y+1,2,h); 398 g.drawLine(x+4,y+2,x+4,y+2); 399 g.drawLine(x+6,y+3,x+6,y+3); 400 g.drawLine(x+8,y+4,x+8,y+5); 401 g.setColor(focus); 402 g.fillRect(x+2,y+2,2,h-2); 403 g.fillRect(x+4,y+3,2,h-4); 404 g.fillRect(x+6,y+4,2,h-6); 405 g.setColor(highlight); 406 g.drawLine(x+2,y+h,x+2,y+h); 407 g.drawLine(x+4,y+h-1,x+4,y+h-1); 408 g.drawLine(x+6,y+h-2,x+6,y+h-2); 409 g.drawLine(x+8,y+h-4,x+8,y+h-3); 410 } else { 411 g.setColor(highlight); 412 g.fillRect(x+7,y+1,2,10); 413 g.drawLine(x+5,y+9,x+5,y+9); 414 g.drawLine(x+3,y+8,x+3,y+8); 415 g.drawLine(x+1,y+6,x+1,y+7); 416 g.setColor(focus); 417 g.fillRect(x+6,y+2,2,8); 418 g.fillRect(x+4,y+3,2,6); 419 g.fillRect(x+2,y+4,2,4); 420 g.setColor(shadow); 421 g.drawLine(x+1,y+4,x+1,y+5); 422 g.drawLine(x+3,y+3,x+3,y+3); 423 g.drawLine(x+5,y+2,x+5,y+2); 424 g.drawLine(x+7,y+1,x+7,y+1); 425 } 426 } else { 427 if( MotifGraphicsUtils.isLeftToRight(c) ){ 428 g.setColor(highlight); 429 g.drawLine(x+1,y+1,x+1,y+h); 430 g.drawLine(x+2,y+1,x+2,y+h-2); 431 g.fillRect(x+3,y+2,2,2); 432 g.fillRect(x+5,y+3,2,2); 433 g.fillRect(x+7,y+4,2,2); 434 g.setColor(shadow); 435 g.drawLine(x+2,y+h-1,x+2,y+h); 436 g.fillRect(x+3,y+h-2,2,2); 437 g.fillRect(x+5,y+h-3,2,2); 438 g.fillRect(x+7,y+h-4,2,2); 439 g.setColor(oldColor); 440 } else { 441 g.setColor(highlight); 442 g.fillRect(x+1,y+4,2,2); 443 g.fillRect(x+3,y+3,2,2); 444 g.fillRect(x+5,y+2,2,2); 445 g.drawLine(x+7,y+1,x+7,y+2); 446 g.setColor(shadow); 447 g.fillRect(x+1,y+h-4,2,2); 448 g.fillRect(x+3,y+h-3,2,2); 449 g.fillRect(x+5,y+h-2,2,2); 450 g.drawLine(x+7,y+3,x+7,y+h); 451 g.drawLine(x+8,y+1,x+8,y+h); 452 g.setColor(oldColor); 453 } 454 } 455 456 } 457 public int getIconWidth() { return 10; } 458 public int getIconHeight() { return 10; } 459 } // End class MenuArrowIcon 460 }