Java Code Examples for org.eclipse.emf.ecore.util.EcoreUtil#getURI()

The following examples show how to use org.eclipse.emf.ecore.util.EcoreUtil#getURI() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public IRenameElementContext createLocalRenameElementContext(EObject targetElement, XtextEditor editor,
		ITextSelection selection, XtextResource resource) {
	EObject declarationTarget = getDeclarationTarget(targetElement);
	if (declarationTarget instanceof XtendFunction && ((XtendFunction) declarationTarget).isDispatch()) {
		IProject project = projectUtil.getProject(declarationTarget.eResource().getURI());
		ResourceSet resourceSet = resourceSetProvider.get(project);
		XtendFunction relaodedDispatchFunction = (XtendFunction) resourceSet.getEObject(
				EcoreUtil2.getPlatformResourceOrNormalizedURI(declarationTarget), true);
		Iterable<JvmOperation> allDispatchOperations = dispatchRenameSupport
				.getAllDispatchOperations(relaodedDispatchFunction);
		Map<URI, IJavaElement> jvm2javaElement = newLinkedHashMap();
		for (JvmOperation jvmOperation : allDispatchOperations) {
			IJavaElement javaElement = getJavaElementFinder().findExactElementFor(jvmOperation);
			if (javaElement != null) {
				URI jvmOperationURI = EcoreUtil.getURI(jvmOperation);
				jvm2javaElement.put(jvmOperationURI, javaElement);
			}
		}
		if (!jvm2javaElement.isEmpty()) {
			return new DispatchMethodRenameContext(relaodedDispatchFunction, jvm2javaElement, editor, selection,
					resource);
		}
	}
	return super.createLocalRenameElementContext(targetElement, editor, selection, resource);
}
 
Example 2
@Override
public List<URI> getDependentElementURIs(EObject baseElement, IProgressMonitor monitor) {
	SubMonitor progress = SubMonitor.convert(monitor, 2);
	List<URI> elementURIs = Lists.newArrayList();
	int counter = 0;
	for (Iterator<EObject> iterator = EcoreUtil.getAllProperContents(baseElement, false); iterator.hasNext();) {
		if(progress.isCanceled()) {
			throw new OperationCanceledException();
		}
		EObject childElement = iterator.next();
		if (nameProvider.getFullyQualifiedName(childElement)!=null) {
			URI childURI = EcoreUtil.getURI(childElement);
			if (childURI != null) {
				elementURIs.add(childURI);
			}
		}
		counter++;
		if (counter % MONITOR_CHUNK_SIZE == 0) {
			progress.worked(1);
			progress.setWorkRemaining(2);
		}
	}
	return elementURIs;
}
 
Example 3
/**
* @generated
*/
private static IEditorInput getEditorInput(Diagram diagram) {
	Resource diagramResource = diagram.eResource();
	for (EObject nextEObject : diagramResource.getContents()) {
		if (nextEObject == diagram) {
			return new FileEditorInput(WorkspaceSynchronizer.getFile(diagramResource));
		}
		if (nextEObject instanceof Diagram) {
			break;
		}
	}
	URI uri = EcoreUtil.getURI(diagram);
	String editorName = uri.lastSegment() + '#' + diagram.eResource().getContents().indexOf(diagram);
	IEditorInput editorInput = new URIEditorInput(uri, editorName);
	return editorInput;
}
 
Example 4
private static IEditorInput getEditorInput(Diagram diagram) {
	Resource diagramResource = diagram.eResource();
	for (Iterator<EObject> it = diagramResource.getContents().iterator(); it
			.hasNext();) {
		EObject nextEObject = (EObject) it.next();
		if (nextEObject == diagram) {
			return new FileEditorInput(
					WorkspaceSynchronizer.getFile(diagramResource));
		}
		if (nextEObject instanceof Diagram) {
			break;
		}
	}
	URI uri = EcoreUtil.getURI(diagram);
	String editorName = uri.lastSegment() + "#"
			+ diagram.eResource().getContents().indexOf(diagram);
	IEditorInput editorInput = new URIEditorInput(uri, editorName);
	return editorInput;
}
 
Example 5
/**
 * Gets the catalog qualified name. Instead of using the qualified name provider, which is based
 * on the object attribute values, this method uses the resource properties for determining the
 * fully qualified name. This is legitimate, because the check catalog FQN consists of the Java
 * package name concatenated with the file name (which must be equal to the model name).
 *
 * @param catalog
 *          the catalog
 * @return the catalog qualified name
 */
public String getCatalogQualifiedName(final CheckCatalog catalog) {
  final URI uri = EcoreUtil.getURI(catalog);
  IFile file = RuntimeProjectUtil.findFileStorage(uri, mapper);
  IJavaProject project = JavaCore.create(file.getProject());
  try {
    IPackageFragment packageFragment = project.findPackageFragment(file.getParent().getFullPath());
    final String fileNameWithoutExtension = file.getName().substring(0, file.getName().length() - (file.getFileExtension().length() + 1));
    return packageFragment.getElementName() + '.' + fileNameWithoutExtension;
  } catch (JavaModelException e) {
    LOGGER.error("Could not determine plugin path for catalog " + catalog.getName(), e);
  }
  return null;
}
 
