001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets.plaf.wml;
006:
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010:
011: import com.javelin.swinglets.*;
012: import com.javelin.swinglets.plaf.*;
013:
014: /**
015: * WMLButtonUI defines a look and feel for default WML. A Button must
016: * exist inside a form.
017: * <P>
018: * A SButton of type SUBMIT renders itself as a card submit.
019: * <P>
020: * To use as a form submit button, add to an SForm that contains all the variables you wish to submit.
021: * Each component's variable name must be the same as the component name.
022: * For example:
023: * <code>
024: * SForm form = new SForm();
025: * form.add( new STextField( "User Name" ) );
026: * form.add( new SPasswordField( "Password" ) );
027: * form.add( new SButton( "Login", SButton.SUBMIT ) );
028: * </code>
029: *
030: * @author Robin Sharp
031: */
032:
033: public class WMLButtonUI extends WMLComponentUI {
034: /**
035: * Render the UI on the PrintWriter
036: */
037: public void update(PrintWriter out, SComponent c) {
038: if (!c.isVisible())
039: return;
040: if (!c.isEnabled())
041: return;
042:
043: SAbstractButton button = (SAbstractButton) c;
044:
045: SForm form = (SForm) button.getAncestorOfClass(SForm.class);
046:
047: if (form != null) {
048: out.print("<anchor>");
049:
050: if (button.getText() != null) {
051: out.println(button.getText());
052: }
053:
054: out.print("<go method=\"post\"");
055:
056: out.print(" href=\"");
057:
058: if (form.getAction() == null) {
059: out.print(WMLUtility.encodeURL(form, form
060: .getComponentUrl()));
061: } else {
062: out.print(WMLUtility.encodeURL(form, form.getAction()
063: .getUrl()));
064: }
065:
066: out.println("\">");
067:
068: // Output frame name as a postfield
069: out.print("<postfield name=\"");
070: out.print(SConstants.SOURCE_CONTAINER);
071: out.print("\" value=\"");
072: out.print(form.getTopLevelAncestor().getName());
073: out.println("\"/>");
074:
075: // Output **form** component name as a postfield
076: out.print("<postfield name=\"");
077: out.print(SConstants.SOURCE_COMPONENT);
078: out.print("\" value=\"");
079: out.print(form.getName());
080: out.println("\"/>");
081:
082: // Matts fix
083: updateChildComponents(out, form, button);
084:
085: out.println("</go>");
086:
087: out.println("</anchor>");
088: }
089: }
090:
091: protected void updateChildComponents(PrintWriter out,
092: SContainer parent, SAbstractButton button) {
093: SComponent child = null;
094: for (int index = 0; index < parent.getComponentCount(); index++) {
095: child = parent.getComponent(index);
096: if (child == null)
097: continue;
098:
099: if (child instanceof SContainer) {
100: updateChildComponents(out, (SContainer) child, button);
101: } else {
102: //Don't post this buttons data
103: if (!child.getName().equals(button.getName())) {
104: updateChild(out, child);
105: }
106: }
107: }
108: }
109:
110: protected void updateChild(PrintWriter out, SComponent comp) {
111: //Don't post labels, characters
112: if (comp instanceof SLabel || comp instanceof SCharacter)
113: return;
114:
115: // Output OTHER sub-components as variable names inside postfields
116: // assumes variable name is same as component name
117: out.print("<postfield name=\"");
118: out.print(comp.getName());
119: out.print("\" value=\"$(");
120: out.print(comp.getName());
121: out.println(")\"/>");
122: }
123:
124: }
125:
126: /*
127: out.print( "<do type=\"" );
128: out.print( button.getType() );
129: out.print( "\"" );
130: out.print( " label=\"" + button.getText() + "\"" );
131: out.println(" >");
132:
133: out.print( "</do>" );
134: */
|