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: /*
043: * Created on Jun 6, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.swing.propertyeditor;
047:
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
049:
050: import org.netbeans.modules.uml.core.metamodel.core.foundation.INamedElement;
051: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
052: import org.netbeans.modules.uml.core.metamodel.core.foundation.NameCollisionHandler;
053: import org.netbeans.modules.uml.core.support.umlsupport.IResultCell;
054: import org.netbeans.modules.uml.core.support.umlutils.ETList;
055: import org.netbeans.modules.uml.core.support.umlutils.IPropertyElement;
056: import org.netbeans.modules.uml.ui.support.applicationmanager.IProductGraphPresentation;
057: import org.netbeans.modules.uml.ui.swing.drawingarea.DiagramEngine;
058: import org.openide.DialogDisplayer;
059: import org.openide.NotifyDescriptor;
060: import org.openide.nodes.Node;
061: import org.openide.util.NbBundle;
062: import org.openide.windows.TopComponent;
063:
064: /**
065: * @author sumitabhk
066: *
067: */
068: public class PropertyEditorCollisionHandler extends
069: NameCollisionHandler implements IPropertyEditorCollisionHandler {
070: private IPropertyEditor m_rawPropertyEditorControl = null;
071: private boolean handled = false;
072: private IPropertyElement pPropertyElement;
073:
074: /**
075: *
076: */
077: public PropertyEditorCollisionHandler() {
078: super ();
079: }
080:
081: /* (non-Javadoc)
082: * @see org.netbeans.modules.uml.ui.swing.propertyeditor.IPropertyEditorCollisionHandler#getPropertyEditor()
083: */
084: public IPropertyEditor getPropertyEditor() {
085: return m_rawPropertyEditorControl;
086: }
087:
088: /* (non-Javadoc)
089: * @see org.netbeans.modules.uml.ui.swing.propertyeditor.IPropertyEditorCollisionHandler#setPropertyEditor(org.netbeans.modules.uml.ui.swing.propertyeditor.IPropertyEditor)
090: */
091: public void setPropertyEditor(IPropertyEditor value) {
092: m_rawPropertyEditorControl = value;
093: }
094:
095: public void setPropertyElement(IPropertyElement element) {
096: pPropertyElement = element;
097: }
098:
099: public IPropertyElement getPropertyElement() {
100: return pPropertyElement;
101: }
102:
103: /**
104: * Notification that a name collision event is about to happen
105: */
106: public long onPreNameCollision(INamedElement pElement,
107: String sProposedName,
108: ETList<INamedElement> pCollidingElements, IResultCell pCell) {
109: if (!handled) {
110: // Get the first colliding element
111: INamedElement pFirstCollidingElement = null;
112: if (pCollidingElements != null) {
113: int count = pCollidingElements.getCount();
114: if (count > 0) {
115: pFirstCollidingElement = pCollidingElements.get(0);
116: }
117: }
118:
119: questionUserAboutNameCollision(pElement, sProposedName,
120: pFirstCollidingElement, pCell);
121:
122: handled = true;
123: }
124: return 0;
125: }
126:
127: /**
128: * Notification that a name collision event occurred
129: */
130: public long onNameCollision(INamedElement pElement,
131: ETList<INamedElement> pCollidingElements, IResultCell pCell) {
132: // Get the first colliding element
133: INamedElement pFirstCollidingElement = null;
134: if (pCollidingElements != null) {
135: int count = pCollidingElements.getCount();
136: if (count > 0) {
137: pFirstCollidingElement = pCollidingElements.get(0);
138: }
139: }
140:
141: // if(pFirstCollidingElement != null)
142: // {
143: // if (m_rawPropertyEditorControl != null)
144: // {
145: // m_rawPropertyEditorControl.loadElement(pFirstCollidingElement);
146: // }
147: // }
148:
149: loadElement(pFirstCollidingElement);
150:
151: return 0;
152: }
153:
154: /**
155: * Notification that the parent listener was disabled
156: */
157: public long listenerDisabled() {
158: return 0;
159: }
160:
161: /**
162: * Reattaches the presentation element to a new model element
163: */
164: protected void reattachPresentationElement(INamedElement pElement,
165: INamedElement pFirstCollidingElement) {
166: if (pElement != null && pFirstCollidingElement != null) {
167: if (getPropertyElement() != null) {
168: Object pDispElement = getPropertyElement().getElement();
169: if (pDispElement instanceof IPresentationElement) {
170: IPresentationElement pPE = (IPresentationElement) pDispElement;
171: boolean bIsSubject = pPE.isSubject(pElement);
172: if (bIsSubject) {
173: // This presentation element should be reattached.
174: if (pPE instanceof IProductGraphPresentation) {
175: IProductGraphPresentation pGraphPresentation = (IProductGraphPresentation) pPE;
176: // Reconnect the presentation element
177: pGraphPresentation
178: .reconnectPresentationElement(pFirstCollidingElement);
179: }
180: }
181: }
182: }
183: }
184: }
185:
186: public long onPreAliasNameModified(INamedElement element,
187: String proposedName, IResultCell cell) {
188: /*
189: CComPtr< INamedElement > cpNamedElement( element );
190: const UserAliasChoice choice = HandlePreAliasNameModified( &(cpNamedElement.p), proposedName );
191:
192: switch( choice )
193: {
194: case UAC_CANCEL:
195: _VH( cell->put_Continue( VARIANT_FALSE ));
196: break;
197:
198: case UAC_NAMED_ELEMENT:
199: // do nothing, the element's name changed
200: break;
201:
202: case UAC_CHANGED_ELEMENT:
203: {
204: CComPtr < IPropertyElements > pPropertyElements;
205: long count = 0;
206: if (m_rawPropertyEditorControl)
207: {
208: _VH(m_rawPropertyEditorControl->get_PropertyElements(&pPropertyElements));
209: if (pPropertyElements)
210: {
211: _VH(pPropertyElements->get_Count(&count));
212: }
213:
214: for (long i = 0 ; i < count ; i++)
215: {
216: CComPtr < IPropertyElement > pThisPropertyElement;
217: _VH(pPropertyElements->Item(CComVariant(i), &pThisPropertyElement));
218: if (pThisPropertyElement)
219: {
220: CComPtr < IDispatch > pDispElement;
221: VARIANT_BOOL bIsSubject = VARIANT_FALSE;
222: _VH(pThisPropertyElement->get_Element(&pDispElement));
223:
224: CComQIPtr < IProductGraphPresentation > pThisPE(pDispElement);
225: if (pThisPE)
226: {
227: _VH(pThisPE->IsSubject(element, &bIsSubject));
228:
229: if (bIsSubject)
230: {
231: // Need to reattach this presentation element to the new element
232: _VH(pThisPE->ReconnectPresentationElement(cpNamedElement));
233: }
234: }
235: }
236: }
237: }
238:
239: // Delete the old element
240: _VH( element->Delete() );
241: }
242: break;
243:
244: default:
245: break;
246: }
247: */
248: return 0;
249: }
250:
251: /**
252: * Ask the user what to do about a name collision
253: *
254: * @param pElement [in] The element being renamed
255: * @param sProposedName [in] The new name
256: * @param pFirstCollidingElement [in] The first colliding element
257: * @param pCell [in] The result cell. Used to cancel the rename.
258: */
259: public long questionUserAboutNameCollision(INamedElement pElement,
260: String sProposedName, INamedElement pFirstCollidingElement,
261: IResultCell pCell) {
262: if (pFirstCollidingElement != null && pElement != null
263: && pCell != null) {
264:
265: DialogDisplayer.getDefault().notify(
266: new NotifyDescriptor.Message(NbBundle.getMessage(
267: DiagramEngine.class,
268: "IDS_NAMESPACECOLLISION")));
269: // Cancel the editing to abort the name collision
270: pCell.setContinue(false);
271: // refresh the property to revert back to the original value
272: Node[] activatedNodes = TopComponent.getRegistry()
273: .getActivatedNodes();
274: if (activatedNodes != null) {
275: int arrayLength = activatedNodes.length;
276: for (int i = 0; i < arrayLength; i++) {
277: // The setDisplayName causes the property sets to be updated.
278: Node myNode = activatedNodes[i];
279: myNode.setDisplayName(myNode.getDisplayName());
280: }
281: }
282:
283: // // Ask the user if he wants to reconnect the presentation element to a different model element
284: // IQuestionDialog pDiag = new SwingQuestionDialogImpl();
285: // if ( pDiag != null )
286: // {
287: // String title = PropertyEditorResources.getString("PropertyEditor.NameCollisionTitle");
288: // String msg = PropertyEditorResources.getString("PropertyEditor.NameCollision");
289: // QuestionResponse result = pDiag.displaySimpleQuestionDialog(SimpleQuestionDialogKind.SQDK_YESNOCANCEL, MessageIconKindEnum.EDIK_ICONWARNING, msg, 0, null, title);
290: // if (result.getResult() == SimpleQuestionDialogResultKind.SQDRK_RESULT_YES)
291: // {
292: // // TODO: User wants to allow the name collision.
293: // reattachPresentationElement(pElement, pFirstCollidingElement);
294: // }
295: // else if (result.getResult() == SimpleQuestionDialogResultKind.SQDRK_RESULT_CANCEL)
296: // {
297: // // Cancel the editing to abort the name collision
298: // pCell.setContinue(false);
299: // // refresh the property to revert back to the original value
300: // Node[] activatedNodes = TopComponent.getRegistry().getActivatedNodes();
301: // if (activatedNodes != null)
302: // {
303: // int arrayLength = activatedNodes.length;
304: // for (int i = 0; i < arrayLength; i++)
305: // {
306: // // The setDisplayName causes the property sets to be updated.
307: // Node myNode = activatedNodes[i];
308: // myNode.setDisplayName(myNode.getDisplayName());
309: // }
310: // }
311: // }
312: // else if (result.getResult() == SimpleQuestionDialogResultKind.SQDRK_RESULT_NO)
313: // {
314: // // go ahead with the same name
315: // }
316: // }
317: }
318: return 0;
319: }
320:
321: /**
322: * Load the passed in IElement into the property editor
323: *
324: * @param pElement[in] The element to put into the editor
325: *
326: * @return HRESULT
327: */
328: public long loadElement(IElement pElement) {
329: // if (PropertyEditorBlocker.inProcess() == false /*&& m_Model != null*/)
330: // {
331: // processLastCell(true);
332: // clear();
333: // //
334: // // the property editor is loaded by creating property definitions(which
335: // // represent the structure of the data) and property elements(which represent
336: // // the actual data)
337: // //
338: // String kind = ""; //$NON-NLS-1$
339: // if (pElement instanceof IDiagram)
340: // {
341: // kind = "Diagram"; //$NON-NLS-1$
342: // }
343: //
344: // Vector<IPropertyElement> propElems = new Vector<IPropertyElement>();
345: // Vector<IPropertyDefinition> propDefs = new Vector<IPropertyDefinition>();
346: // IPropertyElement pEle = processSelectedItem(kind, propDefs, pElement);
347: // if (pEle != null)
348: // {
349: // // since we are coming from the drawing area, we will also want the
350: // // capability of showing the presentation information, so store the
351: // // presentation element on the property element
352: // pEle.setElement(pElement);
353: // propElems.add(pEle);
354: // }
355: // setPropertyDefinitions(propDefs);
356: // setPropertyElements(propElems);
357: // m_CurLoadedObject = pElement;
358: // }
359: // TODO Auto-generated method stub
360: return 0;
361: }
362: }
|