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.core.metamodel.infrastructure.coreinfrastructure;
043:
044: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
045: import org.netbeans.modules.uml.core.eventframework.IEventDispatchController;
046: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
047:
048: /**
049: * @author sumitabhk
050: *
051: */
052: public class OperationSignatureChangeContextManager implements
053: IOperationSignatureChangeContextManager {
054: private IOperation m_Operation = null;
055: private String m_ContextName = null;
056:
057: /**
058: *
059: */
060: public OperationSignatureChangeContextManager() {
061: super ();
062: }
063:
064: /**
065: *
066: * Gets the operation whose signature change is encapsulated by a context managed by this manager.
067: *
068: * @param pVal[out] The operation.
069: *
070: * @return HRESULT
071: *
072: */
073: public IOperation getOperation() {
074: return m_Operation;
075: }
076:
077: /**
078: *
079: * Constructs a signature change context, setting the operation of the context,
080: * and pushes that context onto the event dispatch controller. If this manager
081: * is already responsible for a context, the current one is popped and a new
082: * one is pushed. To change the signature of more than one operation without
083: * popping a context, create a new manager.
084: *
085: * @param newVal[in] The operation.
086: *
087: * @return HRESULT
088: *
089: */
090: public void startSignatureChange(IOperation newVal) {
091: // Before we start a new context, do we have an old one to pop?
092: endSignatureChange();
093:
094: IEventDispatchController pController = getController();
095: if (newVal != null && pController != null) {
096: // Push a new context
097: IOperationSignatureChangeContext pContext = new OperationSignatureChangeContext();
098: String name = buildContextName(newVal);
099:
100: // Setup the context
101: pContext.setName(name);
102: pContext.setOperation(newVal);
103:
104: // Now push the final context
105: pController.pushEventContext3(pContext);
106: m_Operation = newVal;
107: }
108: }
109:
110: /**
111: *
112: * Pops the context from the event dispatch controller.
113: * This function can be called to force the context to be popped before the
114: * manager destructs.
115: *
116: *
117: * @return HRESULT
118: *
119: */
120: public void endSignatureChange() {
121: if (m_Operation != null) {
122: IEventDispatchController pController = getController();
123: if (pController != null) {
124: // Pop the context
125: pController.removeEventContextByName(m_ContextName);
126: }
127: }
128: m_Operation = null;
129: }
130:
131: /**
132: *
133: * Get the dispatch controller on which the context will be pushed and popped.
134: *
135: * @param pController[out] The dispatch controller
136: *
137: * @return HRESULT
138: *
139: */
140: private IEventDispatchController getController() {
141: IEventDispatchController retObj = null;
142: ICoreProduct pProd = ProductRetriever.retrieveProduct();
143: if (pProd != null) {
144: retObj = pProd.getEventDispatchController();
145: }
146: return retObj;
147: }
148:
149: /**
150: *
151: * Each context pushed must have a name. We want this name to be unique so that
152: * the correct context is popped from the controller. The terms push and pop are
153: * not really accurate for contexts, because they might not be popped in a stack
154: * like fashion.
155: *
156: * @param pOperation[in] The operation
157: * @param sName[out] The context name
158: *
159: * @return HRESULT
160: *
161: */
162: private String buildContextName(IOperation pOper) {
163: String retName = null;
164: if (pOper != null) {
165: m_ContextName = "";
166:
167: // We must use a truly unique value. This means the XMI id of the op.
168: String xmiid = pOper.getXMIID();
169:
170: // Now, we just add the "reason" of the context.
171: m_ContextName = "Signature change : ";
172: m_ContextName += xmiid;
173: retName = m_ContextName;
174: }
175: return retName;
176: }
177:
178: /**
179: *
180: * The destructor ensures that any current context is popped.
181: *
182: */
183: protected void finalize() {
184: try {
185: //This is a deviation from C++, but we need to make sure that this endSignatureChanged is called
186: //when we are done with this object. It is used in PropertyEditor, EditControl's Translator
187: // and JavaMethodChangeHandler only.
188: // endSignatureChange();
189: super .finalize();
190: } catch (Throwable e) {
191: e.printStackTrace();
192: }
193: }
194: }
|