001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive.testscreen;
031:
032: import java.io.BufferedReader;
033: import java.io.ByteArrayInputStream;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.InputStreamReader;
037: import java.io.PrintWriter;
038: import java.io.StringWriter;
039:
040: import nextapp.echo2.app.Button;
041: import nextapp.echo2.app.Extent;
042: import nextapp.echo2.app.Insets;
043: import nextapp.echo2.app.Label;
044: import nextapp.echo2.app.Column;
045: import nextapp.echo2.app.StyleSheet;
046: import nextapp.echo2.app.TextArea;
047: import nextapp.echo2.app.WindowPane;
048: import nextapp.echo2.app.componentxml.ComponentXmlException;
049: import nextapp.echo2.app.componentxml.StyleSheetLoader;
050: import nextapp.echo2.app.event.ActionEvent;
051: import nextapp.echo2.app.event.ActionListener;
052: import nextapp.echo2.app.layout.SplitPaneLayoutData;
053: import nextapp.echo2.testapp.interactive.InteractiveApp;
054: import nextapp.echo2.testapp.interactive.Styles;
055:
056: /**
057: * A test for <code>StyleSheet</code>s.
058: */
059: public class StyleSheetTest extends Column {
060:
061: private static final String DEFAULT_STYLE_SHEET_TEXT;
062: static {
063: InputStream in = null;
064: try {
065: in = StyleSheetTest.class
066: .getResourceAsStream(Styles.STYLE_PATH
067: + "Default.stylesheet");
068: BufferedReader reader = new BufferedReader(
069: new InputStreamReader(in));
070: StringBuffer out = new StringBuffer();
071: String line;
072: while ((line = reader.readLine()) != null) {
073: out.append(line);
074: out.append("\n");
075: }
076: DEFAULT_STYLE_SHEET_TEXT = out.toString();
077: } catch (IOException ex) {
078: throw new RuntimeException(ex.toString());
079: } finally {
080: if (in != null) {
081: try {
082: in.close();
083: } catch (IOException ex) {
084: }
085: }
086: }
087: }
088:
089: private TextArea styleSheetEntryTextArea;
090:
091: public StyleSheetTest() {
092: super ();
093:
094: SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData();
095: splitPaneLayoutData.setInsets(new Insets(10));
096: setLayoutData(splitPaneLayoutData);
097: setCellSpacing(new Extent(20));
098:
099: Column controlsColumn = new Column();
100: add(controlsColumn);
101:
102: Button defaultButton = new Button(
103: "Slate Blue Style Sheet (DEFAULT)");
104: defaultButton.setStyleName("Default");
105: defaultButton.addActionListener(new ActionListener() {
106: public void actionPerformed(ActionEvent e) {
107: getApplicationInstance().setStyleSheet(
108: Styles.DEFAULT_STYLE_SHEET);
109: }
110: });
111: controlsColumn.add(defaultButton);
112:
113: Button greenButton = new Button("Forest Green Style Sheet");
114: greenButton.setStyleName("Default");
115: greenButton.addActionListener(new ActionListener() {
116: public void actionPerformed(ActionEvent e) {
117: getApplicationInstance().setStyleSheet(
118: Styles.GREEN_STYLE_SHEET);
119: }
120: });
121: controlsColumn.add(greenButton);
122:
123: Button nullButton = new Button("No Style Sheet");
124: nullButton.setStyleName("Default");
125: nullButton.addActionListener(new ActionListener() {
126: public void actionPerformed(ActionEvent e) {
127: getApplicationInstance().setStyleSheet(null);
128: }
129: });
130: controlsColumn.add(nullButton);
131:
132: Button customButton = new Button(
133: "Custom Style Sheet (Edit Below)");
134: customButton.setStyleName("Default");
135: customButton.addActionListener(new ActionListener() {
136: public void actionPerformed(ActionEvent e) {
137: try {
138: StyleSheet styleSheet = loadStyleSheet(styleSheetEntryTextArea
139: .getDocument().getText());
140: getApplicationInstance().setStyleSheet(styleSheet);
141: } catch (ComponentXmlException ex) {
142: displayCustomStyleError(ex);
143: }
144: }
145: });
146: controlsColumn.add(customButton);
147:
148: styleSheetEntryTextArea = new TextArea();
149: styleSheetEntryTextArea.getDocument().setText(
150: DEFAULT_STYLE_SHEET_TEXT);
151: styleSheetEntryTextArea.setStyleName("Default");
152: styleSheetEntryTextArea.setWidth(new Extent(600));
153: styleSheetEntryTextArea.setHeight(new Extent(300));
154: add(styleSheetEntryTextArea);
155: }
156:
157: private void displayCustomStyleError(Exception exception) {
158: try {
159: StringWriter w = new StringWriter();
160: exception.printStackTrace(new PrintWriter(w));
161: w.close();
162: WindowPane windowPane = new WindowPane();
163: windowPane.setStyleName("Default");
164: windowPane.setTitle("Exception Setting Custom Style");
165: windowPane.add(new Label(w.toString()));
166: InteractiveApp.getApp().getDefaultWindow().getContent()
167: .add(windowPane);
168: } catch (IOException ex) {
169: throw new RuntimeException(ex);
170: }
171: }
172:
173: private StyleSheet loadStyleSheet(String text)
174: throws ComponentXmlException {
175: InputStream in = null;
176: try {
177: // Not i18n safe.
178: in = new ByteArrayInputStream(text.getBytes());
179: StyleSheet styleSheet = StyleSheetLoader.load(in,
180: StyleSheetTest.class.getClassLoader());
181: return styleSheet;
182: } finally {
183: if (in != null) {
184: try {
185: in.close();
186: } catch (IOException ex) {
187: }
188: }
189: }
190: }
191: }
|