Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5cd2d63390dc16648709acf08c01b375f9687c95 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.ui.navigator;

import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.ui.internal.ImageConsts;
import org.eclipse.tcf.te.tcf.ui.navigator.images.PeerImageDescriptor;
import org.eclipse.tcf.te.tcf.ui.navigator.nodes.PeerRedirectorGroupNode;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.jface.images.AbstractImageDescriptor;


/**
 * Label provider implementation.
 */
public class LabelProviderDelegate extends LabelProvider implements ILabelDecorator {

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
	 */
	@Override
	public String getText(Object element) {
		if (element instanceof IPeerModel) {
			String label = null;

			final IPeer peer = ((IPeerModel)element).getPeer();
			final String[] peerName = new String[1];
			if (Protocol.isDispatchThread()) {
				peerName[0] = peer.getName();
			} else {
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						peerName[0] = peer.getName();
					}
				});
			}
			label = peerName[0];

			if (label != null && !"".equals(label.trim())) { //$NON-NLS-1$
				return label;
			}
		} else if (element instanceof PeerRedirectorGroupNode) {
			return Messages.RemotePeerDiscoveryRootNode_label;
		}

		return ""; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
	 */
	@Override
	public Image getImage(final Object element) {
		if (element instanceof IPeerModel) {
			final AtomicBoolean isStatic = new AtomicBoolean();

			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					String value = ((IPeerModel)element).getPeer().getAttributes().get("static.transient"); //$NON-NLS-1$
					isStatic.set(value != null && Boolean.parseBoolean(value.trim()));
				}
			};

			if (Protocol.isDispatchThread()) runnable.run();
			else Protocol.invokeAndWait(runnable);

			return isStatic.get() ? UIPlugin.getImage(ImageConsts.PEER) : UIPlugin.getImage(ImageConsts.PEER_DISCOVERED);
		}
		if (element instanceof PeerRedirectorGroupNode) {
			return UIPlugin.getImage(ImageConsts.DISCOVERY_ROOT);
		}

		return super.getImage(element);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ILabelDecorator#decorateImage(org.eclipse.swt.graphics.Image, java.lang.Object)
	 */
	@Override
	public Image decorateImage(Image image, Object element) {
		Image decoratedImage = null;

		if (image != null && element instanceof IPeerModel) {
			AbstractImageDescriptor descriptor = new PeerImageDescriptor(UIPlugin.getDefault().getImageRegistry(),
			                                                             image,
			                                                             (IPeerModel)element);
			decoratedImage = UIPlugin.getSharedImage(descriptor);
		}

		return decoratedImage;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ILabelDecorator#decorateText(java.lang.String, java.lang.Object)
	 */
	@Override
	public String decorateText(final String text, final Object element) {
		if (element instanceof IPeerModel) {
			String label = text;

			final StringBuilder builder = new StringBuilder(label != null && !"".equals(label.trim()) ? label.trim() : "<noname>"); //$NON-NLS-1$ //$NON-NLS-2$

			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					IPeer peer = ((IPeerModel)element).getPeer();
					String dnsName = ((IPeerModel)element).getStringProperty("dns.name.transient"); //$NON-NLS-1$

					doDecorateText(builder, peer, dnsName);
				}
			};

			if (Protocol.isDispatchThread()) runnable.run();
			else Protocol.invokeAndWait(runnable);

			label = builder.toString();

			if (label != null && !"".equals(label.trim()) && !"<noname>".equals(label.trim())) { //$NON-NLS-1$ //$NON-NLS-2$
				return label;
			}
		}
		return null;
	}

	/**
	 * Decorate the text with some peer attributes.
	 * <p>
	 * <b>Note:</b> Must be called with the TCF event dispatch thread.
	 *
	 * @param builder The string builder to decorate. Must not be <code>null</code>.
	 * @param peer The peer. Must not be <code>null</code>.
	 * @param dnsName The peers DNS name or <code>null</code>.
	 */
	/* default */ void doDecorateText(StringBuilder builder, IPeer peer, String dnsName) {
		Assert.isNotNull(builder);
		Assert.isNotNull(peer);
		Assert.isTrue(Protocol.isDispatchThread());

		String ip = peer.getAttributes().get(IPeer.ATTR_IP_HOST);
		String port = peer.getAttributes().get(IPeer.ATTR_IP_PORT);

		if (ip != null && !"".equals(ip.trim())) { //$NON-NLS-1$
			builder.append(" ["); //$NON-NLS-1$
			builder.append(dnsName != null && !"".equals(dnsName.trim()) ? dnsName.trim() : ip.trim()); //$NON-NLS-1$

			if (port != null && !"".equals(port.trim()) && !"1534".equals(port.trim())) { //$NON-NLS-1$ //$NON-NLS-2$
				builder.append(":"); //$NON-NLS-1$
				builder.append(port.trim());
			}
			builder.append("]"); //$NON-NLS-1$
		}
	}
}

Back to the top