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;
042:
043: import com.sun.data.provider.FieldKey;
044: import com.sun.data.provider.TableDataProvider;
045: import com.sun.data.provider.impl.CachedRowSetDataProvider;
046: import com.sun.rave.designtime.DesignBean;
047: import com.sun.rave.designtime.DesignProperty;
048: import com.sun.rave.designtime.DisplayAction;
049: import com.sun.rave.designtime.DisplayActionSet;
050: import com.sun.rave.designtime.Result;
051: import com.sun.rave.designtime.faces.FacesDesignContext;
052: import com.sun.rave.designtime.markup.MarkupDesignBean;
053: import com.sun.rave.designtime.markup.MarkupTableDesignInfo;
054: import com.sun.rave.web.ui.component.StaticText;
055: import com.sun.rave.web.ui.component.Table;
056: import com.sun.rave.web.ui.component.TableRowGroup;
057: import java.awt.Image;
058: import org.netbeans.modules.visualweb.web.ui.dt.AbstractDesignInfo;
059: import org.netbeans.modules.visualweb.web.ui.dt.component.customizers.TableBindToDataAction;
060: import org.netbeans.modules.visualweb.web.ui.dt.component.customizers.TableCustomizerAction;
061: import org.netbeans.modules.visualweb.web.ui.dt.component.table.TableDesignHelper;
062: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
063: import org.netbeans.modules.visualweb.web.ui.dt.component.table.TableDesignState;
064: import com.sun.rave.web.ui.model.DefaultTableDataProvider;
065: import java.io.StringWriter;
066: import javax.swing.ImageIcon;
067: import org.w3c.dom.Element;
068:
069: /**
070: * DesignInfo for the <code>Table</code> component. The following behavior is
071: * implemented:
072: * <ul>
073: * <li>Upon component creation, pre-populate with one Table Row group.</li>
074: * </ul>
075: *
076: * @author Winston Prakash
077: */
078: public class TableDesignInfo extends AbstractDesignInfo implements
079: MarkupTableDesignInfo {
080:
081: //private static final String TITLE_FACET = "title"; //NOI18N
082: //private static final String TITLE_FACET_TEXT_PROPERTY = "text"; //NOI18N
083: private static final String SOURCE_DATA_PROPERTY = "sourceData"; //NOI18N
084: private static final String TITLE_PROPERTY = "title"; //NOI18N
085: private static final String AUGMENT_TITLE_PROPERTY = "augmentTitle"; //NOI18N
086: private static final String WIDTH_PROPERTY = "width"; //NOI18N
087:
088: public TableDesignInfo() {
089: super (Table.class);
090: }
091:
092: public DisplayActionSet getContextItemsExt(final DesignBean bean) {
093:
094: return new DisplayActionSet() {
095:
096: public DisplayAction[] getDisplayActions() {
097: return new DisplayAction[] {
098: new TableCustomizerAction(bean),
099: new TableBindToDataAction(bean) };
100: }
101:
102: public boolean isPopup() {
103: return true;
104: }
105:
106: public boolean isEnabled() {
107: return true;
108: }
109:
110: public Result invoke() {
111: throw new UnsupportedOperationException(
112: "Not supported yet."); //NOI18N
113: }
114:
115: public String getDisplayName() {
116: return "";
117: }
118:
119: public String getDescription() {
120: return "";
121: }
122:
123: public Image getLargeIcon() {
124: return new ImageIcon(getClass().getResource(
125: AbstractDesignInfo.DECORATION_ICON)).getImage();
126: }
127:
128: public Image getSmallIcon() {
129: return new ImageIcon(getClass().getResource(
130: AbstractDesignInfo.DECORATION_ICON)).getImage();
131: }
132:
133: public String getHelpKey() {
134: throw new UnsupportedOperationException(
135: "Not supported yet."); //NOI18N
136: }
137:
138: };
139: }
140:
141: /** {@inheritDoc} */
142: public DisplayAction[] getContextItems(DesignBean bean) {
143: return new DisplayAction[] { new TableCustomizerAction(bean),
144: new TableBindToDataAction(bean) };
145: }
146:
147: /**
148: * {@inheritDoc}
149: * Accept only TableRowGroup as Child
150: */
151: public boolean acceptChild(DesignBean parentBean,
152: DesignBean childBean, Class childClass) {
153: return childClass.isAssignableFrom(TableRowGroup.class)
154: || childClass.isAssignableFrom(StaticText.class);
155: }
156:
157: /**
158: * Create TableDesignState with the table bean and a TableRowGroup and save it as source bean and
159: * then save the state of TableDesignState which in turn would fill the TableRowRroup with default
160: * data.
161: *
162: * {@inheritDoc}
163: */
164: public Result beanCreatedSetup(DesignBean tableBean) {
165: FacesDesignContext fcontext = (FacesDesignContext) tableBean
166: .getDesignContext();
167: String tableTitle = DesignMessageUtil.getMessage(
168: TableDesignInfo.class, "table.title");
169: DesignProperty titleProperty = tableBean
170: .getProperty(TITLE_PROPERTY); //NOI18N
171: titleProperty.setValue(tableTitle);
172: // By default do not augment the title with row information
173: DesignProperty augmentTitleProperty = tableBean
174: .getProperty(AUGMENT_TITLE_PROPERTY); //NOI18N
175: augmentTitleProperty.setValue(new Boolean(false));
176:
177: DesignBean tableGroupBean = fcontext.createBean(
178: TableRowGroup.class.getName(), tableBean, null);
179: TableDesignState ts = new TableDesignState(tableBean);
180: // Create the Default Table Model. It will be used by TableRowGroup to populate itself
181: ts.setDataProviderBean(TableDesignHelper
182: .createDefaultDataProvider(tableBean));
183: ts.saveState();
184: return Result.SUCCESS;
185: }
186:
187: /**
188: * Find if the row group of the pasted table is bound to default model.
189: * If the model doesn't exist recreate it.
190: * @param bean The bean that has been pasted
191: */
192: public Result beanPastedSetup(DesignBean tableBean) {
193: for (int i = 0; i < tableBean.getChildBeanCount(); i++) {
194: DesignBean tableRowGroupBean = tableBean.getChildBean(i);
195: if (tableRowGroupBean.getInstance() instanceof TableRowGroup) {
196: DesignProperty designProperty = tableRowGroupBean
197: .getProperty(SOURCE_DATA_PROPERTY);
198: if (designProperty != null) {
199: String sourceDataStr = designProperty
200: .getValueSource();
201: if (sourceDataStr != null) {
202: if (sourceDataStr
203: .indexOf(TableDesignHelper.DEFAULT_TABLE_DATA_PROVIDER) != -1) {
204: DesignBean dpBean = TableDesignHelper
205: .createDefaultDataProvider(tableBean);
206: FacesDesignContext fcontext = (FacesDesignContext) tableBean
207: .getDesignContext();
208: String modelBindingExpr = fcontext
209: .getBindingExpr(dpBean);
210: designProperty
211: .setValueSource(modelBindingExpr);
212: }
213: }
214: }
215: }
216: }
217: return Result.SUCCESS;
218: }
219:
220: /**
221: * Accept only Reult Set (may be not required in future) or TableDataProvider as links
222: *
223: * {@inheritDoc}
224: */
225: public boolean acceptLink(DesignBean targetBean,
226: DesignBean sourceBean, Class sourceClass) {
227: if (TableDataProvider.class.isAssignableFrom(sourceClass)) {
228: return true;
229: }
230: return false;
231: }
232:
233: /**
234: * If the child bean is a TableDataProvider create TableDesignState with the target bean
235: * and set TableDataProvider as source bean and then save its state which in turn would create
236: * corresponding TableRowRroup and fill it with data from the TableDataProvider.
237: *
238: * {@inheritDoc}
239: */
240: public Result linkBeans(DesignBean targetBean, DesignBean sourceBean) {
241: if (sourceBean.getInstance() instanceof TableDataProvider) {
242: // Bug 6333281 - After cancelling rowset previous table binding lost
243: // After dropping a CachedRowsetDataProvider, a dialog pops up if
244: // already a CachedRowset exists in the session bean. If the user
245: // Cancels this dialog, then the rowset is not set and the data provider
246: // is deleted.
247: if (sourceBean.getInstance() instanceof CachedRowSetDataProvider) {
248: CachedRowSetDataProvider cachedRowSetDataProvider = (CachedRowSetDataProvider) sourceBean
249: .getInstance();
250: if (cachedRowSetDataProvider.getCachedRowSet() == null) {
251: return Result.FAILURE;
252: }
253: }
254:
255: TableDataProvider tdp = (TableDataProvider) sourceBean
256: .getInstance();
257: FieldKey[] columns = tdp.getFieldKeys();
258: if ((columns == null) || (columns.length == 0)) {
259: return Result.FAILURE;
260: }
261:
262: TableDesignState ts = new TableDesignState(targetBean);
263: ts.loadState();
264: ts.setDataProviderBean(sourceBean);
265: ts.saveState();
266: TableDesignHelper.deleteDefaultDataProvider(targetBean);
267: }
268: return Result.SUCCESS;
269: }
270:
271: /** {@inheritDoc} */
272: public Result beanDeletedCleanup(DesignBean bean) {
273: TableDesignHelper.deleteDefaultDataProvider(bean);
274: return Result.SUCCESS;
275: }
276:
277: // ------ Implements MarkupTableDesignInfo ------------------------
278:
279: public int testResizeRow(MarkupDesignBean mdBean, int row,
280: int column, int height) {
281: return -1;
282: }
283:
284: public Result resizeRow(MarkupDesignBean mdBean, int row, int height) {
285: return Result.SUCCESS;
286: }
287:
288: public Result clearRowSize(MarkupDesignBean mdBean, int row) {
289: return Result.SUCCESS;
290: }
291:
292: public int testResizeColumn(MarkupDesignBean mdBean, int row,
293: int column, int width) {
294: return width;
295: }
296:
297: public Result resizeColumn(MarkupDesignBean tableBean, int colNo,
298: int columnWidth) {
299: //System.out.println("\n\nAdjusting Width of column - " + colNo);
300: if (tableBean.getInstance() instanceof Table) {
301: DesignBean tableColumnBean = TableDesignHelper
302: .getTableColumnBean(TableDesignHelper
303: .getTableRowGroupBean(tableBean), colNo);
304: DesignProperty designProperty = tableColumnBean
305: .getProperty("width");
306: int oldColumnWidth = -1;
307: try {
308: oldColumnWidth = Integer
309: .parseInt((String) designProperty.getValue());
310: } catch (Exception exc) {
311: }
312: designProperty.setValue(String.valueOf(columnWidth));
313: }
314: return Result.SUCCESS;
315: }
316:
317: public Result clearColumnSize(MarkupDesignBean mdBean, int column) {
318: return Result.SUCCESS;
319: }
320:
321: }
|