Search in sources :

Example 41 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project bndtools by bndtools.

the class SelectionUtils method getSelectionMembers.

public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<T> clazz, Predicate<? super T> filter) throws Exception {
    if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
        return Collections.emptyList();
    }
    IStructuredSelection structSel = (IStructuredSelection) selection;
    List<T> result = new ArrayList<T>(structSel.size());
    Iterator<?> iter = structSel.iterator();
    while (iter.hasNext()) {
        Object element = iter.next();
        if (clazz.isInstance(element)) {
            @SuppressWarnings("unchecked") T casted = (T) element;
            if (filter == null || filter.test(casted)) {
                result.add(casted);
            }
        } else if (element instanceof IAdaptable) {
            @SuppressWarnings("unchecked") T adapted = (T) ((IAdaptable) element).getAdapter(clazz);
            if (adapted != null) {
                if (filter == null || filter.test(adapted)) {
                    result.add(adapted);
                }
            }
        }
    }
    return result;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 42 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project sling by apache.

the class ServersActionModeFiddlerActionDelegate method initToolbarContributedActions.

private void initToolbarContributedActions() {
    cleanAction = new Action("Clean Publish...", IAction.AS_PUSH_BUTTON) {

        public void run() {
            if (server == null) {
                MessageDialog.openInformation(view.getSite().getShell(), "No server selected", "A server must be selected");
                return;
            }
            int selection = 2;
            if (!doNotAskAgain) {
                MessageDialog dialog = new MessageDialog(view.getSite().getShell(), Messages.defaultDialogTitle, null, Messages.dialogPublishClean, MessageDialog.QUESTION_WITH_CANCEL, new String[] { "Cancel", "OK (do not ask again)", "OK" }, 1) {

                    @Override
                    protected void configureShell(Shell shell) {
                        super.configureShell(shell);
                        setShellStyle(getShellStyle() | SWT.SHEET);
                    }
                };
                selection = dialog.open();
            }
            if (selection != 0) {
                if (selection == 1) {
                    doNotAskAgain = true;
                }
                IAdaptable info = new IAdaptable() {

                    public Object getAdapter(Class adapter) {
                        if (Shell.class.equals(adapter))
                            return view.getSite().getShell();
                        if (String.class.equals(adapter))
                            return "user";
                        return null;
                    }
                };
                server.publish(IServer.PUBLISH_CLEAN, modules, info, null);
            }
        }
    };
    cleanAction.setText("Clean Publish...");
    cleanAction.setToolTipText("Clean and Publish...");
    ImageDescriptor cleanAndPublishImageDesc = new DecorationOverlayIcon(ImageResource.getImageDescriptor(ImageResource.IMG_CLCL_PUBLISH).createImage(), ImageDescriptor.createFromFile(SharedImages.class, "refresh.gif"), IDecoration.BOTTOM_RIGHT);
    cleanAction.setImageDescriptor(cleanAndPublishImageDesc);
    cleanAction.setId("org.apache.sling.ide.eclipse.ui.actions.CleanPublishAction");
    publishAction = new Action("Publish", IAction.AS_PUSH_BUTTON) {

        public void run() {
            if (server == null) {
                MessageDialog.openInformation(view.getSite().getShell(), "No server selected", "A server must be selected");
                return;
            }
            IAdaptable info = new IAdaptable() {

                public Object getAdapter(Class adapter) {
                    if (Shell.class.equals(adapter))
                        return view.getSite().getShell();
                    if (String.class.equals(adapter))
                        return "user";
                    return null;
                }
            };
            server.publish(IServer.PUBLISH_INCREMENTAL, modules, info, null);
        }
    };
    publishAction.setText("Publish");
    publishAction.setToolTipText("Publish");
    publishAction.setImageDescriptor(ImageResource.getImageDescriptor(ImageResource.IMG_CLCL_PUBLISH));
    publishAction.setId("org.apache.sling.ide.eclipse.ui.actions.PublishAction");
    cleanAction.setEnabled(false);
    publishAction.setEnabled(false);
    cleanActionContributionItem = new ActionContributionItem(cleanAction);
    publishActionContributionItem = new ActionContributionItem(publishAction);
    appendedToolbarActionContributionItems.add(publishActionContributionItem);
    appendedToolbarActionContributionItems.add(cleanActionContributionItem);
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) DecorationOverlayIcon(org.eclipse.jface.viewers.DecorationOverlayIcon) IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) Shell(org.eclipse.swt.widgets.Shell) ActionContributionItem(org.eclipse.jface.action.ActionContributionItem) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) MessageDialog(org.eclipse.jface.dialogs.MessageDialog)

