001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.awt.*;
008: import java.awt.event.*;
009: import java.util.*;
010: import java.io.*;
011:
012: import com.javelin.swinglets.event.*;
013:
014: /**
015: * SForm defines a Form.
016: *
017: * @author Robin Sharp
018: */
019:
020: public class SForm extends SContainer {
021: /**
022: * The following Methods.
023: */
024: public final static String GET = "GET";
025: public final static String POST = "POST";
026: public final static String PUT = "PUT";
027: public final static String DELETE = "DELETE";
028: public final static String TRACE = "TRACE";
029: public final static String OPTIONS = "OPTIONS";
030:
031: /**
032: * Creates a SForm with the following method and action.
033: */
034: public SForm(String method, SLink action) {
035: if (method != null)
036: setMethod(method);
037: if (action != null)
038: setAction(action);
039: }
040:
041: /**
042: * Creates a SForm with the following method POST or GET.
043: */
044: public SForm(String method) {
045: this (method, null);
046: }
047:
048: /**
049: * Creates a SForm.
050: */
051: public SForm() {
052: this (POST, null);
053: }
054:
055: /**
056: * Returns the name of the L&F class that renders this component.
057: */
058: public Class getUIClass() {
059: return SForm.class;
060: }
061:
062: /**
063: * Get the method.
064: */
065: public String getMethod() {
066: return method;
067: }
068:
069: /**
070: * Set the method.
071: */
072: public SForm setMethod(String method) {
073: firePropertyChange("method", this .method, this .method = method);
074: return this ;
075: }
076:
077: /**
078: * Get the action.
079: */
080: public SLink getAction() {
081: return action;
082: }
083:
084: /**
085: * Set the action.
086: */
087: public SForm setAction(SLink action) {
088: firePropertyChange("action", this .action, this .action = action);
089: return this ;
090: }
091:
092: // EVENT /////////////////////////////////////////////////////////
093:
094: /**
095: * Add a FormEventListener.
096: */
097: public SForm addFormEventListener(FormListener formListener) {
098: if (formListeners == null)
099: formListeners = new Vector();
100:
101: formListeners.addElement(formListener);
102:
103: return this ;
104: }
105:
106: /**
107: * Remove a FormEventListener.
108: */
109: public SForm removeFormEventListener(FormListener formListener) {
110: if (formListeners == null)
111: return this ;
112:
113: formListeners.removeElement(formListener);
114:
115: return this ;
116: }
117:
118: /**
119: * Processes events occurring on this component.
120: */
121: protected void processEvent(AWTEvent event) {
122: if (event instanceof FormEvent) {
123: processFormEvent((FormEvent) event);
124: }
125: }
126:
127: /**
128: * Process an FormEvent. This will set the values of all the form components
129: * then notify any listeners that the form have been modified.
130: */
131: public void processFormEvent(FormEvent event) {
132: //DEFAULT CHECK BOXES TO FALSE IF THEY ARE NOT IN THE LIST
133: resetCheckBoxes(this , event);
134:
135: //UPDATE FORM COMPONENTS
136: for (Enumeration names = event.getParameterNames(); names
137: .hasMoreElements();) {
138: String name = names.nextElement().toString();
139:
140: //System.out.println( "name=" + name + " value=" + event.getParameter( name ) );
141:
142: SComponent component = getComponent(name);
143: if (component != null) {
144: component.setValueAsText(event.getParameter(name));
145:
146: //send an action event to all components
147: component
148: .processEvent(new ActionEvent(component,
149: ActionEvent.ACTION_PERFORMED, event
150: .getAction()));
151: }
152: }
153:
154: //FIRE EVENT OUT TO LISTENERS
155: for (int index = 0; formListeners != null
156: && index < formListeners.size(); index++) {
157: if (formListeners.elementAt(index) != null) {
158: ((FormListener) formListeners.elementAt(index))
159: .formSubmitted(event);
160: }
161: }
162: }
163:
164: /**
165: * Recursively descend all components setting the checkboxes
166: * to false, IF they are not passed as parameters.
167: */
168: protected void resetCheckBoxes(SContainer container, FormEvent event) {
169: for (int index = 0; index < container.getComponentCount(); index++) {
170: if (container.getComponent(index) instanceof SCheckBox) {
171: SCheckBox checkBox = (SCheckBox) container
172: .getComponent(index);
173: if (event.getParameter(checkBox.getName()) == null) {
174: checkBox.setSelected(false);
175: }
176: } else if (container.getComponent(index) instanceof STable) {
177: resetCheckBoxes((STable) container.getComponent(index),
178: event);
179: } else if (container.getComponent(index) instanceof SContainer) {
180: resetCheckBoxes((SContainer) container
181: .getComponent(index), event);
182: }
183: }
184: }
185:
186: /**
187: * Recursively descend all components setting the checkboxes
188: * to false, IF they are not passed as parameters.
189: */
190: protected void resetCheckBoxes(STable table, FormEvent event) {
191: for (int row = 0; row < table.getRowCount(); row++) {
192: for (int column = 0; column < table.getColumnCount(); column++) {
193: if (table.getValueAt(row, column) instanceof SCheckBox) {
194: SCheckBox checkBox = (SCheckBox) table.getValueAt(
195: row, column);
196: if (event.getParameter(checkBox.getName()) == null) {
197: checkBox.setSelected(false);
198: }
199: } else if (table.getValueAt(row, column) instanceof STable) {
200: resetCheckBoxes((STable) table.getValueAt(row,
201: column), event);
202: } else if (table.getValueAt(row, column) instanceof SContainer) {
203: resetCheckBoxes((SContainer) table.getValueAt(row,
204: column), event);
205: }
206: }
207: }
208:
209: }
210:
211: // PRIVATE /////////////////////////////////////////////////////////////
212:
213: protected String method;
214: protected SLink action;
215:
216: protected Vector formListeners;
217: }
|