1 14 package org.compiere.plaf; 15 16 import java.awt.Color ; 17 import java.awt.Font ; 18 import java.awt.SystemColor ; 19 import java.io.FileInputStream ; 20 import java.io.FileOutputStream ; 21 import java.util.Properties ; 22 23 import javax.swing.plaf.ColorUIResource ; 24 import javax.swing.plaf.FontUIResource ; 25 import javax.swing.plaf.metal.MetalTheme ; 26 27 import org.compiere.util.Ini; 28 29 import sun.awt.AppContext; 30 31 38 public class CompiereTheme extends MetalTheme 39 { 40 43 public CompiereTheme() 44 { 45 } 47 51 public String getName() 52 { 53 return NAME; 54 } 56 public static final String NAME = "Compiere Theme"; 57 58 59 protected static ColorUIResource primary0 = new ColorUIResource (51, 51, 102); 60 61 protected static ColorUIResource primary1; 62 63 protected static ColorUIResource primary2; 64 65 protected static ColorUIResource primary3; 66 67 68 protected final ColorUIResource secondary0 = new ColorUIResource (0, 0, 0); 69 70 protected static ColorUIResource secondary1; 71 72 protected static ColorUIResource secondary2; 73 74 protected static ColorUIResource secondary3; 75 76 protected final ColorUIResource secondary4 = new ColorUIResource (255, 255, 255); 77 78 79 protected static ColorUIResource black; 80 81 protected static ColorUIResource white; 82 83 84 protected static ColorUIResource mandatory; 85 86 protected static ColorUIResource error; 87 88 protected static ColorUIResource inactive; 89 90 protected static ColorUIResource info; 91 92 93 protected static ColorUIResource txt_ok; 94 95 protected static ColorUIResource txt_error; 96 97 98 protected static FontUIResource controlFont; 99 100 protected static FontUIResource systemFont; 101 102 protected static FontUIResource userFont; 103 104 protected static FontUIResource smallFont; 105 106 protected static FontUIResource windowFont; 107 108 protected static FontUIResource menuFont; 109 110 111 public static final String FONT_DEFAULT = "Dialog"; 112 113 public static final int FONT_SIZE = 12; 114 115 118 private static void setDefault () 119 { 120 121 primary1 = new ColorUIResource (102, 102, 153); 122 123 primary2 = new ColorUIResource (153, 153, 204); 124 125 primary3 = new ColorUIResource (204, 204, 255); 126 127 secondary1 = new ColorUIResource (102, 102, 102); 128 129 secondary2 = new ColorUIResource (153, 153, 153); 130 131 secondary3 = new ColorUIResource (204, 204, 204); 132 133 134 black = new ColorUIResource (Color.black); 135 136 white = new ColorUIResource (Color.white); 137 138 139 mandatory = new ColorUIResource (224, 224, 255); 141 error = new ColorUIResource (255, 204, 204); 143 inactive = new ColorUIResource (234, 234, 234); 145 info = new ColorUIResource (253, 237, 207); 147 148 txt_ok = new ColorUIResource (51, 51, 102); 150 txt_error = new ColorUIResource (204, 0, 0); 152 153 controlFont = null; 154 _getControlTextFont(); 155 156 systemFont = null; 157 _getSystemTextFont(); 158 159 userFont = null; 160 _getUserTextFont(); 161 162 smallFont = null; 163 _getSubTextFont(); 164 165 windowFont = null; 166 _getWindowTitleFont(); 167 168 menuFont = null; 169 _getMenuTextFont(); 170 } 172 175 static 176 { 177 setDefault(); 178 } 179 180 183 public static void setTheme () 184 { 185 AppContext context = AppContext.getAppContext(); 186 MetalTheme copyFrom = (MetalTheme )context.get("currentMetalTheme"); 187 setTheme (copyFrom); 188 } 190 194 public static void setTheme (MetalTheme copyFrom) 195 { 196 if (copyFrom == null || copyFrom instanceof CompiereTheme) 197 return; 198 primary1 = copyFrom.getPrimaryControlDarkShadow(); 200 primary2 = copyFrom.getPrimaryControlShadow(); 201 primary3 = copyFrom.getPrimaryControl(); 202 secondary1 = copyFrom.getControlDarkShadow(); 203 secondary2 = copyFrom.getControlShadow(); 204 secondary3 = copyFrom.getControl(); 205 CompierePanelUI.setDefaultBackground(new CompiereColor(secondary3, true)); 206 white = copyFrom.getPrimaryControlHighlight(); 207 black = copyFrom.getPrimaryControlInfo(); 208 controlFont = copyFrom.getControlTextFont(); 210 systemFont = copyFrom.getSystemTextFont(); 211 userFont = copyFrom.getUserTextFont(); 212 smallFont = copyFrom.getSubTextFont(); 213 menuFont = copyFrom.getMenuTextFont(); 214 windowFont = copyFrom.getWindowTitleFont(); 215 } 217 218 219 220 224 public ColorUIResource getPrimary1() 225 { 226 return ColorBlind.getDichromatColorUIResource(primary1); 227 } 228 public ColorUIResource getPrimary2() 229 { 230 return ColorBlind.getDichromatColorUIResource(primary2); 231 } 232 public ColorUIResource getPrimary3() 233 { 234 return ColorBlind.getDichromatColorUIResource(primary3); 235 } 236 237 241 public ColorUIResource getSecondary0() 242 { 243 return ColorBlind.getDichromatColorUIResource(secondary0); 244 } 245 public ColorUIResource getSecondary1() 246 { 247 return ColorBlind.getDichromatColorUIResource(secondary1); 248 } 249 public ColorUIResource getSecondary2() 250 { 251 return ColorBlind.getDichromatColorUIResource(secondary2); 252 } 253 public ColorUIResource getSecondary3() 254 { 255 return ColorBlind.getDichromatColorUIResource(secondary3); 256 } 257 public ColorUIResource getSecondary4() 258 { 259 return ColorBlind.getDichromatColorUIResource(secondary4); 260 } 261 262 public ColorUIResource getBlack() 263 { 264 return ColorBlind.getDichromatColorUIResource(black); 265 } 266 public ColorUIResource getWhite() 267 { 268 return ColorBlind.getDichromatColorUIResource(white); 269 } 270 271 275 private static FontUIResource _getControlTextFont() 276 { 277 if (controlFont == null) 278 { 279 try 280 { 281 controlFont = new FontUIResource (Font.getFont("swing.plaf.metal.controlFont", 282 new Font (FONT_DEFAULT, Font.PLAIN, FONT_SIZE))); 283 } 284 catch (Exception e) 285 { 286 controlFont = new FontUIResource (FONT_DEFAULT, Font.PLAIN, FONT_SIZE); 287 } 288 } 289 return controlFont; 290 } 291 public FontUIResource getControlTextFont() {return _getControlTextFont();} 292 293 297 private static FontUIResource _getSystemTextFont() 298 { 299 if (systemFont == null) 300 { 301 try 302 { 303 systemFont = new FontUIResource (Font.getFont("swing.plaf.metal.systemFont", 304 new Font (FONT_DEFAULT, Font.PLAIN, FONT_SIZE))); 305 } 306 catch (Exception e) 307 { 308 systemFont = new FontUIResource (FONT_DEFAULT, Font.PLAIN, FONT_SIZE); 309 } 310 } 311 return systemFont; 312 } 313 public FontUIResource getSystemTextFont() {return _getSystemTextFont();} 314 315 319 private static FontUIResource _getUserTextFont() 320 { 321 if (userFont == null) 322 { 323 try 324 { 325 userFont = new FontUIResource (Font.getFont("swing.plaf.metal.userFont", 326 new Font (FONT_DEFAULT, Font.PLAIN, FONT_SIZE))); 327 } 328 catch (Exception e) 329 { 330 userFont = new FontUIResource (FONT_DEFAULT, Font.PLAIN, FONT_SIZE); 331 } 332 } 333 return userFont; 334 } 335 public FontUIResource getUserTextFont() {return _getUserTextFont();} 336 337 341 private static FontUIResource _getMenuTextFont() 342 { 343 if (menuFont == null) 344 { 345 try 346 { 347 menuFont = new FontUIResource (Font.getFont("swing.plaf.metal.menuFont", 348 new Font (FONT_DEFAULT, Font.PLAIN, FONT_SIZE))); 349 } 350 catch (Exception e) 351 { 352 menuFont = new FontUIResource (FONT_DEFAULT, Font.PLAIN, FONT_SIZE); 353 } 354 } 355 return menuFont; 356 } 357 public FontUIResource getMenuTextFont() {return _getMenuTextFont();} 358 359 363 private static FontUIResource _getWindowTitleFont() 364 { 365 if (windowFont == null) 366 { 367 try 368 { 369 windowFont = new FontUIResource (Font.getFont("swing.plaf.metal.windowFont", 370 new Font (FONT_DEFAULT, Font.BOLD, FONT_SIZE+2))); 371 } 372 catch (Exception e) 373 { 374 windowFont = new FontUIResource (FONT_DEFAULT, Font.BOLD, FONT_SIZE+2); 375 } 376 } 377 return windowFont; 378 } 379 public FontUIResource getWindowTitleFont() {return _getWindowTitleFont();} 380 381 385 private static FontUIResource _getSubTextFont() 386 { 387 if (smallFont == null) 388 { 389 try 390 { 391 smallFont = new FontUIResource (Font.getFont("swing.plaf.metal.smallFont", 392 new Font (FONT_DEFAULT, Font.PLAIN, FONT_SIZE-2))); 393 } 394 catch (Exception e) 395 { 396 smallFont = new FontUIResource (FONT_DEFAULT, Font.PLAIN, FONT_SIZE-2); 397 } 398 } 399 return smallFont; 400 } 401 public FontUIResource getSubTextFont() {return _getSubTextFont();} 402 403 404 405 409 public static void save (String fileName) 410 { 411 Properties p = new Properties (); 412 save (p); 413 try 415 { 416 FileOutputStream fos = new FileOutputStream (fileName); 417 p.store(fos, NAME); 418 fos.close(); 419 } 420 catch (Exception e) 421 { 422 System.err.println("CompiereTheme.store - " + e.getMessage()); 423 } 424 } 426 430 public static void load (String fileName) 431 { 432 Properties p = new Properties (); 433 try 434 { 435 FileInputStream fis = new FileInputStream (fileName); 436 p.load(fis); 437 fis.close(); 438 } 439 catch (Exception e) 440 { 441 System.err.println("CompiereTheme.load - " + e.getMessage()); 442 return; 443 } 444 load (p); 445 } 447 449 private static final String P_Primary1 = "#ColorPrimary1"; 450 private static final String P_Primary2 = "#ColorPrimary2"; 451 private static final String P_Primary3 = "#ColorPrimary3"; 452 private static final String P_Secondary1 = "#ColorSecondary1"; 453 private static final String P_Secondary2 = "#ColorSecondary2"; 454 private static final String P_Secondary3 = "#ColorSecondary3"; 455 private static final String P_Black = "#ColorBlack"; 456 private static final String P_White = "#ColorWhite"; 457 private static final String P_Error = "#ColorError"; 458 private static final String P_Info = "#ColorInfo"; 459 private static final String P_Mandatory = "#ColorMandatory"; 460 private static final String P_Inactive = "#ColorInactive"; 461 private static final String P_Txt_OK = "#ColorTextOK"; 462 private static final String P_Txt_Error = "#ColorTextError"; 463 private static final String P_Control = "#FontControl"; 465 private static final String P_System = "#FontSystem"; 466 private static final String P_User = "#FontUser"; 467 private static final String P_Small = "#FontSmall"; 468 private static final String P_Window = "#FontWindow"; 469 private static final String P_Menu = "#FontMenu"; 470 protected static final String P_CompiereColor = "#CompiereColor"; 472 473 477 public static void save (Properties p) 478 { 479 p.setProperty(P_Primary1, getColorAsString(primary1)); 480 p.setProperty(P_Primary2, getColorAsString(primary2)); 481 p.setProperty(P_Primary3, getColorAsString(primary3)); 482 p.setProperty(P_Secondary1, getColorAsString(secondary1)); 483 p.setProperty(P_Secondary2, getColorAsString(secondary2)); 484 p.setProperty(P_Secondary3, getColorAsString(secondary3)); 485 p.setProperty(P_Error, getColorAsString(error)); 486 p.setProperty(P_Info, getColorAsString(info)); 487 p.setProperty(P_Mandatory, getColorAsString(mandatory)); 488 p.setProperty(P_Inactive, getColorAsString(inactive)); 489 p.setProperty(P_White, getColorAsString(white)); 490 p.setProperty(P_Black, getColorAsString(black)); 491 p.setProperty(P_Txt_OK, getColorAsString(txt_ok)); 492 p.setProperty(P_Txt_Error, getColorAsString(txt_error)); 493 p.setProperty(P_Control, ((Font )controlFont).toString()); 495 p.setProperty(P_System, ((Font )systemFont).toString()); 496 p.setProperty(P_User, ((Font )userFont).toString()); 497 p.setProperty(P_Small, ((Font )smallFont).toString()); 498 p.setProperty(P_Window, ((Font )windowFont).toString()); 499 p.setProperty(P_Menu, ((Font )menuFont).toString()); 500 p.setProperty(P_CompiereColor, CompiereColor.getDefaultBackground().toString()); 502 } 504 511 public static String getColorAsString (Color c) 512 { 513 if (c == null) 514 c = SystemColor.control; 515 StringBuffer sb = new StringBuffer ("[r=").append(c.getRed()) 516 .append(",g=").append(c.getGreen()) 517 .append(",b=").append(c.getBlue()) 518 .append(",a=").append(c.getAlpha()) 519 .append("]"); 520 return sb.toString(); 522 } 524 527 public static void load () 528 { 529 load (Ini.getProperties()); 530 } 532 536 public static void load (Properties p) 537 { 538 primary1 = parseColor (p.getProperty(P_Primary1), primary1); 539 primary2 = parseColor (p.getProperty(P_Primary2), primary2); 540 primary3 = parseColor (p.getProperty(P_Primary3), primary3); 541 secondary1 = parseColor (p.getProperty(P_Secondary1), secondary1); 542 secondary2 = parseColor (p.getProperty(P_Secondary2), secondary2); 543 secondary3 = parseColor (p.getProperty(P_Secondary3), secondary3); 544 error = parseColor(p.getProperty(P_Error), error); 545 info = parseColor(p.getProperty(P_Info), info); 546 mandatory = parseColor(p.getProperty(P_Mandatory), mandatory); 547 inactive = parseColor(p.getProperty(P_Inactive), inactive); 548 white = parseColor(p.getProperty(P_White), white); 549 black = parseColor(p.getProperty(P_Black), black); 550 txt_ok = parseColor(p.getProperty(P_Txt_OK), txt_ok); 551 txt_error = parseColor(p.getProperty(P_Txt_Error), txt_error); 552 controlFont = parseFont(p.getProperty(P_Control), controlFont); 554 systemFont = parseFont(p.getProperty(P_System), systemFont); 555 userFont = parseFont(p.getProperty(P_User), userFont); 556 smallFont = parseFont(p.getProperty(P_Small), smallFont); 557 windowFont = parseFont(p.getProperty(P_Window), windowFont); 558 menuFont = parseFont(p.getProperty(P_Menu), menuFont); 559 CompiereColor.setDefaultBackground(CompiereColor.parse(p.getProperty(P_CompiereColor))); 561 } 563 567 public static void reset (Properties p) 568 { 569 p.remove (P_Primary1); 570 p.remove (P_Primary2); 571 p.remove (P_Primary3); 572 p.remove (P_Secondary1); 573 p.remove (P_Secondary2); 574 p.remove (P_Secondary3); 575 p.remove (P_Error); 576 p.remove (P_Info); 577 p.remove (P_Mandatory); 578 p.remove (P_Inactive); 579 p.remove (P_White); 580 p.remove (P_Black); 581 p.remove (P_Txt_OK); 582 p.remove (P_Txt_Error); 583 p.remove (P_Control); 585 p.remove (P_System); 586 p.remove (P_User); 587 p.remove (P_Small); 588 p.remove (P_Window); 589 p.remove (P_Menu); 590 p.remove(P_CompiereColor); 592 593 p.setProperty(Ini.P_UI_LOOK, CompiereLookAndFeel.NAME); 595 p.setProperty(Ini.P_UI_THEME, NAME); 596 setDefault(); 598 599 CompiereColor cc = new CompiereColor(secondary3, false); 603 CompiereColor.setDefaultBackground (cc); 604 save(p); } 608 618 protected static ColorUIResource parseColor (String information, ColorUIResource stdColor) 619 { 620 if (information == null || information.length() == 0) 621 return stdColor; 622 try 624 { 625 int r = Integer.parseInt(information.substring(information.indexOf("r=")+2, information.indexOf(",g="))); 626 int g = Integer.parseInt(information.substring(information.indexOf("g=")+2, information.indexOf(",b="))); 627 int b = 0; 628 int a = 255; 629 if (information.indexOf("a=") == -1) 630 b = Integer.parseInt(information.substring(information.indexOf("b=")+2, information.indexOf("]"))); 631 else 632 { 633 b = Integer.parseInt(information.substring(information.indexOf("b=")+2, information.indexOf(",a="))); 634 a = Integer.parseInt(information.substring(information.indexOf("a=")+2, information.indexOf("]"))); 635 } 636 ColorUIResource retValue = new ColorUIResource (new Color (r, g, b, a)); 637 return retValue; 639 } 640 catch (Exception e) 641 { 642 System.err.println("ParseColor=" + information + " - cannot parse - " + e.toString()); 643 } 644 return stdColor; 645 } 647 656 private static FontUIResource parseFont(String information, FontUIResource stdFont) 657 { 658 if (information == null) 659 return stdFont; 660 try 662 { 663 String name = information.substring(information.indexOf("name=")+5, information.indexOf(",style=")); 664 String s = information.substring(information.indexOf("style=")+6, information.indexOf(",size=")); 665 int style = Font.PLAIN; 666 if (s.equals("bold")) 667 style = Font.BOLD; 668 else if (s.equals("italic")) 669 style = Font.ITALIC; 670 else if (s.equals("bolditalic")) 671 style = Font.BOLD | Font.ITALIC; 672 int size = Integer.parseInt(information.substring(information.indexOf(",size=")+6, information.lastIndexOf("]"))); 673 FontUIResource retValue = new FontUIResource (name,style,size); 674 return retValue; 676 } 677 catch (Exception e) 678 { 679 System.err.println("ParseFont=" + information + " - cannot parse - " + e.toString()); 680 } 681 return stdFont; 682 } 684 } | Popular Tags |