Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e75b8879e8a7f2034e7876a96945979cc1b92aea (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
/*******************************************************************************
 * Copyright (c) 2011, 2014 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.dialogs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.locator.interfaces.IPeerModelListener;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProperties;
import org.eclipse.tcf.te.tcf.locator.model.ModelManager;
import org.eclipse.tcf.te.tcf.ui.help.IContextHelpIds;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;

/**
 * Peer selection dialog implementation.
 */
public class PeerNodeSelectionDialog extends AbstractArraySelectionDialog implements IPeerModelListener {

	final String[] services;

	/**
	 * Constructor.
	 *
	 * @param shell The shell used to view the dialog, or <code>null</code>.
	 */
	public PeerNodeSelectionDialog(Shell shell) {
		this(shell, null);
	}

	/**
	 * Constructor.
	 *
	 * @param shell The shell used to view the dialog, or <code>null</code>.
	 */
	public PeerNodeSelectionDialog(Shell shell, String[] services) {
		super(shell, IContextHelpIds.PEER_NODE_SELECTION_DIALOG);

		this.services = services;

		ModelManager.getPeerModel().addListener(this);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTitleAreaDialog#dispose()
	 */
	@Override
	protected void dispose() {
	    super.dispose();
		ModelManager.getPeerModel().removeListener(this);
	}

	@Override
    protected Object[] getInput() {
		List<IPeerNode> peerNodes = new ArrayList<IPeerNode>();
	    for (final IPeerNode peerNode : ModelManager.getPeerModel().getPeerNodes()) {
	    	if (peerNode.isVisible() && (getType() == null || getType().equals(peerNode.getPeerType()))) {
	    		if (services != null && services.length > 0) {
	    			final AtomicBoolean hasServices = new AtomicBoolean();
	    			Protocol.invokeAndWait(new Runnable() {
						@Override
						public void run() {
			    			String offlineServices = peerNode.getStringProperty(IPeerNodeProperties.PROP_OFFLINE_SERVICES);
			    			String remoteServices = peerNode.getStringProperty(IPeerNodeProperties.PROP_REMOTE_SERVICES);
			    			List<String> offline = offlineServices != null ? Arrays.asList(offlineServices.split(",\\s*")) : Collections.EMPTY_LIST; //$NON-NLS-1$
			    			List<String> remote = remoteServices != null ? Arrays.asList(remoteServices.split(",\\s*")) : null; //$NON-NLS-1$
			    			boolean hasOfflineService = true;
			    			for (String service : services) {
				    			hasOfflineService &= (remote == null) ? offline.contains(service) : remote.contains(service);
				    			if (!hasOfflineService) {
				    				break;
				    			}
                            }
			    			hasServices.set(hasOfflineService);
						}
					});
	    			if (!hasServices.get()) {
	    				continue;
	    			}
	    		}
	    		peerNodes.add(peerNode);
	    	}
        }

	    return peerNodes.toArray();
	}

	/**
	 * Get the peer node type to filter.
	 * @return The peer type id or <code>null</code> for all peer nodes.
	 */
	protected String getType() {
		return null;
	}

	/**
	 * Returns the dialog title.
	 *
	 * @return The dialog title.
	 */
	@Override
    protected String getDialogTitle() {
		return Messages.PeerNodeSelectionDialog_dialogTitle;
	}

	/**
	 * Returns the title.
	 *
	 * @return The title.
	 */
	@Override
    protected String getTitle() {
		return Messages.PeerNodeSelectionDialog_title;
	}

	/**
	 * Returns the default message.
	 *
	 * @return The default message.
	 */
	@Override
    protected String getDefaultMessage() {
		return Messages.PeerNodeSelectionDialog_message;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.locator.interfaces.IPeerModelListener#modelChanged(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel, org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode, boolean)
	 */
    @Override
    public void modelChanged(IPeerModel model, IPeerNode peerNode, boolean added) {
    	refresh();
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.locator.interfaces.IPeerModelListener#modelDisposed(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel)
	 */
    @Override
    public void modelDisposed(IPeerModel model) {
    }
}

Back to the top