Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e2f3e8067e4cfef28d732ac29dead7a58c41fd2 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************************
 * Copyright (c) 2015, 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.launch;

import static org.eclipse.linuxtools.internal.docker.ui.launch.IDockerComposeLaunchConfigurationConstants.DOCKER_CONNECTION;
import static org.eclipse.linuxtools.internal.docker.ui.launch.IDockerComposeLaunchConfigurationConstants.WORKING_DIR;
import static org.eclipse.linuxtools.internal.docker.ui.launch.IDockerComposeLaunchConfigurationConstants.WORKING_DIR_WORKSPACE_RELATIVE_LOCATION;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.docker.ui.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.views.navigator.ResourceComparator;

/**
 * The main launch configuration tab for the {@code docker-compose up} process.
 */
public class DockerComposeUpLaunchConfigurationMainTab
		extends AbstractLaunchConfigurationTab {

	/** the Docker daemon to use for the image build. */
	private ComboViewer connectionSelectionComboViewer;
	/** the path to the docker compose config file. */
	private Text dockerComposeWorkingDirPathText;
	/**
	 * whether path to the docker compose config file is relative to the
	 * workspace or not.
	 */
	private boolean dockerComposeFilePathWorkspaceRelative;

	@Override
	public void createControl(final Composite parent) {
		final Composite container = new Composite(parent, SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
				.grab(true, false).applyTo(container);
		GridLayoutFactory.fillDefaults().margins(6, 6).applyTo(container);
		setControl(container);

		// connection selection
		final Group connectionGroup = new Group(container, SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
				.grab(true, false).applyTo(connectionGroup);
		GridLayoutFactory.fillDefaults().numColumns(2).margins(6, 6)
				.applyTo(connectionGroup);
		connectionGroup.setText(LaunchMessages.getString(
				"DockerComposeUpLaunchConfigurationMainTab.connection.group.label")); //$NON-NLS-1$
		connectionGroup.setToolTipText(LaunchMessages.getString(
				"DockerComposeUpLaunchConfigurationMainTab.connection.group.tooltip")); //$NON-NLS-1$
		final Combo connectionSelectionCombo = new Combo(connectionGroup,
				SWT.NONE);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(true, false).applyTo(connectionSelectionCombo);
		this.connectionSelectionComboViewer = new ComboViewer(
				connectionSelectionCombo);
		this.connectionSelectionComboViewer
				.setContentProvider(new ArrayContentProvider());
		this.connectionSelectionComboViewer.setInput(
				DockerConnectionManager.getInstance().getConnectionNames());
		connectionSelectionCombo
				.addSelectionListener(new LaunchConfigurationChangeListener());
		// docker compose config file
		createDockerComposeWorkingDirLocationGroup(container);
	}

	private void createDockerComposeWorkingDirLocationGroup(final Composite container) {
		final Group dockerComposeWorkingDirLocationGroup = new Group(container,
				SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
				.grab(true, false).applyTo(dockerComposeWorkingDirLocationGroup);
		GridLayoutFactory.fillDefaults().margins(6, 6).numColumns(3)
				.applyTo(dockerComposeWorkingDirLocationGroup);
		dockerComposeWorkingDirLocationGroup
				.setText(LaunchMessages.getString(
						"DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.group.label")); //$NON-NLS-1$
		this.dockerComposeWorkingDirPathText = new Text(dockerComposeWorkingDirLocationGroup,
				SWT.BORDER);
		this.dockerComposeWorkingDirPathText
				.addModifyListener(new LaunchConfigurationChangeListener());
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(true, false).span(3, 1)
				.applyTo(this.dockerComposeWorkingDirPathText);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(true, false)
				.applyTo(new Label(dockerComposeWorkingDirLocationGroup, SWT.NONE));
		final Button browseWorkspaceButton = new Button(
				dockerComposeWorkingDirLocationGroup, SWT.NONE);
		browseWorkspaceButton.setText(LaunchMessages.getString(
				"DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.browseworkspace.button.label")); //$NON-NLS-1$
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(browseWorkspaceButton);
		browseWorkspaceButton.addSelectionListener(
				onBrowseWorkspace(dockerComposeWorkingDirPathText, IContainer.class));
		final Button browseFileSystemButton = new Button(
				dockerComposeWorkingDirLocationGroup, SWT.NONE);
		browseFileSystemButton.setText(LaunchMessages.getString(
				"DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.browsefilesystem.button.label")); //$NON-NLS-1$
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.grab(false, false).applyTo(browseFileSystemButton);
		browseFileSystemButton.addSelectionListener(
				onBrowseFileSystemForDirectory(this.dockerComposeWorkingDirPathText));
	}

	/**
	 * Opens a dialog to browse the workspace
	 * 
	 * @return
	 */
	private SelectionListener onBrowseWorkspace(final Text pathText,
			final Class<?> expectedType) {
		return new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				final ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
						getShell(), new WorkbenchLabelProvider(),
						new WorkbenchContentProvider());
				dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
				dialog.setTitle(LaunchMessages.getString(
						"DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.browseworkspace.dialog.title")); //$NON-NLS-1$
				dialog.setComparator(
						new ResourceComparator(ResourceComparator.NAME));
				dialog.setAllowMultiple(false);
				dialog.setValidator(selection -> {
					if (selection.length == 1 && expectedType
							.isAssignableFrom(selection[0].getClass())) {
						return new Status(IStatus.OK, Activator.PLUGIN_ID,
								null);
					}
					return new Status(IStatus.ERROR, Activator.PLUGIN_ID, null);
				});
				if (dialog.open() == IDialogConstants.OK_ID) {
					final IResource selection = (IResource) dialog
							.getFirstResult();
					pathText.setText(selection.getFullPath().toOSString());
					dockerComposeFilePathWorkspaceRelative = true;
				}
			}
		};
	}

	/**
	 * Opens a dialog to browse the file system and select a directory
	 * 
	 * @return
	 */
	private SelectionListener onBrowseFileSystemForDirectory(
			final Text pathText) {
		return new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				final DirectoryDialog dialog = new DirectoryDialog(getShell());
				final String selection = dialog.open();
				if (selection != null) {
					pathText.setText(selection);
					dockerComposeFilePathWorkspaceRelative = false;
				}
			}
		};
	}

	@Override
	public void setDefaults(
			final ILaunchConfigurationWorkingCopy configuration) {
	}

	@Override
	public void initializeFrom(final ILaunchConfiguration configuration) {
		try {
			this.connectionSelectionComboViewer
					.setSelection(new StructuredSelection(
							configuration.getAttribute(DOCKER_CONNECTION, "")));
			this.dockerComposeWorkingDirPathText
					.setText(configuration.getAttribute(WORKING_DIR, ""));
			this.dockerComposeFilePathWorkspaceRelative = configuration
					.getAttribute(WORKING_DIR_WORKSPACE_RELATIVE_LOCATION,
							false);
		} catch (CoreException e) {
			Activator.log(e);
		}
	}

	@Override
	public boolean isValid(final ILaunchConfiguration launchConfig) {
		try {
			// verify the connection
			final String dockerConnection = launchConfig
					.getAttribute(DOCKER_CONNECTION, ""); // $NON-NLS-1$
			// verify the source path
			final String sourcePathLocation = launchConfig
					.getAttribute(WORKING_DIR, ""); // $NON-NLS-1$
			final boolean sourcePathWorkspaceRelativeLocation = launchConfig
					.getAttribute(WORKING_DIR_WORKSPACE_RELATIVE_LOCATION,
							false);
			final IPath sourcePath = BuildDockerImageUtils.getPath(
					sourcePathLocation, sourcePathWorkspaceRelativeLocation);
			if (dockerConnection.isEmpty() || dockerConnection == null
					|| DockerConnectionManager.getInstance()
							.findConnection(dockerConnection) == null) {
				setErrorMessage(LaunchMessages.getString(
						"DockerComposeUpLaunchConfigurationMainTab.connection.missing")); //$NON-NLS-1$
				return false;
			} else if (sourcePathLocation.isEmpty() || sourcePath == null) {
				setErrorMessage(LaunchMessages.getString(
						"DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.missing")); //$NON-NLS-1$
				return false;
			} else if (!sourcePath.append("docker-compose.yml").toFile()
					.exists()) {
				setErrorMessage(LaunchMessages.getString(
						"DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.missingDockerComposeFile")); //$NON-NLS-1$
				return false;
			} else {
				setErrorMessage(null);
			}
		} catch (CoreException e) {
			Activator.log(e);
		}
		return super.isValid(launchConfig);
	}

	@Override
	public void performApply(
			final ILaunchConfigurationWorkingCopy configuration) {
		final IStructuredSelection connectionSelection = (IStructuredSelection) this.connectionSelectionComboViewer
				.getSelection();
		if (connectionSelection.getFirstElement() != null) {
			configuration.setAttribute(DOCKER_CONNECTION,
					connectionSelection.getFirstElement().toString());
		}
		configuration.setAttribute(WORKING_DIR,
				this.dockerComposeWorkingDirPathText.getText());
		configuration.setAttribute(WORKING_DIR_WORKSPACE_RELATIVE_LOCATION,
				this.dockerComposeFilePathWorkspaceRelative);
	}

	@Override
	public String getName() {
		return LaunchMessages
				.getString("DockerComposeUpLaunchConfigurationMainTab.name"); //$NON-NLS-1$
	}

	private class LaunchConfigurationChangeListener extends SelectionAdapter
			implements ModifyListener {

		@Override
		public void modifyText(final ModifyEvent e) {
			updateLaunchConfigurationDialog();
		}

		@Override
		public void widgetSelected(final SelectionEvent e) {
			updateLaunchConfigurationDialog();
		}
	}

}

Back to the top