Example 6
Source Project: n4js   File: N4JSRenameStrategy.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Custom logics for applying declaration changes to constituent members of composed members
 */
@Override
public void applyDeclarationChange(String newName, ResourceSet resourceSet) {
	EObject targetElement = resourceSet.getEObject(getTargetElementOriginalURI(), true);
	if (TypeModelUtils.isComposedTElement(targetElement)) {
		targetElementNewURIs.clear();
		List<TMember> constituentMembers = ((TMember) targetElement).getConstituentMembers();
		for (TMember constituentMember : constituentMembers) {
			URI constituentMemberURI = EcoreUtil.getURI(constituentMember);
			EObject renamedElement = setName(constituentMemberURI, newName, resourceSet);
			targetElementNewURIs.add(EcoreUtil.getURI(renamedElement));
		}
	}
	super.applyDeclarationChange(getNameAsValue(newName), resourceSet);
}
 
Example 7
Source Project: dsl-devkit   File: EObjectUtil.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a location string for an EObject suitable to include in debugging error messages.
 *
 * @param obj
 *          object
 * @return location string including URI of object
 */
public static String getLocationString(final EObject obj) {
  if (obj == null) {
    return null;
  }
  ICompositeNode node = NodeModelUtils.findActualNodeFor(obj);
  URI uri = EcoreUtil.getURI(obj);
  if (node != null) {
    return uri.trimFragment() + ":" + node.getStartLine() + " (" + obj + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  }
  return uri.toString() + " (" + obj + ")"; //$NON-NLS-1$ //$NON-NLS-2$
}
 
Example 8
/**
 * Gets the catalog plugin path.
 *
 * @param catalog
 *          the catalog
 * @return the catalog plugin path
 */
public String getCatalogPluginPath(final CheckCatalog catalog) {
  final URI uri = EcoreUtil.getURI(catalog);
  IFile file = RuntimeProjectUtil.findFileStorage(uri, mapper);
  IJavaProject project = JavaCore.create(file.getProject());
  try {
    IPackageFragment packageFragment = project.findPackageFragment(file.getParent().getFullPath());
    String result = packageFragment.getElementName().replace('.', '/');
    return result + '/' + file.getName();
  } catch (JavaModelException e) {
    LOGGER.error("Could not determine plugin path for catalog " + catalog.getName(), e);
  }
  return null;
}
 
Example 9
Source Project: xtext-core   File: ParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testUriFormat2() throws Exception {
	URI uri = EcoreUtil.getURI(AsubpackagePackage.Literals.ATYPE);
	assertTrue(AsubpackagePackage.eNS_URI.equals(uri.trimFragment().toString()));
	ResourceSet set = new ResourceSetImpl();
	EObject eObject = set.getEObject(uri, true);
	assertSame(AsubpackagePackage.Literals.ATYPE, eObject);
}
 
Example 10
Source Project: xtext-core   File: EcoreUtil2.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the eobject's URI in the normalized form or as is if it is a platform:/resource URI.
 * @since 2.4
 */
public static URI getPlatformResourceOrNormalizedURI(EObject eObject) {
	URI rawURI = EcoreUtil.getURI(eObject);
	if (rawURI.isPlatformResource()) {
		return rawURI;
	}
	Resource resource = eObject.eResource();
	if(resource != null && resource.getResourceSet() != null) {
		return resource.getResourceSet().getURIConverter().normalize(rawURI);
	} else {
		return URIConverter.INSTANCE.normalize(rawURI);
	}
}
 
Example 11
Source Project: xtext-core   File: EcoreUtil2.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public static URI getNormalizedURI(EObject eObject) {
	URI rawURI = EcoreUtil.getURI(eObject);
	Resource resource = eObject.eResource();
	if(resource != null && resource.getResourceSet() != null) {
		return resource.getResourceSet().getURIConverter().normalize(rawURI);
	} else {
		return URIConverter.INSTANCE.normalize(rawURI);
	}
}
 
Example 12
protected AbstractNode(EObject eObject, String text, Font font, Region textRegion) {
	if (eObject != null)
		grammarElementURI = EcoreUtil.getURI(eObject);
	setLayoutManager(new ToolbarLayout());
	setBackgroundColor(getUnselectedBackgroundColor());
	label = new Label(text);
	add(label);
	setBorder(createBorder());
	setFont(font);
	this.textRegion = textRegion;
}
 
Example 13
public static MainProcessEditPart getMainProcessEditPart(final Diagram diag) {

        /*
         * need to get the URI after save because the name can change as it is
         * synchronized with the MainProcess name
         */
        final URI uri = EcoreUtil.getURI(diag);

        /* open the process editor */
        final DiagramEditor processEditor = (DiagramEditor) EditorService.getInstance()
                .openEditor(new URIEditorInput(uri, diag.getName()));
        return (MainProcessEditPart) processEditor.getDiagramEditPart();
    }
 
