Java Code Examples for org.eclipse.core.runtime.Adapters#adapt()

The following examples show how to use org.eclipse.core.runtime.Adapters#adapt() . 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: AbstractDebugAdapterLaunchShortcut.java    From wildwebdeveloper with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IResource getLaunchableResource(ISelection selection) {
	if (selection instanceof IStructuredSelection) {
		IStructuredSelection structuredSelection = (IStructuredSelection) selection;
		if (structuredSelection.size() != 1) {
			return null;
		}
		Object firstObject = structuredSelection.getFirstElement();
		IResource resource = Adapters.adapt(firstObject, IResource.class);
		int resourceType = resource.getType();
		if (resourceType == IResource.FILE) {
			if (canLaunch(resource.getLocation().toFile())) {
				return resource;
			}
		} else if (resourceType == IResource.PROJECT || resourceType == IResource.FOLDER) {
			return getLaunchableResource(Adapters.adapt(resource, IContainer.class));
		}
	}
	return null;
}
 
Example 2
Source File: EditorUtils.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public static XtextEditor getActiveXtextEditor() {
	IWorkbench workbench = PlatformUI.getWorkbench();
	IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
	if (workbenchWindow == null) 
		return null;
	
	IWorkbenchPage activePage = workbenchWindow.getActivePage();
	if (activePage == null) 
		return null;
	
	IEditorPart activeEditor = activePage.getActiveEditor();
	if (activeEditor == null)
		return null;
	
	return Adapters.adapt(activeEditor, XtextEditor.class);
}
 
Example 3
Source File: LaunchShortcutUtil.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public static IStructuredSelection replaceWithJavaElementDelegates(IStructuredSelection selection, Class<? extends JavaElementDelegate> delegateType) {
	Object[] originalSelection = selection.toArray();
	Object[] fakeSelection = new Object[originalSelection.length];
	for(int i = 0; i < originalSelection.length; i++) {
		Object original = originalSelection[i];
		if (original == null || original instanceof IJavaElement || original instanceof JavaElementDelegate || !(original instanceof IAdaptable)) {
			fakeSelection[i] = original;
		} else {
			
			JavaElementDelegate javaElementDelegate = Adapters.adapt(original, delegateType);
			if (javaElementDelegate != null) {
				fakeSelection[i] = javaElementDelegate;
			} else {
				fakeSelection[i] = original;
			}
		}
	}
	StructuredSelection newSelection = new StructuredSelection(fakeSelection);
	return newSelection;
}
 
Example 4
Source File: DotnetDebugLaunchShortcut.java    From aCute with Eclipse Public License 2.0 6 votes vote down vote up
@Override public IResource getLaunchableResource(ISelection selection) {
	Set<IResource> resources = new HashSet<>();
	if (selection instanceof IStructuredSelection) {
		for (Object o : ((IStructuredSelection) selection).toArray()) {
			IResource resource = Adapters.adapt(o, IResource.class);
			if (resource != null) {
				resources.add(resource);
			}
		}
	}
	if (resources.isEmpty()) {
		return null;
	} else if (resources.size() == 1) {
		return resources.iterator().next();
	} else {
		// TODO ambiguous
		return null;
	}
}
 
Example 5
Source File: PythonSnippetUtils.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get a IScriptConsoleCodeGenerator adapted object for object
 * @param object to adapt
 * @return adapted object, or <code>null</code> if not adaptable
 * @see IScriptConsoleCodeGenerator
 */
public static IScriptConsoleCodeGenerator getScriptConsoleCodeGeneratorAdapter(Object object) {
    if (object == null) {
        return null;
    }
    if (object instanceof IScriptConsoleCodeGenerator) {
        return (IScriptConsoleCodeGenerator) object;
    }
    Object adaptedNode = Adapters.adapt(object, IScriptConsoleCodeGenerator.class, true);
    if (adaptedNode instanceof IScriptConsoleCodeGenerator) {
        return (IScriptConsoleCodeGenerator) adaptedNode;
    }
    return null;
}
 
