001: /*
002: * Copyright 2006 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package org.wingx.plaf.css;
015:
016: import java.io.IOException;
017: import java.util.*;
018:
019: import org.wings.*;
020: import org.wings.header.SessionHeaders;
021: import org.wings.header.Header;
022: import org.wings.event.SParentFrameEvent;
023: import org.wings.event.SParentFrameListener;
024: import org.wings.io.Device;
025: import org.wings.plaf.css.*;
026: import org.wings.plaf.css.script.OnPageRenderedScript;
027: import org.wings.plaf.css.script.OnHeadersLoadedScript;
028: import org.wings.plaf.Update;
029: import org.wings.session.ScriptManager;
030: import org.wingx.XSuggest;
031:
032: /**
033: *
034: * @author Christian Schyma
035: */
036: public class SuggestCG extends TextFieldCG implements
037: org.wingx.plaf.SuggestCG {
038: protected final List<Header> headers = new ArrayList<Header>();
039:
040: public SuggestCG() {
041: headers
042: .add(Utils
043: .createExternalizedJSHeaderFromProperty(Utils.JS_YUI_AUTOCOMPLETE));
044: headers
045: .add(Utils
046: .createExternalizedCSSHeader("org/wingx/suggest/suggest.css"));
047: headers
048: .add(Utils
049: .createExternalizedJSHeader("org/wingx/suggest/suggest.js"));
050: }
051:
052: public void installCG(final SComponent comp) {
053: super .installCG(comp);
054: SessionHeaders.getInstance().registerHeaders(headers);
055: }
056:
057: public void uninstallCG(SComponent component) {
058: super .uninstallCG(component);
059: SessionHeaders.getInstance().deregisterHeaders(headers);
060: }
061:
062: protected void onChangeSubmitListener(STextField textField) {
063: }
064:
065: protected void printPostInput(Device device, STextField textField)
066: throws IOException {
067: device.print("<div id=\"" + textField.getName()
068: + "_popup\"></div>");
069: StringBuilder builder = STRING_BUILDER.get();
070: builder.setLength(0);
071:
072: String name = textField.getName();
073: builder.append("var ds_");
074: builder.append(name);
075: builder.append(" = new wingS.suggest.SuggestDS(\"");
076: builder.append(name);
077: builder.append("\",\"");
078: builder.append(name);
079: builder.append("_popup\");\n");
080: builder.append("var ac_");
081: builder.append(name);
082: builder.append(" = new YAHOO.widget.AutoComplete(\"");
083: builder.append(name);
084: builder.append("\", \"");
085: builder.append(name);
086: builder.append("_popup\", ds_");
087: builder.append(name);
088: builder.append(");\n");
089:
090: if (textField.getDocumentListeners().length > 1
091: || textField instanceof SFormattedTextField) {
092: builder.append("var onchange_");
093: builder.append(name);
094: builder
095: .append(" = function(sType, aArgs) { wingS.request.sendEvent(null, true, "
096: + !textField.isReloadForced() + "); };\n");
097: builder.append("ac_");
098: builder.append(name);
099: builder.append(".textboxBlurEvent.subscribe(onchange_"
100: + name + ");\n");
101: }
102: ScriptManager.getInstance().addScriptListener(
103: new OnHeadersLoadedScript(builder.toString()));
104: }
105:
106: public void parentFrameAdded(SParentFrameEvent e) {
107: SessionHeaders.getInstance().registerHeaders(headers);
108: }
109:
110: public void parentFrameRemoved(SParentFrameEvent e) {
111: SessionHeaders.getInstance().deregisterHeaders(headers);
112: }
113:
114: public Update getSuggestionsUpdate(XSuggest suggest, String text,
115: List<Map.Entry<String, String>> suggestions) {
116: return new SuggestUpdate(suggest, text, suggestions);
117: }
118:
119: protected class SuggestUpdate extends AbstractUpdate {
120: private String string;
121: private List<Map.Entry<String, String>> suggestions;
122:
123: public SuggestUpdate(SComponent component, String string,
124: List<Map.Entry<String, String>> suggestions) {
125: super (component);
126: this .string = string;
127: this .suggestions = suggestions;
128: }
129:
130: public int getPriority() {
131: return 0;
132: }
133:
134: public Handler getHandler() {
135: UpdateHandler handler = new UpdateHandler("suggest");
136: handler.addParameter(component.getName());
137: handler.addParameter(string);
138: for (Map.Entry suggestion : suggestions) {
139: handler.addParameter(suggestion.getKey());
140: handler.addParameter(suggestion.getValue());
141: }
142: return handler;
143: }
144: }
145: }
|