Search in sources :

Example 6 with IResourceWrapper

use of org.jboss.tools.openshift.internal.ui.models.IResourceWrapper in project jbosstools-openshift by jbosstools.

the class DeleteResourcesHandler method deleteResources.

/**
 * Resources are deleted with one job per resource and job are in a job group so
 * that an error dialog will be displayed at the end of the job group.
 *
 * made protected for test purposes only
 *
 * @param uiResources
 *            the UI resources to delete
 */
protected void deleteResources(final IResourceWrapper<?, ?>[] uiResources) {
    final JobGroup group = new JobGroup("Delete Openshift resources", 1, uiResources.length) {

        /*
			 * Overridden because job group cancel job at first job error by default
			 */
        @Override
        protected boolean shouldCancel(IStatus lastCompletedJobResult, int numberOfFailedJobs, int numberOfCanceledJobs) {
            return false;
        }
    };
    try (Stream<IResourceWrapper<?, ?>> stream = Arrays.stream(uiResources)) {
        stream.forEach(uiResource -> {
            DeleteResourceJob job = OpenShiftJobs.createDeleteResourceJob(uiResource.getWrapped());
            job.setJobGroup(group);
            job.schedule();
        });
    }
}
Also used : JobGroup(org.eclipse.core.runtime.jobs.JobGroup) IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) IStatus(org.eclipse.core.runtime.IStatus) DeleteResourceJob(org.jboss.tools.openshift.internal.core.job.DeleteResourceJob)

Example 7 with IResourceWrapper

use of org.jboss.tools.openshift.internal.ui.models.IResourceWrapper in project jbosstools-openshift by jbosstools.

the class DeleteResourcesHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResourceWrapper<?, ?>[] wrappers = UIUtils.getElements(UIUtils.getCurrentSelection(event), IResourceWrapper.class);
    if (ArrayUtils.isEmpty(wrappers) || wrappers[0].getWrapped() == null) {
        return OpenShiftUIActivator.statusFactory().cancelStatus("Could not delete resources: " + "No resource selected that we can get the connection and namespace from.");
    }
    IResource selectedResource = wrappers[0].getWrapped();
    Connection connection = ConnectionsRegistryUtil.getConnectionFor(selectedResource);
    if (connection == null) {
        return OpenShiftUIActivator.statusFactory().cancelStatus(NLS.bind("Could not delete resources: No connection found for selected resource {0}", selectedResource.getName()));
    }
    String namespace = selectedResource.getNamespaceName();
    openDialog(connection, namespace, HandlerUtil.getActiveShell(event));
    return null;
}
Also used : IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) Connection(org.jboss.tools.openshift.core.connection.Connection) IResource(com.openshift.restclient.model.IResource)

Example 8 with IResourceWrapper

use of org.jboss.tools.openshift.internal.ui.models.IResourceWrapper in project jbosstools-openshift by jbosstools.

the class DeleteResourceHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = UIUtils.getCurrentSelection(event);
    IResourceWrapper<?, ?>[] resources = UIUtils.getElements(selection, IResourceWrapper.class);
    if (resources == null || resources.length == 0) {
        // $NON-NLS-1$
        return OpenShiftUIActivator.statusFactory().cancelStatus("No resource selected that we can delete.");
    }
    try (Stream<IResourceWrapper<?, ?>> stream = Arrays.stream(resources)) {
        boolean hasProject = stream.anyMatch(resource -> resource instanceof IProjectWrapper);
        String title = getDialogTitle(hasProject);
        String message = getDialogMessage(resources, hasProject);
        boolean confirm = MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), title, message);
        if (confirm) {
            deleteResources(resources);
        }
        return null;
    }
}
Also used : IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) ISelection(org.eclipse.jface.viewers.ISelection) IProjectWrapper(org.jboss.tools.openshift.internal.ui.models.IProjectWrapper)

Example 9 with IResourceWrapper

use of org.jboss.tools.openshift.internal.ui.models.IResourceWrapper in project jbosstools-openshift by jbosstools.

the class DeleteResourceHandler method deleteResources.

/**
 * Resources are deleted with one job per resource and job are in a job
 * group so that an error dialog will be displayed at the end of the job
 * group.
 *
 * made protected for test purposes only
 *
 * @param uiResources
 *            the UI resources to delete
 */
protected JobGroup deleteResources(final IResourceWrapper<?, ?>[] uiResources) {
    final JobGroup group = new JobGroup("Deleting OpenShift resources...", 1, uiResources.length) {

        /*
			 * Overridden because job group cancel job at first job error by
			 * default
			 */
        @Override
        protected boolean shouldCancel(IStatus lastCompletedJobResult, int numberOfFailedJobs, int numberOfCanceledJobs) {
            return false;
        }
    };
    try (Stream<IResourceWrapper<?, ?>> stream = Arrays.stream(uiResources)) {
        stream.forEach(uiResource -> {
            DeleteResourceJob job = OpenShiftJobs.createDeleteResourceJob(uiResource.getWrapped());
            job.setJobGroup(group);
            job.schedule();
        });
    }
    return group;
}
Also used : JobGroup(org.eclipse.core.runtime.jobs.JobGroup) IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) IStatus(org.eclipse.core.runtime.IStatus) DeleteResourceJob(org.jboss.tools.openshift.internal.core.job.DeleteResourceJob)

