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:
042: package org.netbeans.modules.uml.ui.products.ad.diagramengines.diagramComponentEngine;
043:
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import org.netbeans.modules.uml.common.ETException;
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
049: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
050: import org.netbeans.modules.uml.core.metamodel.core.foundation.IReference;
051: import org.netbeans.modules.uml.core.metamodel.diagrams.IGraphEventKind;
052: import org.netbeans.modules.uml.core.support.umlsupport.ETSize;
053: import org.netbeans.modules.uml.core.support.umlsupport.IETRect;
054: import org.netbeans.modules.uml.core.support.umlsupport.IETSize;
055: import org.netbeans.modules.uml.core.support.umlutils.ETList;
056: import org.netbeans.modules.uml.ui.products.ad.ADDrawEngines.ADNodeDrawEngine;
057: import org.netbeans.modules.uml.ui.products.ad.drawEngineManagers.IADInterfaceEventManager;
058: import org.netbeans.modules.uml.ui.products.ad.drawengines.ETComponentDrawEngine;
059: import org.netbeans.modules.uml.ui.products.ad.drawengines.IComponentDrawEngine;
060: import org.netbeans.modules.uml.ui.products.ad.drawengines.IPortDrawEngine;
061: import org.netbeans.modules.uml.ui.support.PresentationReferenceHelper;
062: import org.netbeans.modules.uml.ui.support.applicationmanager.INodePresentation;
063: import org.netbeans.modules.uml.ui.support.umltsconversions.RectConversions;
064: import org.netbeans.modules.uml.ui.support.viewfactorysupport.ICompartment;
065: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine;
066: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo;
067: import org.netbeans.modules.uml.ui.support.viewfactorysupport.IEventManager;
068: import org.netbeans.modules.uml.ui.support.viewfactorysupport.TypeConversions;
069: import com.tomsawyer.editor.TSENode;
070: import com.tomsawyer.editor.graphics.TSEGraphics; //import com.tomsawyer.jnilayout.TSSide;
071: import org.netbeans.modules.uml.ui.support.TSSide; //import com.tomsawyer.util.TSConstPoint;
072: import com.tomsawyer.drawing.geometry.TSConstPoint; //import com.tomsawyer.util.TSConstRect;
073: import com.tomsawyer.drawing.geometry.TSConstRect; //import com.tomsawyer.util.TSPoint;
074: import com.tomsawyer.drawing.geometry.TSPoint; //import com.tomsawyer.util.TSRect;
075: import com.tomsawyer.drawing.geometry.TSRect; //import com.tomsawyer.util.TSTransform;
076: import com.tomsawyer.editor.TSTransform;
077:
078: /**
079: * @author josephg
080: *
081: */
082: public class PortDrawEngine extends ADNodeDrawEngine implements
083: IPortDrawEngine {
084: /* (non-Javadoc)
085: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine#getDrawEngineID()
086: */
087: public String getDrawEngineID() {
088: return "PortDrawEngine";
089: }
090:
091: /* (non-Javadoc)
092: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine#createCompartments()
093: */
094: public void createCompartments() throws ETException {
095: clearCompartments();
096:
097: createAndAddCompartment("BoxCompartment", 0);
098: }
099:
100: public boolean isDrawEngineValidForModelElement() {
101: boolean result = false;
102:
103: if (getMetaTypeOfElement().equals("Port")) {
104: result = true;
105: }
106:
107: return result;
108: }
109:
110: public final static int NODE_WIDTH = 10;
111: public final static int NODE_HEIGHT = 10;
112:
113: /* (non-Javadoc)
114: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine#sizeToContents()
115: */
116: public void sizeToContents() {
117: // Originally the last parameter was set to true. However, this did not work when
118: // the diagram was zoomed in and the user created an interface. The port was too big.
119: sizeToContentsWithMin(NODE_WIDTH, NODE_HEIGHT, false, false);
120: }
121:
122: /* (non-Javadoc)
123: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine#onGraphEvent(int)
124: */
125: public void onGraphEvent(int nKind) {
126: switch (nKind) {
127: case IGraphEventKind.GEK_POST_MOVE:
128: case IGraphEventKind.GEK_POST_RESIZE:
129: case IGraphEventKind.GEK_POST_CREATE:
130: IComponentDrawEngine componentDE = getComponentDrawEngine();
131: if (componentDE != null) {
132: componentDE.restoreAllPortPositions();
133: }
134: break;
135: case IGraphEventKind.GEK_POST_COPY:
136: case IGraphEventKind.GEK_POST_PASTE_ALL:
137: reestablishPresentationReference();
138: break;
139: }
140: }
141:
142: public ETComponentDrawEngine getComponentDrawEngine() {
143: IPresentationElement this PE = getPresentationElement();
144:
145: if (this PE != null) {
146: ETList<IReference> references = this PE
147: .getReferredReferences();
148:
149: Iterator iter = references.iterator();
150: while (iter.hasNext()) {
151: IReference reference = (IReference) iter.next();
152:
153: if (reference != null) {
154: IElement tempElement = reference
155: .getReferencingElement();
156:
157: if (tempElement instanceof IPresentationElement) {
158: IPresentationElement componentPE = (IPresentationElement) tempElement;
159:
160: IDrawEngine drawEngine = TypeConversions
161: .getDrawEngine(componentPE);
162: if (drawEngine != null) {
163: if (drawEngine instanceof ETComponentDrawEngine)
164: return (ETComponentDrawEngine) drawEngine;
165: }
166: }
167: }
168: }
169: }
170:
171: return null;
172: }
173:
174: /**
175: * @return TSSide value indicating which side this port is located on for its parant component
176: */
177: public int getComponentSide() {
178: int side = TSSide.TS_SIDE_UNDEFINED;
179:
180: ETComponentDrawEngine engine = getComponentDrawEngine();
181: if (engine != null) {
182: TSENode portNode = getOwnerNode();
183: TSENode componentNode = TypeConversions
184: .getOwnerNode(engine);
185: if ((portNode != null) && (componentNode != null)) {
186: TSConstRect componentRect = componentNode.getBounds();
187: TSConstPoint portCenter = portNode.getCenter();
188:
189: side = RectConversions.getClosestSide(componentRect,
190: portCenter);
191: }
192: }
193:
194: return side;
195: }
196:
197: public void distributeAttachedInterfaces(boolean redraw) {
198: IEventManager eventManager = getEventManager();
199: if (eventManager instanceof IADInterfaceEventManager) {
200: IADInterfaceEventManager interfaceEventManager = (IADInterfaceEventManager) eventManager;
201: interfaceEventManager.distributeAttachedInterfaces(redraw);
202: }
203: }
204:
205: public String getManagerMetaType(int nManagerKind) {
206: String type = null;
207:
208: if (nManagerKind == MK_EVENTMANAGER) {
209: type = "ADInterfaceEventManager";
210: }
211:
212: return type;
213: }
214:
215: public void reestablishPresentationReference() {
216: INodePresentation nodePresentation = getNodePresentation();
217: IElement portElement = getFirstModelElement();
218:
219: if (nodePresentation == null || portElement == null)
220: return;
221:
222: ETList<IPresentationElement> qualifierParents = PresentationReferenceHelper
223: .getAllReferencingElements(nodePresentation);
224:
225: if (qualifierParents != null && qualifierParents.size() > 0)
226: return;
227:
228: IElement component = portElement.getOwner();
229:
230: if (component == null)
231: return;
232:
233: IPresentationElement peToAttachTo = nodePresentation
234: .findNearbyElement(true, component,
235: "ComponentDrawEngine");
236:
237: if (peToAttachTo == null)
238: return;
239:
240: PresentationReferenceHelper.createPresentationReference(
241: peToAttachTo, nodePresentation);
242: }
243:
244: /* (non-Javadoc)
245: * @see org.netbeans.modules.uml.ui.products.ad.drawengines.ETNodeDrawEngine#drawContents(org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo)
246: */
247: protected void drawContents(IDrawInfo pDrawInfo) {
248: TSEGraphics graphics = pDrawInfo.getTSEGraphics();
249: TSTransform transform = graphics.getTSTransform();
250: IETRect deviceBounds = pDrawInfo.getDeviceBounds();
251:
252: List compartments = getCompartments();
253:
254: Iterator iter = compartments.iterator();
255:
256: while (iter.hasNext()) {
257: ICompartment compartment = (ICompartment) iter.next();
258:
259: compartment.draw(pDrawInfo, deviceBounds);
260: }
261: }
262:
263: /* (non-Javadoc)
264: * @see org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawEngine#calculateOptimumSize(org.netbeans.modules.uml.ui.support.viewfactorysupport.IDrawInfo, boolean)
265: */
266: public IETSize calculateOptimumSize(IDrawInfo pDrawInfo,
267: boolean bAt100Pct) {
268: IETSize retVal = new ETSize(NODE_WIDTH, NODE_HEIGHT);
269:
270: if (!bAt100Pct && (retVal != null)) {
271: TSTransform transform = pDrawInfo != null ? pDrawInfo
272: .getTSTransform() : getTransform();
273: retVal = scaleSize(retVal, transform);
274: }
275:
276: return retVal;
277: }
278:
279: public void initCompartments(IPresentationElement pElement) {
280: int numComps = getNumCompartments();
281: if (numComps == 0) {
282: try {
283: createCompartments();
284: numComps = getNumCompartments();
285: } catch (Exception e) {
286: e.printStackTrace();
287: }
288: }
289: }
290:
291: public void movePortsToSide(int nSide,
292: ETList<IPresentationElement> selectedPorts) {
293: ETComponentDrawEngine compDrawingEngine = this
294: .getComponentDrawEngine();
295: if (compDrawingEngine != null) {
296: if (selectedPorts != null) {
297: compDrawingEngine.movePortsToSide(nSide, selectedPorts);
298: }
299: }
300: }
301: }
|