Example 6
Source File: JavaApplicationLaunchShortcut.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void launch(IEditorPart editor, String mode) {
	JavaElementDelegate javaElementDelegate = Adapters.adapt(editor, JavaElementDelegateMainLaunch.class);
	if (javaElementDelegate != null) {
		launch(new StructuredSelection(javaElementDelegate), mode);
	} else {
		super.launch(editor, mode);
	}
}
 
Example 7
Source File: JUnitLaunchShortcut.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void launch(IEditorPart editor, String mode) {
	JavaElementDelegate javaElementDelegate = Adapters.adapt(editor, JavaElementDelegateJunitLaunch.class);
	if (javaElementDelegate != null) {
		launch(new StructuredSelection(javaElementDelegate), mode);
	} else {
		super.launch(editor, mode);
	}
}
 
Example 8
Source File: ImportsAwareClipboardAction.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void update() {
	super.update();
	if (isModifyOperation() && !canModifyEditor()) {
		setEnabled(false);
		return;
	}
	if (textOperationTarget == null) {
		textOperationTarget = Adapters.adapt(getTextEditor(), ITextOperationTarget.class);
	}
	boolean isEnabled = (textOperationTarget != null && textOperationTarget.canDoOperation(getOperationCode()));
	setEnabled(isEnabled);
}
 
Example 9
Source File: XtextEditorPropertyTester.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	if (Constants.LANGUAGE_NAME.equals(property)) {
		XtextEditor xtextEditor = Adapters.adapt(receiver, XtextEditor.class);
		if (xtextEditor != null) {
			return xtextEditor.getLanguageName().equals(expectedValue);
		}
	}
	return false;
}
 
Example 10
Source File: NewCargoProjectWizard.java    From corrosion with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
	wizardPage = new NewCargoProjectWizardPage();
	setWindowTitle(Messages.NewCargoProjectWizard_title);

	Iterator<?> selectionIterator = selection.iterator();
	Set<IWorkingSet> workingSets = new HashSet<>();
	IResource selectedResource = null;

	while (selectionIterator.hasNext()) {
		Object element = selectionIterator.next();
		IResource asResource = toResource(element);

		if (asResource != null && selectedResource == null) {
			selectedResource = asResource;
		} else {
			IWorkingSet asWorkingSet = Adapters.adapt(element, IWorkingSet.class);
			if (asWorkingSet != null) {
				workingSets.add(asWorkingSet);
			}
		}
	}

	if (workingSets.isEmpty() && selectedResource != null) {
		workingSets.addAll(getWorkingSets(selectedResource));
	}
	wizardPage.setWorkingSets(workingSets);

	if (selectedResource != null) {
		wizardPage.setDirectory(toFile(selectedResource));
	} else {
		wizardPage.setDirectory(newFolderLocation());
	}
}
 
Example 11
Source File: ProjectExplorerViewerComparator.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private boolean comparingWebPages(Object e1, Object e2) {
    IFolder project1 = Adapters.adapt(e1, IFolder.class);
    IFolder project2 = Adapters.adapt(e2, IFolder.class);
    if (project1 != null && project2 != null) {
        return UIDArtifactFilters.isUIDArtifactFrom(project1, "web_page")
                && UIDArtifactFilters.isUIDArtifactFrom(project2, "web_page");
    }
    return false;
}
 
Example 12
Source File: LaunchableAdapterFactory.java    From aCute with Eclipse Public License 2.0 5 votes vote down vote up
@Override public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
	IResource resource = Adapters.adapt(adaptableObject, IResource.class);
	if (adapterType.equals(ILaunchable.class) && Tester.isDotnetProject(resource.getProject())) {
		return adapterType.cast(DUMMY);
	}
	return null;
}
 