Example 10 with IResourceWrapper

use of org.jboss.tools.openshift.internal.ui.models.IResourceWrapper in project jbosstools-openshift by jbosstools.

the class RefreshTest method testRefreshConnection.

@Test
public void testRefreshConnection() throws InterruptedException, TimeoutException {
    IProject project1 = stubProject("test1", 1);
    IProject project2 = stubProject("test2", 1);
    IProject project2prime = stubProject("test2", 2);
    when(getConnectionMock().getResources(ResourceKind.PROJECT)).thenReturn(Collections.singletonList(project1));
    IConnectionWrapper connection = getConnection();
    getConnection().load(IExceptionHandler.NULL_HANDLER);
    UITestUtils.waitForState(connection, LoadingState.LOADED);
    assertEquals(1, connection.getResources().size());
    assertTrue(connection.getResources().stream().anyMatch(projectWrapper -> project1.equals(projectWrapper.getWrapped())));
    registry.fireConnectionChanged(getConnectionMock(), ConnectionProperties.PROPERTY_PROJECTS, null, Arrays.asList(project1, project2));
    verify(listener).elementChanged(connection);
    assertEquals(2, connection.getResources().size());
    assertTrue(connection.getResources().stream().anyMatch(projectWrapper -> project1.equals(projectWrapper.getWrapped())));
    assertTrue(connection.getResources().stream().anyMatch(projectWrapper -> project2.equals(projectWrapper.getWrapped())));
    registry.fireConnectionChanged(getConnectionMock(), ConnectionProperties.PROPERTY_PROJECTS, null, Arrays.asList(project2));
    verify(listener, times(2)).elementChanged(connection);
    assertEquals(1, connection.getResources().size());
    Optional<IResourceWrapper<?, ?>> project2Wrapper = connection.getResources().stream().filter(projectWrapper -> project2.equals(projectWrapper.getWrapped())).findFirst();
    assertTrue(project2Wrapper.isPresent());
    registry.fireConnectionChanged(getConnectionMock(), ConnectionProperties.PROPERTY_PROJECTS, null, Arrays.asList(project2prime));
    verify(listener, times(2)).elementChanged(connection);
    verify(listener).elementChanged(project2Wrapper.get());
    assertEquals(1, connection.getResources().size());
    assertTrue(connection.getResources().stream().anyMatch(projectWrapper -> {
        IResource resource = projectWrapper.getWrapped();
        String version = resource.getResourceVersion();
        return project2.equals(resource) && version.equals("2");
    }));
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) TimeoutException(java.util.concurrent.TimeoutException) IConnectionWrapper(org.jboss.tools.openshift.internal.ui.models.IConnectionWrapper) IProject(com.openshift.restclient.model.IProject) IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) UITestUtils(org.jboss.tools.openshift.test.util.UITestUtils) ConnectionsRegistry(org.jboss.tools.openshift.common.core.connection.ConnectionsRegistry) Before(org.junit.Before) LoadingState(org.jboss.tools.openshift.internal.ui.models.LoadingState) IResource(com.openshift.restclient.model.IResource) ResourceKind(com.openshift.restclient.ResourceKind) ConnectionProperties(org.jboss.tools.openshift.core.connection.ConnectionProperties) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) OpenshiftUIModel(org.jboss.tools.openshift.internal.ui.models.OpenshiftUIModel) IExceptionHandler(org.jboss.tools.openshift.internal.ui.models.IExceptionHandler) Mockito.verify(org.mockito.Mockito.verify) IOpenShiftConnection(org.jboss.tools.openshift.core.connection.IOpenShiftConnection) Optional(java.util.Optional) IElementListener(org.jboss.tools.openshift.internal.ui.models.IElementListener) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) IConnectionWrapper(org.jboss.tools.openshift.internal.ui.models.IConnectionWrapper) IProject(com.openshift.restclient.model.IProject) IResource(com.openshift.restclient.model.IResource) Test(org.junit.Test)

Aggregations

IResourceWrapper (org.jboss.tools.openshift.internal.ui.models.IResourceWrapper)11 IResource (com.openshift.restclient.model.IResource)4 ResourceKind (com.openshift.restclient.ResourceKind)3 ISelection (org.eclipse.jface.viewers.ISelection)3 Viewer (org.eclipse.jface.viewers.Viewer)3 IPod (com.openshift.restclient.model.IPod)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IStatus (org.eclipse.core.runtime.IStatus)2 JobGroup (org.eclipse.core.runtime.jobs.JobGroup)2 TableViewer (org.eclipse.jface.viewers.TableViewer)2 ViewerComparator (org.eclipse.jface.viewers.ViewerComparator)2 SWT (org.eclipse.swt.SWT)2 ConnectionsRegistry (org.jboss.tools.openshift.common.core.connection.ConnectionsRegistry)2 Connection (org.jboss.tools.openshift.core.connection.Connection)2 DeleteResourceJob (org.jboss.tools.openshift.internal.core.job.DeleteResourceJob)2 ResourceUtils (org.jboss.tools.openshift.internal.core.util.ResourceUtils)2 IConnectionWrapper (org.jboss.tools.openshift.internal.ui.models.IConnectionWrapper)2 IElementListener (org.jboss.tools.openshift.internal.ui.models.IElementListener)2