001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.ui;
019:
020: import java.awt.event.*;
021: import java.util.*;
022:
023: import javax.swing.*;
024: import javax.swing.plaf.basic.*;
025:
026: import net.ar.webonswing.*;
027: import net.ar.webonswing.helpers.*;
028: import net.ar.webonswing.managers.script.*;
029: import net.ar.webonswing.remote.*;
030: import net.ar.webonswing.render.markup.*;
031: import net.ar.webonswing.render.templates.*;
032: import net.ar.webonswing.render.templates.html.*;
033: import net.ar.webonswing.wrapping.*;
034:
035: import org.apache.commons.lang.*;
036: import org.apache.regexp.*;
037:
038: public class ComboBoxUIContributor extends
039: AbstractSwingComponentUIContributor {
040: public void doRenderingContribution(
041: RenderingContributionContainer theContribManager) {
042: JComboBox aComboBox = (JComboBox) getJComponent();
043:
044: String theComponentPath = theComponent.getName();
045:
046: Tag theTag = new Tag("select");
047:
048: theTag.addAttribute(new TagAttribute("name", theComponentPath));
049: theTag.addAttribute(new TagAttribute("size", "1"));
050:
051: if (!aComboBox.isEnabled())
052: theTag.addAttribute(new TagAttribute("DISABLED", ""));
053:
054: if (aComboBox.getListeners(ActionListener.class).length > 0)
055: theTag.addAttribute(new TagAttribute("onchange",
056: "ed.dispatch(new ActionEvent(this.name, '"
057: + RemoteHelper.escapeSingleQuotes(aComboBox
058: .getActionCommand()) + "'));"));
059:
060: RemoteHelper.addMouseListeners(aComboBox, theComponentPath,
061: theTag);
062:
063: if (aComboBox.getWidth() != 0 || aComboBox.getHeight() != 0)
064: theTag.addAttribute(new TagAttribute("style",
065: "position:absolute; width:" + aComboBox.getWidth()
066: + "; height:" + aComboBox.getHeight()));
067:
068: for (int i = 0; i < aComboBox.getModel().getSize(); i++) {
069: Tag theOption = new Tag("option");
070: String theValue = aComboBox.getModel().getElementAt(i)
071: .toString();
072:
073: theOption.addAttribute(new TagAttribute("value", i));
074:
075: if (aComboBox.getSelectedIndex() == i)
076: theOption
077: .addAttribute(new TagAttribute("SELECTED", ""));
078:
079: theOption.getTheMarkupContainer().addElement(
080: new TagContent(theValue));
081:
082: theTag.getTheMarkupContainer().addElement(theOption);
083: }
084:
085: Template theTemplate = WosSwingHelper.getTemplateForComponent(
086: "JComboBox", aComboBox);
087: theTemplate.addElement(new IdTagTemplateElement("theComboBox",
088: theTag, theTag));
089:
090: theContribManager.doContribution(theComponent, theTemplate,
091: theTag, getInitScript(theComponent));
092: }
093:
094: public String getInitScript(VisualComponent aComponent) {
095: String theComponentName = aComponent.getName();
096: StringBuffer theScript = new StringBuffer();
097: theScript
098: .append("getComponent('page').addListener(new ListFinishListener('"
099: + theComponentName + "'));");
100: theScript.append(RemoteHelper.getListenersAdds(aComponent));
101:
102: return theScript.toString();
103: }
104:
105: public void dispatchEvents(List anEvents) {
106: for (Iterator it = anEvents.iterator(); it.hasNext();) {
107: RemoteEvent theEvent = (RemoteEvent) it.next();
108:
109: JComboBox aComboBox = (JComboBox) ((ComponentWrapper) theEvent
110: .getSource()).getWrappedComponent();
111:
112: if (aComboBox.getModel() instanceof DefaultComboBoxModel) {
113: if (theEvent.getName().endsWith(".values")
114: && theEvent.getType().equals("update"))
115: processUpdateEvent(theEvent, aComboBox);
116: else if (theEvent.getType().equals("mouseClicked"))
117: RemoteHelper.fireClickEvent(aComboBox);
118: }
119: }
120: }
121:
122: private void processUpdateEvent(RemoteEvent theEvent,
123: JComboBox aComboBox) {
124: DefaultComboBoxModel theModel = (DefaultComboBoxModel) aComboBox
125: .getModel();
126: Vector theSelectedVector = new Vector();
127: int theSelectedIndex = aComboBox.getSelectedIndex();
128:
129: String[] theElements = getSplitRe().split(
130: RemoteHelper.wosunescape((String) theEvent
131: .getParameters()[0]));
132:
133: for (int i = 0; i < theElements.length; i++) {
134: String theValue = theElements[i];
135: String theReplacedValue = StringUtils.replace(theValue,
136: "_s", "");
137:
138: if (theModel.getSize() > i) {
139: if (!theModel.getElementAt(i).toString().equals(
140: theReplacedValue))
141: theModel.insertElementAt(theReplacedValue, i);
142: } else
143: theModel.insertElementAt(theReplacedValue, i);
144:
145: if (!theValue.equals(theReplacedValue))
146: theSelectedVector.add(new Integer(i));
147: }
148:
149: for (Iterator i = theSelectedVector.iterator(); i.hasNext();) {
150: if (aComboBox.getEditor() == null)
151: aComboBox.setEditor(new BasicComboBoxEditor()); //TODO: ojo vieja - (aguante Moyo!) - bug de IBM VM
152:
153: int theNewSelectedIndex = ((Integer) i.next()).intValue();
154:
155: if (theSelectedIndex != theNewSelectedIndex)
156: aComboBox.setSelectedIndex(theNewSelectedIndex);
157: }
158: }
159:
160: public void doScriptContribution(
161: ScriptContributionContainer aContributionManager) {
162: aContributionManager.addInclude(WosFramework.getInstance()
163: .getCompleteResourcePath()
164: + "/js/JList.js");
165: }
166:
167: public static RE getSplitRe() {
168: try {
169: return new RE("_,");
170: } catch (RESyntaxException e) {
171: return null;
172: }
173: }
174: }
|