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.util.*;
021:
022: import javax.swing.*;
023: import javax.swing.table.*;
024:
025: import net.ar.webonswing.remote.*;
026: import net.ar.webonswing.render.*;
027: import net.ar.webonswing.render.markup.*;
028: import net.ar.webonswing.visitor.*;
029: import net.ar.webonswing.walkers.*;
030: import net.ar.webonswing.wrapping.*;
031: import net.ar.webonswing.wrapping.swing.*;
032:
033: import org.apache.commons.lang.*;
034:
035: public class TableUIContributor extends
036: AbstractSwingComponentUIContributor {
037: public void doRenderingContribution(
038: RenderingContributionContainer theContribManager) {
039: JTable theTable = (JTable) getJComponent();
040:
041: RemoteHelper.removeToolTipListeners(theTable);
042:
043: StringBuffer theResult = new StringBuffer();
044: StringBuffer theResultScript = new StringBuffer();
045:
046: theResult.append(renderHeader(theTable));
047: theResult.append(renderRows(theContribManager, theTable,
048: theComponent.getName(), theResultScript));
049:
050: Tag theTableTag = new Tag("table").addAttribute("border", "1")
051: .addAttribute("id", theComponent.getName());
052: theTableTag.addTextToContainer(theResult);
053:
054: Tag span = new Tag("span");
055: String selectedRowName = theComponent.getName()
056: + ".selectedRow";
057: span.addElementToContainer(new Tag("input").addAttribute(
058: "type", "hidden").addAttribute("id", selectedRowName)
059: .addAttribute("name", selectedRowName));
060: span.addElementToContainer(theTableTag);
061:
062: String theMainScript = RemoteHelper
063: .getListenersAdds(theComponent)
064: + theResultScript.toString();
065: theContribManager.doContribution(theComponent, span,
066: theTableTag, theMainScript);
067: }
068:
069: private StringBuffer renderRows(
070: RenderingContributionContainer theContribManager,
071: JTable theTable, String theComponentName,
072: StringBuffer theResultScript) {
073: StringBuffer theResult = new StringBuffer();
074:
075: for (int i = 0; i < theTable.getRowCount(); i++) {
076: Tag theRow = new Tag("tr").addAttribute("align", "right");
077: theRow.addAttribute("onclick", "document.getElementById('"
078: + theComponentName + ".selectedRow').value=" + i
079: + ";ed.dispatch(new MouseClickedEvent('"
080: + theComponentName + "'));");
081:
082: for (int j = 0; j < theTable.getColumnCount(); j++) {
083: Tag theColumn = new Tag("td");
084:
085: JComponent theCellRenderer, theEditorRenderer;
086:
087: theCellRenderer = (JComponent) theTable
088: .prepareRenderer(
089: theTable.getCellRenderer(i, j), i, j);
090: theEditorRenderer = (JComponent) theTable
091: .getCellEditor(i, j)
092: .getTableCellEditorComponent(theTable,
093: theTable.getValueAt(i, j), true, i, j);
094:
095: VisualComponent theCellRendererComponent = new ComponentStandardizer()
096: .standardizeChildsComponentHierarchy(new SwingComponentWrapper(
097: theCellRenderer));
098: theCellRendererComponent.setName(theComponentName
099: + ".CellRenderer." + i + "." + j);
100:
101: VisualComponent theEditorRendererComponent = new ComponentStandardizer()
102: .standardizeChildsComponentHierarchy(new SwingComponentWrapper(
103: theEditorRenderer));
104: theEditorRendererComponent.setName(theComponentName
105: + ".EditorRenderer." + i + "." + j);
106:
107: VisualComponent renderer = "edit".equals(theTable
108: .getClientProperty("mode")) ? theEditorRendererComponent
109: : theCellRendererComponent;
110:
111: Visitable theCellResult = theContribManager
112: .getContainerRenderer().render(renderer);
113: theColumn.addTextToContainer(new ContentRenderer(
114: theCellResult).getResult());
115:
116: String theComponentScript = theContribManager
117: .getComponentInitScripts(renderer)[0];
118: if (theComponentScript.length() > 0)
119: theResultScript.append(theComponentScript + ";");
120:
121: theRow.addElementToContainer(theColumn);
122: }
123:
124: theResult.append(new ContentRenderer(theRow).getResult());
125: }
126:
127: return theResult;
128: }
129:
130: private StringBuffer renderHeader(JTable theTable) {
131: StringBuffer theResult = new StringBuffer();
132:
133: Tag theRow = new Tag("tr").addAttribute("align", "right");
134: for (int j = 0; j < theTable.getColumnCount(); j++) {
135: Tag theColumn = new Tag("td");
136: theColumn.addTextToContainer(theTable.getColumnName(j));
137: theRow.addElementToContainer(theColumn);
138: }
139: theResult.append(new ContentRenderer(theRow).getResult());
140:
141: return theResult;
142: }
143:
144: public void dispatchEvents(List anEvents) {
145: for (Iterator i = anEvents.iterator(); i.hasNext();) {
146: RemoteEvent theEvent = (RemoteEvent) i.next();
147: JTable theTable = (JTable) ((ComponentWrapper) theEvent
148: .getSource()).getWrappedComponent();
149:
150: if (theEvent.getName().endsWith(".selectedRow")) {
151: if (theEvent.getParameters()[0].toString().trim()
152: .length() > 0) {
153: int index = Integer.parseInt(theEvent
154: .getParameters()[0].toString());
155: theTable.getSelectionModel().setSelectionInterval(
156: index, index);
157: }
158: } else if (!theEvent.getName().endsWith("mouseClicked")) {
159: TableModel theModel = theTable.getModel();
160:
161: String[] theElements = StringUtils.split(theEvent
162: .getName(), ".");
163: int theRow = Integer.parseInt(theElements[2]);
164: int theCol = Integer.parseInt(theElements[3]);
165:
166: Object theValue = theEvent.getParameters()[0];
167:
168: if (theTable.getColumnClass(theCol).equals(
169: Boolean.class))
170: theValue = theValue.equals("on") ? Boolean.TRUE
171: : Boolean.FALSE;
172: else if (theTable.getColumnClass(theCol).equals(
173: Integer.class))
174: theValue = getIntegerFromString((String) theValue);
175:
176: theModel.setValueAt(theValue, theRow, theCol);
177: }
178: }
179: }
180:
181: private Integer getIntegerFromString(String theValue) {
182: try {
183: return Integer.decode(theValue.toString());
184: } catch (NumberFormatException e) {
185: return new Integer(0);
186: }
187: }
188:
189: public VisitableContainer getContainerVisitable() {
190: return new NullContainerVisitable();
191: }
192: }
|