Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c100684302bc66679844358d3b3804479d2083d (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
297
298
299
300
301
302
303
304
/*******************************************************************************
 * Copyright (c) 2018 Red Hat Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contibutors:
 * 		Red Hat Inc. - initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.docker.launcher.ui.launchbar;

import java.util.ArrayList;

import org.eclipse.cdt.docker.launcher.IContainerLaunchTarget;
import org.eclipse.cdt.internal.docker.launcher.SWTImagesFactory;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.launchbar.core.target.ILaunchTarget;
import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerConnectionManagerListener;
import org.eclipse.linuxtools.docker.core.IDockerImage;
import org.eclipse.linuxtools.docker.core.IDockerImageListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * @since 1.2
 * @author jjohnstn
 *
 */
public class NewContainerTargetWizardPage extends WizardPage
		implements IDockerImageListener, IDockerConnectionManagerListener {

	private final ILaunchTarget launchTarget;

	private Text nameText;
	private Combo imageCombo;
	private Combo connectionSelector;
	private IDockerConnection connection;
	private IDockerConnection[] connections;
	private IDockerImageListener wizardPage;
	private String imageName;
	private String connectionName;
	private String connectionUri = "";

	public NewContainerTargetWizardPage(ILaunchTarget launchTarget) {
		super(NewContainerTargetWizardPage.class.getName());
		if (launchTarget == null) {
			setTitle(Messages.NewContainerTargetWizardPage_title);
			setDescription(Messages.NewContainerTargetWizardPage_description);
		} else {
			setTitle(Messages.EditContainerTargetWizardPage_title);
			setDescription(Messages.EditContainerTargetWizardPage_description);
		}
		this.launchTarget = launchTarget;
		this.wizardPage = this;
		if (launchTarget != null) {
			connectionUri = launchTarget.getAttribute(IContainerLaunchTarget.ATTR_CONNECTION_URI, null);
			imageName = launchTarget.getAttribute(IContainerLaunchTarget.ATTR_IMAGE_ID, null);
		}
	}

	private ModifyListener connectionModifyListener = new ModifyListener() {

		@Override
		public void modifyText(ModifyEvent e) {
			int index = connectionSelector.getSelectionIndex();
			if (connection != null)
				connection.removeImageListener(wizardPage);
			connection = connections[index];
			connectionUri = connection.getUri();
			if (!connectionName.equals(connection.getName())) {
				setErrorMessage(null);
				imageName = null;
				initializeImageCombo();
				setPageComplete(false);
			}
			connectionName = connection.getName();
		}

	};

	@Override
	public void createControl(Composite parent) {
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout(2, false));

		Label label = new Label(comp, SWT.NONE);
		label.setText(Messages.NewContainerTargetWizardPage_name);

		nameText = new Text(comp, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		if (launchTarget != null) {
			nameText.setText(launchTarget.getId());
			nameText.setEnabled(false);
		}

		label = new Label(comp, SWT.NONE);
		label.setText(Messages.NewContainerTargetWizardPage_connection);

		connectionSelector = new Combo(comp, SWT.BORDER | SWT.READ_ONLY);
		initializeConnectionSelector();
		connectionSelector.addModifyListener(connectionModifyListener);
		// Following is a kludge so that on Linux the Combo is read-only but
		// has a white background.
		connectionSelector.addVerifyListener(new VerifyListener() {
			@Override
			public void verifyText(VerifyEvent e) {
				e.doit = false;
			}
		});
		GridData gd = new GridData();
		gd.horizontalSpan = 2;
		gd.horizontalIndent = 5;
		connectionSelector.setLayoutData(gd);

		Label imageSelectorLabel = new Label(comp, SWT.NULL);
		imageSelectorLabel.setText(Messages.NewContainerTargetWizardPage_image);
		imageCombo = new Combo(comp, SWT.DROP_DOWN);
		GridData gd2 = new GridData();
		gd2.horizontalSpan = 2;
		gd2.horizontalIndent = 5;
		imageCombo.setLayoutData(gd2);

		initializeImageCombo();

		imageCombo.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				imageName = imageCombo.getText();
				setPageComplete(imageName != null && !imageName.isEmpty());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

		});

		setPageComplete(false);
		setControl(comp);
	}

	public String getTargetName() {
		return nameText.getText().trim();
	}

	public String getConnectionURI() {
		return connectionUri;
	}

	public String getImageId() {
		return imageName;
	}

	@Override
	public Image getImage() {
		return SWTImagesFactory.get(SWTImagesFactory.IMG_CONTAINER);
	}

	private void initializeConnectionSelector() {
		int defaultIndex = -1;
		connections = DockerConnectionManager.getInstance().getConnections();
		if (connections.length == 0) {
			setErrorMessage(Messages.NewContainerTargetWizardPage_no_connections);
			return;
		}
		String[] connectionNames = new String[connections.length];
		for (int i = 0; i < connections.length; ++i) {
			connectionNames[i] = connections[i].getName();
			if (connections[i].getUri().equals(connectionUri))
				defaultIndex = i;
		}
		if (defaultIndex < 0) {
			defaultIndex = 0;
		}
		connectionSelector.setItems(connectionNames);
		if (connections.length > 0) {
			connectionSelector.setText(connectionNames[defaultIndex]);
			connection = connections[defaultIndex];
			connectionName = connection.getName();
			connectionUri = connection.getUri();
		}
	}

	private void initializeImageCombo() {
		if (connection != null) {
			java.util.List<IDockerImage> images = connection.getImages();
			if (images == null || images.size() == 0) {
				setErrorMessage(Messages.NewContainerTargetWizardPage_no_images);
				return;
			}
			connection.removeImageListener(wizardPage);
			ArrayList<String> imageNames = new ArrayList<String>();
			for (IDockerImage image : images) {
				java.util.List<String> tags = image.repoTags();
				if (tags != null) {
					for (String tag : tags) {
						if (!tag.equals("<none>:<none>")) //$NON-NLS-1$
							imageNames.add(tag);
					}
				}
			}
			imageCombo.setItems(imageNames.toArray(new String[0]));
			if (imageName != null)
				imageCombo.setText(imageName);
			connection.addImageListener(wizardPage);
		}
	}

	@Override
	public void changeEvent(IDockerConnection changedConnection, int type) {
		String currUri = null;
		int currIndex = 0;
		setErrorMessage(null);
		connections = DockerConnectionManager.getInstance().getConnections();
		if (connection != null) {
			currUri = connection.getUri();
			currIndex = connectionSelector.getSelectionIndex();
		}
		String[] connectionNames = new String[connections.length];
		int index = 0;
		for (int i = 0; i < connections.length; ++i) {
			connectionNames[i] = connections[i].getName();
			if (connections[i].getUri().equals(currUri))
				index = i;
		}
		if (type == IDockerConnectionManagerListener.RENAME_EVENT) {
			index = currIndex; // no change in connection displayed
		}
		connectionSelector.removeModifyListener(connectionModifyListener);
		connectionSelector.setItems(connectionNames);
		if (connectionNames.length > 0) {
			connectionSelector.setText(connectionNames[index]);
			connection = connections[index];
			connectionUri = connection.getUri();
			java.util.List<IDockerImage> images = connection.getImages();
			if (images == null || images.size() == 0) {
				setErrorMessage(Messages.NewContainerTargetWizardPage_no_images);
			}
		} else {
			setErrorMessage(Messages.NewContainerTargetWizardPage_no_connections);
			connection = null;
			connectionUri = "";
			connectionSelector.setText("");
		}
		connectionSelector.addModifyListener(connectionModifyListener);
	}

	public void listChanged(IDockerConnection c, java.util.List<IDockerImage> list) {
		setErrorMessage(null);
		final IDockerImage[] finalList = list.toArray(new IDockerImage[0]);
		if (finalList.length == 0) {
			setErrorMessage(Messages.NewContainerTargetWizardPage_no_images);
		}
		if (c.getName().equals(connection.getName())) {
			Display.getDefault().syncExec(new Runnable() {
				@Override
				public void run() {
					connection.removeImageListener(wizardPage);
					ArrayList<String> imageNames = new ArrayList<String>();
					for (IDockerImage image : finalList) {
						java.util.List<String> tags = image.repoTags();
						if (tags != null) {
							for (String tag : tags) {
								imageNames.add(tag);
							}
						}
					}
					if (!imageCombo.isDisposed())
						imageCombo.setItems(imageNames.toArray(new String[0]));
					connection.addImageListener(wizardPage);
				}

			});
		}
	}

	@Override
	public void dispose() {
		if (connection != null)
			connection.removeImageListener(this);
		super.dispose();
	}

}

Back to the top