KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > syncpeer > TriCellTable


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webcontainer.syncpeer;
31
32 import nextapp.echo2.app.Extent;
33 import nextapp.echo2.webcontainer.RenderContext;
34 import nextapp.echo2.webcontainer.propertyrender.ExtentRender;
35 import nextapp.echo2.webrender.Service;
36 import nextapp.echo2.webrender.WebRenderServlet;
37 import nextapp.echo2.webrender.service.StaticBinaryService;
38
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * Renders an HTML table that has two or three "container" cells and
44  * independently settable margins between them. These tables are useful for
45  * rendering buttons that have two or three elements (images, text labels, and
46  * state indicators). This class supports all possible permutations for
47  * placement of each of the two or three contained components.
48  * <p>
49  * This class should not be extended or used by classes outside of the
50  * Echo framework.
51  */

52 class TriCellTable {
53
54     private static final Service TRANSPARENT_SPACER_IMAGE_SERVICE = StaticBinaryService.forResource(
55             "Echo.TriCellTable.TransparentImage", "image/gif", "/nextapp/echo2/webcontainer/resource/image/Transparent.gif");
56     
57     static {
58         WebRenderServlet.getServiceRegistry().add(TRANSPARENT_SPACER_IMAGE_SERVICE);
59     }
60     
61     static final int INVERTED = 1;
62     static final int VERTICAL = 2;
63     
64     public static final int LEADING_TRAILING = 0;
65     public static final int TRAILING_LEADING = INVERTED;
66     public static final int TOP_BOTTOM = VERTICAL;
67     public static final int BOTTOM_TOP = INVERTED | VERTICAL;
68
69     private Element JavaDoc[] tdElements;
70     private Element JavaDoc[] marginTdElements = null;
71     private Element JavaDoc tableElement;
72     private Element JavaDoc tbodyElement;
73     private Document JavaDoc document;
74     private RenderContext rc;
75     
76     /**
77      * This constructor is called by the non-private constructors to set up
78      * common properties.
79      *
80      * @param rc the relevant <code>RenderContext</code>
81      * @param id The id the id upon which the inner elements will be based.
82      * the id of the returned table element is not set and must be
83      * set appropriately by the caller.
84      */

85     private TriCellTable(RenderContext rc, Document JavaDoc document, String JavaDoc id) {
86         super();
87         this.rc = rc;
88         this.document = document;
89         tableElement = document.createElement("table");
90         tbodyElement = document.createElement("tbody");
91         tbodyElement.setAttribute("id", id + "_tbody");
92         tableElement.appendChild(tbodyElement);
93     }
94     
95     /**
96      * Creates a two-celled <code>TriCellTable</code>.
97      *
98      * @param rc the relevant <code>RenderContext</code>
99      * @param document the outgoing XML document
100      * @param id the id of the root element
101      * @param orientation0_1 The orientation of Element 0 with respect to
102      * Element 1, one of the following values:
103      * <ul>
104      * <li>LEFT_RIGHT (element 0 is to the left of element 1)</li>
105      * <li>RIGHT_LEFT (element 1 is to the left of element 0)</li>
106      * <li>TOP_BOTTOM (element 0 is above element 1)</li>
107      * <li>BOTTOM_TOP (element 1 is above element 0)</li>
108      * </ul>
109      * @param margin0_1 The margin size between element 0 and element 1.
110      */

111     TriCellTable(RenderContext rc, Document JavaDoc document, String JavaDoc id, int orientation0_1, Extent margin0_1) {
112         this(rc, document, id);
113         
114         marginTdElements = new Element JavaDoc[1];
115         tdElements = new Element JavaDoc[2];
116         tdElements[0] = document.createElement("td");
117         tdElements[0].setAttribute("id", id + "_td_0");
118         tdElements[1] = document.createElement("td");
119         tdElements[1].setAttribute("id", id + "_td_1");
120         
121         if (margin0_1 != null && margin0_1.getValue() > 0) {
122             marginTdElements[0] = document.createElement("td");
123             marginTdElements[0].setAttribute("id", id + "_tdmargin_0_1");
124             int size = ExtentRender.toPixels(margin0_1, 1);
125             if ((orientation0_1 & VERTICAL) == 0) {
126                 marginTdElements[0].setAttribute("style", "width:" + size + "px;");
127                 addSpacer(marginTdElements[0], size, false);
128             } else {
129                 marginTdElements[0].setAttribute("style", "height:" + size + "px;");
130                 addSpacer(marginTdElements[0], size, true);
131             }
132         }
133                 
134         if ((orientation0_1 & VERTICAL) == 0) {
135             // horizontally oriented
136
Element JavaDoc trElement = document.createElement("tr");
137             trElement.setAttribute("id", id + "_tr_0_1");
138             if ((orientation0_1 & INVERTED) == 0) {
139                 // normal (left to right)
140
addColumn(trElement, tdElements[0]);
141                 addColumn(trElement, marginTdElements[0]);
142                 addColumn(trElement, tdElements[1]);
143             } else {
144                 // inverted (right to left)
145
addColumn(trElement, tdElements[1]);
146                 addColumn(trElement, marginTdElements[0]);
147                 addColumn(trElement, tdElements[0]);
148             }
149             tbodyElement.appendChild(trElement);
150         } else {
151             // vertically oriented
152
if ((orientation0_1 & INVERTED) == 0) {
153                 // normal (top to bottom)
154
addRow(tdElements[0], id + "_tr_0");
155                 addRow(marginTdElements[0], id + "_trmargin_0_1");
156                 addRow(tdElements[1], id + "_tr_1");
157             } else {
158                 // inverted (bottom to top)
159
addRow(tdElements[1], id + "_tr_1");
160                 addRow(marginTdElements[0], id + "_trmargin_0_1");
161                 addRow(tdElements[0], id + "_tr_0");
162             }
163         }
164     }
165     
166     /**
167      * Creates a three-celled <code>TriCellTable</code>.
168      *
169      * @param rc the relevant <code>RenderContext</code>
170      * @param document the outgoing XML document
171      * @param id the id of the root element
172      * @param orientation0_1 The orientation of Element 0 with respect to
173      * Element 1, one of the following values:
174      * <ul>
175      * <li>LEFT_RIGHT (element 0 is to the left of element 1)</li>
176      * <li>RIGHT_LEFT (element 1 is to the left of element 0)</li>
177      * <li>TOP_BOTTOM (element 0 is above element 1)</li>
178      * <li>BOTTOM_TOP (element 1 is above element 0)</li>
179      * </ul>
180      * @param margin0_1 The margin size between element 0 and element 1.
181      * @param orientation01_2 The orientation of Elements 0 and 1 with
182      * respect to Element 2, one of the following values:
183      * <ul>
184      * <li>LEFT_RIGHT (elements 0 and 1 are to the left of
185      * element 1)</li>
186      * <li>RIGHT_LEFT (element 2 is to the left of elements 0 and 1)</li>
187      * <li>TOP_BOTTOM (elements 0 and 1 are above element 2)</li>
188      * <li>BOTTOM_TOP (element 2 is above elements 0 and 1)</li>
189      * </ul>
190      * @param margin01_2 The margin size between the combination
191      * of elements 0 and 1 and element 2.
192      */

193     TriCellTable(RenderContext rc, Document JavaDoc document, String JavaDoc id, int orientation0_1, Extent margin0_1, int orientation01_2,
194             Extent margin01_2) {
195         this(rc, document, id);
196         
197         Element JavaDoc trElement;
198         
199         marginTdElements = new Element JavaDoc[2];
200         tdElements = new Element JavaDoc[3];
201         tdElements[0] = document.createElement("td");
202         tdElements[0].setAttribute("id", id + "_td_0");
203         tdElements[1] = document.createElement("td");
204         tdElements[1].setAttribute("id", id + "_td_1");
205         tdElements[2] = document.createElement("td");
206         tdElements[2].setAttribute("id", id + "_td_2");
207
208         // Create margin cells
209
if (margin0_1 != null || margin01_2 != null) {
210             if (margin0_1 != null && margin0_1.getValue() > 0) {
211                 marginTdElements[0] = document.createElement("td");
212                 marginTdElements[0].setAttribute("id", id + "_tdmargin_0_1");
213                 
214                 int size = ExtentRender.toPixels(margin0_1, 1);
215                 if ((orientation0_1 & VERTICAL) == 0) {
216                     marginTdElements[0].setAttribute("style", "width:" + size + "px;");
217                     addSpacer(marginTdElements[0], size, false);
218                 } else {
219                     marginTdElements[0].setAttribute("style", "height:" + size + "px;");
220                     addSpacer(marginTdElements[0], size, true);
221                 }
222             }
223             if (margin01_2 != null && margin01_2.getValue() > 0) {
224                 marginTdElements[1] = document.createElement("td");
225                 marginTdElements[1].setAttribute("id", id + "_tdmargin_01_2");
226
227                 int size = ExtentRender.toPixels(margin01_2, 1);
228                 if ((orientation01_2 & VERTICAL) == 0) {
229                     marginTdElements[1].setAttribute("style", "width:" + size + "px;");
230                     addSpacer(marginTdElements[1], size, false);
231                 } else {
232                     marginTdElements[1].setAttribute("style", "height:" + size + "px;");
233                     addSpacer(marginTdElements[1], size, true);
234                 }
235             }
236         }
237         
238         if ((orientation0_1 & VERTICAL) == 0) {
239             // horizontally oriented 0/1
240
if ((orientation01_2 & VERTICAL) == 0) {
241                 // horizontally oriented 01/2
242
trElement = document.createElement("tr");
243                 trElement.setAttribute("id", id + "_tr_0");
244                 if ((orientation01_2 & INVERTED) != 0) {
245                     // 2 before 01: render #2 and margin at beginning of TR.
246
addColumn(trElement, tdElements[2]);
247                     addColumn(trElement, marginTdElements[1]);
248                 }
249                 
250                 // Render 01
251
if ((orientation0_1 & INVERTED) == 0) {
252                     // normal (left to right)
253
addColumn(trElement, tdElements[0]);
254                     addColumn(trElement, marginTdElements[0]);
255                     addColumn(trElement, tdElements[1]);
256                 } else {
257                     // inverted (right to left)
258
addColumn(trElement, tdElements[1]);
259                     addColumn(trElement, marginTdElements[0]);
260                     addColumn(trElement, tdElements[0]);
261                 }
262                 
263                 if ((orientation01_2 & INVERTED) == 0) {
264                     addColumn(trElement, marginTdElements[1]);
265                     addColumn(trElement, tdElements[2]);
266                 }
267                 
268                 tbodyElement.appendChild(trElement);
269             } else {
270                 // vertically oriented 01/2
271

272                 // determine and apply column span based on presence of margin between 0 and 1
273
int columns = (margin0_1 != null && margin0_1.getValue() > 0) ? 3 : 2;
274                 tdElements[2].setAttribute("colspan", Integer.toString(columns));
275                 if (marginTdElements[1] != null) {
276                     marginTdElements[1].setAttribute("colspan", Integer.toString(columns));
277                 }
278                 
279                 if ((orientation01_2 & INVERTED) != 0) {
280                     // 2 before 01: render #2 and margin at beginning of TR.
281
addRow(tdElements[2], id + "_tr_2");
282                     addRow(marginTdElements[1], id + "_trmargin_01_2");
283                 }
284                 
285                 // Render 01
286
trElement = document.createElement("tr");
287                 trElement.setAttribute("id", "tr_" + id);
288                 if ((orientation0_1 & INVERTED) == 0) {
289                     // normal (left to right)
290
addColumn(trElement, tdElements[0]);
291                     addColumn(trElement, marginTdElements[0]);
292                     addColumn(trElement, tdElements[1]);
293                 } else {
294                     // inverted (right to left)
295
addColumn(trElement, tdElements[1]);
296                     addColumn(trElement, marginTdElements[0]);
297                     addColumn(trElement, tdElements[0]);
298                 }
299                 tbodyElement.appendChild(trElement);
300                 
301                 if ((orientation01_2 & INVERTED) == 0) {
302                     // 01 before 2: render margin and #2 at end of TR.
303
addRow(marginTdElements[1], id + "_trmargin_01_2");
304                     addRow(tdElements[2], id + "_tr_2");
305                 }
306             }
307         } else {
308             // vertically oriented 0/1
309
if ((orientation01_2 & VERTICAL) == 0) {
310                 // horizontally oriented 01/2
311

312                 // determine and apply row span based on presence of margin between 0 and 1
313
int rows = (margin0_1 != null && margin0_1.getValue() > 0) ? 3 : 2;
314                 tdElements[2].setAttribute("rowspan", Integer.toString(rows));
315                 if (marginTdElements[1] != null) {
316                     marginTdElements[1].setAttribute("rowspan", Integer.toString(rows));
317                 }
318                 
319                 trElement = document.createElement("tr");
320                 trElement.setAttribute("id", id + "_tr_0");
321                 if ((orientation01_2 & INVERTED) != 0) {
322                     addColumn(trElement, tdElements[2]);
323                     addColumn(trElement, marginTdElements[1]);
324                     if ((orientation0_1 & INVERTED) == 0) {
325                         addColumn(trElement, tdElements[0]);
326                     } else {
327                         addColumn(trElement, tdElements[1]);
328                     }
329                 } else {
330                     if ((orientation0_1 & INVERTED) == 0) {
331                         addColumn(trElement, tdElements[0]);
332                     } else {
333                         addColumn(trElement, tdElements[1]);
334                     }
335                     addColumn(trElement, marginTdElements[1]);
336                     addColumn(trElement, tdElements[2]);
337                 }
338                 tbodyElement.appendChild(trElement);
339                 addRow(marginTdElements[0], id + "_trmargin_0_1");
340                 if ((orientation0_1 & INVERTED) == 0) {
341                     addRow(tdElements[1], id + "_tr_1");
342                 } else {
343                     addRow(tdElements[0], id + "_tr_0");
344                 }
345             } else {
346                 // vertically oriented 01/2
347
if ((orientation01_2 & INVERTED) != 0) {
348                     // 2 before 01: render #2 and margin at beginning of TABLE.
349
addRow(tdElements[2], id + "_tr_2");
350                     addRow(marginTdElements[1], id + "_trmargin_01_2");
351                 }
352                 
353                 // Render 01
354
if ((orientation0_1 & INVERTED) == 0) {
355                     // normal (top to bottom)
356
addRow(tdElements[0], id + "_tr_0");
357                     addRow(marginTdElements[0], id + "_trmargin_0_1");
358                     addRow(tdElements[1], id + "_tr_1");
359                 } else {
360                     // inverted (bottom to top)
361
addRow(tdElements[1], id + "_tr_1");
362                     addRow(marginTdElements[0], id + "_trmargin_1_0");
363                     addRow(tdElements[0], id + "_tr_0");
364                 }
365                 
366                 if ((orientation01_2 & INVERTED) == 0) {
367                     // 01 before 2: render margin and #2 at end of TABLE.
368
addRow(marginTdElements[1], id + "_trmargin_01_2");
369                     addRow(tdElements[2], id + "_tr_2");
370                 }
371             }
372         }
373     }
374     
375     /**
376      * Adds an cell element to a table row. The element will not be added
377      * if null is provided for the value of <code>td</code>.
378      *
379      * @param tr The table row to which the cell element is to be added.
380      * @param td The cell element to be added (if not null).
381      */

382     private void addColumn(Element JavaDoc tr, Element JavaDoc td) {
383         if (td != null) {
384             tr.appendChild(td);
385         }
386     }
387     
388     /**
389      * Appends CSS text to the 'style' attribute of each table cell 'td'
390      * element.
391      *
392      * @param cssText the CSS text to add
393      */

394     void addCellCssText(String JavaDoc cssText) {
395         for (int i = 0; i < tdElements.length; ++i) {
396             if (tdElements[i].hasAttribute("style")) {
397                 tdElements[i].setAttribute("style", tdElements[i].getAttribute("style") + cssText);
398             } else {
399                 tdElements[i].setAttribute("style", cssText);
400             }
401         }
402         if (marginTdElements != null) {
403             for (int i = 0; i < marginTdElements.length; ++i) {
404                 if (marginTdElements[i] != null) {
405                     if (marginTdElements[i].hasAttribute("style")) {
406                         marginTdElements[i].setAttribute("style", marginTdElements[i].getAttribute("style") + cssText);
407                     } else {
408                         marginTdElements[i].setAttribute("style", cssText);
409                     }
410                 }
411             }
412         }
413     }
414     
415     /**
416      * Adds a row containing a single column element to a table.
417      * The row will not be added if <code>td</code> is null.
418      *
419      * @param tdElement The row element to be added.
420      * @param trElementId the id to assign to the row element.
421      */

422     private void addRow(Element JavaDoc tdElement, String JavaDoc trElementId) {
423         if (tdElement != null) {
424             Element JavaDoc trElement = document.createElement("tr");
425             trElement.setAttribute("id", trElementId);
426             trElement.appendChild(tdElement);
427             tbodyElement.appendChild(trElement);
428         }
429     }
430     
431     /**
432      * Adds a spacer element containing a transparent GIF image.
433      * These are unfortunately necessary to prevent spacer cells
434      * from collapsing in circumstances where horizontal real-estate
435      * is not available.
436      *
437      * @param parentElement the <code>Element</code> to which the spacer image
438      * is to be appended
439      * @param size the size of the spacer cell, in pixels
440      * @param vertical a flag indicating the direction; specify
441      * <code>true</code> for a vertical spacer or <code>false</code> for
442      * a horizontal spacer.
443      */

444     private void addSpacer(Element JavaDoc parentElement, int size, boolean vertical) {
445         Element JavaDoc imgElement = document.createElement("img");
446         imgElement.setAttribute("src", rc.getContainerInstance().getServiceUri(TRANSPARENT_SPACER_IMAGE_SERVICE));
447         imgElement.setAttribute("alt", "");
448         imgElement.setAttribute("width", vertical ? "1" : Integer.toString(size));
449         imgElement.setAttribute("height", vertical ? Integer.toString(size) : "1");
450         parentElement.appendChild(imgElement);
451     }
452
453     /**
454      * Returns the created table element.
455      *
456      * @return the table element
457      */

458     Element JavaDoc getTableElement() {
459         return tableElement;
460     }
461
462     /**
463      * Returns the specified container element.
464      *
465      * @param index The index of the table element to return. For two-celled tables,
466      * legitimate values are 0 and 1. For three-celled tables,
467      * legitimate values are 0, 1, and 2.
468      * @return The specified container element.
469      */

470     Element JavaDoc getTdElement(int index) {
471         return tdElements[index];
472     }
473     
474     /**
475      * Returns the specified margin element.
476      *
477      * @param index The index of the table element to return. Index 0 is the margin
478      * element between container cells 0 and 1. Index 1 is the margin
479      * element between container cells 0/1 and 2.
480      * @return The specified margin element. Returns null if the margin is zero
481      * pixels.
482      */

483     Element JavaDoc getMarginTdElement(int index) {
484         return marginTdElements[index];
485     }
486 }
487
Popular Tags