Example 43 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project xtext-xtend by eclipse.

the class FieldInitializerUtil method getSelectedResource.

@SuppressWarnings("cast")
public IJavaElement getSelectedResource(IStructuredSelection selection) {
    IJavaElement elem = null;
    if (selection != null && !selection.isEmpty()) {
        Object o = selection.getFirstElement();
        if (o instanceof IAdaptable) {
            IAdaptable adaptable = (IAdaptable) o;
            elem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
            if (elem == null) {
                elem = getPackage(adaptable);
            }
        }
    }
    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 = doc.getAdapter(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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IAdaptable(org.eclipse.core.runtime.IAdaptable) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ContentOutline(org.eclipse.ui.views.contentoutline.ContentOutline) IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 44 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project Palladio-Editors-Sirius by PalladioSimulator.

the class AllocationCreationWizard method finish.

@Override
protected void finish() {
    Allocation allocation = (Allocation) modelObject;
    Session session = SessionManager.INSTANCE.getSession(modelObject);
    TransactionalEditingDomain domain = session.getTransactionalEditingDomain();
    String fileString = modelURI.toPlatformString(true);
    IPath path = new Path(fileString);
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
    List<IFile> list = new ArrayList<IFile>();
    list.add(file);
    AbstractTransactionalCommand command = new AbstractTransactionalCommand(domain, "Save Allocation model.", list) {

        @Override
        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            allocation.setTargetResourceEnvironment_Allocation(resourceEnvironmentSelectorpage.getSelectedResourceEnvironment(session));
            allocation.setSystem_Allocation(systemSelectorPage.getSelectedSystem(session));
            return CommandResult.newOKCommandResult();
        }
    };
    try {
        OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null);
    } catch (ExecutionException e) {
        System.out.println("Unable to save allocation model.");
        e.printStackTrace();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IAdaptable(org.eclipse.core.runtime.IAdaptable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) Allocation(org.palladiosimulator.pcm.allocation.Allocation) ExecutionException(org.eclipse.core.commands.ExecutionException) Session(org.eclipse.sirius.business.api.session.Session)

Example 45 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project eclipse.platform.text by eclipse.

the class MarkerRulerAction method execute.

/**
 * Execute the specified undoable operation.
 *
 * @param operation the operation to execute
 * @since 3.3
 */
private void execute(IUndoableOperation operation) {
    final Shell shell = getTextEditor().getSite().getShell();
    IAdaptable context = new IAdaptable() {

        @SuppressWarnings("unchecked")
        @Override
        public <T> T getAdapter(Class<T> adapter) {
            if (adapter == Shell.class)
                return (T) shell;
            return null;
        }
    };
    IOperationHistory operationHistory = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory();
    try {
        operationHistory.execute(operation, null, context);
    } catch (ExecutionException e) {
        Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
        ILog log = Platform.getLog(bundle);
        // $NON-NLS-2$ //$NON-NLS-1$
        String msg = getString(fBundle, fPrefix + "error.dialog.message", fPrefix + "error.dialog.message");
        log.log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, msg, e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IAdaptable(org.eclipse.core.runtime.IAdaptable) Shell(org.eclipse.swt.widgets.Shell) ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) IOperationHistory(org.eclipse.core.commands.operations.IOperationHistory) ILog(org.eclipse.core.runtime.ILog) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

IAdaptable (org.eclipse.core.runtime.IAdaptable)58 IResource (org.eclipse.core.resources.IResource)21 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)15 IFile (org.eclipse.core.resources.IFile)13 IProject (org.eclipse.core.resources.IProject)12 ISelection (org.eclipse.jface.viewers.ISelection)8 CoreException (org.eclipse.core.runtime.CoreException)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IEditorInput (org.eclipse.ui.IEditorInput)6 List (java.util.List)5 ExecutionException (org.eclipse.core.commands.ExecutionException)5 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)5 Iterator (java.util.Iterator)4 IPath (org.eclipse.core.runtime.IPath)4 IStatus (org.eclipse.core.runtime.IStatus)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 IEditorPart (org.eclipse.ui.IEditorPart)4 IOException (java.io.IOException)3 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)3