KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > TableCG


1 /*
2  * $Id: TableCG.java,v 1.20 2005/05/27 16:07:40 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf.css;
15
16
17 import org.wings.*;
18 import org.wings.io.Device;
19 import org.wings.plaf.CGManager;
20 import org.wings.session.SessionManager;
21 import org.wings.table.STableCellRenderer;
22 import org.wings.table.SDefaultTableCellRenderer;
23
24 import java.awt.*;
25 import java.io.IOException JavaDoc;
26
27 public class TableCG extends AbstractComponentCG implements
28         org.wings.plaf.TableCG {
29
30     private String JavaDoc fixedTableBorderWidth;
31
32     /**
33      * Initialize properties from config
34      */

35     public TableCG() {
36         final CGManager manager = SessionManager.getSession().getCGManager();
37         setFixedTableBorderWidth((String JavaDoc) manager.getObject("TableCG.fixedTableBorderWidth", String JavaDoc.class));
38     }
39
40
41     public void installCG(final SComponent comp) {
42         super.installCG(comp);
43         final STable component = (STable) comp;
44         final CGManager manager = component.getSession().getCGManager();
45         Object JavaDoc value;
46         value = manager.getObject("STable.defaultRenderer", STableCellRenderer.class);
47         if (value != null) {
48             component.setDefaultRenderer((STableCellRenderer) value);
49             if (value instanceof SDefaultTableCellRenderer) {
50                 SDefaultTableCellRenderer cellRenderer = (SDefaultTableCellRenderer) value;
51                 cellRenderer.setEditIcon(manager.getIcon("TableCG.editIcon"));
52             }
53         }
54         value = manager.getObject("STable.headerRenderer", STableCellRenderer.class);
55         if (value != null) {
56             component.setHeaderRenderer((STableCellRenderer) value);
57         }
58     }
59
60     /**
61      * write a specific cell to the device
62      */

63     protected void writeCell(Device device, STable table, SCellRendererPane rendererPane, int row, int col)
64             throws IOException JavaDoc {
65         SComponent component = null;
66         boolean isEditingCell = table.isEditing()
67                 && row == table.getEditingRow()
68                 && col == table.getEditingColumn();
69         boolean selectable = table.getSelectionMode() != SListSelectionModel.NO_SELECTION && !table.isEditable();
70         boolean showAsFormComponent = table.getShowAsFormComponent();
71
72         if (isEditingCell)
73             component = table.getEditorComponent();
74         else
75             component = table.prepareRenderer(table.getCellRenderer(row, col), row, col);
76
77         device.print("<td col=\"");
78         device.print(col);
79         device.print("\"");
80
81         if (component == null) {
82             device.print("></td>");
83             return;
84         }
85
86         switch (component.getHorizontalAlignment()) {
87             case LEFT_ALIGN:
88                 device.print(" align=\"left\"");
89                 break;
90             case CENTER_ALIGN:
91                 device.print(" align=\"center\"");
92                 break;
93             case RIGHT_ALIGN:
94                 device.print(" align=\"right\"");
95                 break;
96         }
97
98         switch (component.getVerticalAlignment()) {
99             case TOP_ALIGN:
100                 device.print(" valign=\"top\"");
101                 break;
102             case CENTER_ALIGN:
103                 device.print(" valign=\"center\"");
104                 break;
105             case BOTTOM_ALIGN:
106                 device.print(" align=\"bottom\"");
107                 break;
108         }
109         device.print(">");
110
111         String JavaDoc parameter = null;
112         if (table.isEditable() && !isEditingCell && table.isCellEditable(row, col))
113             parameter = table.getEditParameter(row, col);
114         else if (selectable)
115             parameter = table.getToggleSelectionParameter(row, col);
116
117         if (parameter != null && !isEditingCell) {
118             if (showAsFormComponent) {
119                 writeButtonStart(device, table, parameter);
120                 device.print(" type=\"submit\" name=\"");
121                 Utils.write(device, Utils.event(table));
122                 device.print(SConstants.UID_DIVIDER);
123                 Utils.write(device, parameter);
124                 device.print("\" value=\"\">");
125             } else {
126                 RequestURL selectionAddr = table.getRequestURL();
127                 selectionAddr.addParameter(Utils.event(table), parameter);
128
129                 writeLinkStart(device, selectionAddr);
130             }
131         } else
132             device.print("<span>");
133
134         rendererPane.writeComponent(device, component, table);
135
136         if (parameter != null && !isEditingCell) {
137             if (showAsFormComponent)
138                 device.print("</button>");
139             else
140                 device.print("</a>");
141         } else
142             device.print("</span>");
143
144         device.print("</td>\n");
145     }
146
147
148     /**
149      * @param device
150      * @param selectionAddr
151      * @throws IOException
152      */

153     protected void writeLinkStart(Device device, RequestURL selectionAddr) throws IOException JavaDoc {
154         device.print("<a HREF=\"");
155         Utils.write(device, selectionAddr.toString());
156         device.print("\">");
157     }
158
159
160     /**
161      * @param device
162      * @param table
163      * @param parameter
164      * @throws IOException
165      */