Example 13
Source File: IsDartProjectPropertyTester.java    From dartboard with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	if (IS_DART_PROJECT_PROPERTY.equals(property)) {
		IResource resource = Adapters.adapt(receiver, IResource.class);
		if (resource == null) {
			return false;
		}

		IProject project = resource.getProject();
		if (project == null) {
			return false;
		}
		if (project.findMember(GlobalConstants.PUBSPEC_YAML) != null) {
			return true;
		}

		try {
			for (IResource res : project.members()) {
				if ("dart".equals(res.getFileExtension())) { //$NON-NLS-1$
					return true;
				}
			}
		} catch (CoreException e) {
			LOG.log(DartLog.createError("Couldn't list members of project " + project.getName(), e)); //$NON-NLS-1$
		}
	}

	return false;
}
 
Example 14
Source File: ToggleXtextNatureCommand.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	ISelection selection = HandlerUtil.getCurrentSelection(event);
	if (selection instanceof IStructuredSelection) {
		for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();) {
			IProject project = Adapters.adapt(it.next(), IProject.class);
			if (project != null) {
				toggleNature(project);
			}
		}
	}
	return null;
}
 
Example 15
Source File: FieldInitializerUtil.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public IJavaElement getSelectedResource(IStructuredSelection selection) {
	IJavaElement elem = null;
	if(selection != null && !selection.isEmpty()){
		Object o = selection.getFirstElement();
		elem = Adapters.adapt(o, IJavaElement.class);
		if(elem == null){
			elem = getPackage(o);
		}
	}
	if (elem == null) {
		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IWorkbenchPart part = activePage.getActivePart();
		if (part instanceof ContentOutline) {
			part= activePage.getActiveEditor();
		}
		if (part instanceof XtextEditor) {
			IXtextDocument doc = ((XtextEditor)part).getDocument();
			IFile file = Adapters.adapt(doc, IFile.class);
			elem = getPackage(file);
		}
	}
	if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
		try {
			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;
}
 
Example 16
Source File: MarkerResolutionGenerator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public XtextEditor findEditor(IResource resource) {
	if(resource instanceof IFile) {
		IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
		IEditorPart editor = activePage.findEditor(new FileEditorInput((IFile) resource));
		if(editor instanceof XtextEditor) {
			return (XtextEditor)editor;
		} else if (editor != null) {
			return Adapters.adapt(editor, XtextEditor.class);
		}
	}
	return null;

}
 
Example 17
Source File: HTMLLaunchableAdapterFactory.java    From wildwebdeveloper with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
	if (!ILaunchable.class.equals(adapterType)) {
		return null;
	} 
		IResource resource = Adapters.adapt(adaptableObject, IResource.class);
		if  (new FirefoxRunDebugLaunchShortcut().canLaunch(resource.getLocation().toFile())) {
			return adapterType.cast(DUMMY);
	}

	return null;
}
 
Example 18
Source File: IsFlutterProjectPropertyTester.java    From dartboard with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	if (IS_FLUTTER_PROJECT_PROPERTY.equalsIgnoreCase(property)) {
		IResource resource = Adapters.adapt(receiver, IResource.class);
		if (resource == null) {
			return false;
		}

		IProject project = resource.getProject();
		if (project == null) {
			return false;
		}
		IResource pubspec = project.findMember(GlobalConstants.PUBSPEC_YAML);
		if (pubspec == null) {
			return false;
		}
		File pubspecFile = pubspec.getRawLocation().toFile();
		CharSource pubspecContent = Files.asCharSource(pubspecFile, Charset.defaultCharset());
		try {
			for (String line : pubspecContent.readLines()) {
				if (FLUTTER_SDK.matcher(line).matches()) {
					return true;
				}
			}
		} catch (IOException e) {
			LOG.log(DartLog.createError("Could not open pubspec.yaml", e));
		}
	}
	return false;
}
 
Example 19
Source File: AbstractPreferencePage.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void setElement(IAdaptable element) {
	this.project = Adapters.adapt(element, IProject.class);
}
 
Example 20
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public IResource getResource() {
	return Adapters.adapt(getEditorInput(), IResource.class);
}