Example 14
protected void handleLink(List<?> fragments) {
	if (fragments == null || fragments.isEmpty()) {
		return;
	}
	URI elementURI = null;
	String firstFragment = fragments.get(0).toString();
	int hashIndex = firstFragment.indexOf("#");
	if (hashIndex != -1) {
		JvmDeclaredType resolvedDeclarator = getResolvedDeclarator(firstFragment.substring(0, hashIndex));
		if (resolvedDeclarator != null && !resolvedDeclarator.eIsProxy()) {
			String signature = firstFragment.substring(hashIndex + 1);
			int indexOfOpenBracket = signature.indexOf("(");
			int indexOfClosingBracket = signature.indexOf(")");
			String simpleNameOfFeature = indexOfOpenBracket != -1 ? signature.substring(0, indexOfOpenBracket)
					: signature;
			Iterable<JvmConstructor> constructorCandidates = new ArrayList<JvmConstructor>();
			if (resolvedDeclarator.getSimpleName().equals(simpleNameOfFeature)) {
				constructorCandidates = resolvedDeclarator.getDeclaredConstructors();
			}
			Iterable<JvmFeature> possibleCandidates = Iterables.concat(
				resolvedDeclarator.findAllFeaturesByName(simpleNameOfFeature),
				constructorCandidates
			);
			List<String> parameterNames = null;
			if (indexOfOpenBracket != -1 && indexOfClosingBracket != -1) {
				parameterNames = Strings.split(signature.substring(indexOfOpenBracket + 1, indexOfClosingBracket),
						",");
			}
			Iterator<JvmFeature> featureIterator = possibleCandidates.iterator();
			while (elementURI == null && featureIterator.hasNext()) {
				JvmFeature feature = featureIterator.next();
				boolean valid = false;
				if (feature instanceof JvmField) {
					valid = true;
				} else if (feature instanceof JvmExecutable) {
					JvmExecutable executable = (JvmExecutable) feature;
					EList<JvmFormalParameter> parameters = executable.getParameters();
					if (parameterNames == null) {
						valid = true;
					} else if (parameters.size() == parameterNames.size()) {
						valid = true;
						for (int i = 0; (i < parameterNames.size() && valid); i++) {
							URI parameterTypeURI = EcoreUtil.getURI(parameters.get(i).getParameterType().getType());
							IEObjectDescription expectedParameter = scopeProvider.getScope(context,
									new HoverReference(TypesPackage.Literals.JVM_TYPE)).getSingleElement(
									qualifiedNameConverter.toQualifiedName(parameterNames.get(i)));
							if (expectedParameter == null
									|| !expectedParameter.getEObjectURI().equals(parameterTypeURI)) {
								valid = false;
							}
						}
					}
				}
				if (valid)
					elementURI = EcoreUtil.getURI(feature);
			}
		}
	} else {
		IScope scope = scopeProvider.getScope(context, new HoverReference(TypesPackage.Literals.JVM_TYPE));
		IEObjectDescription singleElement = scope.getSingleElement(qualifiedNameConverter
				.toQualifiedName(firstFragment));
		if (singleElement != null)
			elementURI = singleElement.getEObjectURI();
	}
	String label = "";
	if (fragments.size() > 1) {
		for (int i = 1; i < fragments.size(); i++) {
			String portentialLabel = fragments.get(i).toString();
			if (portentialLabel.trim().length() > 0)
				label += portentialLabel;
		}
	}
	if (label.length() == 0)
		label = firstFragment;
	if (elementURI == null)
		buffer.append(label);
	else {
		buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, elementURI, label));
	}
}
 
Example 15
public static void saveState(IMemento memento, DiagramEditorInput input) {
	URI uri = EcoreUtil.getURI(input.getDiagram());
	String fileString = uri.toString();
	memento.putString(EMF_URI, fileString);
}
 
Example 16
protected boolean isJavaElement(EObject element) {
	URI uri = EcoreUtil.getURI(element);
	return "java".equals(uri.scheme());
}
 
Example 17
Source Project: gama   File: Symbol.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public URI getURI() {
	if (description == null) { return null; }
	final EObject object = description.getUnderlyingElement();
	return object == null ? null : EcoreUtil.getURI(object);
}
 
Example 18
@Override
public URI getUriToProblem() {
	return EcoreUtil.getURI(problematicObject);
}
 
Example 19
Source Project: xtext-core   File: HoverContext.java    License: Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String toString() {
	return "HoverContext [document=" + document + ", resource=" + (resource == null ? "null" : resource.getURI())
			+ ", offset=" + offset + ", region=" + region + ", element="
			+ (element == null ? "null" : EcoreUtil.getURI(element)) + "]";
}
 
Example 20
/**
 * A {@link BackgroundOutlineTreeProvider} must use
 * {@link #EObjectNode(EObject, IOutlineNode, ImageDescriptor, Object, boolean)} instead.
 */
public EObjectNode(EObject eObject, IOutlineNode parent, Image image, Object text, boolean isLeaf) {
	super(parent, image, text, isLeaf);
	this.eObjectURI = EcoreUtil.getURI(eObject);
	this.eClass = eObject.eClass();
}