KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > InteractiveTemplateExample


1 /*
2  * $Id: InteractiveTemplateExample.java,v 1.3 2004/12/01 07:54:05 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.template.StringTemplateSource;
18
19 import java.awt.event.ActionEvent JavaDoc;
20 import java.awt.event.ActionListener JavaDoc;
21 import java.io.BufferedReader JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23
24 /**
25  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
26  * @version $Revision: 1.3 $
27  */

28 public class InteractiveTemplateExample
29         extends WingSetPane
30         implements SConstants {
31
32     final static String JavaDoc TEMPLATE = "/templates/InteractiveTemplateExample.thtml";
33     final static String JavaDoc FALLBACK_TEMPLATE = "/templates/FallbackInteractiveTemplateExample.thtml";
34
35     private String JavaDoc fallbackTemplateString;
36
37
38     protected SComponent createExample() {
39         SPanel examplePanel = new SPanel();
40
41         try {
42             java.net.URL JavaDoc templateURL = getSession().getServletContext().getResource(TEMPLATE);
43             examplePanel.setLayout(new STemplateLayout(templateURL));
44         } catch (Exception JavaDoc e) {
45             // template not found ?
46
}
47
48         try {
49             java.net.URL JavaDoc fallbackTemplateURL = getSession().getServletContext().getResource(FALLBACK_TEMPLATE);
50             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
51             BufferedReader JavaDoc reader =
52                     new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fallbackTemplateURL.openStream()));
53
54             String JavaDoc line = reader.readLine();
55             while (line != null) {
56                 buffer.append(line).append('\n');
57                 line = reader.readLine();
58             } // end of while ()
59

60             fallbackTemplateString = buffer.toString();
61         } catch (Exception JavaDoc ex) {
62             fallbackTemplateString =
63                     "A simple interactive example how to use template layouts:<br/>\n" +
64                     "<input type=textarea column=\"80\" rows=\"20\" name=\"TemplateInput\"/> <br/>\n" +
65                     "<input type=submit text=\"Apply\" name=\"Apply\"/>";
66             ex.printStackTrace();
67             // template not found ?
68
}
69
70         SForm form = new SForm();
71
72         final StringTemplateSource templateSource =
73                 new StringTemplateSource(fallbackTemplateString);
74
75         final STextArea templateInput = new STextArea();
76         templateInput.setText(fallbackTemplateString);
77
78         SButton applyButton = new SButton("Apply");
79         applyButton.addActionListener(new ActionListener JavaDoc() {
80             public void actionPerformed(ActionEvent JavaDoc e) {
81                 templateSource.setTemplate(templateInput.getText());
82             }
83         });
84
85         SButton resetButton = new SButton("Reset");
86         resetButton.addActionListener(new ActionListener JavaDoc() {
87             public void actionPerformed(ActionEvent JavaDoc e) {
88                 templateSource.setTemplate(fallbackTemplateString);
89                 templateInput.setText(fallbackTemplateString);
90             }
91         });
92
93         SLabel label = new SLabel("Simple Label");
94
95         form.setLayout(new STemplateLayout(templateSource));
96
97         form.add(templateInput, "TemplateInput");
98         form.add(applyButton, "Apply");
99         form.add(label, "Label");
100
101         examplePanel.add(form, "DynamicTemplate");
102         examplePanel.add(resetButton, "Reset");
103         return examplePanel;
104     }
105 }
106
107
108
Popular Tags