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.Table;
047: import com.sun.rave.web.ui.component.TableRowGroup;
048: import com.sun.rave.web.ui.model.DefaultTableDataProvider;
049: import java.io.StringWriter;
050: import java.util.Iterator;
051: import java.util.Properties;
052: import java.util.StringTokenizer;
053: import org.openide.ErrorManager;
054:
055: /**
056: * Helper class for Table designtime
057: * @author Winston Prakash
058: */
059: public final class TableDesignHelper {
060:
061: private static final String SOURCE_DATA_PROPERTY = "sourceData";
062: public static final String DEFAULT_TABLE_DATA_PROVIDER = "defaultTableDataProvider"; //NOI18N
063: private static final int INITIAL_TABLE_COLUMN_WIDTH = 150;
064: private static final String WIDTH_PROPRTY = "width";
065:
066: // Should not be instantiated
067: private TableDesignHelper() {
068: }
069:
070: /**
071: * Create the default table model bean
072: */
073: public static DesignBean createDefaultDataProvider(
074: DesignBean tableBean) {
075: FacesDesignContext fcontext = (FacesDesignContext) tableBean
076: .getDesignContext();
077: DesignBean[] defTableDataProviderBeans = fcontext
078: .getBeansOfType(DefaultTableDataProvider.class);
079: if ((defTableDataProviderBeans == null)
080: || (defTableDataProviderBeans.length < 1)) {
081: DesignBean defaultDataProvider = fcontext.createBean(
082: DefaultTableDataProvider.class.getName(), null,
083: null);
084: defaultDataProvider
085: .setInstanceName(DEFAULT_TABLE_DATA_PROVIDER);
086: return defaultDataProvider;
087: } else {
088: return defTableDataProviderBeans[0];
089: }
090: }
091:
092: public static boolean isDefaultDataProvider(DesignBean tableBean,
093: DesignBean dataProviderBean) {
094: FacesDesignContext fcontext = (FacesDesignContext) tableBean
095: .getDesignContext();
096: DesignBean[] defTableDataProviderBeans = fcontext
097: .getBeansOfType(DefaultTableDataProvider.class);
098: if ((defTableDataProviderBeans != null)
099: && (defTableDataProviderBeans.length > 0)) {
100: // Always assume there can be only one Default Table Data Provider per page
101: if (dataProviderBean == defTableDataProviderBeans[0]) {
102: return true;
103: }
104: }
105: return false;
106: }
107:
108: /**
109: * Delete the default model if another data model data is used to fill the table row group
110: */
111: public static void deleteDefaultDataProvider(DesignBean designBean) {
112: DesignBean tableBean;
113: FacesDesignContext fcontext = (FacesDesignContext) designBean
114: .getDesignContext();
115: DesignBean[] defTableDataProviderBeans = fcontext
116: .getBeansOfType(DefaultTableDataProvider.class);
117: // Always assume there can be only one Default Table Data Provider per page context
118: if ((defTableDataProviderBeans != null)
119: && (defTableDataProviderBeans.length > 0)) {
120: // Check if the bean passed is a TableRowGroup, if so see if the
121: // containing table has anyother row group that references the
122: // Default Table Provider
123: if (designBean.getInstance() instanceof TableRowGroup) {
124: tableBean = designBean.getBeanParent();
125: int childCount = tableBean.getChildBeanCount();
126: for (int j = 0; j < childCount; j++) {
127: DesignBean childBean = tableBean.getChildBean(j);
128: // Don't check the passed TableRowGroup bean it is being deleted anyway
129: if ((childBean.getInstance() instanceof TableRowGroup)
130: && (childBean != designBean)) {
131: String sourceDataStr = null;
132: DesignProperty designProperty = childBean
133: .getProperty(SOURCE_DATA_PROPERTY);
134: if (designProperty != null) {
135: sourceDataStr = designProperty
136: .getValueSource();
137: String modelBindingExpr = fcontext
138: .getBindingExpr(defTableDataProviderBeans[0]);
139: if (sourceDataStr
140: .startsWith(modelBindingExpr)) {
141: // OK there is a reference so return with out deleting
142: return;
143: }
144: }
145: }
146: }
147: } else {
148: tableBean = designBean;
149: }
150: DesignBean[] tableBeans = fcontext
151: .getBeansOfType(Table.class);
152: if (tableBeans != null) {
153: for (int i = 0; i < tableBeans.length; i++) {
154: // We don't have to check the passed bean. It is being deleted anyway
155: // or already checked if passed bean is a TableRowGroup
156: if (tableBean != tableBeans[i]) {
157: int childCount = tableBeans[i]
158: .getChildBeanCount();
159: for (int j = 0; j < childCount; j++) {
160: DesignBean childBean = tableBeans[i]
161: .getChildBean(j);
162: if (childBean.getInstance() instanceof TableRowGroup) {
163: TableRowGroup tableRowGroup = (TableRowGroup) childBean
164: .getInstance();
165: String sourceDataStr = null;
166: DesignProperty designProperty = childBean
167: .getProperty(SOURCE_DATA_PROPERTY);
168: if (designProperty != null) {
169: sourceDataStr = designProperty
170: .getValueSource();
171: String modelBindingExpr = fcontext
172: .getBindingExpr(defTableDataProviderBeans[0]);
173: if (modelBindingExpr
174: .startsWith(sourceDataStr)) {
175: // OK there is a reference so return with out deleting
176: return;
177: }
178: }
179: }
180: }
181: }
182: }
183: }
184: for (int i = 0; i < defTableDataProviderBeans.length; i++) {
185: fcontext.deleteBean(defTableDataProviderBeans[i]);
186: }
187: }
188: }
189:
190: /**
191: * Find the first TableRowGroup bean. Should never return null!
192: */
193: public static DesignBean getTableRowGroupBean(DesignBean tableBean) {
194: DesignBean tableRowGroupBean = null;
195: int childCount = tableBean.getChildBeanCount();
196: for (int i = 0; i < childCount; i++) {
197: tableRowGroupBean = tableBean.getChildBean(i);
198: if (tableRowGroupBean.getInstance() instanceof TableRowGroup) {
199: break;
200: }
201: }
202: return tableRowGroupBean;
203: }
204:
205: /**
206: * Find the TableColumnBean corresponding to the column no.
207: * Should never return null!
208: */
209: public static DesignBean getTableColumnBean(
210: DesignBean tableRowGroupBean, int colNo) {
211: DesignBean tableColumnBean = null;
212: int childCount = tableRowGroupBean.getChildBeanCount();
213: for (int i = 0; i < childCount; i++) {
214: tableColumnBean = tableRowGroupBean.getChildBean(i);
215: //TableColumn tableColumn = (TableColumn) tableColumnBean.getInstance();
216: if (i == colNo) {
217: break;
218: }
219: }
220: return tableColumnBean;
221: }
222:
223: public static void adjustTableWidth(DesignBean tableBean,
224: int oldColumnWidth, int newColumnWidth) {
225: DesignBean tableRowGroupBean = getTableRowGroupBean(tableBean);
226: if (tableRowGroupBean == null) {
227: return;
228: }
229: // Adjust the width of the table in its style property
230: int tableWidth = -1;
231:
232: DesignProperty widthProperty = tableBean
233: .getProperty(WIDTH_PROPRTY); //NOI18N
234: String widthValue = (String) widthProperty.getValue();
235:
236: if (widthProperty.getValue() != null) {
237: try {
238: tableWidth = Integer.parseInt(widthValue);
239: } catch (Exception exc) {
240: tableWidth = -1;
241: }
242: }
243:
244: int childCount = tableRowGroupBean.getChildBeanCount();
245:
246: if (tableWidth == -1) {
247: //System.out.println("Table width null! ");
248: for (int i = 0; i < childCount; i++) {
249: DesignBean tableColumnBean = tableRowGroupBean
250: .getChildBean(i);
251: DesignProperty columnWidthProperty = tableColumnBean
252: .getProperty(WIDTH_PROPRTY); //NOI18N
253: if ((columnWidthProperty != null)
254: && (columnWidthProperty.getValue() != null)) {
255: try {
256: int colWidth = Integer
257: .parseInt((String) columnWidthProperty
258: .getValue());
259: //System.out.println("Width of column - " + i + " is " + colWidth);
260: tableWidth += colWidth;
261: } catch (Exception exc) {
262: ErrorManager.getDefault().notify(exc);
263: tableWidth += INITIAL_TABLE_COLUMN_WIDTH;
264: }
265: } else {
266: tableWidth += INITIAL_TABLE_COLUMN_WIDTH;
267: }
268: }
269: }
270:
271: //System.out.println("Table old width - " + tableWidth);
272: if (oldColumnWidth == -1) {
273: int noWidthCols = 0;
274: int setColumnsWidth = 0;
275: for (int i = 0; i < childCount; i++) {
276: DesignBean tableColumnBean = tableRowGroupBean
277: .getChildBean(i);
278: DesignProperty tcWidthProperty = tableColumnBean
279: .getProperty(WIDTH_PROPRTY);
280: if ((tcWidthProperty != null)
281: && (tcWidthProperty.getValue() != null)) {
282: try {
283: int colWidth = Integer
284: .parseInt((String) tcWidthProperty
285: .getValue());
286: //System.out.println("Width of column - " + i + " is " + colWidth);
287: setColumnsWidth += colWidth;
288: } catch (Exception exc) {
289: ErrorManager.getDefault().notify(exc);
290: noWidthCols++;
291: }
292: } else {
293: noWidthCols++;
294: }
295: }
296: //System.out.println("No of columns without width - " + noWidthCols);
297: //System.out.println("Total width of columns set - " + setColumnsWidth);
298: if (noWidthCols != 0) {
299: oldColumnWidth = (tableWidth - setColumnsWidth)
300: / noWidthCols;
301: //System.out.println(" Old Width - " + oldColumnWidth + " New Column Width " + newColumnWidth);
302: tableWidth = tableWidth
303: + (newColumnWidth - oldColumnWidth);
304: //System.out.println("Table new width - " + tableWidth);
305: }
306: }
307:
308: widthValue = String.valueOf(tableWidth);
309: widthProperty.setValue(widthValue);
310: }
311:
312: public static void adjustTableWidth(DesignBean tableRowGroupBean) {
313: // Adjust the width of the table in its style property
314: //System.out.println("Adjusting Table Width .. ");
315: DesignBean tableBean = tableRowGroupBean.getBeanParent();
316: DesignProperty tableWidthProperty = tableBean
317: .getProperty(WIDTH_PROPRTY); //NOI18N
318: int oldTableWidth = 0;
319: int childCount = tableRowGroupBean.getChildBeanCount();
320:
321: if ((tableWidthProperty != null)
322: && (tableWidthProperty.getValue() != null)) {
323: try {
324: oldTableWidth = Integer
325: .parseInt((String) tableWidthProperty
326: .getValue());
327: } catch (Exception exc) {
328: ErrorManager.getDefault().notify(exc);
329: }
330: } else {
331: // When the table is dropped set its initial width, else the default width will be 100%
332: for (int i = 0; i < childCount; i++) {
333: oldTableWidth += INITIAL_TABLE_COLUMN_WIDTH;
334: }
335: tableWidthProperty.setValue(String.valueOf(oldTableWidth));
336: }
337:
338: int newTableWidth = 0;
339:
340: for (int i = 0; i < childCount; i++) {
341: DesignBean tableColumnBean = tableRowGroupBean
342: .getChildBean(i);
343: DesignProperty columnWidthProperty = tableColumnBean
344: .getProperty(WIDTH_PROPRTY); //NOI18N
345: if ((columnWidthProperty != null)
346: && (columnWidthProperty.getValue() != null)) {
347: try {
348: int colWidth = Integer
349: .parseInt((String) columnWidthProperty
350: .getValue());
351: newTableWidth += colWidth;
352: //System.out.println("Width of column - " + i + " is " + colWidth);
353: } catch (Exception exc) {
354: ErrorManager.getDefault().notify(exc);
355: }
356: }
357: }
358:
359: if (newTableWidth > oldTableWidth) {
360: tableWidthProperty.setValue(String.valueOf(newTableWidth));
361: }
362: }
363: }
|