001: // package
002: package com.javelin.examples.swinglets;
003:
004: // import
005: import java.awt.event.*;
006: import java.awt.*;
007: import javax.servlet.http.*;
008:
009: import com.javelin.swinglets.*;
010: import com.javelin.swinglets.event.*;
011:
012: import javax.servlet.*;
013: import java.io.*;
014: import java.util.*;
015:
016: public class SwingletEventTester extends HttpServlet {
017:
018: //=====================================================================================
019: // Static Variables (ordered by public, protected, <default>, private)
020: //=====================================================================================
021:
022: static String EVENT_TESTER = "EventTester";
023:
024: //=====================================================================================
025: // Instance Variables (ordered by public, protected, <default>, private)
026: //=====================================================================================
027:
028: private SLabel _changingLabel;
029:
030: //=====================================================================================
031: // Static Methods (ordered by public, protected, <default>, private)
032: //=====================================================================================
033:
034: //=====================================================================================
035: // Instance Methods (ordered by public, protected, <default>, private)
036: //=====================================================================================
037:
038: public synchronized void init(ServletConfig config)
039: throws ServletException {
040: super .init(config);
041: SwingletManager.setDefaultRealPath(config.getServletContext()
042: .getRealPath(""));
043: SwingletManager.setDefaultImagePath("/images/");
044: }
045:
046: public synchronized void service(HttpServletRequest request,
047: HttpServletResponse response) throws IOException,
048: ServletException {
049: super .service(request, response);
050: }
051:
052: public synchronized void doPost(HttpServletRequest request,
053: HttpServletResponse response) throws IOException,
054: ServletException {
055: doGet(request, response);
056: }
057:
058: public synchronized void doGet(HttpServletRequest request,
059: HttpServletResponse response) throws IOException,
060: ServletException {
061: doGetFrame(request, response);
062: }
063:
064: private synchronized void doGetFrame(HttpServletRequest request,
065: HttpServletResponse response) throws IOException,
066: ServletException {
067: try {
068: ServletManager servletManager = ServletManager.getManager(
069: request, getServletConfig());
070:
071: if (servletManager.getValue(EVENT_TESTER) == null) {
072: // Binds Swinglet to SwingletManager
073: SFrame defaultFrame = getFrame();
074:
075: try {
076: PrintWriter pw = new PrintWriter(
077: new FileOutputStream(
078: "C:/temp/swingletuser.txt", true));
079: pw.print(request.getRemoteHost());
080: pw.print(' ');
081: pw.print(request.getRemoteAddr());
082: pw.print(' ');
083: pw.println(new java.util.Date());
084: pw.close();
085: } catch (Exception e) {
086: System.out
087: .println("Exception occurred writing to swingletuser.txt file...");
088: e.printStackTrace();
089: response.sendError(
090: response.SC_INTERNAL_SERVER_ERROR, e
091: .getMessage());
092: }
093: }
094:
095: //Get the frame and handle the response
096: SComponent component = servletManager.handle(request,
097: response, EVENT_TESTER);
098:
099: response.setContentType(component.getContentType());
100: component.paint(ServletManager.getOutput(component,
101: response));
102: } catch (Throwable e) {
103: PrintWriter pw = new PrintWriter(new FileOutputStream(
104: "C:/temp/swinglet.txt"));
105: e.printStackTrace(pw);
106: pw.close();
107: e.printStackTrace();
108: response.sendError(response.SC_INTERNAL_SERVER_ERROR, e
109: .getMessage());
110: }
111: }
112:
113: private SFrame getFrame() {
114: SFrame defaultFrame = new SFrame(EVENT_TESTER);
115: defaultFrame.setLayoutManager(new SBorderLayout());
116:
117: _changingLabel = new SLabel("Click Buttons to Change Me");
118:
119: SButton leftButton = new SButton("Left");
120: leftButton.addActionListener(new ActionListener() {
121: public void actionPerformed(ActionEvent event) {
122: _changingLabel.setText("Left Button Was Clicked");
123: }
124: });
125:
126: SButton rightButton = new SButton("Right");
127: rightButton.addActionListener(new ActionListener() {
128: public void actionPerformed(ActionEvent event) {
129: _changingLabel.setText("Right Button Was Clicked");
130: }
131: });
132:
133: SForm simpleForm = new SForm();
134: simpleForm.addFormEventListener(new MyFormListener());
135:
136: simpleForm.add(leftButton);
137: simpleForm.add(_changingLabel);
138: simpleForm.add(rightButton);
139:
140: SToolBar toolBar = new SToolBar();
141: SButton toolButton = new SButton("ToolBar Button");
142: toolButton.addActionListener(new ActionListener() {
143: public void actionPerformed(ActionEvent event) {
144: _changingLabel.setText("ToolBar Button Was Clicked");
145: }
146: });
147:
148: toolBar.add(toolButton);
149:
150: defaultFrame.add(toolBar, SBorderLayout.NORTH);
151: toolBar.setParent(defaultFrame); /* ToolBar events are not dispatched unless parent is set. */
152:
153: /* When the form is on a panel, the form apparently knows that the panel is its */
154: /* parent but it is still necessary to tell the panel that the frame is its parent */
155: /* for events to work. */
156:
157: //SPanel formPanel = new SPanel();
158: //formPanel.add(simpleForm);
159: //defaultFrame.add(formPanel, SBorderLayout.CENTER);
160: //formPanel.setParent(defaultFrame);
161:
162: /* When adding the form to the frame directly, it is still necessary to tell the */
163: /* form that its parent is the frame for events to work. */
164:
165: defaultFrame.add(simpleForm, SBorderLayout.CENTER);
166: simpleForm.setParent(defaultFrame); // <-- without this statement, events aren't dispatched
167:
168: return defaultFrame;
169: }
170:
171: //=====================================================================================
172: // Static Inner Classes (ordered by public, protected, <default>, private)
173: //=====================================================================================
174:
175: //=====================================================================================
176: // Instance Inner Classes (ordered by public, protected, <default>, private)
177: //=====================================================================================
178:
179: private class MyFormListener implements FormListener {
180: public void formSubmitted(FormEvent formEvent) {
181: System.out.println("formSubmitted: "
182: + formEvent.getSource().toString());
183: System.out.println("Action: " + formEvent.getAction());
184: }
185: }
186:
187: //=====================================================================================
188: // main method
189: //=====================================================================================
190:
191: }
|