diff options
author | Mickael ADAM | 2015-10-05 13:02:51 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-12-10 16:07:38 +0000 |
commit | d63a234173147425b1cc8cbe4689e00bd28d6ad3 (patch) | |
tree | a102d89aea40e61623d87d68c380701b183355b5 /plugins/uml | |
parent | 46ff949a0e22eb38ef76cb6aa52c662db9f77f8d (diff) | |
download | org.eclipse.papyrus-d63a234173147425b1cc8cbe4689e00bd28d6ad3.tar.gz org.eclipse.papyrus-d63a234173147425b1cc8cbe4689e00bd28d6ad3.tar.xz org.eclipse.papyrus-d63a234173147425b1cc8cbe4689e00bd28d6ad3.zip |
Bug 479041 - NullPointerException in
CompletionProposalUtils.getQualifiedNameLabelWithSufficientDepth (135)
https://bugs.eclipse.org/bugs/show_bug.cgi?id=479041
Change-Id: Ic734e00a61dc5a845f458b6ac75c2656c617ffbe
Signed-off-by: Mickael ADAM <mickael.adam@ALL4TEC.net>
Diffstat (limited to 'plugins/uml')
-rw-r--r-- | plugins/uml/xtext/org.eclipse.papyrus.uml.xtext.integration.ui/src/org/eclipse/papyrus/uml/xtext/integration/CompletionProposalUtils.java | 346 |
1 files changed, 180 insertions, 166 deletions
diff --git a/plugins/uml/xtext/org.eclipse.papyrus.uml.xtext.integration.ui/src/org/eclipse/papyrus/uml/xtext/integration/CompletionProposalUtils.java b/plugins/uml/xtext/org.eclipse.papyrus.uml.xtext.integration.ui/src/org/eclipse/papyrus/uml/xtext/integration/CompletionProposalUtils.java index aa3d1e533a3..4936b3e0286 100644 --- a/plugins/uml/xtext/org.eclipse.papyrus.uml.xtext.integration.ui/src/org/eclipse/papyrus/uml/xtext/integration/CompletionProposalUtils.java +++ b/plugins/uml/xtext/org.eclipse.papyrus.uml.xtext.integration.ui/src/org/eclipse/papyrus/uml/xtext/integration/CompletionProposalUtils.java @@ -1,166 +1,180 @@ -/*****************************************************************************
- * Copyright (c) 2010 CEA LIST.
- *
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * CEA LIST - Initial API and implementation
- *
- *****************************************************************************/
-
-package org.eclipse.papyrus.uml.xtext.integration;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.papyrus.infra.core.utils.DisplayUtils;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.NamedElement;
-import org.eclipse.uml2.uml.Namespace;
-import org.eclipse.uml2.uml.Package;
-import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
-
-/**
- * @author CEA LIST - Initial contribution and API
- */
-@SuppressWarnings("nls")
-public class CompletionProposalUtils {
-
- protected final static ILabelProvider labelProvider = DisplayUtils.getLabelProvider();
-
- /**
- * Public Utility method for creating a completion proposal
- *
- * @param namedElement
- * The named element for which completion proposal must be created
- * @param completionString
- * The actual completion string
- * @param displayString
- * The way the completion is displayed in the completion list
- * @param context
- * Some information related to the context of the completion
- * @return completion proposal
- */
- public static CustomCompletionProposal createCompletionProposal(NamedElement namedElement,
- String completionString,
- String displayString,
- ContentAssistContext context) {
- String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')';
-
- CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, // String to be inserted
- context.getOffset(), // Offset
- context.getSelectedText().length(), // Replacement length
- completionString.length(), // cursorPosition
- labelProvider.getImage(namedElement), // image
- " " + displayString, // displayString
- null, // contextInformation
- additionalProposalInfo, // additionalProposalInfo
- context);
- return completionProposal;
- }
-
- /**
- * Public Utility method for creating a completion proposal with replacement of prefix
- *
- * @param namedElement
- * The named element for which completion proposal must be created
- * @param completionString
- * The actual completion string
- * @param displayString
- * The way the completion is displayed in the completion list
- * @param context
- * Some information related to the context of the completion
- * @return completion proposal
- */
- public static CustomCompletionProposal createCompletionProposalWithReplacementOfPrefix(NamedElement namedElement,
- String completionString,
- String displayString,
- ContentAssistContext context) {
- String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')';
-
- CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, // String to be inserted
- context.getOffset() - context.getPrefix().length(), // Offset
- context.getPrefix().length(), // Replacement length
- completionString.length(), // cursorPosition
- labelProvider.getImage(namedElement), // image
- " " + displayString, // displayString
- null, // contextInformation
- additionalProposalInfo, // additionalProposalInfo
- context);
- return completionProposal;
- }
-
- /**
- * Public Utility method for creating a completion proposal
- *
- * @param completionString
- * The actual completion string
- * @param displayString
- * The way the completion is displayed in the completion list
- * @param context
- * Some information related to the context of the completion
- * @return completion proposal
- */
- public static CustomCompletionProposal createCompletionProposal(String completionString,
- String displayString,
- ContentAssistContext context) {
-
- CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, // String to be inserted
- context.getOffset(), // Offset
- context.getSelectedText().length(), // Replacement length
- completionString.length(), // cursorPosition
- null, // image
- " " + displayString, // displayString
- null, // contextInformation
- null, // additionalProposalInfo
- context);
- return completionProposal;
- }
-
- /**
- * Public utility method that computes a qualified name, taking into account packages imported by the namespace model
- *
- * @param namedElement
- * @param model
- * @return the qualified name label
- */
- public static String getQualifiedNameLabelWithSufficientDepth(NamedElement namedElement, Namespace model) {
- String label = "";
-
- List<Package> importedPackages = new ArrayList<Package>(model.getImportedPackages());
-
- List<Namespace> visitedNamespaces = new ArrayList<Namespace>();
- Namespace currentNamespace = namedElement.getNamespace();
-
- boolean rootFound = false;
- boolean modelIsTheRoot = false;
-
- while (currentNamespace != null && !rootFound) {
- visitedNamespaces.add(currentNamespace);
- if (importedPackages.contains(currentNamespace) || currentNamespace == model) {
- rootFound = true;
- if (currentNamespace == model) {
- modelIsTheRoot = true;
- }
- }
- Element owner = currentNamespace.getOwner();
- while (owner != null && !(owner instanceof Namespace)) {
- owner = owner.getOwner();
- }
-
- currentNamespace = owner != null ? (Namespace) owner : null;
- }
-
- for (int i = visitedNamespaces.size() - 1 - (modelIsTheRoot ? 1 : 0); i >= 0; i--) {
- label += visitedNamespaces.get(i).getName() + "::";
- }
-
- return label + namedElement.getName();
- }
-
-}
+/***************************************************************************** + * Copyright (c) 2010 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * CEA LIST - Initial API and implementation + * Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - bug 479041 + * + *****************************************************************************/ + +package org.eclipse.papyrus.uml.xtext.integration; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.papyrus.infra.core.utils.DisplayUtils; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.Namespace; +import org.eclipse.uml2.uml.Package; +import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext; + +/** + * @author CEA LIST - Initial contribution and API + */ +@SuppressWarnings("nls") +public class CompletionProposalUtils { + + protected final static ILabelProvider labelProvider = DisplayUtils.getLabelProvider(); + + /** + * Public Utility method for creating a completion proposal + * + * @param namedElement + * The named element for which completion proposal must be created + * @param completionString + * The actual completion string + * @param displayString + * The way the completion is displayed in the completion list + * @param context + * Some information related to the context of the completion + * @return completion proposal + */ + public static CustomCompletionProposal createCompletionProposal(NamedElement namedElement, + String completionString, + String displayString, + ContentAssistContext context) { + String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')'; + + CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, // String to be inserted + context.getOffset(), // Offset + context.getSelectedText().length(), // Replacement length + completionString.length(), // cursorPosition + labelProvider.getImage(namedElement), // image + " " + displayString, // displayString + null, // contextInformation + additionalProposalInfo, // additionalProposalInfo + context); + return completionProposal; + } + + /** + * Public Utility method for creating a completion proposal with replacement of prefix + * + * @param namedElement + * The named element for which completion proposal must be created + * @param completionString + * The actual completion string + * @param displayString + * The way the completion is displayed in the completion list + * @param context + * Some information related to the context of the completion + * @return completion proposal + */ + public static CustomCompletionProposal createCompletionProposalWithReplacementOfPrefix(NamedElement namedElement, + String completionString, + String displayString, + ContentAssistContext context) { + String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')'; + + CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, // String to be inserted + context.getOffset() - context.getPrefix().length(), // Offset + context.getPrefix().length(), // Replacement length + completionString.length(), // cursorPosition + labelProvider.getImage(namedElement), // image + " " + displayString, // displayString + null, // contextInformation + additionalProposalInfo, // additionalProposalInfo + context); + return completionProposal; + } + + /** + * Public Utility method for creating a completion proposal + * + * @param completionString + * The actual completion string + * @param displayString + * The way the completion is displayed in the completion list + * @param context + * Some information related to the context of the completion + * @return completion proposal + */ + public static CustomCompletionProposal createCompletionProposal(String completionString, + String displayString, + ContentAssistContext context) { + + CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, // String to be inserted + context.getOffset(), // Offset + context.getSelectedText().length(), // Replacement length + completionString.length(), // cursorPosition + null, // image + " " + displayString, // displayString + null, // contextInformation + null, // additionalProposalInfo + context); + return completionProposal; + } + + /** + * Public utility method that computes a qualified name, taking into account packages imported by the namespace model + * + * @param namedElement + * @param model + * @return the qualified name label + */ + public static String getQualifiedNameLabelWithSufficientDepth(final NamedElement namedElement, final Namespace model) { + Assert.isNotNull(namedElement); + + StringBuffer label = new StringBuffer(); + + List<Package> importedPackages = null; + if (null != model) { + importedPackages = new ArrayList<Package>(model.getImportedPackages()); + } + + List<Namespace> visitedNamespaces = new ArrayList<Namespace>(); + Namespace currentNamespace = namedElement.getNamespace(); + + boolean rootFound = false; + boolean modelIsTheRoot = false; + + while (currentNamespace != null && !rootFound) { + visitedNamespaces.add(currentNamespace); + + if (null != importedPackages && (importedPackages.contains(currentNamespace) || currentNamespace.equals(model))) { + rootFound = true; + if (currentNamespace.equals(model)) { + modelIsTheRoot = true; + } + } + + Element owner = currentNamespace.getOwner(); + while (null != owner && !(owner instanceof Namespace)) { + owner = owner.getOwner(); + } + + currentNamespace = owner != null ? (Namespace) owner : null; + } + + int qualifiedNameDepht = visitedNamespaces.size() - 1 - (modelIsTheRoot ? 1 : 0); + + for (int i = qualifiedNameDepht; i >= 0; i--) { + label.append(visitedNamespaces.get(i).getName()); + label.append("::");//$NON-NLS-1$ + } + + label.append(namedElement.getName()); + + return label.toString(); + } + +} |