KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > testscreen > StyleSheetTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive.testscreen;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.io.StringWriter JavaDoc;
39
40 import nextapp.echo2.app.Button;
41 import nextapp.echo2.app.Extent;
42 import nextapp.echo2.app.Insets;
43 import nextapp.echo2.app.Label;
44 import nextapp.echo2.app.Column;
45 import nextapp.echo2.app.StyleSheet;
46 import nextapp.echo2.app.TextArea;
47 import nextapp.echo2.app.WindowPane;
48 import nextapp.echo2.app.componentxml.ComponentXmlException;
49 import nextapp.echo2.app.componentxml.StyleSheetLoader;
50 import nextapp.echo2.app.event.ActionEvent;
51 import nextapp.echo2.app.event.ActionListener;
52 import nextapp.echo2.app.layout.SplitPaneLayoutData;
53 import nextapp.echo2.testapp.interactive.InteractiveApp;
54 import nextapp.echo2.testapp.interactive.Styles;
55
56 /**
57  * A test for <code>StyleSheet</code>s.
58  */

59 public class StyleSheetTest extends Column {
60     
61     private static final String JavaDoc DEFAULT_STYLE_SHEET_TEXT;
62     static {
63         InputStream JavaDoc in = null;
64         try {
65             in = StyleSheetTest.class.getResourceAsStream(Styles.STYLE_PATH + "Default.stylesheet");
66             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
67             StringBuffer JavaDoc out = new StringBuffer JavaDoc();
68             String JavaDoc line;
69             while ((line = reader.readLine()) != null) {
70                 out.append(line);
71                 out.append("\n");
72             }
73             DEFAULT_STYLE_SHEET_TEXT = out.toString();
74         } catch (IOException JavaDoc ex) {
75             throw new RuntimeException JavaDoc(ex.toString());
76         } finally {
77             if (in != null) { try { in.close(); } catch (IOException JavaDoc ex) { } }
78         }
79     }
80     
81     private TextArea styleSheetEntryTextArea;
82     
83     public StyleSheetTest() {
84         super();
85         
86         SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData();
87         splitPaneLayoutData.setInsets(new Insets(10));
88         setLayoutData(splitPaneLayoutData);
89         setCellSpacing(new Extent(20));
90         
91         Column controlsColumn = new Column();
92         add(controlsColumn);
93         
94         Button defaultButton = new Button("Slate Blue Style Sheet (DEFAULT)");
95         defaultButton.setStyleName("Default");
96         defaultButton.addActionListener(new ActionListener() {
97             public void actionPerformed(ActionEvent e) {
98                 getApplicationInstance().setStyleSheet(Styles.DEFAULT_STYLE_SHEET);
99             }
100         });
101         controlsColumn.add(defaultButton);
102         
103         Button greenButton = new Button("Forest Green Style Sheet");
104         greenButton.setStyleName("Default");
105         greenButton.addActionListener(new ActionListener() {
106             public void actionPerformed(ActionEvent e) {
107                 getApplicationInstance().setStyleSheet(Styles.GREEN_STYLE_SHEET);
108             }
109         });
110         controlsColumn.add(greenButton);
111         
112         Button nullButton = new Button("No Style Sheet");
113         nullButton.setStyleName("Default");
114         nullButton.addActionListener(new ActionListener() {
115             public void actionPerformed(ActionEvent e) {
116                 getApplicationInstance().setStyleSheet(null);
117             }
118         });
119         controlsColumn.add(nullButton);
120         
121         Button customButton = new Button("Custom Style Sheet (Edit Below)");
122         customButton.setStyleName("Default");
123         customButton.addActionListener(new ActionListener() {
124             public void actionPerformed(ActionEvent e) {
125                 try {
126                     StyleSheet styleSheet = loadStyleSheet(styleSheetEntryTextArea.getDocument().getText());
127                     getApplicationInstance().setStyleSheet(styleSheet);
128                 } catch (ComponentXmlException ex) {
129                     displayCustomStyleError(ex);
130                 }
131             }
132         });
133         controlsColumn.add(customButton);
134         
135         styleSheetEntryTextArea = new TextArea();
136         styleSheetEntryTextArea.getDocument().setText(DEFAULT_STYLE_SHEET_TEXT);
137         styleSheetEntryTextArea.setStyleName("Default");
138         styleSheetEntryTextArea.setWidth(new Extent(600));
139         styleSheetEntryTextArea.setHeight(new Extent(300));
140         add(styleSheetEntryTextArea);
141     }
142     
143     private void displayCustomStyleError(Exception JavaDoc exception) {
144         try {
145             StringWriter JavaDoc w = new StringWriter JavaDoc();
146             exception.printStackTrace(new PrintWriter JavaDoc(w));
147             w.close();
148             WindowPane windowPane = new WindowPane();
149             windowPane.setStyleName("Default");
150             windowPane.setTitle("Exception Setting Custom Style");
151             windowPane.add(new Label(w.toString()));
152             InteractiveApp.getApp().getDefaultWindow().getContent().add(windowPane);
153         } catch (IOException JavaDoc ex) {
154             throw new RuntimeException JavaDoc(ex);
155         }
156     }
157     
158     private StyleSheet loadStyleSheet(String JavaDoc text)
159     throws ComponentXmlException {
160         InputStream JavaDoc in = null;
161         try {
162             // Not i18n safe.
163
in = new ByteArrayInputStream JavaDoc(text.getBytes());
164             StyleSheet styleSheet = StyleSheetLoader.load(in, StyleSheetTest.class.getClassLoader());
165             return styleSheet;
166         } finally {
167             if (in != null) { try { in.close(); } catch (IOException JavaDoc ex) { } }
168         }
169     }
170 }
171
Popular Tags