001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: /**
008: * SUIManager is the manager for Look and Feels.
009: *
010: * @author Robin Sharp
011: */
012:
013: import java.awt.*;
014: import java.util.*;
015: import java.io.*;
016:
017: import javax.servlet.http.*;
018:
019: import com.javelin.swinglets.plaf.*;
020:
021: public class SUIManager {
022: /**
023: * Get the currently installed LAF's class names.
024: */
025: public synchronized static String[] getInstalledLookAndFeels() {
026: return installedLAFNames;
027: }
028:
029: /**
030: * Replaces the current array of installed LookAndFeelInfos class names.
031: */
032: public synchronized static void setInstalledLookAndFeels(
033: String[] classNames) {
034: String[] newClassNames = new String[classNames.length];
035: System.arraycopy(classNames, 0, newClassNames, 0,
036: classNames.length);
037: installedLAFNames = newClassNames;
038: }
039:
040: /**
041: * If the look and feel has not yet been set the cross platform look and
042: * feel is installed.
043: * <p>
044: * @return The current default look and feel, or null.
045: */
046: public synchronized static SLookAndFeel getLookAndFeel() {
047: if (lookAndFeel == null) {
048: //new Exception().printStackTrace();
049: setLookAndFeel(getCrossPlatformLookAndFeelClassName());
050: }
051:
052: return lookAndFeel;
053: }
054:
055: /**
056: * Get a look and feel by name. This will try to create a look and feel by name.
057: * This does not install the look and feel, as the default look and feel.
058: * <p>
059: * If the the class name is one of the installed look and feels then load
060: * the look and feel into a cache.
061: */
062: public synchronized static SLookAndFeel getLookAndFeel(
063: String className) {
064: try {
065: int index = 0;
066: for (; index < installedLAFNames.length; index++) {
067: if (installedLAFNames[index].equals(className)) {
068: //Find the index of the class name
069: break;
070: }
071: }
072:
073: if (index >= installedLAFNames.length) {
074: //Return the look and feel but dont install it
075: return (SLookAndFeel) (Class.forName(className))
076: .newInstance();
077: }
078:
079: //If the laf has not yet been installed, then install it.
080: if (installedLAFs[index] == null) {
081: installedLAFs[index] = (SLookAndFeel) (Class
082: .forName(className)).newInstance();
083: }
084:
085: return installedLAFs[index];
086: } catch (Exception e) {
087: e.printStackTrace();
088: return null;
089: }
090: }
091:
092: /**
093: * Adds the specified look and feel to the current array.
094: */
095: public synchronized static void installLookAndFeel(String className) {
096: String[] classNames = installedLAFNames;
097: String[] newClassNames = new String[classNames.length + 1];
098: System.arraycopy(classNames, 0, newClassNames, 0,
099: classNames.length);
100: newClassNames[classNames.length] = className;
101:
102: SLookAndFeel[] lAFs = installedLAFs;
103: SLookAndFeel[] newLAFS = new SLookAndFeel[lAFs.length + 1];
104: System.arraycopy(lAFs, 0, newLAFS, 0, lAFs.length);
105:
106: setInstalledLookAndFeels(newClassNames);
107: }
108:
109: /**
110: * Set the current default look and feel using a LookAndFeel object.
111: */
112: public synchronized static void setLookAndFeel(
113: SLookAndFeel newLookAndFeel) {
114: lookAndFeel = newLookAndFeel;
115: }
116:
117: /**
118: * Set the current default look and feel using a class name. If the
119: * look an feel is one of the installed look and feels, put it in
120: * the cache.
121: */
122: public synchronized static void setLookAndFeel(String className) {
123: try {
124: SLookAndFeel laf = getLookAndFeel(className);
125: if (laf != null) {
126: setLookAndFeel(laf);
127: }
128: } catch (Exception e) {
129: e.printStackTrace();
130: }
131: }
132:
133: /**
134: * Returns the name of the LookAndFeel class that implements
135: * the default cross platform look and feel.
136: */
137: public synchronized static String getCrossPlatformLookAndFeelClassName() {
138: return "com.javelin.swinglets.plaf.html.HTMLLookAndFeel";
139: }
140:
141: // PRIVATE //////////////////////////////////////////////////////////////////////////////
142:
143: protected static SLookAndFeel lookAndFeel;
144: protected static String[] installedLAFNames = new String[] {
145: "com.javelin.swinglets.plaf.html.HTMLLookAndFeel",
146: "com.javelin.swinglets.plaf.javascript.JSLookAndFeel",
147: "com.javelin.swinglets.plaf.wml.WMLLookAndFeel",
148: "com.javelin.swinglets.plaf.jfc.JFCLookAndFeel" };
149: protected static SLookAndFeel[] installedLAFs = new SLookAndFeel[installedLAFNames.length];
150:
151: }
|