Java Code Examples for org.eclipse.xtext.ui.editor.model.IXtextDocument#getAdapter()

The following examples show how to use org.eclipse.xtext.ui.editor.model.IXtextDocument#getAdapter() . 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 check out the related API usage on the sidebar.
Example 1
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T> T getAdapter(Class<T> adapter) {
	if (IContentOutlinePage.class.isAssignableFrom(adapter)) {
		return adapter.cast(getContentOutlinePage());
	}
	if (ValidationJob.class.equals(adapter)) {
		IXtextDocument document = getDocument();
		return document.getAdapter(adapter);
	}
	return super.getAdapter(adapter);
}
 
Example 2
Source File: ResourceNameTemplateVariableResolver.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<String> resolveValues(final TemplateVariable variable, final XtextTemplateContext templateContext) {
  final List<String> result = Lists.newArrayList();
  final IDocument document = templateContext.getDocument();
  final Object obj = variable.getVariableType().getParams().iterator().next();
  if (obj instanceof String) {
    final String variableName = (String) obj;
    final IXtextDocument xtextDocument = (IXtextDocument) document;
    final IFile file = xtextDocument.getAdapter(IFile.class);

    switch (variableName) {
    case "package": //$NON-NLS-1$
      if (document instanceof IXtextDocument && file != null && file.getParent() instanceof IFolder) {
        final IJavaProject javaProject = JavaCore.create(file.getProject());
        try {
          final IPackageFragment packageFragment = javaProject.findPackageFragment(file.getParent().getFullPath());
          result.add(packageFragment.getElementName());
        } catch (final JavaModelException e) {
          LOGGER.error("Could not determine package for file of given document"); //$NON-NLS-1$
        }
      }
      break;
    case "file": //$NON-NLS-1$
      final String fileName = file.getName();
      result.add(fileName.indexOf('.') > 0 ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName);
    }
  }
  return Lists.newArrayList(Iterables.filter(result, Predicates.notNull()));
}
 
Example 3
Source File: CheckResourceUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the name of the resource related to given document.
 *
 * @param xtextDocument
 *          the xtext document
 * @return the name of the resource being edited with given document
 */
public String getNameOfResource(final IXtextDocument xtextDocument) {
  IFile file = xtextDocument.getAdapter(IFile.class);
  if (file != null && file.getName() != null) {
    final String fileName = file.getName();
    int indexOfDotBeforeExtension = fileName.lastIndexOf('.');
    return indexOfDotBeforeExtension > 0 ? fileName.substring(0, indexOfDotBeforeExtension) : fileName;
  }
  return null;
}
 
Example 4
Source File: CheckResourceUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the name of containing package.
 *
 * @param xtextDocument
 *          the xtext document
 * @return the name of containing package
 */
public String getNameOfContainingPackage(final IXtextDocument xtextDocument) {
  IFile file = xtextDocument.getAdapter(IFile.class);
  if (file != null && file.getParent() instanceof IFolder) {
    IJavaProject javaProject = JavaCore.create(file.getProject());
    try {
      IPackageFragment myFragment = javaProject.findPackageFragment(file.getParent().getFullPath());
      return myFragment.getElementName();
    } catch (JavaModelException e) {
      LOGGER.error("Could not determine package for file of given document");
    }
  }
  return null;
}
 
Example 5
Source File: FieldInitializerUtil.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the Java element that corresponds to the given selection.
 *
 * @param selection the current selection.
 * @return the Java element.
 */
@SuppressWarnings("static-method")
public IJavaElement getSelectedResource(IStructuredSelection selection) {
	IJavaElement elem = null;
	if (selection != null && !selection.isEmpty()) {
		final Object object = selection.getFirstElement();
		if (object instanceof IAdaptable) {
			final IAdaptable adaptable = (IAdaptable) object;
			elem = adaptable.getAdapter(IJavaElement.class);
			if (elem == null) {
				elem = getPackage(adaptable);
			}
		}
	}
	if (elem == null) {
		final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IWorkbenchPart part = activePage.getActivePart();
		if (part instanceof ContentOutline) {
			part = activePage.getActiveEditor();
		}
		if (part instanceof XtextEditor) {
			final IXtextDocument doc = ((XtextEditor) part).getDocument();
			final IFile file = doc.getAdapter(IFile.class);
			elem = getPackage(file);
		}
	}
	if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
		try {
			final IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
			if (projects.length == 1) {
				elem = projects[0];
			}
		} catch (JavaModelException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	return elem;
}