0001: /*
0002: * Copyright Javelin Software, All rights reserved.
0003: */
0004:
0005: package com.javelin.examples.swinglets;
0006:
0007: import java.net.*;
0008: import java.util.*;
0009: import java.io.*;
0010: import java.awt.*;
0011: import java.awt.event.*;
0012: import java.text.*;
0013:
0014: import javax.swing.*;
0015: import javax.swing.tree.*;
0016: import javax.servlet.*;
0017: import javax.servlet.http.*;
0018:
0019: import com.javelin.swinglets.*;
0020: import com.javelin.swinglets.event.*;
0021: import com.javelin.swinglets.theme.*;
0022: import com.javelin.swinglets.border.*;
0023: import com.javelin.swinglets.plaf.*;
0024: import com.javelin.swinglets.plaf.html.*;
0025: import com.javelin.swinglets.plaf.javascript.*;
0026:
0027: /**
0028: * Servlet to display a demo Servlet.
0029: *
0030: * @author Robin Sharp
0031: */
0032:
0033: public class AmazingServlet extends HttpServlet {
0034: private SToolBar toolBar;
0035: private SMenuBar menuBar;
0036: private STabbedPane tabbed;
0037: private SDialog dialog;
0038: private SLabel treeLabel;
0039:
0040: public synchronized void init(ServletConfig config)
0041: throws ServletException {
0042: super .init(config);
0043: /*
0044: Class c=this.getClass();
0045: URL u=c.getResource("/");
0046:
0047: ServletContext context=config.getServletContext();
0048: System.out.println("realpath====="+context.getRealPath(""));
0049:
0050: System.out.println("url====="+u.toExternalForm());
0051: */
0052: //SUIManager.setLookAndFeel( "com.javelin.swinglets.plaf.html.HTMLLookAndFeel" );
0053: SUIManager
0054: .setLookAndFeel("com.javelin.swinglets.plaf.javascript.JSLookAndFeel");
0055:
0056: //In VisualCafe set the servlet runner arguments to
0057: //-r file:///F:/java/javelin/swinglets/src/examples/
0058: System.out.println("REAL PATH="
0059: + config.getServletContext().getRealPath(""));
0060: SwingletManager.setDefaultRealPath(config.getServletContext()
0061: .getRealPath(""));
0062: SwingletManager.setDefaultImagePath("/images/");
0063: }
0064:
0065: public synchronized void destroy() {
0066: }
0067:
0068: public synchronized void service(HttpServletRequest request,
0069: HttpServletResponse response) throws IOException,
0070: ServletException {
0071: super .service(request, response);
0072: //or not
0073: }
0074:
0075: public synchronized void doPost(HttpServletRequest request,
0076: HttpServletResponse response) throws IOException,
0077: ServletException {
0078: doGet(request, response);
0079: }
0080:
0081: static String AMAZING = "Amazing";
0082:
0083: public synchronized void doGet(HttpServletRequest request,
0084: HttpServletResponse response) throws IOException,
0085: ServletException {
0086: doGetFrame(request, response);
0087: //doGetNoFrame( request, response );
0088:
0089: }
0090:
0091: public synchronized void doGetFrame(HttpServletRequest request,
0092: HttpServletResponse response) throws IOException,
0093: ServletException {
0094:
0095: //Enumeration en=request.getParameterNames();
0096: //while (en.hasMoreElements())
0097: //{
0098: // System.out.println(en.nextElement());
0099: //}
0100:
0101: try {
0102: ServletManager servletManager = ServletManager.getManager(
0103: request, getServletConfig());
0104:
0105: //Intialise the Swinglet with the default name, and contents
0106: if (servletManager.getValue(AMAZING) == null) {
0107: SFrame defaultFrame = getFrame();
0108: try {
0109: PrintWriter pw = new PrintWriter(
0110: new FileOutputStream(
0111: "C:/temp/swingletuser.txt", true));
0112: pw.print(request.getRemoteHost());
0113: pw.print(' ');
0114: pw.print(request.getRemoteAddr());
0115: pw.print(' ');
0116: pw.println(new java.util.Date());
0117: } catch (Exception e) { /*Fail Silently*/
0118: }
0119: }
0120:
0121: //Get the frame and handle the response
0122: SComponent component = servletManager.handle(request,
0123: response, AMAZING);
0124: response.setContentType(component.getContentType());
0125: component.paint(ServletManager.getOutput(component,
0126: response));
0127:
0128: } catch (Throwable e) {
0129: PrintWriter pw = new PrintWriter(new FileOutputStream(
0130: "C:/temp/swinglet.txt"));
0131: e.printStackTrace(pw);
0132: pw.close();
0133: e.printStackTrace();
0134: response.sendError(response.SC_INTERNAL_SERVER_ERROR, e
0135: .getMessage());
0136: } finally {
0137: System.out.println(SwingletManager.getDefaultRealPath());
0138: }
0139: }
0140:
0141: public SFrame getFrame() throws Exception {
0142: SFrame defaultFrame = new SFrame(AMAZING);
0143: defaultFrame.setFont(SFont.getFont("Arial,Helvetica",
0144: SFont.PLAIN, 12));
0145:
0146: dialog = new SDialog(defaultFrame);
0147: dialog.setTitle("About");
0148: dialog.add(getDialogContents());
0149:
0150: defaultFrame.setTitle("Amazing Servlet");
0151: defaultFrame.setMenuBar(getDemoMenu());
0152: defaultFrame.add(getToolBar());
0153:
0154: defaultFrame.add(getTabbedPane());
0155: //defaultFrame.add( new SLabel( "STATUS BAR" ) );
0156: //defaultFrame.add( getLayout() );
0157: //defaultFrame.add( getTree() );
0158: //defaultFrame.add( new DemoFunds() );
0159:
0160: //Add JavaScript Handlers and Script
0161: defaultFrame
0162: .addScript(new SScript(SScript.JAVASCRIPT)
0163: .setScript("function welcomeToJS() { alert('Welcome To JavaScript LookAndFeel'); }"));
0164:
0165: defaultFrame.addScript(new SScript(SScript.JAVASCRIPT)
0166: .setScript("function welcome() { alert('Welcome'); }"));
0167:
0168: //defaultFrame.addWindowListener( new ScriptedWindowListener( SScript.JAVASCRIPT ).
0169: // setWindowOpenedScript( "welcomeToJS()" ) );
0170:
0171: //Set the look and feel according to the browser profile
0172: //defaultFrame.setLookAndFeel( servletManager.getLookAndFeel() );
0173:
0174: return defaultFrame;
0175: }
0176:
0177: /**
0178: * Test the TabbedPane being dropped straight into the HTML
0179: *Uncomment the #1,#2,#3 pairs to test
0180: */
0181: public synchronized void doGetNoFrame(HttpServletRequest request,
0182: HttpServletResponse response) throws IOException,
0183: ServletException {
0184: try {
0185: ServletManager servletManager = ServletManager.getManager(
0186: request, getServletConfig());
0187:
0188: SFrame defaultFrame = null;
0189:
0190: if ((defaultFrame = (SFrame) servletManager
0191: .getValue(AMAZING)) == null) {
0192: defaultFrame = new SFrame(AMAZING);
0193: defaultFrame.setFont(SFont.getFont("Arial,Helvetica",
0194: SFont.PLAIN, 12));
0195: }
0196:
0197: //#1 Test tabbed pane as JSP - comment out tabbed pane above
0198: if (tabbed == null) {
0199: tabbed = (STabbedPane) getTabbedPane();
0200: servletManager.putValue(tabbed.getName(), tabbed);
0201: }
0202: SComponent component = servletManager.handle(request,
0203: response, tabbed.getName());
0204:
0205: //#2 Test tool bar as JSP - comment out toolbar above
0206: //if( toolBar == null )
0207: //{
0208: // toolBar = (SToolBar)getToolBar();
0209: // servletManager.putValue( toolBar.getName(), toolBar );
0210: //}
0211: //SComponent component = servletManager.handle( request, response, toolBar.getName() );
0212:
0213: //#3 Test menu bar as JSP
0214: //if( menuBar == null )
0215: //{
0216: // dialog = new SDialog();
0217: // dialog.setTitle( "About" );
0218: // dialog.add( getDialogContents() );
0219: // menuBar = getDemoMenu();
0220: // servletManager.putValue( menuBar.getName(), menuBar );
0221: //}
0222: //SComponent component = servletManager.handle( request, response, menuBar.getName() );
0223:
0224: response.setContentType(component.getContentType());
0225:
0226: Object output = ServletManager.getOutput(component,
0227: response);
0228:
0229: //Paint the header - only necessary in Netscrape
0230: defaultFrame.paintComponentHeader(output);
0231:
0232: //#1 Test tabbed pane as JSP - comment out tabbed pane above
0233: tabbed.paint(output);
0234:
0235: //#2 Test tool bar as JSP - comment out toolbar above
0236: //toolBar.paint( ServletManager.getOutput( component, response ) );
0237:
0238: //#3 Test menu bar as JSP
0239: //menuBar.paint( ServletManager.getOutput( component, response ) );
0240:
0241: //Paint the footer - only necessary in Netscrape
0242: defaultFrame.paintComponentFooter(output);
0243:
0244: } catch (Throwable e) {
0245: e.printStackTrace();
0246: response.sendError(response.SC_INTERNAL_SERVER_ERROR, e
0247: .getMessage());
0248: } finally {
0249: SwingletManager.setSwingletManager(null);
0250: }
0251: }
0252:
0253: public SComponent getDialogContents() {
0254: SFont plain10 = SFont.getFont("Arial,Helvetica", SFont.PLAIN,
0255: 10);
0256: SFont plain12 = SFont.getFont("Arial,Helvetica", SFont.PLAIN,
0257: 12);
0258:
0259: STable table = new STable(8, 1);
0260: table.setGridWidth(0);
0261: table.setIntercellSpacing(new Dimension(5, 5));
0262:
0263: table.setHorizontalAlignmentAt(SConstants.CENTER, 0, 0);
0264: table.setValueAt(new SIcon("about/swinglets.gif"), 0, 0);
0265:
0266: table.setHorizontalAlignmentAt(SConstants.CENTER, 1, 0);
0267: table.setValueAt(new SIcon("about/phrase.gif"), 1, 0);
0268:
0269: table
0270: .setValueAt(
0271: new SLabel(
0272: "Program your Servlets like you program your Swing components. Servlets use an identical design to Swing. The Components, Models, Renderers, and LookAndFeels and very similar. In fact Swinglets actually use the Swing models (e.g. TableModel). This means you can take your existing models and start working with Servlets straight away. It's just about as close to a standard as you can get without it acutally coming from Sun themselves.")
0273: .setFont(plain10), 2, 0);
0274: table
0275: .setValueAt(
0276: new SLabel(
0277: "If you are familar with Swing you can quickly get your Swing applications running in Web browsers, without the hassle of downloading 2M of classes. For Servlet developers you'll be amazed at how easy it is to write complex Servlets, and you'll wish you had Swinglets before. For HTML based web applications all the ugly tagging and variable substitution is removed for you.")
0278: .setFont(plain10), 3, 0);
0279:
0280: table.setValueAt(new SLabel(
0281: "Contact robin.sharp@javelinsoft for futher details.")
0282: .setFont(plain10), 4, 0);
0283:
0284: table.setValueAt(new SLabel(new SIcon("about/robin.gif", 100,
0285: 120), "Robin Sharp - Principal Author.")
0286: .setVerticalAlignment(SConstants.CENTER).setFont(
0287: plain12), 5, 0);
0288:
0289: table.setValueAt(new SLabel(new SIcon("about/dino.jpg"),
0290: "Dino Fancellu - Co Author.").setVerticalAlignment(
0291: SConstants.CENTER).setFont(plain12), 6, 0);
0292:
0293: table.setHorizontalAlignmentAt(SConstants.CENTER, 7, 0);
0294: table.setValueAt(new SLabel(
0295: "Copyright Javelin Software, all rights reserved.")
0296: .setFont(plain10), 7, 0);
0297:
0298: return table;
0299: }
0300:
0301: public SMenuBar getDemoMenu() {
0302: //Deeply nested menus
0303: menuBar = new SMenuBar();
0304: menuBar.setSize(-100, 400);
0305: //menuBar.setSize( 700, 400 );
0306:
0307: menuBar.setBorderPainted(true);
0308: //menuBar.setBackground( SColor.lightGray );
0309: menuBar.setFont(SFont.getFont("Arial,Helvetica", SFont.PLAIN,
0310: 12));
0311:
0312: ActionListener listener = new ActionListener() {
0313: public void actionPerformed(ActionEvent event) {
0314: if (tabbed == null)
0315: return;
0316:
0317: if (((SMenuItem) event.getSource()).getText().equals(
0318: "Top"))
0319: tabbed.setTabPlacement(SConstants.TOP);
0320: else if (((SMenuItem) event.getSource()).getText()
0321: .equals("Bottom"))
0322: tabbed.setTabPlacement(SConstants.BOTTOM);
0323: else if (((SMenuItem) event.getSource()).getText()
0324: .equals("Left"))
0325: tabbed.setTabPlacement(SConstants.LEFT);
0326: else if (((SMenuItem) event.getSource()).getText()
0327: .equals("Right"))
0328: tabbed.setTabPlacement(SConstants.RIGHT);
0329: else if (((SMenuItem) event.getSource()).getText()
0330: .equals("metal"))
0331: tabbed.getSwingletManager().setTheme(
0332: STheme.getTheme("metal"));
0333: else if (((SMenuItem) event.getSource()).getText()
0334: .equals("emerald"))
0335: tabbed.getSwingletManager().setTheme(
0336: STheme.getTheme("emerald"));
0337: else if (((SMenuItem) event.getSource()).getText()
0338: .equals("windows"))
0339: tabbed.getSwingletManager().setTheme(
0340: STheme.getTheme("windows"));
0341: else if (((SMenuItem) event.getSource()).getText()
0342: .equals("HTML"))
0343: tabbed
0344: .getTopLevelAncestor()
0345: .setLookAndFeel(
0346: "com.javelin.swinglets.plaf.html.HTMLLookAndFeel");
0347: else if (((SMenuItem) event.getSource()).getText()
0348: .equals("JavaScript"))
0349: tabbed
0350: .getTopLevelAncestor()
0351: .setLookAndFeel(
0352: "com.javelin.swinglets.plaf.javascript.JSLookAndFeel");
0353: }
0354: };
0355:
0356: ActionListener listener1 = new ActionListener() {
0357: public void actionPerformed(ActionEvent event) {
0358: if (menuBar == null)
0359: return;
0360:
0361: if (((SMenuItem) event.getSource()).getText().equals(
0362: "Top"))
0363: menuBar.setMenuPlacement(SConstants.TOP);
0364: else if (((SMenuItem) event.getSource()).getText()
0365: .equals("Bottom"))
0366: menuBar.setMenuPlacement(SConstants.BOTTOM);
0367: else if (((SMenuItem) event.getSource()).getText()
0368: .equals("Left"))
0369: menuBar.setMenuPlacement(SConstants.LEFT);
0370: else if (((SMenuItem) event.getSource()).getText()
0371: .equals("Right"))
0372: menuBar.setMenuPlacement(SConstants.RIGHT);
0373: }
0374: };
0375:
0376: SMenu menu1 = new SMenu("Menu >>");
0377: menu1.add((SMenuItem) new SMenuItem("Top", new SLink(null,
0378: SFrame.TOP)).addActionListener(listener1));
0379: menu1.add((SMenuItem) new SMenuItem("Left", new SLink(null,
0380: SFrame.TOP)).addActionListener(listener1));
0381: menu1.add((SMenuItem) new SMenuItem("Bottom", new SLink(null,
0382: SFrame.TOP)).addActionListener(listener1));
0383: menu1.add((SMenuItem) new SMenuItem("Right", new SLink(null,
0384: SFrame.TOP)).addActionListener(listener1));
0385: menuBar.add(menu1);
0386:
0387: SMenu menu3 = new SMenu("Tabs >>");
0388: menu3.add((SMenuItem) new SMenuItem("Top", new SLink(null,
0389: SFrame.TOP)).addActionListener(listener));
0390: menu3.add((SMenuItem) new SMenuItem("Left", new SLink(null,
0391: SFrame.TOP)).addActionListener(listener));
0392: menu3.add((SMenuItem) new SMenuItem("Bottom", new SLink(null,
0393: SFrame.TOP)).addActionListener(listener));
0394: menu3.add((SMenuItem) new SMenuItem("Right", new SLink(null,
0395: SFrame.TOP)).addActionListener(listener));
0396: menuBar.add(menu3);
0397:
0398: SMenu menuThemes = new SMenu("Themes >>");
0399: menuThemes.add((SMenuItem) new SMenuItem("metal", new SLink(
0400: null, SFrame.TOP)).addActionListener(listener));
0401: menuThemes.add((SMenuItem) new SMenuItem("emerald", new SLink(
0402: null, SFrame.TOP)).addActionListener(listener));
0403: menuThemes.add((SMenuItem) new SMenuItem("windows", new SLink(
0404: null, SFrame.TOP)).addActionListener(listener));
0405: menuBar.add(menuThemes);
0406:
0407: SMenu menuLAF = new SMenu("LookAndFeel >>");
0408: menuLAF.add((SMenuItem) new SMenuItem("HTML", new SLink(null,
0409: SFrame.TOP)).addActionListener(listener));
0410: menuLAF.add((SMenuItem) new SMenuItem("JavaScript", new SLink(
0411: null, SFrame.TOP)).addActionListener(listener));
0412: menuBar.add(menuLAF);
0413:
0414: menuBar.add(getMenu2());
0415:
0416: menuBar.add((SMenuItem) new SMenuItem("About",
0417: new SLink(dialog)).addActionListener(listener));
0418:
0419: return menuBar;
0420: }
0421:
0422: public SMenu getMenu2() {
0423: SMenu menu2 = new SMenu("Test >>");
0424:
0425: SMenu menu21 = new SMenu("Test 2.1 >>");
0426: menu2.add(menu21);
0427:
0428: SMenu menu211 = new SMenu("Test Icons >>");
0429: menu21.add(menu211);
0430:
0431: menu211.add(new SMenuItem("Red", new SIcon("red-ball.gif")));
0432: menu211.add(new SMenuItem("Blue", new SIcon("blue-ball.gif")));
0433: menu211
0434: .add(new SMenuItem("Green", new SIcon("green-ball.gif")));
0435: menu211.add(new SMenuItem("Cyan", new SIcon("cyan-ball.gif")));
0436: menu211.add(new SMenuItem("Magenta", new SIcon(
0437: "magenta-ball.gif")));
0438: menu211.add(new SMenuItem("Yellow",
0439: new SIcon("yellow-ball.gif")));
0440:
0441: menu21.add(new SMenuItem("Item 2.1.2"));
0442:
0443: menu2.add(new SMenuItem("Item 2.2"));
0444:
0445: return menu2;
0446: }
0447:
0448: public SComponent getToolBar() {
0449: toolBar = new SToolBar();
0450: toolBar.setSize(-100, 400);
0451: //toolBar.setSize( 700, 400 );
0452:
0453: //toolBar.setBackground( SColor.lightGray );
0454: toolBar.setBorderPainted(false);
0455:
0456: toolBar
0457: .add(new SToggleButton(false).setIcon(
0458: new SIcon("icons/Document.gif"))
0459: .setSelectedIcon(
0460: new SIcon("icons/ToggleDocument.gif"))
0461: .setEnabled(true));
0462:
0463: toolBar.add(new SButton(new SIcon("icons/Open.gif"))
0464: .setEnabled(false));
0465: toolBar.add(new SButton(new SIcon("icons/Save.gif"))
0466: .setEnabled(false));
0467:
0468: toolBar.add(new SSeparator());
0469:
0470: toolBar.add(new SButton(new SIcon("icons/Cut.gif"))
0471: .setEnabled(false));
0472: toolBar.add(new SButton(new SIcon("icons/Copy.gif"))
0473: .setEnabled(false));
0474: toolBar.add(new SButton(new SIcon("icons/Paste.gif"))
0475: .setEnabled(false));
0476:
0477: toolBar.add(new SSeparator());
0478:
0479: ActionListener rewindListener = new ActionListener() {
0480: public void actionPerformed(ActionEvent event) {
0481: if (tabbed != null && tabbed.getSelectedIndex() > 0)
0482: tabbed.setSelectedIndex(0);
0483: }
0484: };
0485:
0486: ActionListener backListener = new ActionListener() {
0487: public void actionPerformed(ActionEvent event) {
0488: if (tabbed != null && tabbed.getSelectedIndex() > 0)
0489: tabbed
0490: .setSelectedIndex(tabbed.getSelectedIndex() - 1);
0491: }
0492: };
0493:
0494: ActionListener forwardListener = new ActionListener() {
0495: public void actionPerformed(ActionEvent event) {
0496: if (tabbed != null
0497: && tabbed.getSelectedIndex() < tabbed
0498: .getTabCount() - 1)
0499: tabbed
0500: .setSelectedIndex(tabbed.getSelectedIndex() + 1);
0501: }
0502: };
0503:
0504: ActionListener fastForwardListener = new ActionListener() {
0505: public void actionPerformed(ActionEvent event) {
0506: if (tabbed != null
0507: && tabbed.getSelectedIndex() < tabbed
0508: .getTabCount() - 1)
0509: tabbed.setSelectedIndex(tabbed.getTabCount() - 1);
0510: }
0511: };
0512:
0513: toolBar.add(new SButton(new SIcon("icons/VCRRewind.gif"))
0514: .addActionListener(rewindListener));
0515: toolBar.add(new SButton(new SIcon("icons/VCRBack.gif"))
0516: .addActionListener(backListener));
0517: toolBar.add(new SButton(new SIcon("icons/VCRForward.gif"))
0518: .addActionListener(forwardListener));
0519: toolBar.add(new SButton(new SIcon("icons/VCRFastForward.gif"))
0520: .addActionListener(fastForwardListener));
0521:
0522: return toolBar;
0523: }
0524:
0525: public SComponent getTabbedPane() throws Exception {
0526: tabbed = new STabbedPane(SConstants.TOP);
0527: tabbed.setBorderPainted(false);
0528: //tabbed.setSize( 700, 400 );
0529: tabbed.setSize(-100, 400);
0530:
0531: tabbed.setForeground(SColor.black);
0532:
0533: tabbed.addTab("Swinglets", getSplash());
0534: tabbed.addTab("Tree", getTree());
0535: tabbed.addTab("Table", getTable());
0536: tabbed.addTab("SplitPane", getSplitPane());
0537: tabbed.addTab("Image Map", new SIcon("swing/cowSmall.gif"),
0538: getImageMap());
0539: tabbed.addTab("Rollover", getLayout());
0540: tabbed.addTab("Forms", getForms());
0541: tabbed.addTab("Xtra", getExtra());
0542: tabbed.addTab("Source", getSource());
0543: tabbed.addTab("Security", getSecurity());
0544:
0545: //tabbed.setBackground( SColor.getColor( "skyblue1" ) );
0546: tabbed.setTabbedCellRenderer(new HTMLImageTabbedCellRenderer());
0547:
0548: //if( tabbed.getTabCount() > 2 )
0549: // tabbed.setEnabledAt(2, false);
0550:
0551: return tabbed;
0552: }
0553:
0554: public SComponent getSplash() {
0555: SIcon icon = new SIcon("swinglets.jpg", 700, 400);
0556: icon.setLink(new SLink()).addActionListener(
0557: new ActionListener() {
0558: public void actionPerformed(ActionEvent event) {
0559: System.out.println("Splash");
0560: }
0561: });
0562: return icon;
0563: }
0564:
0565: public SComponent getSecurity() {
0566: class SecurityForm extends SForm implements FormListener {
0567: STextField name = new STextField(20);
0568: SButton logoutButton = new SButton("Log out");
0569: SButton loginButton = new SButton("Log in");
0570: boolean login = false;
0571:
0572: public void onPaintHeader() {
0573: ServletManager sman = (ServletManager) getSwingletManager();
0574: HttpSession sess = sman.getSession();
0575: String user = (String) sess.getValue("userId");
0576: if (user == null || user.length() == 0) {
0577: removeAll();
0578: add(new SLabel("Login in to see source/BallsPanel"));
0579: add(name);
0580:
0581: add(loginButton);
0582:
0583: login = true;
0584: } else {
0585: removeAll();
0586: add(new SLabel("Logged in as " + user));
0587: add(logoutButton);
0588: login = false;
0589: }
0590:
0591: }
0592:
0593: public SecurityForm() {
0594: addFormEventListener(this );
0595: }
0596:
0597: public void formReset(FormEvent formEvent) {
0598: }
0599:
0600: public void formSubmitted(FormEvent formEvent) {
0601: System.out.println("formSubmitted"
0602: + formEvent.getSource().toString());
0603: System.out.println("Action" + formEvent.getAction());
0604:
0605: if (login) {
0606: ServletManager sman = (ServletManager) getSwingletManager();
0607: HttpSession sess = sman.getSession();
0608: System.out.println("name=" + name.getText());
0609: sess.putValue("userId", name.getText());
0610: } else // logout
0611: {
0612: ServletManager sman = (ServletManager) getSwingletManager();
0613: HttpSession sess = sman.getSession();
0614: sess.removeValue("userId");
0615: }
0616: //selected.setText(combo1.getSelectedItem().toString());
0617: }
0618: }
0619: SForm form = new SecurityForm();
0620:
0621: return form;
0622: }
0623:
0624: public SComponent getTree() {
0625: treeLabel = (SLabel) new SLabel("").setFont(SFont.getFont(
0626: "Arial,Helvetica", SFont.BOLD, 20));
0627: SPanel panel = new SPanel();
0628: panel
0629: .add(new SLabel(
0630: "Here's a demo of the STree using the Swing TreeModel.")
0631: .setFont(SFont.getFont("Arial,Helvetica",
0632: SFont.PLAIN, 14)));
0633: STree tree = new DemoTree();
0634: tree.setOpaque(false);
0635: ActionListener treeListener = new ActionListener() {
0636: public void actionPerformed(ActionEvent event) {
0637: if (event instanceof TreeActionEvent) {
0638: treeLabel.setText(((TreeActionEvent) event)
0639: .getTreePath().getLastPathComponent()
0640: .toString());
0641:
0642: if (event.getID() == STree.LABEL_SELECTED) {
0643: treeLabel.setForeground(SColor.black);
0644: } else if (event.getID() == STree.NODE_EXPANDED) {
0645: treeLabel.setForeground(SColor.green);
0646: } else if (event.getID() == STree.NODE_COLLAPSED) {
0647: treeLabel.setForeground(SColor.red);
0648: }
0649: }
0650: }
0651: };
0652:
0653: tree.addActionListener(treeListener);
0654:
0655: panel.add(tree);
0656:
0657: SSplitPane splitPane = new SSplitPane(
0658: SSplitPane.VERTICAL_SPLIT, panel, treeLabel);
0659:
0660: splitPane.setSize(700, 400);
0661: return splitPane;
0662: }
0663:
0664: public SComponent getTable() {
0665: return new SPanel() {
0666: public void onFirstPaint() {
0667: setLayoutManager(new SFlowLayout(SConstants.LEFT));
0668: add(new SLabel(
0669: "Here's a demo of the STable using the Swing TableModel."
0670: + "The table header has been set to the SortedTableHeader. "
0671: + "The table footer has been set to the PagedTableFooter. "
0672: + "Click on the table headers to sort the columns!")
0673: .setFont(SFont.getFont("Arial,Helvetica",
0674: SFont.ITALIC, 14)));
0675: add(new SLabel("Click here to go to bottom of table",
0676: new SLink("#bottom")).setAnchor("top"));
0677: add(new DemoTable());
0678: add(new SLabel("Click here to go to top of table",
0679: new SLink("#top")).setAnchor("bottom"));
0680: }
0681: };
0682: }
0683:
0684: public SComponent getSplitPane() {
0685: SSplitPane splitPane = new SSplitPane(
0686: SSplitPane.HORIZONTAL_SPLIT, new BallsPanel(true),
0687: new BallsPanel(false));
0688:
0689: return splitPane;
0690: }
0691:
0692: public SComponent getImageMap() {
0693: SIcon icon = new SIcon("swing/cow.gif");
0694: SLink link = new SLink(icon.getSwingletManager().getUrl());
0695: icon.addArea(new SArea(new Rectangle(17, 166, 100, 100),
0696: "Square", link));
0697: icon
0698: .addArea(new SArea(new Point(423, 208), 40, "Circle",
0699: link));
0700: icon
0701: .addArea(new SArea(new Polygon(new int[] { 7, 37, 59,
0702: 48 }, new int[] { 64, 23, 113, 62 }, 4),
0703: "Black Front", link));
0704: icon.addArea(new SArea(new Polygon(new int[] { 74, 104, 176,
0705: 150, 92 }, new int[] { 141, 21, 31, 231, 129 }, 5),
0706: "Black Middle", link));
0707: icon.addArea(new SArea(new Polygon(new int[] { 266, 171, 209,
0708: 281, 278, 430, 463, 426, 360, 316 }, new int[] { 27,
0709: 89, 294, 159, 102, 128, 48, 14, 64, 20 }, 10),
0710: "Black Back", link));
0711: return icon;
0712: }
0713:
0714: public SComponent getLayout() {
0715: SPanel panel = new SPanel();
0716: panel.setLayoutManager(new SBorderLayout());
0717:
0718: // must create centre text icon first as we will refer to it as a rollover targer
0719: SIcon texticon = new SIcon("orbx/orb-txt0.jpg", 175, 25);
0720: SIcon titleicon = new SIcon("orbx/orb-title.jpg", 175, 110);
0721:
0722: //NORTH
0723: panel.add(new SIcon("orbx/orb-topoff.jpg", 350, 110).setLink(
0724: new SLink("javascript:void(0);")).addRolloverTarget(
0725: texticon, new SIcon("orbx/orb-txt1.jpg"))
0726: .setRolloverIcon(
0727: new SIcon("orbx/orb-topon.jpg", 350, 110)),
0728: SBorderLayout.NORTH);
0729:
0730: //WEST
0731: panel.add(new SIcon("orbx/orb-leftoff.jpg", 110, 180).setLink(
0732: new SLink("javascript:void(0);")).addRolloverTarget(
0733: texticon, new SIcon("orbx/orb-txt4.jpg"))
0734: .setRolloverIcon(
0735: new SIcon("orbx/orb-lefton.jpg", 350, 110)),
0736: SBorderLayout.WEST);
0737:
0738: //CENTER
0739: SPanel centerPanel = new SPanel();
0740: centerPanel.setLayoutManager(new SGridLayout(3, 1));
0741: centerPanel.add(titleicon);
0742: centerPanel.add(texticon);
0743: centerPanel.add(new SIcon("orbx/orb-screenbot.jpg", 175, 45));
0744: panel.add(centerPanel, SBorderLayout.CENTER);
0745:
0746: //EAST
0747: panel.add(new SIcon("orbx/orb-rightoff.jpg", 115, 180).setLink(
0748: new SLink("javascript:void(0);")).addRolloverTarget(
0749: texticon, new SIcon("orbx/orb-txt2.jpg"))
0750: //.addRolloverTarget(titleicon,new SIcon("orbx/orb-txt2.jpg"))
0751: .setRolloverIcon(
0752: new SIcon("orbx/orb-righton.jpg", 350, 110)),
0753: SBorderLayout.EAST);
0754:
0755: //SOUTH
0756: panel.add(new SIcon("orbx/orb-botoff.jpg", 350, 110).setLink(
0757: new SLink("javascript:void(0);")).addRolloverTarget(
0758: texticon, new SIcon("orbx/orb-txt3.jpg"))
0759: .setRolloverIcon(
0760: new SIcon("orbx/orb-boton.jpg", 350, 110))
0761: .setToolTipText("Please do not hit me"),
0762: SBorderLayout.SOUTH);
0763:
0764: panel.setBorder(new SEmptyBorder(30, 120, 100, 150)
0765: .setBackgroundIcon(new SIcon("orbx/orb-back.jpg", 700,
0766: 400)));
0767:
0768: return panel;
0769: }
0770:
0771: public SComponent getButtons() //throws MalformedURLException
0772: {
0773: return new SForm() {
0774: public void onFirstPaint() {
0775: setBorder(new SEmptyBorder(60, 40, 20, 0));
0776: setLayoutManager(new SGridLayout(5, 5));
0777: //setBackground( SColor.getColor("lightblue") );
0778:
0779: addScript(new SScript(SScript.JAVASCRIPT)
0780: .setScript("function welcomeButtons() { alert('Welcome Buttons'); }"));
0781:
0782: add(new SButton("Submit")
0783: .addMouseListener(new ScriptedMouseListener(
0784: SScript.JAVASCRIPT)
0785: .setMousePressedScript("welcomeButtons()")));
0786:
0787: add(new SButton("reset"));
0788: add(new SButton("OK"));
0789: add(new SButton("button"));
0790: add(new SButton(new SIcon("red-ball.gif", 40, 20)));
0791:
0792: add(new SButton("Submit").setEnabled(false));
0793: add(new SButton("reset").setEnabled(false));
0794: add(new SButton("OK").setEnabled(false));
0795: add(new SButton("button").setEnabled(false));
0796: add(new SIcon("red-ball.gif", 40, 20).setEnabled(false));
0797:
0798: try {
0799: URL url = SwingletManager.getSwingletManager()
0800: .getImageResource("blubut.gif");
0801: if (url == null)
0802: throw new IllegalStateException("URL IS NULL");
0803:
0804: SImage image = new SImage(SwingletManager
0805: .getSwingletManager().getImageResource(
0806: "blubut.gif"));
0807: //SImage image = new SImage( 90, 30 );
0808: DateFormat formatter = new SimpleDateFormat(
0809: "hh::mm::ss");
0810: image.getGraphics().drawString(
0811: formatter.format(new Date()), 10, 20);
0812: SLabel label = (SLabel) new SLabel(
0813: "A synthetic image showing the current time."
0814: + "This could be used to annotate images or "
0815: + "generate menu items.")
0816: .setFont(SFont.getFont("Comic Sans MS",
0817: SFont.PLAIN, 10));
0818: add(label);
0819:
0820: SIcon icon = new SIcon(image, 150, 40);
0821:
0822: add(new SButton(icon));
0823:
0824: } catch (MalformedURLException e) {
0825: e.printStackTrace();
0826: }
0827: }
0828: };
0829: }
0830:
0831: public SComponent getMoreButtons() {
0832: return new SForm() {
0833: public void onFirstPaint() {
0834: setBorder(new SBevelBorder(1, true, SColor.darkGray));
0835:
0836: STable table = new STable(6, 5);
0837:
0838: table
0839: .setValueAt("Enabled Vertical Radio Buttons",
0840: 0, 0);
0841: SButtonGroup friends = new SButtonGroup();
0842: friends.add(new SRadioButton("Amanda"));
0843: friends.add(new SRadioButton("Amber"));
0844: friends.add(new SRadioButton("Becky"));
0845: friends.add(new SRadioButton("Sabrina"));
0846: friends.add(new SRadioButton("Scarlet", true));
0847: friends.add(new SRadioButton("Tamar"));
0848: table.setValueAt(friends, 1, 0);
0849:
0850: //ADD A BUTTON GROUP OF MATES - MOVE THE TEXT TO THE LEFT OF THE BUTTON
0851: //MAKE THE ALIGNMENT HORIZONTAL AND DISABLE THE SELCTION
0852: //ADD THE GROUP TO THE TABLE AT 5,1
0853: table.setValueAt("Disabled Horizontal Radio Buttons",
0854: 0, 2);
0855: SButtonGroup mates = new SButtonGroup();
0856: mates.add(new SRadioButton("Adam"));
0857: mates.add(new SRadioButton("Dino"));
0858: mates.add(new SRadioButton("John"));
0859: mates.add(new SRadioButton("Ray"));
0860: mates.setHorizontalAlignment(SConstants.LEFT);
0861: mates.setVerticalAlignment(SConstants.HORIZONTAL);
0862: mates.setEnabled(false);
0863: table.setValueAt(mates, 1, 2);
0864:
0865: //Add a Check Box
0866: table.setValueAt("Check Box", 2, 0);
0867: SCheckBox checkBox1 = new SCheckBox("Want free money.");
0868: table.setValueAt(checkBox1, 3, 0);
0869:
0870: table.setValueAt("Disabled Check Box", 2, 1);
0871: SCheckBox checkBox2 = new SCheckBox("Want no money.");
0872: checkBox2.setEnabled(false);
0873: table.setValueAt(checkBox2, 3, 1);
0874:
0875: //Add a Check Box
0876: table.setValueAt("File Chooser", 4, 0);
0877: SFileChooser fileChooser1 = new SFileChooser();
0878: table.setValueAt(fileChooser1, 5, 0);
0879:
0880: table.setValueAt("Disabled File Chooser", 4, 1);
0881: SFileChooser fileChooser2 = new SFileChooser();
0882: fileChooser2.setEnabled(false);
0883: table.setValueAt(fileChooser2, 5, 1);
0884:
0885: table.setBackground(SColor.getColor("lightblue"));
0886:
0887: add(table);
0888: }
0889: };
0890: }
0891:
0892: public SComponent getSelections() {
0893: return new SForm() {
0894: public void onFirstPaint() {
0895: STable table = new STable(5, 5);
0896: table.setBackground(SColor.blue.brighter());
0897:
0898: Object[] data = new Object[] { "The", "Quick", "Brown",
0899: "Fox", "Jumped", "Over", "The", "Lazy", "Dogs",
0900: "Back" };
0901:
0902: table.setValueAt(new SLabel("Multiple Selection")
0903: .setForeground(SColor.blue).setBackground(
0904: SColor.white), 0, 0);
0905: SList list1 = new SList(data);
0906: list1
0907: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
0908: table.setValueAt(list1, 1, 0);
0909:
0910: table.setValueAt(new SLabel("Single Selection")
0911: .setForeground(SColor.blue).setBackground(
0912: SColor.white), 0, 1);
0913: SList list2 = new SList(data);
0914: list2
0915: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0916: table.setValueAt(list2, 1, 1);
0917:
0918: table.setValueAt(new SLabel("Combo Selection")
0919: .setForeground(SColor.blue).setBackground(
0920: SColor.white), 0, 2);
0921: SComboBox combo1 = new SComboBox(data);
0922: table.setValueAt(combo1, 1, 2);
0923:
0924: table.setValueAt(new SLabel(
0925: "Multiple Selection Disabled").setForeground(
0926: SColor.red).setBackground(SColor.white), 2, 0);
0927: SList list3 = new SList(data);
0928: list3
0929: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
0930: list3.setEnabled(false);
0931: table.setValueAt(list3, 3, 0);
0932:
0933: table.setValueAt(
0934: new SLabel("Single Selection Disabled")
0935: .setForeground(SColor.red)
0936: .setBackground(SColor.white), 2, 1);
0937: SList list4 = new SList(data);
0938: list4
0939: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0940: list4.setEnabled(false);
0941: table.setValueAt(list4, 3, 1);
0942:
0943: table.setValueAt(new SLabel("Combo Selection Disabed")
0944: .setForeground(SColor.red).setBackground(
0945: SColor.white), 2, 2);
0946: SComboBox combo2 = new SComboBox(data);
0947: combo2.setEnabled(false);
0948: table.setValueAt(combo2, 3, 2);
0949:
0950: add(table);
0951: }
0952: };
0953: }
0954:
0955: public SComponent getText() {
0956: return new SForm() {
0957: public void onFirstPaint() {
0958: STable table = new STable(6, 6);
0959: table.setSize(600, 200);
0960:
0961: table.setGridWidth(5);
0962:
0963: table.setValueAt("Text Field with 10 columns.", 0, 0);
0964: STextField textField1 = new STextField("0123456789",
0965: 10, 10);
0966: table.setValueAt(textField1, 1, 0);
0967:
0968: table.setValueAt("Disabled Text Field.", 0, 1);
0969: STextField textField2 = new STextField("9876543210",
0970: 10, 10);
0971: textField2.setEnabled(false);
0972: table.setValueAt(textField2, 1, 1);
0973:
0974: table
0975: .setValueAt("Password Field with 8 columns.",
0976: 2, 0);
0977: SPasswordField passwordField1 = new SPasswordField(
0978: "PASWORD", 8, 8);
0979: table.setValueAt(passwordField1, 3, 0);
0980:
0981: table.setValueAt("Disabled Password Field.", 2, 1);
0982: SPasswordField passwordField2 = new SPasswordField(
0983: "PASWORD", 8, 8);
0984: passwordField2.setEnabled(false);
0985: table.setValueAt(passwordField2, 3, 1);
0986:
0987: table.setValueAt("Text Area", 4, 0);
0988: String text = "Swinglets is the Server Side UI Toolkit. Use Swinglets in Servlets to "
0989: + "generate HTML, JavaScript, XML or WML pages depending on the browser.";
0990:
0991: STextArea textArea1 = new STextArea(text, 20, 30);
0992: table.setValueAt(textArea1, 5, 0);
0993:
0994: table.setValueAt("Disabled Text Area", 4, 1);
0995: STextArea textArea2 = new STextArea(text, 20, 30);
0996: textArea2.setEnabled(false);
0997: table.setValueAt(textArea2, 5, 1);
0998:
0999: add(table);
1000: }
1001: };
1002: }
1003:
1004: public SComponent getForms() {
1005: STabbedPane tabbed = new STabbedPane(SConstants.LEFT);
1006:
1007: tabbed
1008: .setFont(SFont.getFont("Arial,Helvetica", SFont.BOLD,
1009: 12));
1010:
1011: tabbed.setForeground(SColor.black);
1012: tabbed.setBackground(SColor.getColor("darkseagreen"));
1013:
1014: tabbed.addTab("Buttons", getButtons());
1015: tabbed.addTab("Buttons++", getMoreButtons());
1016: tabbed.addTab("Selection", getSelections());
1017: tabbed.addTab("Text", getText());
1018: tabbed.addTab("Funds", new DemoFunds());
1019:
1020: return tabbed;
1021: }
1022:
1023: public SComponent getExtra() {
1024: STabbedPane tabbed = new STabbedPane(SConstants.LEFT);
1025: tabbed.setBorderPainted(true);
1026: tabbed.setTabbedCellRenderer(
1027: "com.javelin.swinglets.plaf.html.HTMLLookAndFeel",
1028: new HTMLDefaultTabbedCellRenderer());
1029: tabbed.setTabbedCellRenderer(
1030: "com.javelin.swinglets.plaf.javascript.JSLookAndFeel",
1031: new JSDefaultTabbedCellRenderer());
1032:
1033: tabbed
1034: .setFont(SFont.getFont("Arial,Helvetica", SFont.BOLD,
1035: 12));
1036:
1037: tabbed.addTab("Frames", getIFrames());
1038: tabbed.addTab("SQL", getSQL());
1039:
1040: return tabbed;
1041: }
1042:
1043: public SComponent getSQL() {
1044: return new SPanel() {
1045: public void onFirstPaint() {
1046: add(new DemoSQL());
1047: }
1048: };
1049: }
1050:
1051: int frameCount = 0;
1052:
1053: public SComponent getIFrames() {
1054: return new SPanel() {
1055: void addWindow() {
1056: SInternalFrame iframe = new SInternalFrame();
1057: frameCount++;
1058: iframe.setTitle("Hello: "
1059: + Integer.toString(frameCount));
1060: iframe.setDesktopTitle("Open: "
1061: + Integer.toString(frameCount));
1062: iframe.setSize(200 + ((frameCount % 10) * 10), 100);
1063: iframe.setLayoutManager(new SGridLayout(3, 1, 5));
1064: iframe.add(new SLabel("Hello World @ "
1065: + new java.util.Date()));
1066: iframe.add(new SLabel("How Ya' doing."));
1067: iframe.add(new SLabel("Get a cup'o Java brewing."));
1068: add(iframe);
1069: }
1070:
1071: public void onFirstPaint() {
1072: SForm form = new SForm();
1073:
1074: form.add(new SButton("New window"));
1075:
1076: add(form);
1077: addWindow();
1078:
1079: form.addFormEventListener(new FormListener() {
1080: public void formReset(FormEvent formEvent) {
1081: };
1082:
1083: public void formSubmitted(FormEvent formEvent) {
1084: System.out.println("formSubmitted"
1085: + formEvent.getSource().toString());
1086: System.out.println("Action"
1087: + formEvent.getAction());
1088:
1089: addWindow();
1090: }
1091: });
1092: }
1093: };
1094: }
1095:
1096: public SComponent getSource() {
1097: STabbedPane stabbed = new STabbedPane(SConstants.LEFT) {
1098: public void onPaintHeader() {
1099: ServletManager sman = (ServletManager) getSwingletManager();
1100: HttpSession sess = sman.getSession();
1101: String user = (String) sess.getValue("userId");
1102:
1103: if (user == null || user.length() == 0) {
1104: //System.out.println("Source disabled");
1105: setEnabledAt(2, false);
1106: } else {
1107: //System.out.println("Source enabled");
1108: setEnabledAt(2, true);
1109: }
1110: }
1111: };
1112: stabbed.setSize(700, 400);
1113: stabbed.setFont(SFont.getFont("Arial,Helvetica", SFont.PLAIN,
1114: 10));
1115: stabbed.setForeground(SColor.black);
1116: stabbed.setBackground(SColor.getColor("coral"));
1117:
1118: String[] files = new String[] { "HelloWorld.java",
1119: "AmazingServlet.java", "BallsPanel.java",
1120: "DemoTable.java", "DemoTree.java", "DemoFunds.java" };
1121: for (int index = 0; index < files.length; index++) {
1122: stabbed.addTab(files[index].substring(0, files[index]
1123: .length() - 5), new SPanel().add(
1124: new SInclude(getClass(), files[index])
1125: .setPreformatted(true)).setLayoutManager(
1126: new SGridLayout(1, 1)).setBackground(SColor.white));
1127: }
1128:
1129: try {
1130: URL sunurl = new URL("http://java.sun.com");
1131: SPanel p2 = new SPanel();
1132: p2.add(new SLabel("<BASE HREF=\"" + sunurl.toExternalForm()
1133: + "\">"));
1134: p2.add(new SInclude(sunurl)); //.setVerbatim(false)
1135:
1136: stabbed.add("Sun!!", p2);
1137: } catch (Exception e) {
1138: e.printStackTrace();
1139: }
1140:
1141: return stabbed;
1142: }
1143:
1144: public void showRequest(HttpServletRequest request) {
1145: System.out.println("*");
1146: for (Enumeration e = request.getParameterNames(); e
1147: .hasMoreElements();) {
1148: String name = (String) e.nextElement();
1149: System.out.println("NAME=" + name + " VALUE="
1150: + request.getParameter(name));
1151: }
1152: }
1153:
1154: }
|