Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b91aaf49aeb44f5162a293f4c2e863077a39233f (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
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.linuxtools.docker.core.IDockerContainer;
import org.eclipse.linuxtools.docker.core.IDockerPortMapping;
import org.eclipse.linuxtools.internal.docker.core.DockerPortMapping;

/**
 * @author xcoulon
 *
 */
public class ContainerInfoContentProvider implements ITreeContentProvider {

	private static final Object[] EMPTY = new Object[0];

	@Override
	public void dispose() {

	}

	@Override
	public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {
	}

	@Override
	public Object[] getElements(final Object inputElement) {
		if(inputElement instanceof IDockerContainer) {
			final IDockerContainer container = (IDockerContainer) inputElement;
			return new Object[] {
					new Object[]{"Id", container.id().substring(0,  12)}, //$NON-NLS-1$
					new Object[]{"Image", container.image()}, //$NON-NLS-1$
					new Object[]{"Command", container.command()}, //$NON-NLS-1$
					new Object[]{"Created", LabelProviderUtils.toCreatedDate(container.created())}, //$NON-NLS-1$
					new Object[]{"Status", container.status()}, //$NON-NLS-1$
					new Object[]{"Ports", getPorts(container)}, //$NON-NLS-1$
					new Object[]{"Names", getNames(container)}, //$NON-NLS-1$
			};
		}
		return EMPTY;
	}

	/**
	 * @return the first {@link DockerPortMapping} if the given {@link IDockerContainer} has only one {@link DockerPortMapping}, an empty {@link String} if the given list is empty, otherwise the given list itself.  
	 * @param container the {@link IDockerContainer} of elements to analyze
	 */
	private Object getPorts(final IDockerContainer container) {
		if(container.ports().isEmpty()) {
			return "";
		} else if(container.ports().size() == 1) {
			return LabelProviderUtils.containerPortMappingToString(container.ports().get(0));
		} else {
			final List<String> ports = new ArrayList<>();
			for (IDockerPortMapping portMapping : container.ports()) {
				ports.add(LabelProviderUtils.containerPortMappingToString(portMapping));
			}
			return ports;
		}
	}

	/**
	 * @return the first name if the given {@link IDockerContainer} has only one, an empty {@link String} if the given list is empty, otherwise the given list itself.  
	 * @param container the {@link IDockerContainer} of elements to analyze
	 */
	private Object getNames(final IDockerContainer container) {
		if(container.names().isEmpty()) {
			return "";
		} else if(container.names().size() == 1) {
			return container.name();
		} else {
			return container.names();
		}
	}

	@Override
	public Object[] getChildren(final Object parentElement) {
		final Object propertyValue = ((Object[])parentElement)[1];
		final Object value = ((Object[])parentElement)[1];
		if(value instanceof List) {
			@SuppressWarnings("unchecked")
			final List<Object> propertyValues = (List<Object>)propertyValue;
			final Object[] result = new Object[propertyValues.size()];
			for (int i = 0; i < propertyValues.size(); i++) {
				result[i] = new Object[]{"", LabelProviderUtils.toString(propertyValues.get(i))};
			}
			return result;
		} else if(value instanceof Object[]) {
			return (Object[])value;
		}
		return new Object[]{value};
	}

	@Override
	public Object getParent(final Object element) {
		return null;
	}

	@Override
	public boolean hasChildren(final Object element) {
		if(element instanceof Object[]) {
			return !(((Object[])element)[1] instanceof String);
		}
		return false;
	}

}

Back to the top