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: package org.apache.wicket.examples.velocity;
018:
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.wicket.PageParameters;
024: import org.apache.wicket.examples.WicketExamplePage;
025: import org.apache.wicket.markup.html.form.Form;
026: import org.apache.wicket.markup.html.form.TextArea;
027: import org.apache.wicket.markup.html.panel.FeedbackPanel;
028: import org.apache.wicket.model.Model;
029: import org.apache.wicket.model.PropertyModel;
030: import org.apache.wicket.util.resource.IStringResourceStream;
031: import org.apache.wicket.util.resource.PackageResourceStream;
032: import org.apache.wicket.util.resource.StringResourceStream;
033: import org.apache.wicket.velocity.markup.html.VelocityPanel;
034:
035: /**
036: * Template example page.
037: *
038: * @author Eelco Hillenius
039: */
040: public class TemplatePage extends WicketExamplePage {
041: /**
042: * Form for changing the template contents.
043: */
044: private final class TemplateForm extends Form {
045: private TextArea templateTextArea;
046:
047: /**
048: * Construct.
049: *
050: * @param name
051: * component name
052: */
053: public TemplateForm(String name) {
054: super (name);
055: add(templateTextArea = new TextArea("templateInput",
056: new PropertyModel(new Model(TemplatePage.this ),
057: "template")));
058: }
059:
060: /**
061: * @see org.apache.wicket.markup.html.form.Form#onSubmit()
062: */
063: protected void onSubmit() {
064: }
065: }
066:
067: /** the current template contents. */
068: private IStringResourceStream template = new PackageResourceStream(
069: DynamicPage.class, "persons.vm");
070:
071: /** context to be used by the template. */
072: private final Model templateContext;
073:
074: /**
075: * Constructor
076: *
077: * @param parameters
078: * Page parameters
079: */
080: public TemplatePage(final PageParameters parameters) {
081: Map<String, List<Person>> map = new HashMap<String, List<Person>>();
082: map.put("persons", VelocityTemplateApplication.getPersons());
083: templateContext = Model.valueOf(map);
084:
085: add(new TemplateForm("templateForm"));
086: add(new VelocityPanel("templatePanel", templateContext) {
087: @Override
088: protected IStringResourceStream getTemplateResource() {
089: return template;
090: }
091: });
092: add(new FeedbackPanel("feedback"));
093: }
094:
095: /**
096: * Gets the current template contents.
097: *
098: * @return the current template contents
099: */
100: public final String getTemplate() {
101: return template.asString();
102: }
103:
104: /**
105: * Sets the current template contents.
106: *
107: * @param template
108: * the current template contents
109: */
110: public final void setTemplate(String template) {
111: this .template = new StringResourceStream(template);
112: }
113: }
|