001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.swing.plaf;
017:
018: import java.lang.reflect.Method;
019:
020: import javax.swing.LookAndFeel;
021: import javax.swing.UIManager;
022:
023: import org.tp23.antinstaller.InstallerContext;
024: import org.tp23.antinstaller.input.OutputField;
025:
026: /**
027: * @author Paul Hinds
028: * @version $Id: LookAndFeelFactory.java,v 1.9 2007/01/28 21:31:08 teknopaul Exp $
029: */
030: public class LookAndFeelFactory {
031:
032: public static final String DEFAULT_LAF = "org.tp23.jgoodies.plaf.plastic.PlasticXPLookAndFeel";
033: public static final String GREYMETAL_LAF = "greymetal";
034: public static final String NATIVE_LAF = "native";
035: public static final String JGOODIES_LAF = "jgoodies";
036: public static final String NULL_LAF = "null";
037:
038: private final String specifiedLAF;
039: private final InstallerContext ctx;
040:
041: /**
042: *
043: */
044: public LookAndFeelFactory(InstallerContext ctx) {
045: this .ctx = ctx;
046: this .specifiedLAF = ctx.getInstaller().getLookAndFeel();
047: }
048:
049: public void setLAF() {
050: String lafClassName = null;
051: try {
052: lafClassName = getLafFromToken(specifiedLAF);
053: if (lafClassName == null) {
054: return;
055: }
056: LookAndFeel look = (LookAndFeel) Class
057: .forName(lafClassName).newInstance();
058: try {
059: boolean antialias = OutputField.isTrue(ctx
060: .getInstaller().getAntialiased());
061: // Reflection used here to avoid dependencies on JGoodies
062: Method setAntialiased = look.getClass()
063: .getMethod("setAntiAliased",
064: new Class[] { boolean.class });
065: if (setAntialiased != null) {
066: ctx.log("Setting antialiasing:" + antialias);
067: // JDK1.5 warning fix
068: Object[] args = new Boolean[] { new Boolean(
069: antialias) };
070: setAntialiased.invoke(null, args);
071: }
072: } catch (Exception e) {
073: ctx.getLogger().log(
074: "JGoodies extensions not functioning:"
075: + e.getMessage());
076: }
077: ctx.log("Setting look and feel:" + lafClassName);
078: UIManager.setLookAndFeel(look);
079: } catch (Exception ex) {
080: ctx.getLogger().log(
081: "Can not correctly set Look And Feel:"
082: + ex.getMessage());
083: ctx.getLogger().log(ctx.getInstaller(), ex);
084: }
085: }
086:
087: public static boolean isDefault(String laf) {
088: return (laf == null || laf.equals(JGOODIES_LAF) || laf
089: .equals(DEFAULT_LAF));
090: }
091:
092: /**
093: * Gets a look and feel class name respecting the tokens supported
094: * such as jgoodies, null, native and greymetal
095: * @param token
096: * @return look and feel class name
097: */
098: public static String getLafFromToken(String token) {
099: String laf = null;
100: if (token == null || token.equals(JGOODIES_LAF)) {
101: laf = DEFAULT_LAF;
102: } else if (token.equals(NULL_LAF)) {
103: laf = null;
104: } else if (token.equals(NATIVE_LAF)) {
105: laf = UIManager.getSystemLookAndFeelClassName();
106: } else if (token.equals(GREYMETAL_LAF)) {
107: laf = "org.tp23.antinstaller.renderer.swing.plaf.ModMetalLookAndFeel";
108: } else {
109: laf = token;
110: }
111: return laf;
112: }
113: }
|