001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016: import org.wings.style.CSSProperty;
017: import org.wings.border.SLineBorder;
018: import org.wings.template.StringTemplateSource;
019: import org.wings.template.propertymanagers.DefaultPropertyManager;
020: import org.wings.util.SStringBuilder;
021:
022: import javax.swing.tree.DefaultTreeModel;
023: import java.awt.*;
024: import java.awt.event.ActionEvent;
025: import java.io.BufferedReader;
026: import java.io.InputStreamReader;
027:
028: /**
029: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
030: */
031: public class TemplateLayoutExample extends WingSetPane implements
032: SConstants {
033:
034: final static String TEMPLATE = "/templates/TemplateExample.thtml";
035:
036: private String templateString;
037: protected StringTemplateSource templateSource;
038: protected STextArea templateInput;
039:
040: private ComponentControls controls;
041:
042: protected SComponent createControls() {
043: controls = new Controls();
044: return controls;
045: }
046:
047: protected SComponent createExample() {
048:
049: try {
050: java.net.URL templateURL = getSession().getServletContext()
051: .getResource(TEMPLATE);
052: SStringBuilder buffer = new SStringBuilder();
053: BufferedReader reader = new BufferedReader(
054: new InputStreamReader(templateURL.openStream()));
055:
056: String line = reader.readLine();
057: while (line != null) {
058: buffer.append(line).append('\n');
059: line = reader.readLine();
060: }
061: templateString = buffer.toString();
062: } catch (Exception ex) {
063: templateString = "A simple interactive example how to use template layouts:<br/>\n"
064: + "<input type=textarea column=\"100\" rows=\"10\" name=\"TemplateInput\"/> <br/>\n"
065: + "<input type=submit text=\"Apply\" name=\"Apply\"/>";
066: ex.printStackTrace();
067: }
068:
069: templateSource = new StringTemplateSource(templateString);
070: templateInput = new STextArea(templateString);
071: templateInput.setAttribute(CSSProperty.FONT, "12px monospace");
072:
073: SButton applyButton = new SButton("Apply");
074: applyButton
075: .addActionListener(new java.awt.event.ActionListener() {
076: public void actionPerformed(ActionEvent e) {
077: templateSource.setTemplate(templateInput
078: .getText());
079: reload();
080: }
081: });
082:
083: SLabel label = new SLabel("Simple Label");
084:
085: SPanel panel = new SPanel();
086: panel.add(new SLabel(
087: "BeanScript support not enabled. Define value 'true' for "
088: + "property "
089: + DefaultPropertyManager.BEANSCRIPT_ENABLE
090: + " in web.xml "
091: + "to enable BeanScript support!"), "theLabel");
092: panel.add(new STextField(), "NAME");
093: panel.add(new STextField(), "FIRSTNAME");
094:
095: STree tree = new STree(new DefaultTreeModel(
096: HugeTreeModel.ROOT_NODE));
097: SScrollPane scrollPane = new SScrollPane(tree);
098: scrollPane.setMode(SScrollPane.MODE_COMPLETE);
099: scrollPane.setPreferredSize(new SDimension("320px", "180px"));
100: panel.add(scrollPane, "TREE");
101: panel.setVerticalAlignment(SConstants.TOP_ALIGN);
102: panel.setLayout(new STemplateLayout(templateSource));
103: panel.add(templateInput, "TemplateInput");
104: panel.add(applyButton, "Apply");
105: panel.add(label, "Label");
106: panel.setVerticalAlignment(SConstants.TOP_ALIGN);
107:
108: return panel;
109: }
110:
111: class Controls extends ComponentControls {
112: public Controls() {
113: globalControls.setVisible(false);
114:
115: SLineBorder border = new SLineBorder(new Color(0xCC, 0xCC,
116: 0xCC), 0);
117: border.setThickness(1, SConstants.TOP);
118: setBorder(border);
119: SButton resetButton = new SButton("Reset");
120: resetButton
121: .addActionListener(new java.awt.event.ActionListener() {
122: public void actionPerformed(ActionEvent e) {
123: templateSource.setTemplate(templateString);
124: templateInput.setText(templateString);
125: TemplateLayoutExample.this.reload();
126: }
127: });
128:
129: addControl(resetButton);
130: }
131: }
132: }
|