Path: blob/master/src/demo/share/jfc/Metalworks/PropertiesMetalTheme.java
41149 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.io.IOException;42import java.io.InputStream;43import java.util.Properties;44import java.util.StringTokenizer;4546import javax.swing.plaf.ColorUIResource;47import javax.swing.plaf.metal.DefaultMetalTheme;484950/**51* This class allows you to load a theme from a file.52* It uses the standard Java Properties file format.53* To create a theme you provide a text file which contains54* tags corresponding to colors of the theme along with a value55* for that color. For example:56*57* name=My Ugly Theme58* primary1=255,0,059* primary2=0,255,060* primary3=0,0,25561*62* This class only loads colors from the properties file,63* but it could easily be extended to load fonts - or even icons.64*65* @author Steve Wilson66* @author Alexander Kouznetsov67*/68public class PropertiesMetalTheme extends DefaultMetalTheme {6970private String name = "Custom Theme";71private ColorUIResource primary1;72private ColorUIResource primary2;73private ColorUIResource primary3;74private ColorUIResource secondary1;75private ColorUIResource secondary2;76private ColorUIResource secondary3;77private ColorUIResource black;78private ColorUIResource white;7980/**81* pass an inputstream pointing to a properties file.82* Colors will be initialized to be the same as the DefaultMetalTheme,83* and then any colors provided in the properties file will override that.84*/85public PropertiesMetalTheme(InputStream stream) {86initColors();87loadProperties(stream);88}8990/**91* Initialize all colors to be the same as the DefaultMetalTheme.92*/93private void initColors() {94primary1 = super.getPrimary1();95primary2 = super.getPrimary2();96primary3 = super.getPrimary3();9798secondary1 = super.getSecondary1();99secondary2 = super.getSecondary2();100secondary3 = super.getSecondary3();101102black = super.getBlack();103white = super.getWhite();104}105106/**107* Load the theme name and colors from the properties file108* Items not defined in the properties file are ignored109*/110private void loadProperties(InputStream stream) {111Properties prop = new Properties();112try {113prop.load(stream);114} catch (IOException e) {115System.out.println(e);116}117118Object tempName = prop.get("name");119if (tempName != null) {120name = tempName.toString();121}122123Object colorString = null;124125colorString = prop.get("primary1");126if (colorString != null) {127primary1 = parseColor(colorString.toString());128}129130colorString = prop.get("primary2");131if (colorString != null) {132primary2 = parseColor(colorString.toString());133}134135colorString = prop.get("primary3");136if (colorString != null) {137primary3 = parseColor(colorString.toString());138}139140colorString = prop.get("secondary1");141if (colorString != null) {142secondary1 = parseColor(colorString.toString());143}144145colorString = prop.get("secondary2");146if (colorString != null) {147secondary2 = parseColor(colorString.toString());148}149150colorString = prop.get("secondary3");151if (colorString != null) {152secondary3 = parseColor(colorString.toString());153}154155colorString = prop.get("black");156if (colorString != null) {157black = parseColor(colorString.toString());158}159160colorString = prop.get("white");161if (colorString != null) {162white = parseColor(colorString.toString());163}164165}166167@Override168public String getName() {169return name;170}171172@Override173protected ColorUIResource getPrimary1() {174return primary1;175}176177@Override178protected ColorUIResource getPrimary2() {179return primary2;180}181182@Override183protected ColorUIResource getPrimary3() {184return primary3;185}186187@Override188protected ColorUIResource getSecondary1() {189return secondary1;190}191192@Override193protected ColorUIResource getSecondary2() {194return secondary2;195}196197@Override198protected ColorUIResource getSecondary3() {199return secondary3;200}201202@Override203protected ColorUIResource getBlack() {204return black;205}206207@Override208protected ColorUIResource getWhite() {209return white;210}211212/**213* parse a comma delimited list of 3 strings into a Color214*/215private ColorUIResource parseColor(String s) {216int red = 0;217int green = 0;218int blue = 0;219try {220StringTokenizer st = new StringTokenizer(s, ",");221222red = Integer.parseInt(st.nextToken());223green = Integer.parseInt(st.nextToken());224blue = Integer.parseInt(st.nextToken());225226} catch (Exception e) {227System.out.println(e);228System.out.println("Couldn't parse color :" + s);229}230231return new ColorUIResource(red, green, blue);232}233}234235236