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.data.provider.TableDataProvider;
044: import com.sun.rave.designtime.DesignBean;
045: import com.sun.rave.designtime.DesignProperty;
046: import com.sun.rave.designtime.faces.FacesDesignBean;
047: import com.sun.rave.designtime.faces.FacesDesignContext;
048: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
049: import com.sun.rave.web.ui.component.*;
050:
051: /**
052: * This class defines the design time state of the Table Component
053: *
054: * @author Winston Prakash
055: */
056: public class TableDesignState {
057:
058: private static final String PAGINATION_CONTROLS_PROPERTY = "paginationControls";
059: private static final String PAGINATION_BUTTON_PROPERTY = "paginateButton";
060: private static final String TITLE_PROPERTY = "title";
061: private static final String SUMMARY_PROPERTY = "summary";
062: private static final String FOOTER_TEXT_PROPERTY = "footerText";
063: private static final String DESELECT_MULTIPLE_BUTTON_PROPERTY = "deselectMultipleButton";
064: private static final String SELECT_MULTIPLE_BUTTON_PROPERTY = "selectMultipleButton";
065: private static final String CLEAR_TABLE_SORT_BUTTON_PROPERTY = "clearSortButton";
066: private static final String SORT_PANEL_TOGGLE_BUTTON_PROPERTY = "sortPanelToggleButton";
067:
068: private DesignBean tableBean;
069: private DesignBean tableRowGroupBean = null;
070:
071: private FacesDesignContext fcontext = null;
072:
073: private TableRowGroupDesignState tableRowGroupDesignState = null;
074:
075: private boolean showPaginationControls = false;
076: private boolean showPaginationButton = false;
077: private boolean deselectMultipleButtonShown = false;
078: private boolean selectMultipleButtonShown = false;
079: private boolean clearTableSortButtonShown = false;
080: private boolean sortPanelToggleButtonShown = false;
081:
082: private String tableTitle = null;
083: private String tableSummary = null;
084: private String footerText = null;
085:
086: /** Creates a new instance of TableDesignState */
087: public TableDesignState(DesignBean tblBean) {
088: tableBean = tblBean;
089: fcontext = (FacesDesignContext) tableBean.getDesignContext();
090: }
091:
092: /** Creates a new instance of TableDesignState */
093: public TableDesignState(DesignBean tblBean,
094: DesignBean tblRowGroupBean) {
095: this (tblBean);
096: tableRowGroupBean = tblRowGroupBean;
097: }
098:
099: /**
100: * Load the current design time state of the Table component
101: */
102: public void loadState() {
103: if (tableRowGroupBean == null)
104: tableRowGroupBean = getTableRowGroupBean();
105: tableRowGroupDesignState = new TableRowGroupDesignState(
106: tableRowGroupBean);
107: tableRowGroupDesignState.loadSourceVariable();
108:
109: tableTitle = getStringPropertyValue(TITLE_PROPERTY);
110: tableSummary = getStringPropertyValue(SUMMARY_PROPERTY);
111: footerText = getStringPropertyValue(FOOTER_TEXT_PROPERTY);
112:
113: showPaginationControls = getBooleanPropertyValue(PAGINATION_CONTROLS_PROPERTY);
114: showPaginationButton = getBooleanPropertyValue(PAGINATION_BUTTON_PROPERTY);
115: deselectMultipleButtonShown = getBooleanPropertyValue(DESELECT_MULTIPLE_BUTTON_PROPERTY);
116: selectMultipleButtonShown = getBooleanPropertyValue(SELECT_MULTIPLE_BUTTON_PROPERTY);
117: clearTableSortButtonShown = getBooleanPropertyValue(CLEAR_TABLE_SORT_BUTTON_PROPERTY);
118: sortPanelToggleButtonShown = getBooleanPropertyValue(SORT_PANEL_TOGGLE_BUTTON_PROPERTY);
119: }
120:
121: /**
122: * Find the first TableRowGroup bean. If not found create one
123: */
124: public DesignBean getTableRowGroupBean() {
125: DesignBean tableRowGroupBean = null;
126: int childCount = tableBean.getChildBeanCount();
127: for (int i = 0; i < childCount; i++) {
128: tableRowGroupBean = tableBean.getChildBean(i);
129: if (tableRowGroupBean.getInstance() instanceof TableRowGroup) {
130: break;
131: }
132: }
133: if (tableRowGroupBean == null) {
134: tableRowGroupBean = fcontext.createBean(TableRowGroup.class
135: .getName(), tableBean, null);
136: }
137: return tableRowGroupBean;
138: }
139:
140: /**
141: * Set the TableRowGroup design State
142: */
143: public TableRowGroupDesignState getTableRowGroupDesignState() {
144: if (tableRowGroupDesignState == null) {
145: tableRowGroupDesignState = new TableRowGroupDesignState(
146: getTableRowGroupBean());
147: tableRowGroupDesignState.loadSourceVariable();
148: }
149: return tableRowGroupDesignState;
150: }
151:
152: /**
153: * Clear all the property values set to this state
154: */
155: public void clearProperties() {
156: showPaginationControls = false;
157: showPaginationButton = false;
158: deselectMultipleButtonShown = false;
159: selectMultipleButtonShown = false;
160: clearTableSortButtonShown = false;
161: sortPanelToggleButtonShown = false;
162: tableTitle = null;
163: tableSummary = null;
164: footerText = null;
165: }
166:
167: /**
168: * Get the boolean value of the property
169: */
170: public boolean getBooleanPropertyValue(String propertyname) {
171: boolean value = false;
172: Object propValue = getPropertyValue(propertyname);
173: if (propValue != null) {
174: value = ((Boolean) propValue).booleanValue();
175: }
176: return value;
177: }
178:
179: /**
180: * Get String property value
181: */
182: private String getStringPropertyValue(String propertyname) {
183: Object value = getPropertyValue(propertyname);
184: if (value != null) {
185: return value.toString();
186: } else {
187: return null;
188: }
189: }
190:
191: /**
192: * Load the property value from the bean to this state
193: */
194: private Object getPropertyValue(String propertyname) {
195: Object propertyValue = null;
196: DesignProperty designProperty = tableBean
197: .getProperty(propertyname);
198: if (designProperty != null) {
199: if (designProperty.getValue() != null) {
200: propertyValue = designProperty.getValue();
201: }
202: }
203: return propertyValue;
204: }
205:
206: /**
207: * Get the property value source from the bean to this state
208: */
209: private String getPropertyValueSource(String propertyname) {
210: String propertyValue = null;
211: DesignProperty designProperty = tableBean
212: .getProperty(propertyname);
213: if (designProperty != null) {
214: propertyValue = designProperty.getValueSource();
215: }
216: return propertyValue;
217: }
218:
219: /**
220: * Set the value to the bean property as stored in this state
221: */
222: private void setPropertyValue(String propertyname, Object value) {
223: if (value != null) {
224: DesignProperty designProperty = tableBean
225: .getProperty(propertyname);
226: if (designProperty != null) {
227: Object origValue = getPropertyValue(propertyname);
228: if (value != origValue) {
229: if ((value instanceof String)
230: && value.toString().equals("")) {
231: designProperty.unset();
232: } else {
233: designProperty.setValue(value);
234: }
235: }
236: }
237: }
238: }
239:
240: /**
241: * Set a boolean value to the property
242: */
243: private void setBooleanPropertyValue(String propertyname,
244: boolean value) {
245: DesignProperty designProperty = tableBean
246: .getProperty(propertyname);
247: if (designProperty != null) {
248: boolean origValue = getBooleanPropertyValue(propertyname);
249: if (origValue != value) {
250: if (value) {
251: designProperty.setValue(new Boolean(true));
252: } else {
253: designProperty.unset();
254: }
255: }
256: }
257: }
258:
259: /**
260: * Unset the value to the bean property
261: */
262: private void unsetPropertyValue(String propertyname) {
263: DesignProperty designProperty = tableBean
264: .getProperty(propertyname);
265: if (designProperty != null) {
266: designProperty.unset();
267: }
268: }
269:
270: /**
271: * Set the TableRowGroup design State
272: */
273: public void setTableRowGroupDesignState(
274: TableRowGroupDesignState designState) {
275: tableRowGroupDesignState = designState;
276: }
277:
278: /**
279: * Set the Data model DesignBeean to this table design state
280: * In turn the DataProvider of the TableRowGroup is set
281: */
282: public void setDataProviderBean(DesignBean dataProviderBean) {
283: if (!(dataProviderBean.getInstance() instanceof TableDataProvider)) {
284: throw new IllegalArgumentException(dataProviderBean
285: .getInstanceName()
286: + " not a table data provider.");
287: }
288: getTableRowGroupDesignState().setDataProviderBean(
289: dataProviderBean, true);
290: }
291:
292: /**
293: * Set the footer text
294: */
295: public void setFooterText(String footer) {
296: footerText = footer;
297: }
298:
299: /**
300: * get the footer text
301: */
302: public String getFooterText() {
303: return footerText;
304: }
305:
306: /**
307: * Set the table title
308: */
309: public void setTitle(String title) {
310: tableTitle = title;
311: }
312:
313: /**
314: * get the table title
315: */
316: public String getTitle() {
317: return tableTitle;
318: }
319:
320: /**
321: * Set the table title
322: */
323: public void setSummary(String summary) {
324: tableSummary = summary;
325: }
326:
327: /**
328: * get the table title
329: */
330: public String getSummary() {
331: return tableSummary;
332: }
333:
334: /**
335: * Set if pagination is enabled
336: */
337: public void setPaginationEnabled(boolean enabled) {
338: if (enabled) {
339: showPaginationControls = true;
340: showPaginationButton = true;
341: getTableRowGroupDesignState().setPaginated(true);
342: } else {
343: getTableRowGroupDesignState().setPaginated(false);
344: showPaginationControls = false;
345: showPaginationButton = false;
346: }
347: }
348:
349: /**
350: * Get if pagination is enabled
351: */
352: public boolean isPaginationEnabled() {
353: return showPaginationControls;
354: }
355:
356: /**
357: * Set if deselect multiple button should be shown
358: */
359: public void setDeselectMultipleButtonShown(boolean shown) {
360: deselectMultipleButtonShown = shown;
361: }
362:
363: /**
364: * Get if deselect multiple button should be shown
365: */
366: public boolean isDeselectMultipleButtonShown() {
367: return deselectMultipleButtonShown;
368: }
369:
370: /**
371: * Set if deselect multiple button should be shown
372: */
373: public void setSelectMultipleButtonShown(boolean shown) {
374: selectMultipleButtonShown = shown;
375: }
376:
377: /**
378: * Get if deselect multiple button should be shown
379: */
380: public boolean isSelectMultipleButtonShown() {
381: return selectMultipleButtonShown;
382: }
383:
384: /**
385: * Set if deselect multiple button should be shown
386: */
387: public void setClearTableSortButtonShown(boolean shown) {
388: clearTableSortButtonShown = shown;
389: }
390:
391: /**
392: * Get if deselect multiple button should be shown
393: */
394: public boolean isClearTableSortButtonShown() {
395: return clearTableSortButtonShown;
396: }
397:
398: /**
399: * Set if deselect multiple button should be shown
400: */
401: public void setSortPanelToggleButtonShown(boolean shown) {
402: sortPanelToggleButtonShown = shown;
403: }
404:
405: /**
406: * Get if deselect multiple button should be shown
407: */
408: public boolean isSortPanelToggleButtonShown() {
409: return sortPanelToggleButtonShown;
410: }
411:
412: /**
413: * Save the table design time state to the design bean
414: */
415: public void saveState() {
416: FacesDesignBean ftableBean = (FacesDesignBean) tableBean;
417: setPropertyValue(TITLE_PROPERTY, tableTitle);
418:
419: setPropertyValue(SUMMARY_PROPERTY, tableSummary);
420: setPropertyValue(FOOTER_TEXT_PROPERTY, footerText);
421:
422: setBooleanPropertyValue(CLEAR_TABLE_SORT_BUTTON_PROPERTY,
423: clearTableSortButtonShown);
424: setBooleanPropertyValue(SELECT_MULTIPLE_BUTTON_PROPERTY,
425: selectMultipleButtonShown);
426: setBooleanPropertyValue(DESELECT_MULTIPLE_BUTTON_PROPERTY,
427: deselectMultipleButtonShown);
428: setBooleanPropertyValue(SORT_PANEL_TOGGLE_BUTTON_PROPERTY,
429: sortPanelToggleButtonShown);
430:
431: tableRowGroupDesignState.saveState();
432:
433: setBooleanPropertyValue(PAGINATION_CONTROLS_PROPERTY,
434: showPaginationControls);
435: setBooleanPropertyValue(PAGINATION_BUTTON_PROPERTY,
436: showPaginationButton);
437:
438: }
439: }
|