Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 96b1665c383d6ac294a49b42c285651cc95a01b3 (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
/*******************************************************************************
 * Copyright (c) 2014, 2015 Red Hat.
 * 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.views;


import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.linuxtools.docker.core.EnumDockerStatus;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerContainer;
import org.eclipse.linuxtools.docker.core.IDockerImage;
import org.eclipse.linuxtools.docker.core.IDockerPortMapping;
import org.eclipse.linuxtools.internal.docker.ui.SWTImagesFactory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerLink;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerLinksCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerPortMappingsCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerVolume;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerVolumesCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainersCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerImagesCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.LoadingStub;
import org.eclipse.swt.graphics.Image;

/**
 * @author xcoulon
 *
 */
public class DockerExplorerLabelProvider implements IStyledLabelProvider, ILabelProvider {

	private static final String UNNAMED_CONNECTION = "Connection.unnamed"; //$NON-NLS-1$
	private Image OPEN_CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLE
			.createImage();
	private Image UNOPEN_CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLED
			.createImage();
	private Image CATEGORY_IMAGE = SWTImagesFactory.DESC_DB_GROUP.createImage();
	private Image IMAGE_IMAGE = SWTImagesFactory.DESC_IMAGE.createImage();
	private Image STARTED_CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER_STARTED
			.createImage();
	private Image PAUSED_CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER_PAUSED
			.createImage();
	private Image STOPPED_CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER_STOPPED
			.createImage();
	private Image CONTAINER_LINK_IMAGE = SWTImagesFactory.DESC_CONTAINER_LINK
			.createImage();
	private Image CONTAINER_VOLUME_IMAGE = SWTImagesFactory.DESC_CONTAINER_VOLUME
			.createImage();
	private Image CONTAINER_PORT_IMAGE = SWTImagesFactory.DESC_CONTAINER_PORT
			.createImage();
	private Image LOADING_IMAGE = SWTImagesFactory.DESC_SYSTEM_PROCESS
			.createImage();

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public void dispose() {
		OPEN_CONNECTION_IMAGE.dispose();
		UNOPEN_CONNECTION_IMAGE.dispose();
		CATEGORY_IMAGE.dispose();
		IMAGE_IMAGE.dispose();
		STARTED_CONTAINER_IMAGE.dispose();
		PAUSED_CONTAINER_IMAGE.dispose();
		STOPPED_CONTAINER_IMAGE.dispose();
		CONTAINER_LINK_IMAGE.dispose();
		CONTAINER_VOLUME_IMAGE.dispose();
		CONTAINER_PORT_IMAGE.dispose();
		LOADING_IMAGE.dispose();
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	@Override
	public Image getImage(final Object element) {
		if(element instanceof IDockerConnection) {
			if (((IDockerConnection) element).isOpen()) {
				return OPEN_CONNECTION_IMAGE;
			} else {
				return UNOPEN_CONNECTION_IMAGE;
			}
		} else if(element instanceof DockerImagesCategory) {
			return CATEGORY_IMAGE;
		} else if(element instanceof DockerContainersCategory) {
			return CATEGORY_IMAGE;
		} else if(element instanceof IDockerImage) {
			return IMAGE_IMAGE;
		} else if(element instanceof IDockerContainer) {
			final IDockerContainer container = (IDockerContainer) element;
			final EnumDockerStatus containerStatus = EnumDockerStatus
					.fromStatusMessage(container.status());
			if (containerStatus == EnumDockerStatus.RUNNING) {
				return STARTED_CONTAINER_IMAGE;
			} else if (containerStatus == EnumDockerStatus.PAUSED) {
				return PAUSED_CONTAINER_IMAGE;
			} else {
				return STOPPED_CONTAINER_IMAGE;
			}
		} else if (element instanceof DockerContainerLinksCategory
				|| element instanceof DockerContainerLink) {
			return CONTAINER_LINK_IMAGE;
		} else if (element instanceof DockerContainerVolumesCategory
				|| element instanceof DockerContainerVolume) {
			return CONTAINER_VOLUME_IMAGE;
		} else if (element instanceof DockerContainerPortMappingsCategory
				|| element instanceof IDockerPortMapping) {
			return CONTAINER_PORT_IMAGE;
		} else if(element instanceof LoadingStub) {
			return LOADING_IMAGE;
		}
		return null;
	}

	@Override
	public String getText(final Object element) {
		final StyledString styledText = getStyledText(element);
		if(styledText != null) {
			return styledText.getString();
		}
		return null;
	}

	@Override
	public StyledString getStyledText(Object element) {
		// when the text to display is in the properties view title bar, the given element
		// is the TreeSelection from the contributing view (the Docker Explorer View)
		if(element instanceof IStructuredSelection) {
			return getStyledText(((IStructuredSelection)element).getFirstElement());
		}
		if(element instanceof IDockerConnection) {
			final IDockerConnection connection = (IDockerConnection) element;
			final String connectionName = (connection.getName() != null
					&& !connection.getName().isEmpty())
					? connection.getName()
					: DVMessages.getString(UNNAMED_CONNECTION);
			final StringBuilder messageBuilder = new StringBuilder();
			messageBuilder.append(connectionName);
			if (connection.getUri() != null && !connection.getUri().isEmpty()) {
				messageBuilder.append(" (").append(connection.getUri()) //$NON-NLS-1$
						.append(")"); //$NON-NLS-1$
			}
			final String message = messageBuilder.toString();
			final StyledString styledString = new StyledString(message);
			styledString.setStyle(connectionName.length(), message.length() - connectionName.length(), StyledString.QUALIFIER_STYLER);
			return styledString;
		} else if(element instanceof DockerImagesCategory) {
			return new StyledString(
					DVMessages.getString("DockerImagesCategory.label")); //$NON-NLS-1$
		} else if(element instanceof DockerContainersCategory) {
			return new StyledString(
					DVMessages.getString("DockerContainersCategory.label")); //$NON-NLS-1$
		} else if (element instanceof IDockerContainer) {
			final IDockerContainer dockerContainer = (IDockerContainer) element;
			final String containerName = dockerContainer.name();
			final String image = dockerContainer.image();
			final String message = containerName + " (" + image + ")";
			final StyledString styledString = new StyledString(message);
			styledString.setStyle(containerName.length(),
					message.length() - containerName.length(),
					StyledString.QUALIFIER_STYLER);
			return styledString;
		} else if (element instanceof IDockerImage) {
			return LabelProviderUtils.getStyleString((IDockerImage) element);
		} else if (element instanceof DockerContainerPortMappingsCategory) {
			return new StyledString(DVMessages
					.getString("DockerContainerPortMappingsCategory.label")); //$NON-NLS-1$
		} else if (element instanceof IDockerPortMapping) {
			final IDockerPortMapping mapping = (IDockerPortMapping) element;
			final String hostMapping = mapping.getIp() + ":"
					+ mapping.getPublicPort() + " -> ";
			final String containerMapping = Integer
					.toString(mapping.getPrivatePort());
			final String mappingType = " (" + mapping.getType() + ")";
			final StyledString styledString = new StyledString(
					hostMapping + containerMapping + mappingType);
			styledString.setStyle(
					hostMapping.length() + containerMapping.length(),
					mappingType.length(), StyledString.QUALIFIER_STYLER);
			return styledString;
		} else if (element instanceof DockerContainerVolumesCategory) {
			return new StyledString(DVMessages
					.getString("DockerContainerVolumesCategory.label")); //$NON-NLS-1$
		} else if (element instanceof DockerContainerVolume) {
			final DockerContainerVolume containerVolume = (DockerContainerVolume) element;
			final String hostPath = containerVolume.getHostPath();
			final StyledString styledString = new StyledString();
			if (containerVolume.getHostPath() != null
					&& containerVolume.getContainerPath() != null) {
				styledString.append(hostPath).append(" -> ")
						.append(containerVolume.getContainerPath());
			} else if (containerVolume.getHostPath() == null
					&& containerVolume.getContainerPath() != null) {
				styledString.append(containerVolume.getContainerPath());
			}
			if (containerVolume.getFlags() != null) {
				final int offset = styledString.length();
				styledString.append(" (" + containerVolume.getFlags() + ")");
				styledString.setStyle(offset, styledString.length() - offset,
						StyledString.QUALIFIER_STYLER);
			}
			return styledString;
		} else if (element instanceof DockerContainerLinksCategory) {
			return new StyledString(
					DVMessages.getString("DockerContainerLinksCategory.label")); //$NON-NLS-1$
		} else if (element instanceof DockerContainerLink) {
			final DockerContainerLink containerLink = (DockerContainerLink) element;
			final String containerName = containerLink.getContainerName();
			final String containerAlias = " ("
					+ containerLink.getContainerAlias() + ")";
			final StyledString styledString = new StyledString(
					containerName + containerAlias);
			styledString.setStyle(containerName.length(),
					containerAlias.length(), StyledString.QUALIFIER_STYLER);
			return styledString;
		} else if (element instanceof String) {
			return new StyledString((String) element);
		}

		else if (element instanceof LoadingStub) {
			return new StyledString(DVMessages.getString("Loading.label"));
		}
		return null;
	}

}

Back to the top