0N/A/*
3835N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A
3835N/Aimport java.io.IOException;
3835N/Aimport java.io.InputStream;
3835N/Aimport java.util.Properties;
3835N/Aimport java.util.StringTokenizer;
3835N/A
3835N/Aimport javax.swing.plaf.ColorUIResource;
3835N/Aimport javax.swing.plaf.metal.DefaultMetalTheme;
3835N/A
0N/A
0N/A/**
0N/A * This class allows you to load a theme from a file.
0N/A * It uses the standard Java Properties file format.
0N/A * To create a theme you provide a text file which contains
0N/A * tags corresponding to colors of the theme along with a value
0N/A * for that color. For example:
0N/A *
0N/A * name=My Ugly Theme
0N/A * primary1=255,0,0
0N/A * primary2=0,255,0
0N/A * primary3=0,0,255
0N/A *
0N/A * This class only loads colors from the properties file,
0N/A * but it could easily be extended to load fonts - or even icons.
0N/A *
0N/A * @author Steve Wilson
3835N/A * @author Alexander Kouznetsov
0N/A */
0N/Apublic class PropertiesMetalTheme extends DefaultMetalTheme {
0N/A
0N/A private String name = "Custom Theme";
0N/A private ColorUIResource primary1;
0N/A private ColorUIResource primary2;
0N/A private ColorUIResource primary3;
0N/A private ColorUIResource secondary1;
0N/A private ColorUIResource secondary2;
0N/A private ColorUIResource secondary3;
0N/A private ColorUIResource black;
0N/A private ColorUIResource white;
0N/A
0N/A /**
3835N/A * pass an inputstream pointing to a properties file.
3835N/A * Colors will be initialized to be the same as the DefaultMetalTheme,
3835N/A * and then any colors provided in the properties file will override that.
3835N/A */
3835N/A public PropertiesMetalTheme(InputStream stream) {
0N/A initColors();
0N/A loadProperties(stream);
0N/A }
0N/A
0N/A /**
3835N/A * Initialize all colors to be the same as the DefaultMetalTheme.
3835N/A */
0N/A private void initColors() {
0N/A primary1 = super.getPrimary1();
0N/A primary2 = super.getPrimary2();
0N/A primary3 = super.getPrimary3();
0N/A
0N/A secondary1 = super.getSecondary1();
0N/A secondary2 = super.getSecondary2();
0N/A secondary3 = super.getSecondary3();
0N/A
0N/A black = super.getBlack();
0N/A white = super.getWhite();
0N/A }
0N/A
0N/A /**
3835N/A * Load the theme name and colors from the properties file
3835N/A * Items not defined in the properties file are ignored
3835N/A */
0N/A private void loadProperties(InputStream stream) {
0N/A Properties prop = new Properties();
0N/A try {
0N/A prop.load(stream);
0N/A } catch (IOException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A Object tempName = prop.get("name");
0N/A if (tempName != null) {
0N/A name = tempName.toString();
0N/A }
0N/A
0N/A Object colorString = null;
0N/A
0N/A colorString = prop.get("primary1");
3835N/A if (colorString != null) {
0N/A primary1 = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("primary2");
0N/A if (colorString != null) {
0N/A primary2 = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("primary3");
0N/A if (colorString != null) {
0N/A primary3 = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("secondary1");
0N/A if (colorString != null) {
0N/A secondary1 = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("secondary2");
0N/A if (colorString != null) {
0N/A secondary2 = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("secondary3");
0N/A if (colorString != null) {
0N/A secondary3 = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("black");
0N/A if (colorString != null) {
0N/A black = parseColor(colorString.toString());
0N/A }
0N/A
0N/A colorString = prop.get("white");
0N/A if (colorString != null) {
0N/A white = parseColor(colorString.toString());
0N/A }
0N/A
0N/A }
0N/A
3835N/A @Override
3835N/A public String getName() {
3835N/A return name;
3835N/A }
3835N/A
3835N/A @Override
3835N/A protected ColorUIResource getPrimary1() {
3835N/A return primary1;
3835N/A }
0N/A
3835N/A @Override
3835N/A protected ColorUIResource getPrimary2() {
3835N/A return primary2;
3835N/A }
3835N/A
3835N/A @Override
3835N/A protected ColorUIResource getPrimary3() {
3835N/A return primary3;
3835N/A }
0N/A
3835N/A @Override
3835N/A protected ColorUIResource getSecondary1() {
3835N/A return secondary1;
3835N/A }
3835N/A
3835N/A @Override
3835N/A protected ColorUIResource getSecondary2() {
3835N/A return secondary2;
3835N/A }
0N/A
3835N/A @Override
3835N/A protected ColorUIResource getSecondary3() {
3835N/A return secondary3;
3835N/A }
3835N/A
3835N/A @Override
3835N/A protected ColorUIResource getBlack() {
3835N/A return black;
3835N/A }
3835N/A
3835N/A @Override
3835N/A protected ColorUIResource getWhite() {
3835N/A return white;
3835N/A }
0N/A
0N/A /**
3835N/A * parse a comma delimited list of 3 strings into a Color
3835N/A */
0N/A private ColorUIResource parseColor(String s) {
0N/A int red = 0;
0N/A int green = 0;
0N/A int blue = 0;
0N/A try {
0N/A StringTokenizer st = new StringTokenizer(s, ",");
0N/A
0N/A red = Integer.parseInt(st.nextToken());
0N/A green = Integer.parseInt(st.nextToken());
0N/A blue = Integer.parseInt(st.nextToken());
0N/A
0N/A } catch (Exception e) {
0N/A System.out.println(e);
0N/A System.out.println("Couldn't parse color :" + s);
0N/A }
0N/A
0N/A return new ColorUIResource(red, green, blue);
0N/A }
0N/A}