001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2004 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.util.*;
021:
022: import javax.swing.*;
023:
024: import net.ar.webonswing.managers.script.*;
025: import net.ar.webonswing.remote.*;
026: import net.ar.webonswing.remote.js.*;
027: import net.ar.webonswing.render.markup.*;
028: import net.ar.webonswing.walkers.*;
029: import net.ar.webonswing.wrapping.*;
030:
031: public class Table2UIContributor extends
032: AbstractSwingComponentUIContributor {
033: public void doRenderingContribution(
034: RenderingContributionContainer aRenderingContribManager) {
035: JTable table = (JTable) getJComponent();
036: String theComponentName = theComponent.getName();
037:
038: Tag script = new Tag("script");
039:
040: JsConstructorCall headerArray = new JsConstructorCall("Array",
041: new JsStatement[] {}, true);
042: for (int j = 0; j < table.getColumnCount(); j++)
043: headerArray.add(new StringValue(table.getColumnName(j)));
044:
045: JsConstructorCall dataArray = new JsConstructorCall("Array",
046: new JsStatement[] {}, true);
047: for (int i = 0; i < table.getRowCount(); i++) {
048: JsConstructorCall rowArray = new JsConstructorCall("Array",
049: new JsStatement[] {}, true);
050:
051: for (int j = 0; j < table.getColumnCount(); j++)
052: rowArray.add(new StringValue(table.getValueAt(i, j)
053: .toString()));
054:
055: dataArray.add(rowArray);
056: }
057:
058: JsBuilder jsBuilder = new JsBuilder();
059:
060: jsBuilder.add(new GenericJsStatement(
061: "var mergeTag= document.getElementById('"
062: + theComponentName + ".merge');"));
063: jsBuilder.add(new JsInstance(theComponentName,
064: new JsConstructorCall("Grid",
065: new JsStatement[] {
066: headerArray,
067: new GenericJsStatement(20),
068: dataArray,
069: new GenericJsStatement(table
070: .getSelectedRow()) }, false)));
071: // jsBuilder.add(new JsMethodCall(theComponentName, "setWidth", new JsStatement[]{new GenericJsStatement(table.getWidth())}));
072: // jsBuilder.add(new JsMethodCall(theComponentName, "setHeight", new JsStatement[]{new GenericJsStatement(table.getHeight())}));
073: jsBuilder.add(new JsMethodCall(theComponentName, "setWidth",
074: new JsStatement[] { new GenericJsStatement(
075: "parseInt(mergeTag.style.width)") }));
076: jsBuilder.add(new JsMethodCall(theComponentName, "setHeight",
077: new JsStatement[] { new GenericJsStatement(
078: "parseInt(mergeTag.style.height)") }));
079: //jsBuilder.add(new JsMethodCall(theComponentName, "setLocation", new JsStatement[]{new GenericJsStatement(table.getX()), new GenericJsStatement(table.getY())}));
080: jsBuilder
081: .add(new GenericJsStatement(
082: theComponentName
083: + ".updateListener=function () {document.getElementsByName('"
084: + theComponentName
085: + ".selectedRow')[0].value= "
086: + theComponentName
087: + ".selectedrow; ed.dispatch(new MouseClickedEvent('"
088: + theComponentName + "'));};"));
089: //jsBuilder.add(new JsMethodCall(theComponentName, "setSelected", new JsStatement[]{new StringValue(table.getSelectedRow()+"")}));
090:
091: jsBuilder.addDynApiComponentInit(theComponentName);
092:
093: script.addTextToContainer(jsBuilder.renderJs());
094:
095: Tag hidden = new Tag("input");
096: hidden.addAttribute("type", "hidden");
097: hidden.addAttribute("name", theComponentName + ".selectedRow");
098: hidden.addAttribute("value", table.getSelectedRow());
099:
100: Tag content = new Tag("div");
101: content.addAttribute("id", theComponentName + ".merge");
102: content.addElementToContainer(script);
103: content.addElementToContainer(hidden);
104:
105: aRenderingContribManager.doContribution(theComponent, content,
106: content, RemoteHelper.getListenersAdds(theComponent));
107: }
108:
109: public void dispatchEvents(List anEvents) {
110: for (Iterator i = anEvents.iterator(); i.hasNext();) {
111: RemoteEvent theEvent = (RemoteEvent) i.next();
112: JTable theTable = (JTable) ((ComponentWrapper) theEvent
113: .getSource()).getWrappedComponent();
114:
115: if (theEvent.getName().endsWith(".selectedRow")) {
116: int index = Integer
117: .parseInt(theEvent.getParameters()[0]
118: .toString());
119: theTable.getSelectionModel().setSelectionInterval(
120: index, index);
121: }
122: }
123: }
124:
125: public void doScriptContribution(
126: ScriptContributionContainer aContributionManager) {
127: RemoteHelper.addDynApiInitCode(aContributionManager);
128:
129: aContributionManager
130: .addCodeOnce("dynapi.library.add('dynapi.widgets.Grid','grid.js','DynLayer');");
131: aContributionManager
132: .addCodeOnce("dynapi.library.add('dynapi.widgets.EditLabel','editlabel.js','DynLayer');");
133:
134: aContributionManager
135: .addCodeOnce("dynapi.library.include('DynKeyEvent');");
136:
137: aContributionManager
138: .addCodeOnce("dynapi.library.include('ViewPane');");
139: aContributionManager
140: .addCodeOnce("dynapi.library.include('Label');");
141: aContributionManager
142: .addCodeOnce("dynapi.library.include('EditLabel');");
143: aContributionManager
144: .addCodeOnce("dynapi.library.include('Grid');");
145: }
146:
147: public VisitableContainer getContainerVisitable() {
148: return new NullContainerVisitable();
149: }
150:
151: }
|