1 14 package wingset; 15 16 import org.wings.*; 17 import org.wings.template.StringTemplateSource; 18 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 import java.io.BufferedReader ; 22 import java.io.InputStreamReader ; 23 24 28 public class InteractiveTemplateExample 29 extends WingSetPane 30 implements SConstants { 31 32 final static String TEMPLATE = "/templates/InteractiveTemplateExample.thtml"; 33 final static String FALLBACK_TEMPLATE = "/templates/FallbackInteractiveTemplateExample.thtml"; 34 35 private String fallbackTemplateString; 36 37 38 protected SComponent createExample() { 39 SPanel examplePanel = new SPanel(); 40 41 try { 42 java.net.URL templateURL = getSession().getServletContext().getResource(TEMPLATE); 43 examplePanel.setLayout(new STemplateLayout(templateURL)); 44 } catch (Exception e) { 45 } 47 48 try { 49 java.net.URL fallbackTemplateURL = getSession().getServletContext().getResource(FALLBACK_TEMPLATE); 50 StringBuffer buffer = new StringBuffer (); 51 BufferedReader reader = 52 new BufferedReader (new InputStreamReader (fallbackTemplateURL.openStream())); 53 54 String line = reader.readLine(); 55 while (line != null) { 56 buffer.append(line).append('\n'); 57 line = reader.readLine(); 58 } 60 fallbackTemplateString = buffer.toString(); 61 } catch (Exception 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 } 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 () { 80 public void actionPerformed(ActionEvent e) { 81 templateSource.setTemplate(templateInput.getText()); 82 } 83 }); 84 85 SButton resetButton = new SButton("Reset"); 86 resetButton.addActionListener(new ActionListener () { 87 public void actionPerformed(ActionEvent 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 |