001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.examples.swinglets;
006:
007: import java.awt.*;
008: import java.awt.event.*;
009: import java.io.*;
010: import java.util.Date;
011: import java.text.*;
012:
013: import javax.servlet.*;
014: import javax.servlet.http.*;
015: import javax.swing.*;
016: import javax.swing.table.*;
017: import javax.swing.tree.*;
018:
019: import com.javelin.swinglets.*;
020: import com.javelin.swinglets.table.*;
021: import com.javelin.swinglets.tree.*;
022: import com.javelin.swinglets.event.*;
023:
024: /**
025: * Servlet to display a demo WML Servlet.
026: *
027: * @author Robin Sharp
028: */
029:
030: public class KeyHoleServlet extends HttpServlet implements FormListener {
031: public synchronized void init(ServletConfig config)
032: throws ServletException {
033: super .init(config);
034:
035: //In VisualCafe set the servlet runner arguments to
036: //-r file:///F:/java/javelin/swinglets/src/examples/
037: //System.out.println( "REAL PATH=" + config.getServletContext().getRealPath("") );
038:
039: SwingletManager.setDefaultRealPath(config.getServletContext()
040: .getRealPath(""));
041: SwingletManager.setDefaultImagePath("/images/");
042:
043: }
044:
045: public synchronized void service(HttpServletRequest request,
046: HttpServletResponse response) throws IOException,
047: ServletException {
048: doGet(request, response);
049: }
050:
051: public synchronized void doPost(HttpServletRequest request,
052: HttpServletResponse response) throws IOException,
053: ServletException {
054: doGet(request, response);
055: }
056:
057: static String KEYHOLE = "KeyHole";
058:
059: public synchronized void doGet(HttpServletRequest request,
060: HttpServletResponse response) throws IOException,
061: ServletException {
062: try {
063: ServletManager servletManager = ServletManager
064: .getManager(request);
065:
066: //Intialise the Swinglet with the default name, and contents
067: if (servletManager.getValue(KEYHOLE) == null) {
068: //System.out.println( "KeyHole.CREATE FRAME" );
069: SFrame defaultFrame = new SFrame(KEYHOLE);
070: defaultFrame
071: .setLookAndFeel(SUIManager
072: .getLookAndFeel("com.javelin.swinglets.plaf.wml.WMLLookAndFeel"));
073: defaultFrame.setTitle("Key Hole Servlet");
074: defaultFrame.add(getTabbedPane(servletManager));
075: }
076:
077: //Get the frame and handle the response
078: SComponent component = servletManager.handle(request,
079: response, KEYHOLE);
080: //System.out.println( "KeyHole.FRAME=" + ( frame == null ) );
081:
082: //Set the look and feel according to the browser profile
083: servletManager.applyDetectedLookAndFeel(request
084: .getHeader("USER-AGENT"));
085:
086: response.setContentType(component.getContentType());
087:
088: component.paint(ServletManager.getOutput(component,
089: response));
090: } catch (Exception e) {
091: PrintWriter pw = new PrintWriter(new FileOutputStream(
092: "C:/tmp/swinglet.txt"));
093: e.printStackTrace(pw);
094: pw.close();
095:
096: e.printStackTrace();
097: response.sendError(response.SC_INTERNAL_SERVER_ERROR, e
098: .getMessage());
099: } finally {
100: SwingletManager.setSwingletManager(null);
101: }
102: }
103:
104: public void formSubmitted(FormEvent formEvent) {
105: System.out.println(formEvent.toString());
106: }
107:
108: public void formReset(FormEvent formEvent) {
109: System.out.println(formEvent.toString());
110: }
111:
112: public SComponent getTabbedPane(ServletManager servletManager)
113: throws Exception {
114: STabbedPane tabbed = new STabbedPane(SConstants.TOP);
115: //tabbed.setSize( 700, 400 );
116: //tabbed.addTab( "Swinglets", getTitle() );
117: //tabbed.addTab( "Logon", getLogonPanel() );
118:
119: //tabbed.addTab( "Date Time", getDateTimePanel() );
120: /*
121: tabbed.addTab( "Image", getImagePanel() );
122: tabbed.addTab( "Combo", getComboPanel() );
123: tabbed.addTab( "Multi List", getMultiListPanel() );
124: tabbed.addTab( "Radio Buttons", getRadioButtons() );
125: tabbed.addTab( "Check Boxes", getCheckBoxes() );
126: tabbed.addTab( "Table", getTablePanel() );
127: */
128: tabbed.addTab("Tree", getTreePanel());
129:
130: return tabbed;
131: }
132:
133: public SComponent getTitle() {
134: //TITLE TAB
135: SCharacter BREAK = new SCharacter(SCharacter.BREAK);
136:
137: SPanel swingletsPanel = new SPanel();
138: swingletsPanel
139: .setLayoutManager(new SFlowLayout(SConstants.LEFT));
140: swingletsPanel.add(new SLabel("Swinglets")
141: .setHorizontalAlignment(SConstants.CENTER).setFont(
142: SFont
143: .getFont("Arial,Helvetica", SFont.BOLD,
144: 12)));
145: swingletsPanel.add(BREAK);
146: swingletsPanel.add(new SLabel("Server UI Toolkit")
147: .setHorizontalAlignment(SConstants.CENTER).setFont(
148: SFont.getFont("Arial,Helvetica", SFont.PLAIN,
149: 10)));
150: swingletsPanel.add(BREAK);
151: swingletsPanel.add(new SLabel("Javelin Sofware")
152: .setHorizontalAlignment(SConstants.CENTER).setFont(
153: SFont
154: .getFont("Arial,Helvetica",
155: SFont.PLAIN, 8)));
156: swingletsPanel.add(BREAK);
157: swingletsPanel.add(new SLabel("www.swinglets.com")
158: .setHorizontalAlignment(SConstants.CENTER).setFont(
159: SFont
160: .getFont("Arial,Helvetica",
161: SFont.PLAIN, 8)));
162:
163: return swingletsPanel;
164: }
165:
166: public SComponent getLogonPanel() {
167: //LOGIN TAB
168: logonPanel = new SForm();
169: logonPanel.add(new SLabel("User"));
170: logonPanel.add(new STextField().setColumns(8).setMaxLength(8));
171: logonPanel.add(new SCharacter(SCharacter.BREAK));
172: logonPanel.add(new SLabel("Password"));
173: logonPanel.add(new SPasswordField().setColumns(5).setMaxLength(
174: 5));
175:
176: logonPanel.add(new SCharacter(SCharacter.BREAK));
177: logonPanel.add(new SButton("Logon"));
178: logonPanel.add(new SCharacter());
179: logonPanel.add(new SButton("Cancel"));
180: logonPanel.addFormEventListener(this );
181:
182: return logonPanel;
183: }
184:
185: public SComponent getDateTimePanel() {
186: //DATE TIME TAB
187: SPanel dateTimePanel = new SPanel();
188: dateTimePanel.add(new SLabel("Date="));
189: DateFormat dateFormatter = new SimpleDateFormat("yyy/MM/dd");
190: dateTimePanel.add(new SLabel(dateFormatter.format(new Date()))
191: .setFont(SFont.getFont("Arial,Helvetica", SFont.ITALIC,
192: 10)));
193: dateTimePanel.add(new SCharacter(SCharacter.BREAK));
194: dateTimePanel.add(new SLabel("Time="));
195: DateFormat timeFormatter = new SimpleDateFormat("hh::mm::ss");
196: dateTimePanel.add(new SLabel(timeFormatter.format(new Date()))
197: .setFont(SFont.getFont("Arial,Helvetica", SFont.BOLD,
198: 10)));
199:
200: return dateTimePanel;
201: }
202:
203: public SComponent getImagePanel() {
204: //IMAGE TAB
205: SPanel imagePanel = new SPanel();
206: imagePanel.add(new SLabel("My Big Banana").setFont(SFont
207: .getFont("Arial,Helvetica", SFont.BOLD, 8)));
208: imagePanel.add(new SCharacter(SCharacter.BREAK));
209: imagePanel.add(new SIcon("images/swing/banana.gif").setSize(
210: 100, 200));
211:
212: return imagePanel;
213: }
214:
215: public SComponent getComboPanel() {
216: //COMBO TAB
217: final Object[] data = new Object[] { "The", "Quick", "Brown",
218: "Fox", "Jumped", "Over", "The", "Lazy", "Dogs", "Back" };
219: class ComboPanel extends SForm implements FormListener {
220: SComboBox combo1 = new SComboBox(data);
221: SLabel selected = new SLabel();
222:
223: public ComboPanel() {
224: add(new SLabel("Combo Box"));
225: add(combo1);
226: add(new SCharacter(SCharacter.BREAK));
227: add(new SButton("Select"));
228: add(selected);
229: addFormEventListener(this );
230: }
231:
232: public void formReset(FormEvent formEvent) {
233: }
234:
235: public void formSubmitted(FormEvent formEvent) {
236: selected.setText(combo1.getSelectedItem().toString());
237: }
238: }
239: return new ComboPanel();
240: }
241:
242: public SComponent getMultiListPanel() {
243: //MULTI LIST TAB
244: final Object[] data = new Object[] { "The", "Quick", "Brown",
245: "Fox", "Jumped", "Over", "The", "Lazy", "Dogs", "Back" };
246: SForm multiList = new SForm();
247: multiList.add(new SLabel("Multiple Selection"));
248: SList list1 = new SList(data)
249: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
250: multiList.add(list1);
251: multiList.add(new SButton("Select"));
252:
253: return multiList;
254: }
255:
256: public SComponent getRadioButtons() {
257: //RADIO BUTTONS
258: SForm radioButtons = new SForm();
259: radioButtons.setLayoutManager(new SGridLayout(0, 2));
260:
261: radioButtons.add(new SLabel("Friends"));
262:
263: SButtonGroup friends = new SButtonGroup();
264: friends.add(new SRadioButton("Amanda"));
265: friends.add(new SRadioButton("Amber"));
266: friends.add(new SRadioButton("Becky"));
267: friends.add(new SRadioButton("Sabrina"));
268: friends.add(new SRadioButton("Scarlet", true));
269: friends.add(new SRadioButton("Tamar"));
270: radioButtons.add(friends);
271:
272: radioButtons.add(new SLabel("Mates"));
273:
274: SButtonGroup mates = new SButtonGroup();
275: mates.add(new SRadioButton("Adam"));
276: mates.add(new SRadioButton("Dino"));
277: mates.add(new SRadioButton("John"));
278: mates.add(new SRadioButton("Ray"));
279: radioButtons.add(mates);
280:
281: radioButtons.add(new SButton("Select"));
282:
283: return radioButtons;
284: }
285:
286: public SComponent getCheckBoxes() {
287: //CHECK BOX
288: SForm checkBoxes = new SForm();
289: SCheckBox checkBox = new SCheckBox("Check Me", true);
290: checkBoxes.add(checkBox);
291: checkBoxes.add(new SButton("Select"));
292:
293: return checkBoxes;
294: }
295:
296: /*
297: //SINGLE LIST TAB
298: SPanel singleList = new SForm();
299: tabbed.addTab( "Single List", singleList );
300: singleList.add( new SLabel( "Single Selection" ) );
301: SList list2 = new SList( data ).setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
302: singleList.add( list2 );
303: */
304: public SComponent getTablePanel() {
305: //TABLE TAB
306: SPanel tablePanel = new SPanel();
307: final Object[] columnData = new Object[] { "Name", "Sport",
308: "Number" };
309: final Object[][] objectData = {
310: { "Mike", "Soccer", new Integer(44) },
311: { "Mark", "Baseball", new Integer(2) },
312: { "Tom", "Football", new Integer(99) },
313: { "Alan", "Baseball", new Integer(838) },
314: { "Jeff", "Football", new Integer(8) },
315: { "Amy", "Hockey", new Integer(3) },
316: { "Brian", "Rugby", new Integer(7) },
317: { "James", "Tennis", new Integer(21) },
318: { "Earl", "Bicycling", new Integer(8) },
319: { "David", "Baseball", new Integer(1) },
320: { "Dave", "Football", new Integer(14) },
321: { "Peter", "Scuba Diving", new Integer(12) },
322: { "Dana", "Ice Skating", new Integer(8) },
323: { "Phil", "Rugby", new Integer(3) },
324: { "Dave", "Tennis", new Integer(88) },
325: { "Hans", "Baseball", new Integer(5) } };
326:
327: STable table = new STable(new DefaultTableModel(objectData,
328: columnData));
329: table.setTableHeader(new STableHeader(table));
330:
331: tablePanel.add(table);
332: return tablePanel;
333: }
334:
335: public SComponent getTreePanel() {
336: //TREE PANEL
337: SPanel treePanel = new SPanel();
338: final STree tree = new DemoTree();
339: treePanel.add(tree);
340:
341: tree.addActionListener(new ActionListener() {
342: public void actionPerformed(ActionEvent actionEvent) {
343: TreePath path = tree.getSelectedPath();
344: if (path != null) {
345: System.out.println("SELECTION="
346: + path.getLastPathComponent());
347: }
348: }
349: });
350:
351: return treePanel;
352: }
353:
354: SForm logonPanel;
355: }
|