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.sql.framework.model.impl;
042:
043: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
044: import org.netbeans.modules.sql.framework.model.GUIInfo;
045: import org.netbeans.modules.sql.framework.model.SQLCanvasObject;
046: import org.netbeans.modules.sql.framework.model.SQLConstants;
047: import org.netbeans.modules.sql.framework.model.SQLInputObject;
048: import org.netbeans.modules.sql.framework.model.SQLObject;
049: import org.netbeans.modules.sql.framework.model.VisibleSQLPredicate;
050: import org.w3c.dom.Element;
051: import org.w3c.dom.NodeList;
052:
053: import com.sun.sql.framework.exception.BaseException;
054:
055: /**
056: * Extends SQLPredicate to represent boolean conditional expressions that exist as UI
057: * elements on the SQLBuilder collaboration definition canvas.
058: *
059: * @author Jonathan Giron
060: * @version $Revision$
061: */
062:
063: public class VisibleSQLPredicateImpl extends SQLPredicateImpl implements
064: VisibleSQLPredicate {
065:
066: /**
067: * Extends VisibleSQLPredicateImpl class to handle predicates with single inputs.
068: *
069: * @author Jonathan Giron
070: * @version $Revision$
071: */
072: public static class LeftUnary extends VisibleSQLPredicateImpl {
073:
074: /** Creates a new instance of VisibleSQLPredicateImpl.LeftUnary */
075: public LeftUnary() {
076: super ();
077: }
078:
079: /** Creates a clone using the passed LeftUnary object */
080: public LeftUnary(LeftUnary obj) throws BaseException {
081: super (obj);
082: }
083:
084: /**
085: * Overrides parent implementation to silently discard inputs other than argName =
086: * LEFT.
087: *
088: * @param argName name of argument to add
089: * @param newInput SQLObject to associate with argName as an input
090: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#addInput(java.lang.String,
091: * org.netbeans.modules.sql.framework.model.SQLObject)
092: */
093: public void addInput(String argName, SQLObject newInput)
094: throws BaseException {
095: if (LEFT.equals(argName)) {
096: super .addInput(argName, newInput);
097: }
098: }
099:
100: /**
101: * Clone this object.
102: */
103: public Object clone() throws CloneNotSupportedException {
104: try {
105: LeftUnary unaryPredicate = new LeftUnary(this );
106: return unaryPredicate;
107: } catch (BaseException ex) {
108: throw new CloneNotSupportedException(
109: "can not create clone of "
110: + this .getOperatorType());
111: }
112: }
113:
114: /**
115: * Overrides parent implementation to silently ignore requests to remove inputs
116: * other than those associated with argName = LEFT.
117: *
118: * @param argName name of argument to remove
119: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#removeInputByArgName(java.lang.String,
120: * org.netbeans.modules.sql.framework.model.SQLObject)
121: */
122: public SQLObject removeInputByArgName(String argName,
123: SQLObject sqlObj) throws BaseException {
124: if (LEFT.equals(argName)) {
125: return super .removeInputByArgName(argName, sqlObj);
126: }
127: return null;
128: }
129:
130: /**
131: * Overrides parent implementation to remove RIGHT input once it has been
132: * resolved.
133: *
134: * @see org.netbeans.modules.sql.framework.model.SQLObject#secondPassParse(org.w3c.dom.Element)
135: */
136: public void secondPassParse(Element element)
137: throws BaseException {
138: super .secondPassParse(element);
139:
140: // Unconditionally remove RIGHT input object.
141: inputMap.remove(RIGHT);
142: }
143:
144: /**
145: * Overrides default implementation to remove RIGHT input.
146: *
147: * @see org.netbeans.modules.sql.framework.model.impl.VisibleSQLPredicateImpl#postProcessInputList()
148: */
149: protected void postProcessInputList() {
150: super .postProcessInputList();
151:
152: // Only remove RIGHT input object if it has been resolved. Otherwise, defer
153: // until secondPassParse is called.
154: SQLInputObject rightInput = getInput(RIGHT);
155: if (rightInput != null && rightInput.getSQLObject() != null) {
156: inputMap.remove(RIGHT);
157: }
158: }
159: }
160:
161: /**
162: * Extends VisibleSQLPredicateImpl class to handle predicates with single inputs.
163: */
164: public static class RightUnary extends VisibleSQLPredicateImpl {
165:
166: /** Creates a new instance of VisibleSQLPredicateImpl.LeftUnary */
167: public RightUnary() {
168: super ();
169: }
170:
171: /** Creates a clone using the passed LeftUnary object */
172: public RightUnary(RightUnary obj) throws BaseException {
173: super (obj);
174: }
175:
176: /**
177: * Overrides parent implementation to silently discard inputs other than argName =
178: * RIGHT.
179: *
180: * @param argName name of argument to add
181: * @param newInput SQLObject to associate with argName as an input
182: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#addInput(java.lang.String,
183: * org.netbeans.modules.sql.framework.model.SQLObject)
184: */
185: public void addInput(String argName, SQLObject newInput)
186: throws BaseException {
187: if (RIGHT.equals(argName)) {
188: super .addInput(argName, newInput);
189: }
190: }
191:
192: /**
193: * Clone this object.
194: */
195: public Object clone() throws CloneNotSupportedException {
196: try {
197: RightUnary unaryPredicate = new RightUnary(this );
198: return unaryPredicate;
199: } catch (BaseException ex) {
200: throw new CloneNotSupportedException(
201: "can not create clone of "
202: + this .getOperatorType());
203: }
204: }
205:
206: /**
207: * Overrides parent implementation to silently ignore requests to remove inputs
208: * other than those associated with argName = RIGHT.
209: *
210: * @param argName name of argument to remove
211: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#removeInputByArgName(java.lang.String,
212: * org.netbeans.modules.sql.framework.model.SQLObject)
213: */
214: public SQLObject removeInputByArgName(String argName,
215: SQLObject sqlObj) throws BaseException {
216: if (RIGHT.equals(argName)) {
217: return super .removeInputByArgName(argName, sqlObj);
218: }
219: return null;
220: }
221:
222: /**
223: * Overrides parent implementation to remove LEFT input once it has been resolved.
224: *
225: * @see org.netbeans.modules.sql.framework.model.SQLObject#secondPassParse(org.w3c.dom.Element)
226: */
227: public void secondPassParse(Element element)
228: throws BaseException {
229: super .secondPassParse(element);
230:
231: // Unconditionally remove LEFT input object.
232: inputMap.remove(LEFT);
233: }
234:
235: /**
236: * Overrides default implementation to remove LEFT input.
237: *
238: * @see org.netbeans.modules.sql.framework.model.impl.VisibleSQLPredicateImpl#postProcessInputList()
239: */
240: protected void postProcessInputList() {
241: super .postProcessInputList();
242:
243: // Only remove LEFT input object if it has been resolved. Otherwise, defer
244: // until secondPassParse is called.
245: SQLInputObject rightInput = getInput(LEFT);
246: if (rightInput != null && rightInput.getSQLObject() != null) {
247: inputMap.remove(LEFT);
248: }
249: }
250: }
251:
252: /* GUI state info */
253: protected GUIInfo guiInfo = new GUIInfo();
254:
255: /** Creates a new instance of SQLPredicate */
256: public VisibleSQLPredicateImpl() {
257: super ();
258: type = SQLConstants.VISIBLE_PREDICATE;
259: }
260:
261: public VisibleSQLPredicateImpl(VisibleSQLPredicate src)
262: throws BaseException {
263: this ();
264: if (src == null) {
265: throw new IllegalArgumentException(
266: "Cannot create VisibleSQLPredicate using copy constructor: src is null");
267: }
268:
269: super .copyFrom(src);
270:
271: // copy GUI info
272: GUIInfo gInfo = src.getGUIInfo();
273: this .guiInfo = gInfo != null ? (GUIInfo) gInfo.clone() : null;
274: }
275:
276: public Object clone() throws CloneNotSupportedException {
277: try {
278: VisibleSQLPredicateImpl predicate = new VisibleSQLPredicateImpl(
279: this );
280: return predicate;
281: } catch (BaseException ex) {
282: throw new CloneNotSupportedException(
283: "can not create clone of " + this .getOperatorType());
284: }
285: }
286:
287: /**
288: * @see java.lang.Object#equals
289: */
290: public boolean equals(Object refObj) {
291: if (!(refObj instanceof VisibleSQLPredicate)) {
292: return false;
293: }
294:
295: return super .equals(refObj);
296: }
297:
298: /**
299: * @see SQLCanvasObject#getGUIInfo
300: */
301: public GUIInfo getGUIInfo() {
302: return guiInfo;
303: }
304:
305: /**
306: * @see java.lang.Object#hashCode
307: */
308: public int hashCode() {
309: int myHash = super .hashCode();
310:
311: myHash += (guiInfo != null) ? guiInfo.hashCode() : 0;
312:
313: return myHash;
314: }
315:
316: /**
317: * Parses the given SQLPredicate Element
318: *
319: * @param xmlElement Element to be parsed
320: * @throws BaseException if error occurs while parsing
321: */
322: public void parseXML(Element xmlElement) throws BaseException {
323: super .parseCommonFields(xmlElement);
324:
325: NodeList inputArgList = xmlElement
326: .getElementsByTagName(SQLObject.TAG_INPUT);
327: TagParserUtility.parseInputTagList(this , inputArgList);
328: postProcessInputList();
329:
330: NodeList guiInfoList = xmlElement
331: .getElementsByTagName(GUIInfo.TAG_GUIINFO);
332: if (guiInfoList != null && guiInfoList.getLength() != 0) {
333: Element elem = (Element) guiInfoList.item(0);
334: guiInfo = new GUIInfo(elem);
335: }
336: }
337:
338: /**
339: * Overrides parent implementation to append GUIInfo information.
340: *
341: * @param prefix String to append to each new line of the XML representation
342: * @return XML representation of this SQLObject instance
343: */
344: public String toXMLString(String prefix) throws BaseException {
345: StringBuilder buffer = new StringBuilder(500);
346: if (prefix == null) {
347: prefix = "";
348: }
349:
350: buffer.append(prefix).append(getHeader());
351: buffer.append(toXMLAttributeTags(prefix));
352: buffer.append(TagParserUtility.toXMLInputTag(prefix + "\t",
353: this .inputMap));
354: buffer.append(this .guiInfo.toXMLString(prefix + "\t"));
355: buffer.append(prefix).append(getFooter());
356:
357: return buffer.toString();
358: }
359:
360: /**
361: * Hook to allow subclasses to modify SQLInputObjects arguments after parsing them
362: * into the predicate. Default implementation is empty.
363: */
364: protected void postProcessInputList() {
365: // Do nothing.
366: }
367: }
|