001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.web.ui.dt.component.table;
042:
043: import com.sun.rave.designtime.DesignBean;
044: import com.sun.rave.designtime.DesignProperty;
045: import com.sun.rave.designtime.faces.FacesDesignContext;
046: import com.sun.rave.web.ui.component.Checkbox;
047: import com.sun.rave.web.ui.component.DropDown;
048: import com.sun.rave.web.ui.component.ImageComponent;
049: import com.sun.rave.web.ui.component.Label;
050: import com.sun.rave.web.ui.component.Message;
051: import com.sun.rave.web.ui.component.RadioButton;
052: import com.sun.rave.web.ui.component.StaticText;
053: import com.sun.rave.web.ui.component.TextField;
054: import com.sun.rave.web.ui.component.TextArea;
055: import com.sun.rave.web.ui.component.Button;
056: import com.sun.rave.web.ui.component.Hyperlink;
057: import com.sun.rave.web.ui.component.ImageHyperlink;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * Design state of each column in the TableRowGroup
062: * @author Winston Prakash
063: */
064: public class TableColumnDesignState {
065:
066: private static final String ID_PROPERTY = "id";
067: private static final String TEXT_PROPERTY = "text";
068: private static final String LABEL_PROPERTY = "label";
069: private static final String HEADER_TEXT_PROPERTY = "headerText";
070: private static final String FOOTER_TEXT_PROPERTY = "footerText";
071: private static final String HORIZONTAL_ALIGN_PROPERTY = "align";
072: private static final String VERTICAL_ALIGN_PROPERTY = "valign";
073: private static final String SORT_PROPERTY = "sort";
074: private static final String WIDTH_PROPERTY = "width";
075: private static final String ITEMS_PROPERTY = "items";
076: private static final String URL_PROPERTY = "url";
077: private static final String SELECTED_PROPERTY = "selected";
078:
079: private String columnName = null;
080: private String columnHeader = null;
081: private String columnFooter = null;
082: private String horizontalAlign = null;
083: private String verticalAlign = null;
084: private String valueExpression = null;
085: private String sourceVariable = TableRowGroupDesignState.sourceVarNameBase;
086:
087: private DesignBean tableColumnBean = null;
088: private Class childBeanType = StaticText.class;
089:
090: private boolean sortable = true;
091: private String columnWidth = null;
092:
093: private Class columnType = String.class;
094:
095: public TableColumnDesignState(String colName, String colHeader,
096: String valExpression) {
097: columnName = colName;
098: columnHeader = colHeader;
099: valueExpression = valExpression;
100: }
101:
102: public TableColumnDesignState(String colName) {
103: columnName = colName;
104: if (colName.lastIndexOf(".") > 0) {
105: columnHeader = columnName.substring(columnName
106: .lastIndexOf(".") + 1);
107: } else {
108: columnHeader = columnName;
109: }
110: valueExpression = "#{" + sourceVariable + ".value['"
111: + columnName + "']" + "}";
112: }
113:
114: /** Creates a new instance of TableColumnDesignState */
115: public TableColumnDesignState(DesignBean tblColumnBean) {
116: tableColumnBean = tblColumnBean;
117: }
118:
119: public void loadState() {
120: if (tableColumnBean.getChildBeanCount() > 0) {
121: DesignBean tableColumnChildBean = tableColumnBean
122: .getChildBean(0);
123: Object instance = tableColumnChildBean.getInstance();
124: childBeanType = instance.getClass();
125:
126: // Get the Colum Name from the text value source of the child bean
127: DesignProperty valueProperty = getValueDesignProperty(tableColumnChildBean);
128: if (valueProperty != null) {
129: valueExpression = valueProperty.getValueSource();
130: if ((valueExpression != null)
131: && valueExpression.startsWith("#{")) {
132: // XXX Revisit. For the time being return value enclosed with in [' & ']
133: int startIndex = valueExpression.indexOf('[');
134: int endIndex = valueExpression.indexOf(']');
135: if ((startIndex != -1) && (endIndex != -1)) {
136: columnName = valueExpression.substring(
137: startIndex + 2, endIndex - 1);
138: }
139: }
140: }
141: }
142: if (columnName == null) {
143: columnName = getStringPropertyValue(ID_PROPERTY);
144: }
145:
146: // Get the header text
147: columnFooter = getStringPropertyValue(FOOTER_TEXT_PROPERTY);
148: columnHeader = getStringPropertyValue(HEADER_TEXT_PROPERTY);
149: horizontalAlign = getStringPropertyValue(HORIZONTAL_ALIGN_PROPERTY);
150: verticalAlign = getStringPropertyValue(VERTICAL_ALIGN_PROPERTY);
151:
152: String sort = getStringPropertyValue(SORT_PROPERTY);
153: if (sort != null) {
154: sortable = true;
155: } else {
156: sortable = false;
157: }
158:
159: columnWidth = getStringPropertyValue(WIDTH_PROPERTY);
160: }
161:
162: /**
163: * Clear all the property values set to this state
164: */
165: public void clearProperties() {
166: columnFooter = null;
167: columnHeader = null;
168: horizontalAlign = null;
169: verticalAlign = null;
170: valueExpression = null;
171: sortable = true;
172: }
173:
174: /**
175: * Get int property value
176: */
177: private int getIntegerPropertyValue(String propertyname) {
178: Object value = getPropertyValue(propertyname);
179: if (value != null) {
180: return ((Integer) value).intValue();
181: } else {
182: return -1;
183: }
184: }
185:
186: /**
187: * Get the boolean value of the property
188: */
189: public boolean getBooleanPropertyValue(String propertyname) {
190: boolean value = false;
191: Object propValue = getPropertyValue(propertyname);
192: if (propValue != null) {
193: value = ((Boolean) propValue).booleanValue();
194: }
195: return value;
196: }
197:
198: /**
199: * Get String property value
200: */
201: private String getStringPropertyValue(String propertyname) {
202: Object value = getPropertyValue(propertyname);
203: if (value != null) {
204: return value.toString();
205: } else {
206: return null;
207: }
208: }
209:
210: /**
211: * Load the property value from the bean to this state
212: */
213: private Object getPropertyValue(String propertyname) {
214: Object propertyValue = null;
215: DesignProperty designProperty = tableColumnBean
216: .getProperty(propertyname);
217: if (designProperty != null) {
218: if (designProperty.getValue() != null) {
219: propertyValue = designProperty.getValue();
220: }
221: }
222: return propertyValue;
223: }
224:
225: /**
226: * Get the property value source from the bean to this state
227: */
228: private String getPropertyValueSource(String propertyname) {
229: String propertyValue = null;
230: DesignProperty designProperty = tableColumnBean
231: .getProperty(propertyname);
232: if (designProperty != null) {
233: propertyValue = designProperty.getValueSource();
234: }
235: return propertyValue;
236: }
237:
238: /**
239: * Set the value to the bean property as stored in this state
240: */
241: private void setPropertyValue(String propertyname, Object value) {
242: if (value != null) {
243: DesignProperty designProperty = tableColumnBean
244: .getProperty(propertyname);
245: if (designProperty != null) {
246: Object origValue = getPropertyValue(propertyname);
247: if (value != origValue) {
248: if ((value instanceof String)
249: && value.toString().equals("")) {
250: designProperty.unset();
251: } else {
252: designProperty.setValue(value);
253: }
254: }
255: }
256: }
257: }
258:
259: /**
260: * Set a boolean value to the property
261: */
262: private void setBooleanPropertyValue(String propertyname,
263: boolean value) {
264: DesignProperty designProperty = tableColumnBean
265: .getProperty(propertyname);
266: if (designProperty != null) {
267: boolean origValue = getBooleanPropertyValue(propertyname);
268: if (origValue != value) {
269: if (value) {
270: designProperty.setValue(new Boolean(true));
271: } else {
272: designProperty.unset();
273: }
274: }
275: }
276: }
277:
278: /**
279: * Unset the value to the bean property
280: */
281: private void unsetPropertyValue(String propertyname) {
282: DesignProperty designProperty = tableColumnBean
283: .getProperty(propertyname);
284: if (designProperty != null) {
285: designProperty.unset();
286: }
287: }
288:
289: private DesignProperty getValueDesignProperty(
290: DesignBean tblColumnChildBean) {
291: DesignProperty valueProperty = null;
292: if ((tblColumnChildBean.getInstance() instanceof Button)) {
293: valueProperty = tblColumnChildBean
294: .getProperty(TEXT_PROPERTY); //NOI18N
295: } else if ((tblColumnChildBean.getInstance() instanceof Checkbox)) {
296: valueProperty = tblColumnChildBean
297: .getProperty(SELECTED_PROPERTY); //NOI18N
298: } else if ((tblColumnChildBean.getInstance() instanceof RadioButton)) {
299: valueProperty = tblColumnChildBean
300: .getProperty(LABEL_PROPERTY); //NOI18N
301: } else if (tblColumnChildBean.getInstance() instanceof DropDown) {
302: valueProperty = tblColumnChildBean
303: .getProperty(SELECTED_PROPERTY); //NOI18N
304: } else if (tblColumnChildBean.getInstance() instanceof ImageComponent) {
305: valueProperty = tblColumnChildBean
306: .getProperty(URL_PROPERTY); //NOI18N
307: } else if ((tblColumnChildBean.getInstance() instanceof TextField)) {
308: valueProperty = tblColumnChildBean
309: .getProperty(TEXT_PROPERTY); //NOI18N
310: } else if ((tblColumnChildBean.getInstance() instanceof TextArea)) {
311: valueProperty = tblColumnChildBean
312: .getProperty(TEXT_PROPERTY); //NOI18N
313: } else if ((tblColumnChildBean.getInstance() instanceof StaticText)) {
314: valueProperty = tblColumnChildBean
315: .getProperty(TEXT_PROPERTY); //NOI18N
316: } else if ((tblColumnChildBean.getInstance() instanceof Label)) {
317: valueProperty = tblColumnChildBean
318: .getProperty(TEXT_PROPERTY); //NOI18N
319: } else if ((tblColumnChildBean.getInstance() instanceof Message)) {
320: valueProperty = tblColumnChildBean
321: .getProperty(TEXT_PROPERTY); //NOI18N
322: } else if ((tblColumnChildBean.getInstance() instanceof Hyperlink)) {
323: valueProperty = tblColumnChildBean
324: .getProperty(TEXT_PROPERTY); //NOI18N
325: } else if ((tblColumnChildBean.getInstance() instanceof ImageHyperlink)) {
326: valueProperty = tblColumnChildBean
327: .getProperty(TEXT_PROPERTY); //NOI18N
328: }
329:
330: return valueProperty;
331: }
332:
333: /*
334: * Set the table column bean to this design state
335: */
336: public void setTableColumnBean(DesignBean tblColumnBean) {
337: tableColumnBean = tblColumnBean;
338: }
339:
340: /*
341: * Get the table column bean of this design state
342: */
343: public DesignBean getTableColumnBean() {
344: return tableColumnBean;
345: }
346:
347: /**
348: * Set the value expression
349: */
350: public void setValueExpression(String expression) {
351: valueExpression = expression;
352: }
353:
354: /**
355: * Set the value expression
356: */
357: public String getValueExpression() {
358: return valueExpression;
359: }
360:
361: /**
362: * Set the Source Variable
363: */
364: public void setSourceVariable(String srcVariable) {
365: sourceVariable = srcVariable;
366: }
367:
368: /**
369: * Get the Source Variable
370: */
371: public String getSourceVariable() {
372: return sourceVariable;
373: }
374:
375: /**
376: * Set the type of the Child bean that this container hold
377: */
378:
379: public void setChildType(Class childType) {
380: childBeanType = childType;
381: }
382:
383: /**
384: * Get the type of the child bean
385: */
386: public Class getChildType() {
387: return childBeanType;
388: }
389:
390: /**
391: * Get the column type.
392: */
393: public Class getColumnType() {
394: return this .columnType;
395: }
396:
397: /**
398: * Set the column type.
399: */
400: public void setColumnType(Class columnType) {
401: this .columnType = columnType;
402: }
403:
404: /**
405: * Get the columns name (usually the table data provider filed key)
406: */
407: public String getName() {
408: return columnName;
409: }
410:
411: /**
412: * Set the columns name (usually the table data provider filed key)
413: */
414: public void setName(String name) {
415: columnName = name;
416: }
417:
418: /**
419: * Get the columns header text (Could be same as the data provider filed key)
420: */
421: public String getHeader() {
422: return columnHeader;
423: }
424:
425: /**
426: * Set the columns header text (Could be same as the data provider filed key)
427: */
428: public void setHeader(String header) {
429: columnHeader = header;
430: }
431:
432: /**
433: * Get the column footer text
434: */
435: public String getFooter() {
436: return columnFooter;
437: }
438:
439: /**
440: * Set the column footer text
441: */
442: public void setFooter(String footer) {
443: columnFooter = footer;
444: }
445:
446: /**
447: * Set the horizontal align
448: */
449: public void setHorizontalAlign(String horizAlign) {
450: horizontalAlign = horizAlign;
451: }
452:
453: /**
454: * get the horizontal align
455: */
456: public String getHorizontalAlign() {
457: return horizontalAlign;
458: }
459:
460: /**
461: * Set the vertical align
462: */
463: public void setVerticalAlign(String vertAlign) {
464: verticalAlign = vertAlign;
465: }
466:
467: /**
468: * Get the vertical align
469: */
470: public String getVerticalAlign() {
471: return verticalAlign;
472: }
473:
474: /**
475: * Set if sortable
476: */
477: public void setSortable(boolean sort) {
478: sortable = sort;
479: }
480:
481: /**
482: * Get if sortable
483: */
484: public boolean isSortable() {
485: return sortable;
486: }
487:
488: /**
489: * Set column width
490: */
491: public void setWidth(String width) {
492: setWidth(width, false);
493: }
494:
495: /**
496: * Set column width
497: * immediat - immediatly persist the value (designer refreshed)
498: */
499: public void setWidth(String width, boolean immediat) {
500: columnWidth = width;
501: if (immediat) {
502: setPropertyValue(WIDTH_PROPERTY, String
503: .valueOf(columnWidth));
504: }
505: }
506:
507: /**
508: * Get column width
509: */
510: public String getWidth() {
511: return columnWidth;
512: }
513:
514: public void saveState() {
515: // Create the Child bean and set its text property
516: FacesDesignContext fcontext = (FacesDesignContext) tableColumnBean
517: .getDesignContext();
518: DesignBean tableColumnChildBean = null;
519: if (tableColumnBean.getChildBeanCount() > 0) {
520: tableColumnChildBean = tableColumnBean.getChildBean(0);
521: if (!tableColumnChildBean.getInstance().getClass()
522: .isAssignableFrom(childBeanType)) {
523: fcontext.deleteBean(tableColumnChildBean);
524: tableColumnChildBean = fcontext.createBean(
525: childBeanType.getName(), tableColumnBean, null);
526: }
527: } else {
528: tableColumnChildBean = fcontext.createBean(childBeanType
529: .getName(), tableColumnBean, null);
530: }
531: DesignProperty valueProperty = getValueDesignProperty(tableColumnChildBean);
532: if (valueProperty != null) {
533: if (valueExpression == null) {
534: if ((columnName != null) && (sourceVariable != null)) {
535: valueExpression = "#{" + sourceVariable
536: + ".value['" + columnName + "']" + "}";
537: }
538: }
539: valueProperty.setValue(valueExpression);
540: }
541:
542: // If the child is a DropDown and its items property is not set then set it to valueexpression
543: if (tableColumnChildBean.getInstance() instanceof DropDown) {
544: DesignProperty itemsProperty = tableColumnChildBean
545: .getProperty(ITEMS_PROPERTY);
546: if ((valueExpression != null) && (itemsProperty != null)
547: && (itemsProperty.getValue() == null)) {
548: itemsProperty.setValue(valueExpression);
549: }
550: }
551:
552: // Set the properties of the Table Column bean
553: setPropertyValue(FOOTER_TEXT_PROPERTY, columnFooter);
554: setPropertyValue(HEADER_TEXT_PROPERTY, columnHeader);
555: setPropertyValue(HORIZONTAL_ALIGN_PROPERTY, horizontalAlign);
556: setPropertyValue(VERTICAL_ALIGN_PROPERTY, verticalAlign);
557:
558: if (sortable) {
559: if (isSortAllowed(valueExpression, columnName)) {
560: setPropertyValue(SORT_PROPERTY, columnName);
561: }
562: } else {
563: unsetPropertyValue(SORT_PROPERTY);
564: }
565: setPropertyValue(WIDTH_PROPERTY, columnWidth);
566: }
567:
568: public boolean isSortAllowed() {
569: return isSortAllowed(valueExpression, columnName);
570: }
571:
572: private boolean isSortAllowed(String valueExpression, String colName) {
573: if (DropDown.class.isAssignableFrom(childBeanType)) {
574: return false;
575: }
576: if ((valueExpression != null)
577: && valueExpression.trim().startsWith("#{")) {
578: int startIndex = valueExpression.indexOf('[');
579: int endIndex = valueExpression.indexOf(']');
580: if ((startIndex != -1) && (endIndex != -1)) {
581: String columnName = valueExpression.substring(
582: startIndex + 2, endIndex - 1);
583: if (columnName.trim().equals(columnName.trim())) {
584: return true;
585: }
586: }
587: }
588: return false;
589: }
590: }
|