home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Metalworks / src / PropertiesMetalTheme.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  6.1 KB  |  205 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)PropertiesMetalTheme.java    1.8 02/06/13
  38.  */
  39.  
  40.  
  41. import javax.swing.plaf.*;
  42. import javax.swing.plaf.metal.*;
  43. import javax.swing.*;
  44. import javax.swing.border.*;
  45. import java.awt.*;
  46. import java.io.*;
  47. import java.util.*;
  48.  
  49. /**
  50.  * This class allows you to load a theme from a file.
  51.  * It uses the standard Java Properties file format.
  52.  * To create a theme you provide a text file which contains
  53.  * tags corresponding to colors of the theme along with a value
  54.  * for that color.  For example:
  55.  *
  56.  * name=My Ugly Theme
  57.  * primary1=255,0,0
  58.  * primary2=0,255,0
  59.  * primary3=0,0,255
  60.  *
  61.  * This class only loads colors from the properties file,
  62.  * but it could easily be extended to load fonts -  or even icons.
  63.  *
  64.  * @version 1.8 06/13/02
  65.  * @author Steve Wilson
  66.  */
  67. public class PropertiesMetalTheme extends DefaultMetalTheme {
  68.  
  69.     private String name = "Custom Theme";
  70.  
  71.     private ColorUIResource primary1;
  72.     private ColorUIResource primary2;
  73.     private ColorUIResource primary3;
  74.  
  75.     private ColorUIResource secondary1;
  76.     private ColorUIResource secondary2;
  77.     private ColorUIResource secondary3;
  78.  
  79.     private ColorUIResource black;
  80.     private ColorUIResource white;
  81.  
  82.  
  83.     /**
  84.       * pass an inputstream pointing to a properties file.
  85.       * Colors will be initialized to be the same as the DefaultMetalTheme,
  86.       * and then any colors provided in the properties file will override that.
  87.       */
  88.     public PropertiesMetalTheme( InputStream stream ) {
  89.         initColors();
  90.         loadProperties(stream);
  91.     }
  92.  
  93.     /**
  94.       * Initialize all colors to be the same as the DefaultMetalTheme.
  95.       */
  96.     private void initColors() {
  97.         primary1 = super.getPrimary1();
  98.         primary2 = super.getPrimary2();
  99.         primary3 = super.getPrimary3();
  100.  
  101.         secondary1 = super.getSecondary1();
  102.         secondary2 = super.getSecondary2();
  103.         secondary3 = super.getSecondary3();
  104.  
  105.     black = super.getBlack();
  106.     white = super.getWhite();
  107.     }
  108.  
  109.     /**
  110.       * Load the theme name and colors from the properties file
  111.       * Items not defined in the properties file are ignored
  112.       */
  113.     private void loadProperties(InputStream stream) {
  114.     Properties prop = new Properties();
  115.     try {
  116.         prop.load(stream);
  117.     } catch (IOException e) {
  118.         System.out.println(e);
  119.     }
  120.  
  121.     Object tempName = prop.get("name");
  122.     if (tempName != null) {
  123.         name = tempName.toString();
  124.     }
  125.  
  126.     Object colorString = null;
  127.  
  128.     colorString = prop.get("primary1");
  129.     if (colorString != null){
  130.         primary1 = parseColor(colorString.toString());
  131.     }
  132.  
  133.     colorString = prop.get("primary2");
  134.     if (colorString != null) {
  135.         primary2 = parseColor(colorString.toString());
  136.     }
  137.  
  138.     colorString = prop.get("primary3");
  139.     if (colorString != null) {
  140.         primary3 = parseColor(colorString.toString());
  141.     }
  142.  
  143.     colorString = prop.get("secondary1");
  144.     if (colorString != null) {
  145.         secondary1 = parseColor(colorString.toString());
  146.     }
  147.  
  148.     colorString = prop.get("secondary2");
  149.     if (colorString != null) {
  150.         secondary2 = parseColor(colorString.toString());
  151.     }
  152.  
  153.     colorString = prop.get("secondary3");
  154.     if (colorString != null) {
  155.         secondary3 = parseColor(colorString.toString());
  156.     }
  157.  
  158.     colorString = prop.get("black");
  159.     if (colorString != null) {
  160.         black = parseColor(colorString.toString());
  161.     }
  162.  
  163.     colorString = prop.get("white");
  164.     if (colorString != null) {
  165.         white = parseColor(colorString.toString());
  166.     }
  167.  
  168.     }
  169.  
  170.     public String getName() { return name; }
  171.  
  172.     protected ColorUIResource getPrimary1() { return primary1; }
  173.     protected ColorUIResource getPrimary2() { return primary2; }
  174.     protected ColorUIResource getPrimary3() { return primary3; }
  175.  
  176.     protected ColorUIResource getSecondary1() { return secondary1; }
  177.     protected ColorUIResource getSecondary2() { return secondary2; }
  178.     protected ColorUIResource getSecondary3() { return secondary3; }
  179.  
  180.     protected ColorUIResource getBlack() { return black; }
  181.     protected ColorUIResource getWhite() { return white; }
  182.  
  183.     /**
  184.       * parse a comma delimited list of 3 strings into a Color
  185.       */
  186.     private ColorUIResource parseColor(String s) {
  187.         int red = 0;
  188.     int green = 0;
  189.     int blue = 0;
  190.     try {
  191.         StringTokenizer st = new StringTokenizer(s, ",");
  192.  
  193.         red = Integer.parseInt(st.nextToken());
  194.         green = Integer.parseInt(st.nextToken());
  195.         blue = Integer.parseInt(st.nextToken());
  196.  
  197.     } catch (Exception e) {
  198.         System.out.println(e);
  199.         System.out.println("Couldn't parse color :" + s);
  200.     }
  201.  
  202.     return new ColorUIResource(red, green, blue);
  203.     }
  204. }
  205.