166     protected void writeButtonStart(Device device, STable table, String JavaDoc parameter) throws IOException JavaDoc {
167         device.print("<button");
168     }
169
170
171     protected void writeHeaderCell(Device device, STable table,
172                                    SCellRendererPane rendererPane,
173                                    int c)
174             throws IOException JavaDoc {
175         SComponent comp = table.prepareHeaderRenderer(c);
176
177         device.print("<th>");
178         rendererPane.writeComponent(device, comp, table);
179         device.print("</th>\n");
180     }
181
182
183     public void writeContent(final Device device, final SComponent _c)
184             throws IOException JavaDoc {
185         final STable component = (STable) _c;
186
187         STable table = (STable) component;
188         boolean childSelectorWorkaround = !component.getSession().getUserAgent().supportsCssChildSelector();
189
190         SDimension intercellPadding = table.getIntercellPadding();
191         SDimension intercellSpacing = table.getIntercellSpacing();
192
193         device.print("<table");
194         Utils.printCSSInlineFullSize(device, component.getPreferredSize());
195
196         // TODO: border="" should be obsolete
197
// TODO: cellspacing and cellpadding may be in conflict with border-collapse
198
/* Tweaking: CG configured to have a fixed border="xy" width */
199         Utils.optAttribute(device, "border", fixedTableBorderWidth);
200         Utils.optAttribute(device, "cellspacing", ((intercellSpacing != null) ? ""+intercellSpacing.getIntWidth() : null));
201         Utils.optAttribute(device, "cellpadding", ((intercellPadding != null) ? ""+intercellPadding.getIntHeight() : null));
202         device.print(">\n");
203         /*
204         * get viewable area
205         */

206         int startRow = 0;
207         int startCol = 0;
208         int endRow = table.getRowCount();
209         int endCol = table.getColumnCount();
210         Rectangle viewport = table.getViewportSize();
211         if (viewport != null) {
212             startRow = viewport.y;
213             startCol = viewport.x;
214             endRow = Math.min(startRow + viewport.height, endRow);
215             endCol = Math.min(startCol + viewport.width, endCol);
216         }
217
218         SCellRendererPane rendererPane = table.getCellRendererPane();
219
220         /*
221         * render the header
222         */

223         SListSelectionModel selectionModel = table.getSelectionModel();
224         boolean numbering = selectionModel.getSelectionMode() != SListSelectionModel.NO_SELECTION && table.isEditable();
225         boolean showAsFormComponent = table.getShowAsFormComponent();
226
227         if (table.isHeaderVisible()) {
228             device.print("<thead><tr>\n");
229
230             if (numbering)
231                 device.print("<th></th>");
232
233             for (int c = startCol; c < endCol; c++)
234                 writeHeaderCell(device, table, rendererPane, table.convertColumnIndexToModel(c));
235
236             device.print("</tr></thead>\n");
237         }
238
239         device.print("<tbody>\n");
240         for (int r = startRow; r < endRow; r++) {
241             device.print("<tr");
242             if (selectionModel.isSelectedIndex(r))
243                 device.print(" selected=\"true\"");
244             if (r % 2 != 0)
245                 device.print(" odd=\"true\">");
246             else
247                 device.print(" even=\"true\">");
248
249             if (numbering) {
250                 device.print("<td col=\"numbering\"");
251                 if (childSelectorWorkaround)
252                     Utils.optAttribute(device, "class", "numbering");
253                 device.print(">");
254
255                 if (showAsFormComponent) {
256                     writeButtonStart(device, table, table.getToggleSelectionParameter(r, -1));
257                     device.print(" type=\"submit\" name=\"");
258                     Utils.write(device, Utils.event(table));
259                     device.print(SConstants.UID_DIVIDER);
260                     Utils.write(device, table.getToggleSelectionParameter(r, -1));
261                     device.print("\" value=\"\">");
262                     device.print(r);
263                     device.print("</button>");
264                 } else {
265                     RequestURL selectionAddr = table.getRequestURL();
266                     selectionAddr.addParameter(Utils.event(table),
267                             table.getToggleSelectionParameter(r, -1));
268
269                     device.print("<a HREF=\"");
270                     Utils.write(device, selectionAddr.toString());
271                     device.print("\">");
272                     device.print(r);
273                     device.print("</a>");
274                 }
275                 device.print("</td>");
276             }
277
278             for (int c = startCol; c < endCol; c++)
279                 writeCell(device, table, rendererPane, r, table.convertColumnIndexToModel(c));
280
281             device.print("</tr>\n");
282         }
283         device.print("</tbody>\n");
284         device.print("</table>\n");
285     }
286
287     public String JavaDoc getFixedTableBorderWidth() {
288         return fixedTableBorderWidth;
289     }
290
291     public void setFixedTableBorderWidth(String JavaDoc fixedTableBorderWidth) {
292         this.fixedTableBorderWidth = fixedTableBorderWidth;
293     }
294 }
295
Popular Tags