001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.romaframework.aspect.view.echo2;
017:
018: import java.util.Calendar;
019: import java.util.EventObject;
020:
021: import nextapp.echo2.app.Component;
022: import nextapp.echo2.app.event.ActionEvent;
023: import nextapp.echo2.app.event.ActionListener;
024: import nextapp.echo2.app.event.ChangeEvent;
025: import nextapp.echo2.app.event.ChangeListener;
026: import nextapp.echo2.app.event.DocumentEvent;
027: import nextapp.echo2.app.event.DocumentListener;
028: import nextapp.echo2.app.event.ListDataEvent;
029: import nextapp.echo2.app.event.ListDataListener;
030: import nextapp.echo2.app.filetransfer.UploadEvent;
031: import nextapp.echo2.app.filetransfer.UploadListener;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.romaframework.aspect.view.echo2.component.DynaCalendarSelectionModel;
036: import org.romaframework.aspect.view.echo2.component.TracingDocument;
037: import org.romaframework.aspect.view.echo2.form.EntityForm;
038: import org.romaframework.aspect.view.echo2.form.FormUtil;
039: import org.romaframework.aspect.view.form.ContentForm;
040: import org.romaframework.core.domain.type.Stream;
041: import org.romaframework.core.schema.SchemaField;
042: import org.romaframework.core.schema.SchemaHelper;
043:
044: import echopointng.model.CalendarEvent;
045: import echopointng.model.CalendarSelectionListener;
046:
047: public class GUIEventHandler implements ActionListener,
048: DocumentListener, ListDataListener, UploadListener,
049: CalendarSelectionListener, ChangeListener {
050:
051: private static GUIEventHandler instance = new GUIEventHandler();
052: protected static Log log = LogFactory.getLog(GUIEventHandler.class);
053:
054: protected GUIEventHandler() {
055: }
056:
057: /**
058: * TextField Document Model event.
059: */
060: public void documentUpdate(DocumentEvent iEvent) {
061: if (iEvent.getSource() instanceof TracingDocument) {
062: TracingDocument doc = (TracingDocument) iEvent.getSource();
063: // BIND THE FIELD IN ITS FORM
064: try {
065: doc.getForm().bind(doc.getField(), doc.getText());
066: } catch (RuntimeException e) {
067: doc.getForm().pushException(e);
068: }
069: }
070: }
071:
072: /**
073: * Generic event.
074: */
075: public void actionPerformed(ActionEvent e) {
076: signalUpdate(e);
077: }
078:
079: /**
080: * SelectField event.
081: */
082: public void contentsChanged(ListDataEvent e) {
083: signalUpdate(e);
084: }
085:
086: /**
087: * SelectField event.
088: */
089: public void intervalAdded(ListDataEvent e) {
090: }
091:
092: /**
093: * SelectField event.
094: */
095: public void intervalRemoved(ListDataEvent e) {
096: }
097:
098: public void fileUpload(UploadEvent e) {
099: log.info("[GUIEventHandler.fileUpload] Receiving "
100: + e.getSize() + " bytes...");
101: Stream stream = new Stream(e.getInputStream(), e.getSize(), e
102: .getFileName(), e.getContentType());
103: log.info("[GUIEventHandler.fileUpload] Received: "
104: + stream.getFileName() + " of type "
105: + stream.getContentType());
106: Component c = (Component) e.getSource();
107: EntityForm form = (EntityForm) FormUtil.getComponentForm(c);
108: SchemaField field = (SchemaField) FormUtil
109: .getComponentSchema(c);
110: if (form == null || field == null)
111: return;
112: form.bind(field, stream);
113: try {
114: SchemaHelper.invokeEvent(form, field.getName());
115: } catch (Exception ex) {
116: log
117: .error(
118: "[GUIEventHandler.fileUpload] Error on invoking event callback",
119: ex);
120: }
121: }
122:
123: public void invalidFileUpload(UploadEvent e) {
124: log.error("[GUIEventHandlerinvalidFileUpload] File "
125: + e.getFileName());
126: }
127:
128: private void signalUpdate(EventObject e) {
129: Component component = null;
130: if (e instanceof CalendarEvent) {
131: DynaCalendarSelectionModel model = (DynaCalendarSelectionModel) ((CalendarEvent) e)
132: .getSource();
133: component = model.getComponent();
134: } else if (e.getSource() instanceof Component) {
135: component = (Component) e.getSource();
136: } else
137: return;
138: ContentForm form = FormUtil.getComponentForm(component);
139: SchemaField field = (SchemaField) FormUtil
140: .getComponentSchema(component);
141: if (form == null || field == null)
142: return;
143: form
144: .bind(field, FormUtil.getContentField(component),
145: component);
146: }
147:
148: public void selectedDateChange(CalendarEvent iCalEvent) {
149: Calendar cal = iCalEvent.getCalendar();
150: if (cal == null)
151: log.debug("[GUIEventHandler] reset date");
152: else
153: log.debug("[GUIEventHandler] changed selected date: "
154: + cal.getTime());
155: signalUpdate(iCalEvent);
156: }
157:
158: public void displayedDateChange(CalendarEvent iCalEvent) {
159: }
160:
161: public static GUIEventHandler getInstance() {
162: return instance;
163: }
164:
165: public void stateChanged(ChangeEvent iEvent) {
166: if (log.isDebugEnabled())
167: log.debug("[GUIEventHandler.stateChanged] Source: '"
168: + iEvent.getSource());
169: }
170: }
|