1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.demos;
17
18 import java.awt.BorderLayout;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.awt.event.WindowAdapter;
22 import java.awt.event.WindowEvent;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.OutputStream;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.Enumeration;
29 import java.util.Vector;
30
31 import javax.swing.BorderFactory;
32 import javax.swing.BoxLayout;
33 import javax.swing.DefaultListModel;
34 import javax.swing.JButton;
35 import javax.swing.JComboBox;
36 import javax.swing.JEditorPane;
37 import javax.swing.JFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JList;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTextField;
43
44 import org.apache.commons.jelly.JellyContext;
45 import org.apache.commons.jelly.XMLOutput;
46
47 /***
48 * A sample Swing program that demonstrates the use of Jelly as a templating mechanism
49 *
50 * @author Otto von Wachter
51 */
52 public class HomepageBuilder extends JPanel {
53
54 JTextField nameField;
55 JTextField urlField;
56 JTextField addField;
57 JTextField colorField;
58 JComboBox templateList;
59 JList interestList;
60 DefaultListModel listModel;
61
62
63 public HomepageBuilder() {
64
65 System.out.println("Starting Homepage Builder");
66
67 JPanel leftPanel = new JPanel();
68 leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
69
70 leftPanel.add(new JLabel("Name:"));
71
72 nameField= new JTextField("James Bond");
73 leftPanel.add(nameField);
74
75 leftPanel.add(new JLabel("Favorite Color:"));
76
77 colorField = new JTextField("#007007");
78 leftPanel.add(colorField);
79
80 leftPanel.add(new JLabel("Picture URL:"));
81
82 urlField = new JTextField("http://www.ianfleming.org/007news/images3/c2002_pierce1.jpg");
83 leftPanel.add(urlField);
84
85 leftPanel.add(new JLabel("Choose template:"));
86
87 templateList = new JComboBox(new String[] {"template1.jelly","template2.jelly"});
88 leftPanel.add(templateList);
89
90
91
92
93 leftPanel.add(new JLabel("Add a Hobby:"));
94
95 addField = new JTextField();
96 leftPanel.add(addField);
97
98 JButton addButton = new JButton("Add >>>");
99 addButton.addActionListener(new ActionListener() {
100 public void actionPerformed(ActionEvent e) {
101 listModel.addElement(addField.getText());
102 }
103 });
104 leftPanel.add(addButton);
105
106 listModel = new DefaultListModel();
107 listModel.addElement("Killing bad guys");
108 listModel.addElement("Wrecking cars");
109 listModel.addElement("Eating jelly");
110 interestList = new JList(listModel);
111
112
113 JButton submit = new JButton("Build and preview your page!");
114 submit.addActionListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e) {
116 buildPage(templateList.getSelectedItem().toString(),new JellyContext());
117 showPage();
118 }
119 });
120
121
122 setLayout(new BorderLayout());
123 add(submit, BorderLayout.SOUTH);
124 add(leftPanel, BorderLayout.WEST);
125 add(new JScrollPane(interestList), BorderLayout.EAST);
126 setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
127 }
128
129 public void buildPage(String template, JellyContext ctx) {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 try {
153
154 OutputStream output = new FileOutputStream("demopage.html");
155
156 JellyContext context = new JellyContext();
157 context.setVariable("name",nameField.getText());
158 context.setVariable("background",colorField.getText());
159 context.setVariable("url",urlField.getText());
160
161 Vector v = new Vector();
162 Enumeration items = listModel.elements();
163 while (items.hasMoreElements()) {
164 v.add(items.nextElement());
165 }
166 context.setVariable("hobbies", v);
167
168 XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
169 context.runScript( resolveURL("src/test/org/apache/commons/jelly/demos/"+template), xmlOutput );
170 xmlOutput.flush();
171 System.out.println("Finished merging template");
172
173 } catch (Exception e) {
174 e.printStackTrace();
175 }
176
177 }
178
179 void showPage() {
180
181
182 JFrame frame = new JFrame("Your Homepage");
183
184
185 try {
186
187 URL url = resolveURL("demopage.html");
188 JEditorPane htmlPane = new JEditorPane(url);
189 htmlPane.setEditable(false);
190 frame.setContentPane(new JScrollPane(htmlPane));
191
192 } catch(Exception ioe) {
193 System.err.println("Error displaying page");
194 }
195
196 frame.pack();
197 frame.setSize(500,500);
198 frame.setVisible(true);
199
200 }
201
202 /****
203 * @return the URL for the relative file name or absolute URL
204 */
205 protected URL resolveURL(String name) throws MalformedURLException {
206 File file = new File(name);
207 if (file.exists()) {
208 return file.toURL();
209 }
210 return new URL(name);
211 }
212
213
214 public static void main(String s[]) {
215 JFrame frame = new JFrame("Homepage Builder");
216
217 frame.addWindowListener(new WindowAdapter() {
218 public void windowClosing(WindowEvent e) {System.exit(0);}
219 });
220
221 frame.setContentPane(new HomepageBuilder());
222 frame.pack();
223 frame.setVisible(true);
224 }
225 }