001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Sergey Burlak
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Font;
026: import java.awt.Toolkit;
027: import java.beans.IntrospectionException;
028: import java.beans.PropertyDescriptor;
029: import java.lang.reflect.InvocationTargetException;
030: import java.lang.reflect.Method;
031: import java.net.URL;
032: import java.security.AccessController;
033: import java.security.PrivilegedAction;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import javax.swing.border.Border;
038: import javax.swing.plaf.ColorUIResource;
039: import javax.swing.plaf.ComponentInputMapUIResource;
040: import javax.swing.plaf.FontUIResource;
041: import javax.swing.plaf.IconUIResource;
042: import javax.swing.plaf.InputMapUIResource;
043: import javax.swing.text.JTextComponent;
044:
045: import org.apache.harmony.x.swing.Utilities;
046:
047: import org.apache.harmony.x.swing.internal.nls.Messages;
048:
049: public abstract class LookAndFeel {
050: private static Map classToSettersMap = new HashMap();
051:
052: private UIDefaults uiDefaults;
053:
054: public abstract boolean isSupportedLookAndFeel();
055:
056: public abstract boolean isNativeLookAndFeel();
057:
058: public abstract String getName();
059:
060: public abstract String getID();
061:
062: public abstract String getDescription();
063:
064: public UIDefaults getDefaults() {
065: return uiDefaults;
066: }
067:
068: public String toString() {
069: return "[" + getDescription() + " - "
070: + this .getClass().getName() + "]";
071: }
072:
073: public void provideErrorFeedback(final Component c) {
074: Toolkit.getDefaultToolkit().beep();
075: }
076:
077: public boolean getSupportsWindowDecorations() {
078: return false;
079: }
080:
081: public void initialize() {
082: }
083:
084: public void uninitialize() {
085: }
086:
087: public static void installColorsAndFont(final JComponent comp,
088: final String background, final String foreground,
089: final String fontName) {
090: installColors(comp, background, foreground);
091: Font font = comp.getFont();
092: if (Utilities.isUIResource(font)) {
093: comp.setFont(UIManager.getFont(fontName));
094: }
095: }
096:
097: public static ComponentInputMap makeComponentInputMap(
098: final JComponent comp, final Object[] keys) {
099: ComponentInputMapUIResource result = new ComponentInputMapUIResource(
100: comp);
101: loadKeyBindings(result, keys);
102:
103: return result;
104: }
105:
106: public static void installColors(final JComponent comp,
107: final String background, final String foreground) {
108: Color foregroundColor = comp.getForeground();
109: if (Utilities.isUIResource(foregroundColor)) {
110: comp.setForeground(UIManager.getColor(foreground));
111: }
112: Color backgroundColor = comp.getBackground();
113: if (Utilities.isUIResource(backgroundColor)) {
114: comp.setBackground(UIManager.getColor(background));
115: }
116: }
117:
118: public static Object makeIcon(final Class<?> c, final String path) {
119: return new UIDefaults.LazyValue() {
120: public Object createValue(final UIDefaults uiDefaults) {
121: URL resource = c.getResource(path);
122: return resource != null ? new IconUIResource(
123: new ImageIcon(resource)) : null;
124: }
125:
126: public String toString() {
127: return "Lazy Value: (lf) " + c.getName() + " " + path;
128: }
129: };
130: }
131:
132: public static Object getDesktopPropertyValue(
133: final String propertyName, final Object defaultValue) {
134: Object result = Toolkit.getDefaultToolkit().getDesktopProperty(
135: propertyName);
136:
137: if (result instanceof Color) {
138: return new ColorUIResource((Color) result);
139: }
140: if (result instanceof Font) {
141: return new FontUIResource((Font) result);
142: }
143:
144: return (result == null) ? defaultValue : result;
145: }
146:
147: public static JTextComponent.KeyBinding[] makeKeyBindings(
148: final Object[] bindings) {
149: JTextComponent.KeyBinding[] result = new JTextComponent.KeyBinding[bindings.length / 2];
150: for (int i = 0; i < bindings.length; i += 2) {
151: result[i / 2] = new JTextComponent.KeyBinding(KeyStroke
152: .getKeyStroke((String) bindings[i]),
153: (String) bindings[i + 1]);
154: }
155:
156: return result;
157: }
158:
159: public static InputMap makeInputMap(final Object[] bindings) {
160: InputMapUIResource result = new InputMapUIResource();
161: loadKeyBindings(result, bindings);
162:
163: return result;
164: }
165:
166: public static void installBorder(final JComponent comp,
167: final String borderName) {
168: if (Utilities.isUIResource(comp.getBorder())) {
169: comp.setBorder((Border) UIManager.get(borderName));
170: }
171: }
172:
173: public static void uninstallBorder(final JComponent comp) {
174: if (comp != null && Utilities.isUIResource(comp.getBorder())) {
175: comp.setBorder(null);
176: }
177: }
178:
179: public static void loadKeyBindings(final InputMap resultMap,
180: final Object[] array) {
181: if (array == null) {
182: return;
183: }
184: for (int i = 0; i < array.length; i += 2) {
185: if (array[i] instanceof String) {
186: resultMap.put(
187: KeyStroke.getKeyStroke((String) array[i]),
188: array[i + 1]);
189: } else {
190: resultMap.put((KeyStroke) array[i], array[i + 1]);
191: }
192: }
193: }
194:
195: public static void installProperty(final JComponent c,
196: final String propertyName, final Object propertyValue) {
197: if (c.installablePropertiesExcluded.contains(propertyName)) {
198: return;
199: }
200: Method setter = getSetter(propertyName, c.getClass());
201: if (setter == null) {
202: throw new IllegalArgumentException(Messages
203: .getString("swing.51") + propertyName); //$NON-NLS-1$
204: }
205: try {
206: setter.invoke(c, new Object[] { propertyValue });
207: } catch (IllegalArgumentException e) {
208: throw new ClassCastException();
209: } catch (IllegalAccessException e) {
210: throw new IllegalArgumentException(Messages
211: .getString("swing.51") + propertyName); //$NON-NLS-1$
212: } catch (InvocationTargetException e) {
213: throw new IllegalArgumentException(Messages
214: .getString("swing.51") + propertyName); //$NON-NLS-1$
215: }
216: c.installablePropertiesExcluded.remove(propertyName);
217: }
218:
219: private static Method getSetter(final String propName, final Class c) {
220: Map setters = (Map) classToSettersMap.get(c);
221: if (setters == null) {
222: setters = new HashMap();
223: classToSettersMap.put(c, setters);
224: }
225: Method setter = (Method) setters.get(propName);
226: if (setter == null) {
227: setter = getUncachedSetter(propName, c);
228: setters.put(propName, setter);
229: }
230: return setter;
231: }
232:
233: private static Method getUncachedSetter(final String propName,
234: final Class c) {
235: return (Method) AccessController
236: .doPrivileged(new PrivilegedAction() {
237: public Object run() {
238: try {
239: return new PropertyDescriptor(propName, c)
240: .getWriteMethod();
241: } catch (IntrospectionException e) {
242: return null;
243: }
244: }
245: });
246: }
247:
248: static void markPropertyInstallable(final JComponent c,
249: final String propertyName) {
250: c.installablePropertiesExcluded.remove(propertyName);
251: }
252:
253: static void markPropertyNotInstallable(final JComponent c,
254: final String propertyName) {
255: c.installablePropertiesExcluded.add(propertyName);
256: }
257: }
|