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: package javax.swing.plaf.synth;
019:
020: import java.awt.Component;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.Serializable;
024: import java.text.ParseException;
025:
026: import javax.swing.JComponent;
027: import javax.swing.UIDefaults;
028: import javax.swing.plaf.ComponentUI;
029: import javax.swing.plaf.basic.BasicLookAndFeel;
030: import javax.xml.parsers.ParserConfigurationException;
031: import javax.xml.parsers.SAXParser;
032: import javax.xml.parsers.SAXParserFactory;
033:
034: import org.apache.harmony.x.swing.internal.nls.Messages;
035: import org.xml.sax.SAXException;
036:
037: import org.apache.harmony.luni.util.NotImplementedException;
038:
039: public class SynthLookAndFeel extends BasicLookAndFeel implements
040: Serializable {
041:
042: /** Path used in UIDefaults */
043: private final static String pathToSynthLaf = "javax.swing.plaf.synth.SynthLookAndFeel"; //$NON-NLS-1$
044:
045: private static SynthStyleFactory currentFactory;
046:
047: private UIDefaults uiDefaults;
048:
049: public static SynthStyle getStyle(JComponent c, Region r) {
050:
051: return currentFactory.getStyle(c, r);
052: }
053:
054: public static SynthStyleFactory getStyleFactory() {
055:
056: return currentFactory;
057: }
058:
059: public static void setStyleFactory(SynthStyleFactory proposed) {
060: currentFactory = proposed;
061: }
062:
063: /**
064: * Creates the Synth UI object corresponds JComponent given. (This method
065: * used by UIManager because all the UIs classes are package-protected
066: * according to spec, so reference in defaults table points to
067: * SynthLookAndFeel)
068: *
069: * @see SynthLookAndFeel#initClassDefaults(UIDefaults)
070: */
071: @SuppressWarnings("nls")
072: public static ComponentUI createUI(JComponent c)
073: throws NotImplementedException {
074:
075: // Commented because UI's patch is not ready for now
076:
077: // String uiClassID = c.getUIClassID().intern();
078: //
079: // if (uiClassID == "InternalFrameUI") {
080: // return SynthInternalFrameUI.createUI(c);
081: // } else if (uiClassID == "ViewportUI") {
082: // return SynthViewportUI.createUI(c);
083: // } else if (uiClassID == "ScrollBarUI") {
084: // SynthScrollBarUI.createUI(c);
085: // } else if (uiClassID == "ToolTipUI") {
086: // return SynthToolTipUI.createUI(c);
087: // } else if (uiClassID == "MenuItemUI") {
088: // return SynthMenuItemUI.createUI(c);
089: // } else if (uiClassID == "MenuUI") {
090: // return SynthMenuUI.createUI(c);
091: // } else if (uiClassID == "TextAreaUI") {
092: // return SynthTextAreaUI.createUI(c);
093: // } else if (uiClassID == "PopupMenuUI") {
094: // return SynthPopupMenuUI.createUI(c);
095: // } else if (uiClassID == "ScrollPaneUI") {
096: // return SynthScrollPaneUI.createUI(c);
097: // } else if (uiClassID == "SliderUI") {
098: // return SynthSliderUI.createUI(c);
099: // } else if (uiClassID == "ComboBoxUI") {
100: // return SynthComboBoxUI.createUI(c);
101: // } else if (uiClassID == "RadioButtonUI") {
102: // return SynthRadioButtonUI.createUI(c);
103: // } else if (uiClassID == "FormattedTextFieldUI") {
104: // return SynthFormattedTextFieldUI.createUI(c);
105: // } else if (uiClassID == "TreeUI") {
106: // return SynthTreeUI.createUI(c);
107: // } else if (uiClassID == "MenuBarUI") {
108: // return SynthMenuBarUI.createUI(c);
109: // } else if (uiClassID == "RadioButtonMenuItemUI") {
110: // return SynthRadioButtonMenuItemUI.createUI(c);
111: // } else if (uiClassID == "ProgressBarUI") {
112: // return SynthProgressBarUI.createUI(c);
113: // } else if (uiClassID == "ToolBarUI") {
114: // return SynthToolBarUI.createUI(c);
115: // } else if (uiClassID == "ColorChooserUI") {
116: // return SynthColorChooserUI.createUI(c);
117: // } else if (uiClassID == "ToolBarSeparatorUI") {
118: // return SynthToolBarSeparatorUI.createUI(c);
119: // } else if (uiClassID == "TabbedPaneUI") {
120: // return SynthTabbedPaneUI.createUI(c);
121: // } else if (uiClassID == "DesktopPaneUI") {
122: // return SynthDesktopPaneUI.createUI(c);
123: // } else if (uiClassID == "TableUI") {
124: // return SynthTableUI.createUI(c);
125: // } else if (uiClassID == "PanelUI") {
126: // return SynthPanelUI.createUI(c);
127: // } else if (uiClassID == "CheckBoxMenuItemUI") {
128: // return SynthCheckBoxMenuItemUI.createUI(c);
129: // } else if (uiClassID == "PasswordFieldUI") {
130: // return SynthPasswordFieldUI.createUI(c);
131: // } else if (uiClassID == "CheckBoxUI") {
132: // return SynthCheckBoxUI.createUI(c);
133: // } else if (uiClassID == "TableHeaderUI") {
134: // return SynthTableHeaderUI.createUI(c);
135: // } else if (uiClassID == "SplitPaneUI") {
136: // return SynthSplitPaneUI.createUI(c);
137: // } else if (uiClassID == "EditorPaneUI") {
138: // return SynthEditorPaneUI.createUI(c);
139: // } else if (uiClassID == "ListUI") {
140: // return SynthListUI.createUI(c);
141: // } else if (uiClassID == "SpinnerUI") {
142: // return SynthSpinnerUI.createUI(c);
143: // } else if (uiClassID == "DesktopIconUI") {
144: // return SynthDesktopIconUI.createUI(c);
145: // } else if (uiClassID == "TextFieldUI") {
146: // return SynthTextFieldUI.createUI(c);
147: // } else if (uiClassID == "TextPaneUI") {
148: // return SynthTextPaneUI.createUI(c);
149: // } else if (uiClassID == "ButtonUI") {
150: // return SynthButtonUI.createUI(c);
151: // } else if (uiClassID == "LabelUI") {
152: // return SynthLabelUI.createUI(c);
153: // } else if (uiClassID == "ToggleButtonUI") {
154: // SynthToggleButtonUI.createUI(c);
155: // } else if (uiClassID == "OptionPaneUI") {
156: // return SynthOptionPaneUI.createUI(c);
157: // } else if (uiClassID == "PopupMenuSeparatorUI") {
158: // return SynthPopupMenuSeparatorUI.createUI(c);
159: // } else if (uiClassID == "RootPaneUI") {
160: // return SynthRootPaneUI.createUI(c);
161: // } else if (uiClassID == "SeparatorUI") {
162: // return SynthSeparatorUI.createUI(c);
163: // }
164: // compatible with RI
165: return null;
166: }
167:
168: /**
169: * Renew the synth styles for the JComponent. This method isn't fully
170: * correct, but does what needs (The method is unused in package)
171: */
172: public static void updateStyles(Component c) {
173: c.setName(c.getName() + " "); //$NON-NLS-1$
174: }
175:
176: /**
177: * Calculates the region corresponds JComponent given
178: */
179: public static Region getRegion(JComponent c) {
180:
181: return Region.getRegionFromUIID(c.getUIClassID());
182: }
183:
184: @Override
185: public String getName() {
186:
187: return "Synth Look and Feel"; //$NON-NLS-1$
188: }
189:
190: @Override
191: public String getID() {
192:
193: return "Synth"; //$NON-NLS-1$
194: }
195:
196: @Override
197: public String getDescription() {
198:
199: return Messages.getString("swing.B4"); //$NON-NLS-1$
200: }
201:
202: @Override
203: public boolean isNativeLookAndFeel() {
204:
205: return false;
206: }
207:
208: @Override
209: public boolean isSupportedLookAndFeel() {
210:
211: return true;
212: }
213:
214: @Override
215: public UIDefaults getDefaults() {
216:
217: if (uiDefaults == null) {
218: uiDefaults = new UIDefaults();
219: initClassDefaults(uiDefaults);
220: initComponentDefaults(uiDefaults);
221: }
222:
223: return uiDefaults;
224: }
225:
226: @Override
227: public void initialize() {
228: // Do nothing
229: }
230:
231: @Override
232: public void uninitialize() {
233: // Do nothing
234: }
235:
236: @SuppressWarnings("unused")
237: public void load(InputStream input, Class<?> resourceBase)
238: throws ParseException, IllegalArgumentException {
239:
240: if (input == null || resourceBase == null) {
241: throw new IllegalArgumentException(Messages
242: .getString("swing.err.1D")); //$NON-NLS-1$
243: }
244:
245: try {
246:
247: SAXParser saxParser = SAXParserFactory.newInstance()
248: .newSAXParser();
249: saxParser.parse(input, new XMLSynthParser(resourceBase));
250:
251: } catch (ParserConfigurationException e) {
252: throw new ParseException(e.getMessage(), 0);
253: } catch (SAXException e) {
254: e.printStackTrace();
255: throw new ParseException(e.getMessage(), 0);
256: } catch (IOException e) {
257: throw new ParseException(e.getMessage(), 0);
258: }
259: }
260:
261: /** The default implementation returns false */
262: public boolean shouldUpdateStyleOnAncestorChanged() {
263:
264: return false;
265: }
266:
267: @SuppressWarnings("nls")
268: @Override
269: protected void initClassDefaults(UIDefaults defaults) {
270: Object[] initDefaults = { "InternalFrameUI", pathToSynthLaf,
271: "ViewportUI", pathToSynthLaf, "ScrollBarUI",
272: pathToSynthLaf, "ToolTipUI", pathToSynthLaf,
273: "MenuItemUI", pathToSynthLaf, "MenuUI", pathToSynthLaf,
274: "TextAreaUI", pathToSynthLaf, "PopupMenuUI",
275: pathToSynthLaf, "ScrollPaneUI", pathToSynthLaf,
276: "SliderUI", pathToSynthLaf, "ComboBoxUI",
277: pathToSynthLaf, "RadioButtonUI", pathToSynthLaf,
278: "FormattedTextFieldUI", pathToSynthLaf, "TreeUI",
279: pathToSynthLaf, "MenuBarUI", pathToSynthLaf,
280: "RadioButtonMenuItemUI", pathToSynthLaf,
281: "ProgressBarUI", pathToSynthLaf, "ToolBarUI",
282: pathToSynthLaf, "ColorChooserUI", pathToSynthLaf,
283: "ToolBarSeparatorUI", pathToSynthLaf, "TabbedPaneUI",
284: pathToSynthLaf, "DesktopPaneUI", pathToSynthLaf,
285: "TableUI", pathToSynthLaf, "PanelUI", pathToSynthLaf,
286: "CheckBoxMenuItemUI", pathToSynthLaf,
287: "PasswordFieldUI", pathToSynthLaf, "CheckBoxUI",
288: pathToSynthLaf, "TableHeaderUI", pathToSynthLaf,
289: "SplitPaneUI", pathToSynthLaf, "EditorPaneUI",
290: pathToSynthLaf, "ListUI", pathToSynthLaf, "SpinnerUI",
291: pathToSynthLaf, "DesktopIconUI", pathToSynthLaf,
292: "TextFieldUI", pathToSynthLaf, "TextPaneUI",
293: pathToSynthLaf, "ButtonUI", pathToSynthLaf, "LabelUI",
294: pathToSynthLaf, "ToggleButtonUI", pathToSynthLaf,
295: "OptionPaneUI", pathToSynthLaf, "PopupMenuSeparatorUI",
296: pathToSynthLaf, "RootPaneUI", pathToSynthLaf,
297: "SeparatorUI", pathToSynthLaf };
298: defaults.putDefaults(initDefaults);
299: }
300:
301: }
|