Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 39ebf7d23b843081a8cddeed3e6875f97835d33a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2014, 2016 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/

package org.eclipse.linuxtools.internal.docker.ui.commands;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerContainer;
import org.eclipse.linuxtools.internal.docker.ui.RunConsole;
import org.eclipse.linuxtools.internal.docker.ui.views.DVMessages;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

/**
 * Command handler to kill all the selected {@link IDockerContainer}
 * 
 * @author xcoulon
 *
 */
public class RemoveContainersCommandHandler extends BaseContainersCommandHandler {

	private static final String CONTAINERS_REMOVE_MSG = "ContainersRemove.msg"; //$NON-NLS-1$
	private static final String CONTAINER_REMOVE_MSG = "ContainerRemove.msg"; //$NON-NLS-1$
	private static final String CONTAINER_REMOVE_ERROR_MSG = "ContainerRemoveError.msg"; //$NON-NLS-1$
	private static final String CONTAINER_REMOVE_CONFIRM = "ContainerRemoveConfirm.msg"; //$NON-NLS-1$
	private static final String CONTAINER_REMOVE_LIST = "ContainerRemoveList.msg"; //$NON-NLS-1$

	@Override
	void executeInJob(final IDockerContainer container,
			final IDockerConnection connection) {
		try {
			connection.removeContainer(container.id());
			RunConsole rc = RunConsole.findConsole(container.id());
			if (rc != null)
				RunConsole.removeConsole(rc);
		} catch (DockerException | InterruptedException e) {
			final String errorMessage = DVMessages.getFormattedString(
					CONTAINER_REMOVE_ERROR_MSG, container.name());
			openError(errorMessage, e);
		} finally {
			// always get images as we sometimes get errors on intermediate
			// images
			// being removed but we will remove some top ones successfully
			connection.getContainers(true);
		}
	}

	@Override
	String getJobName(final List<IDockerContainer> selectedContainers) {
		return DVMessages.getString(CONTAINERS_REMOVE_MSG);
	}

	@Override
	String getTaskName(final IDockerContainer image) {
		return DVMessages.getFormattedString(CONTAINER_REMOVE_MSG, image.name());
	}

	private class DialogResponse {
		private boolean response;

		public boolean getResponse() {
			return response;
		}

		public void setResponse(boolean value) {
			response = value;
		}
	}

	@Override
	boolean confirmed(List<IDockerContainer> selectedContainers) {
		// ask for confirmation before deleting images
		List<String> containersToRemove = new ArrayList<>();
		for (IDockerContainer container : selectedContainers) {
			containersToRemove.add(container.name());
		}
		final List<String> names = containersToRemove;
		final DialogResponse response = new DialogResponse();
		Display.getDefault().syncExec(() -> {
			boolean result = MessageDialog.openConfirm(
					PlatformUI.getWorkbench().getActiveWorkbenchWindow()
							.getShell(),
					DVMessages.getString(CONTAINER_REMOVE_CONFIRM),
					DVMessages.getFormattedString(CONTAINER_REMOVE_LIST,
							names.toString()));
			response.setResponse(result);
		});
		return response.getResponse();
	}

}

Back to the top