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-2006 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.form.j2ee;
042:
043: import java.lang.reflect.InvocationTargetException;
044: import java.util.ArrayList;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047: import org.netbeans.modules.form.CreationDescriptor;
048: import org.netbeans.modules.form.FormProperty;
049: import org.netbeans.modules.form.codestructure.CodeExpression;
050: import org.netbeans.modules.form.codestructure.CodeExpressionOrigin;
051:
052: /**
053: * Creator for query result list obtained from entity manager.
054: *
055: * @author Jan Stola
056: */
057: class QueryResultListCreator implements CreationDescriptor.Creator {
058: /** Parameter types. */
059: private Class[] paramTypes = new Class[] { String.class,
060: boolean.class, boolean.class };
061: /** Exception types. */
062: private Class[] exTypes = new Class[0];
063: /** Property names. */
064: private String[] propNames = new String[] { "query", "observable",
065: "modifiableWrapper" }; // NOI18N
066:
067: /**
068: * Returns number of parameters of the creator.
069: *
070: * @return number of parameters of the creator.
071: */
072: public int getParameterCount() {
073: return propNames.length;
074: }
075:
076: /**
077: * Returns parameter types of the creator.
078: *
079: * @return parameter types of the creator.
080: */
081: public Class[] getParameterTypes() {
082: return paramTypes;
083: }
084:
085: /**
086: * Returns exception types of the creator.
087: *
088: * @return exception types of the creator.
089: */
090: public Class[] getExceptionTypes() {
091: return exTypes;
092: }
093:
094: /**
095: * Returns property names of the creator.
096: *
097: * @return property names of the creator.
098: */
099: public String[] getPropertyNames() {
100: return propNames;
101: }
102:
103: /**
104: * Creates instance according to given properties.
105: *
106: * @param props properties describing the instance to create.
107: * @return instance that reflects values of the given properties.
108: */
109: public Object createInstance(FormProperty[] props)
110: throws InstantiationException, IllegalAccessException,
111: IllegalArgumentException, InvocationTargetException {
112: return new ArrayList(); // Hack
113: }
114:
115: /**
116: * Creates instance according to given parameter values.
117: *
118: * @param paramValues parameter values describing the instance to create.
119: * @return instance that reflects values of the given parameters.
120: */
121: public Object createInstance(Object[] paramValues)
122: throws InstantiationException, IllegalAccessException,
123: IllegalArgumentException, InvocationTargetException {
124: return new ArrayList(); // Hack
125: }
126:
127: /**
128: * Returns creation code according to given properties.
129: *
130: * @param props properties describing the instance whose creation code should be returned.
131: * @param expressionType type of the expression to create.
132: * @return creation code that reflects values of the given properties.
133: */
134: public String getJavaCreationCode(FormProperty[] props,
135: Class expressionType, String genericTypes) {
136: assert (props.length == propNames.length);
137:
138: String query = null;
139: Object observableValue = null;
140: Object modifiableValue = null;
141: for (int i = 0; i < props.length; i++) {
142: String propName = props[i].getName();
143: if (propNames[0].equals(propName)) {
144: query = props[i].getJavaInitializationString();
145: } else if (propNames[1].equals(propName)) {
146: try {
147: observableValue = props[i].getRealValue();
148: } catch (Exception ex) {
149: Logger.getLogger(getClass().getName()).log(
150: Level.INFO, ex.getMessage(), ex);
151: }
152: } else if (propNames[2].equals(propName)) {
153: try {
154: modifiableValue = props[i].getRealValue();
155: } catch (Exception ex) {
156: Logger.getLogger(getClass().getName()).log(
157: Level.INFO, ex.getMessage(), ex);
158: }
159: } else {
160: assert false;
161: }
162: }
163:
164: StringBuilder sb = new StringBuilder();
165: sb
166: .append("java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : "); // NOI18N
167: boolean observable = Boolean.TRUE.equals(observableValue);
168: if (observable) {
169: sb
170: .append("org.jdesktop.observablecollections.ObservableCollections.observableList("); // NOI18N
171: }
172: boolean modifiable = Boolean.TRUE.equals(modifiableValue);
173: if (modifiable) {
174: sb.append("new java.util.LinkedList("); // NOI18N
175: }
176: if ("null".equals("" + query)) { // NOI18N
177: sb.append("((javax.persistence.Query)null)"); // NOI18N
178: } else {
179: sb.append(query);
180: }
181: sb.append(".getResultList()"); // NOI18N
182: if (modifiable) {
183: sb.append(')');
184: }
185: if (observable) {
186: sb.append(')');
187: }
188: return sb.toString();
189: }
190:
191: public CodeExpressionOrigin getCodeOrigin(CodeExpression[] params) {
192: return null; // PENDING how is this used?
193: }
194:
195: }
|