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.ui.view.graph;
042:
043: import java.awt.Dimension;
044: import java.awt.Point;
045:
046: import org.netbeans.modules.sql.framework.model.GUIInfo;
047: import org.netbeans.modules.sql.framework.model.SQLCanvasObject;
048: import org.netbeans.modules.sql.framework.model.SQLCaseOperator;
049: import org.netbeans.modules.sql.framework.model.SQLCastOperator;
050: import org.netbeans.modules.sql.framework.model.SQLConstants;
051: import org.netbeans.modules.sql.framework.model.SQLDBTable;
052: import org.netbeans.modules.sql.framework.model.SQLJoinView;
053: import org.netbeans.modules.sql.framework.model.SQLOperator;
054: import org.netbeans.modules.sql.framework.model.VisibleSQLLiteral;
055: import org.netbeans.modules.sql.framework.ui.graph.IGraphNode;
056: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
057: import org.netbeans.modules.sql.framework.ui.graph.impl.BasicCanvasArea;
058: import org.netbeans.modules.sql.framework.ui.graph.impl.OperatorXmlInfoModel;
059: import org.netbeans.modules.sql.framework.ui.view.join.JoinViewGraphNode;
060:
061: import com.nwoods.jgo.JGoObject;
062: import com.sun.sql.framework.exception.BaseException;
063:
064: /**
065: * @author Ritesh Adval
066: */
067: public class BasicGraphFactory extends AbstractGraphFactory {
068:
069: private Point sourceTableLoc = new Point(80, 50);
070: private Point targetTableLoc = new Point(400, 50);
071: private String operatorFolder;
072:
073: public BasicGraphFactory(String operatorFolder) {
074: this .operatorFolder = operatorFolder;
075: }
076:
077: /**
078: * Factory method for creating instance of IGraphNode given an SQLObject
079: *
080: * @param canvasObj sql object to be represented in the graph
081: * @return an instance of IGraphNode
082: */
083: public IGraphNode createGraphNode(SQLCanvasObject canvasObj)
084: throws BaseException {
085: int objectType = canvasObj.getObjectType();
086: GUIInfo gInfo = canvasObj.getGUIInfo();
087:
088: Point location = new Point(gInfo.getX(), gInfo.getY());
089: Dimension size = new Dimension(gInfo.getWidth(), gInfo
090: .getHeight());
091:
092: IGraphNode graphNode = null;
093: boolean setSavedSize = true;
094: SQLOperator operator = null;
095: String operatorType = null;
096: IOperatorXmlInfo opXmlInfo = null;
097:
098: // TODO Refactor! Break it down into protected methods that accept concrete
099: // instances of SQLCanvasObject.
100: if (canvasObj instanceof SQLOperator) {
101: operator = (SQLOperator) canvasObj;
102: operatorType = operator.getOperatorType();
103: opXmlInfo = OperatorXmlInfoModel
104: .getInstance(operatorFolder).findOperatorXmlInfo(
105: operatorType);
106:
107: if (canvasObj instanceof SQLCastOperator) {
108: graphNode = new SQLCastAsGraphNode(opXmlInfo);
109: } else {
110: if (operator.isCustomOperator()) {
111: graphNode = new CustomSQLOperatorGraphNode(operator
112: .getOperatorXmlInfo(), true, operator
113: .getCustomOperatorName());
114: } else {
115: graphNode = new SQLOperatorGraphNode(opXmlInfo,
116: opXmlInfo.isShowParenthesis());
117: }
118: }
119: setSavedSize = false;
120: } else if (canvasObj instanceof VisibleSQLLiteral) {
121: IOperatorXmlInfo literal = OperatorXmlInfoModel
122: .getInstance(operatorFolder).findOperatorXmlInfo(
123: "literal");
124: if (literal != null) {
125: SQLLiteralGraphNode literalObj = new SQLLiteralGraphNode(
126: literal);
127:
128: literalObj.setDataObject(canvasObj);
129: literalObj.setLocation(location);
130:
131: if (size.width == -1 && size.height == -1) {
132: size = new Dimension(literalObj.getMaximumWidth(),
133: literalObj.getMaximumHeight());
134: }
135: literalObj.setSize(size);
136: literalObj.setExpandedState(gInfo.isExpanded());
137:
138: return literalObj;
139: }
140: } else if (canvasObj instanceof SQLDBTable) {
141: TableGraphNodeFactory fac = new TableGraphNodeFactory();
142: graphNode = fac.createGraphNode(canvasObj);
143: if (size.width == -1 && size.height == -1) {
144: if (graphNode instanceof BasicCanvasArea) {
145: size = ((BasicCanvasArea) graphNode)
146: .getBoundingRect().getSize();
147: } else {
148: size = new Dimension(150, 150);
149: }
150: }
151:
152: //if tables are dropped first time using wizard their location will be
153: // -1, -1 so we need to set location for them
154: if (location.x == -1 && location.y == -1) {
155: if (objectType == SQLConstants.SOURCE_TABLE
156: || objectType == SQLConstants.RUNTIME_INPUT) {
157: location = sourceTableLoc;
158: } else {
159: location = targetTableLoc;
160: }
161: }
162: } else if (canvasObj instanceof SQLCaseOperator) {
163: SQLCaseArea caseArea = new SQLCaseArea();
164: graphNode = caseArea;
165: graphNode.setDataObject(canvasObj);
166:
167: if (size.width == -1 && size.height == -1) {
168: size = new Dimension(caseArea.getMaximumWidth() + 10,
169: caseArea.getMaximumHeight());
170: }
171:
172: caseArea.setLocation(location);
173: caseArea.setSize(size);
174:
175: graphNode.setExpandedState(gInfo.isExpanded());
176: return graphNode;
177: } else if (canvasObj instanceof SQLJoinView) {
178: SQLJoinView joinView = (SQLJoinView) canvasObj;
179: JoinViewGraphNode joinNode = new JoinViewGraphNode(joinView);
180: graphNode = joinNode;
181: }
182:
183: // If graph node is not null set canvas object as data object
184: // also set location and add this graph node
185: if (graphNode != null) {
186: graphNode.setDataObject(canvasObj);
187: JGoObject gNode = (JGoObject) graphNode;
188: gNode.setLocation(location);
189: if (setSavedSize && size.width != -1 && size.height != -1) {
190: gNode.setSize(size);
191: }
192: graphNode.setExpandedState(gInfo.isExpanded());
193: } else {
194: throw new BaseException(
195: "Failed to create GUI representation of "
196: + canvasObj.getDisplayName());
197: }
198:
199: return graphNode;
200: }
201: }
|