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.faces.dt_1_2.component;
042:
043: import org.netbeans.modules.visualweb.faces.dt.HtmlDesignInfoBase;
044: import java.sql.ResultSet;
045: import java.sql.ResultSetMetaData;
046: import java.sql.SQLException;
047:
048: import javax.faces.application.Application;
049: import javax.faces.context.FacesContext;
050: import javax.faces.el.*;
051: import com.sun.rave.designtime.*;
052: import com.sun.rave.designtime.faces.*;
053: import org.netbeans.modules.visualweb.faces.dt.std.ColumnDataBindingCustomizerAction;
054: import javax.faces.component.UISelectItems;
055:
056: public class UISelectItemsDesignInfo implements DesignInfo {
057:
058: public Class getBeanClass() {
059: return UISelectItems.class;
060: }
061:
062: public DisplayAction[] getContextItems(DesignBean bean) {
063: return new DisplayAction[] { new ColumnDataBindingCustomizerAction(
064: bean),
065: // This action is now "hardcoded" into the designer. All
066: // FacesDesignBeans will get this action. This is done such
067: // that third party libraries pick it up too.
068: //new BindingsCustomizerAction(bean),
069: };
070: }
071:
072: public boolean acceptParent(DesignBean parentBean,
073: DesignBean childBean, Class childClass) {
074: return true;
075: }
076:
077: public boolean acceptChild(DesignBean parentBean,
078: DesignBean childBean, Class childClass) {
079: return true;
080: }
081:
082: public Result beanCreatedSetup(DesignBean bean) {
083: return null;
084: }
085:
086: public Result beanPastedSetup(DesignBean bean) {
087: return null;
088: }
089:
090: public Result beanDeletedCleanup(DesignBean bean) {
091: return null;
092: }
093:
094: public boolean acceptLink(DesignBean targetBean,
095: DesignBean sourceBean, Class sourceClass) {
096: return false;
097: }
098:
099: public Result linkBeans(DesignBean targetBean, DesignBean sourceBean) {
100: return null;
101: }
102:
103: public void beanContextActivated(DesignBean bean) {
104: }
105:
106: public void beanContextDeactivated(DesignBean bean) {
107: }
108:
109: public void instanceNameChanged(DesignBean bean,
110: String oldInstanceName) {
111: }
112:
113: public void beanChanged(DesignBean bean) {
114: }
115:
116: public void propertyChanged(final DesignProperty prop,
117: Object oldValue) {
118: if ("value".equals(prop.getPropertyDescriptor().getName())) { //NOI18N
119:
120: /* MBOHM 6194849
121: String vb = prop.getValueSource();
122: if (vb == null || "".equals(vb)) {
123: final DesignContext context = prop.getDesignBean().getDesignContext();
124: final String siBeanName = prop.getDesignBean().getInstanceName();
125: SwingUtilities.invokeLater(new Runnable() {
126: public void run() {
127: DesignBean siBean = context.getBeanByName(siBeanName);
128: if (siBean != null) {
129: DesignProperty valProp = siBean.getProperty("value"); //NOI18N
130: String valExpr = valProp.getValueSource();
131: if (valExpr == null || "".equals(valExpr)) {
132: valExpr = HtmlDesignInfoBase.maybeSetupDefaultSelectItems(siBean,
133: valExpr);
134: valProp.setValueSource(valExpr);
135: HtmlDesignInfoBase.maybeSetupConverter(siBean, 0);
136: }
137: }
138: }
139: });
140: }
141: */
142:
143: DesignBean siBean = prop.getDesignBean();
144:
145: String origValExpr = prop.getValueSource();
146:
147: //create default select items, blow away existing default select items, or leave alone existing default select items
148: String valExpr = HtmlDesignInfoBase
149: .maybeSetupDefaultSelectItems(siBean, origValExpr);
150:
151: //if we created new default select items, set this si's value property to point to those default select items
152: if (origValExpr == null || origValExpr.length() < 1) {
153: if (valExpr != null && valExpr.length() > 0) {
154: prop.setValueSource(valExpr); //this will cause the propertyChanged method to be called again
155: return;
156: }
157: }
158:
159: UISelectItemsDesignInfo.maybeSetupConverter(siBean);
160:
161: }
162: }
163:
164: public static void maybeSetupConverter(DesignBean siBean) {
165: Object instance = siBean.getInstance();
166: if (!(instance instanceof UISelectItems)) {
167: throw new IllegalArgumentException("siBean's instance was "
168: + instance + ", which is not of type UISelectItems");
169: }
170: DesignProperty prop = siBean.getProperty("value"); //NOI18N
171: String valExpr = prop.getValueSource();
172: if (valExpr == null) {
173: HtmlDesignInfoBase.maybeSetupConverter(siBean, 0);
174: return;
175: }
176: String beginJargon = ".selectItems['"; //NOI18N
177: String endJargon = "']"; //NOI18N
178: int beginJargonIndex = valExpr.indexOf(beginJargon);
179: if (beginJargonIndex >= 0) {
180: String resultSetExpr = valExpr.substring(0,
181: beginJargonIndex);
182: resultSetExpr += "}"; //NOI18N
183: DesignContext dcontext = siBean.getDesignContext();
184: if (dcontext instanceof FacesDesignContext) {
185: FacesDesignContext fdcontext = (FacesDesignContext) dcontext;
186: FacesContext fcontext = fdcontext.getFacesContext();
187: Application application = fcontext.getApplication();
188: ValueBinding binding = application
189: .createValueBinding(resultSetExpr);
190: Object value = null;
191: try {
192: value = binding.getValue(fcontext);
193: } catch (EvaluationException ee) {
194: //let value be null
195: }
196: if (value instanceof ResultSet) {
197: ResultSet rs = (ResultSet) value;
198: int endJargonIndex = valExpr.indexOf(endJargon,
199: beginJargonIndex);
200: if (endJargonIndex >= beginJargonIndex) {
201: String columnStr = valExpr
202: .substring(beginJargonIndex
203: + beginJargon.length(),
204: endJargonIndex);
205:
206: //could have internal commas, in say, selectItems['employee.employeeid, employee.firstname || \' , \' || employee.lastname']
207: boolean quoteOpen = false;
208: int realCommaIndex = -1;
209: String valueField = null;
210: for (int i = 0; i < columnStr.length(); i++) {
211: char c = columnStr.charAt(i);
212: if (c == '\'') {
213: quoteOpen = !quoteOpen;
214: } else if (c == ',' && !quoteOpen) {
215: realCommaIndex = i;
216: String field = columnStr
217: .substring(0, i);
218: if (field.length() > 0) {
219: valueField = field;
220: }
221: break;
222: }
223: }
224: //if no "real" comma was found
225: if (realCommaIndex == -1) {
226: if (columnStr.length() > 0) {
227: valueField = columnStr;
228: }
229: }
230:
231: if (valueField != null) {
232: String columnName = valueField;
233: int lastDotIndex = valueField
234: .lastIndexOf('.');
235: if (lastDotIndex >= 0
236: && lastDotIndex != valueField
237: .length() - 1) {
238: columnName = valueField
239: .substring(lastDotIndex + 1);
240: }
241: int sqlType = Integer.MIN_VALUE;
242: try {
243: ResultSetMetaData rsmd = rs
244: .getMetaData();
245: int cols = rsmd.getColumnCount();
246: for (int c = 1; c <= cols; c++) {
247: String aColumnName = rsmd
248: .getColumnName(c);
249: if (aColumnName.equals(columnName)) {
250: sqlType = rsmd.getColumnType(c);
251: break;
252: }
253:
254: }
255: } catch (SQLException sqle) {
256: ;
257: }
258: if (sqlType != Integer.MIN_VALUE) {
259: HtmlDesignInfoBase.maybeSetupConverter(
260: siBean, sqlType);
261: return;
262: }
263: }
264: }
265: }
266: }
267:
268: }
269: HtmlDesignInfoBase.maybeSetupConverter(siBean, 0);
270: }
271:
272: public void eventChanged(DesignEvent event) {
273: }
274: }
|