Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62b27f20172b5df8255112fa8a8684ae1daf051f (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
/*******************************************************************************
 * 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.processes.ui.search;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.services.ServiceUtils;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.processes.core.model.interfaces.runtime.IRuntimeModel;
import org.eclipse.tcf.te.tcf.processes.ui.interfaces.IProcessMonitorUIDelegate;
import org.eclipse.tcf.te.tcf.processes.ui.navigator.runtime.LabelProviderDelegate;
import org.eclipse.tcf.te.tcf.processes.ui.nls.Messages;
import org.eclipse.tcf.te.ui.interfaces.ISearchable;
import org.eclipse.tcf.te.ui.utils.CompositeSearchable;

/**
 * The ISearchable adapter for a ProcessTreeNode which creates a UI for the user to
 * input the matching condition and returns a matcher to do the matching.
 */
public class ProcessSearchable extends CompositeSearchable {
	// The label provider used to get a text for a process.
	ILabelProvider labelProvider = new LabelProviderDelegate();

	/**
	 * Constructor
	 *
	 * @param node The peer model node context. Must not be <code>null</code>.
	 */
	public ProcessSearchable(IPeerNode peerNode) {
		super();

		IProcessMonitorUIDelegate delegate = ServiceUtils.getUIServiceDelegate(peerNode, peerNode, IProcessMonitorUIDelegate.class);
		ISearchable[] searchables = delegate != null ? delegate.getSearchables(peerNode) : null;
		if (searchables == null) {
			searchables = new ISearchable[] { new GeneralSearchable(), new ProcessUserSearchable(), new ProcessStateSearchable() };
		}
		setSearchables(searchables);
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#getSearchTitle(java.lang.Object)
	 */
	@Override
	public String getSearchTitle(final Object rootElement) {
		final AtomicReference<IPeerNode> node = new AtomicReference<IPeerNode>();

		if (rootElement instanceof IRuntimeModel) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					if (rootElement instanceof IRuntimeModel) {
						node.set(((IRuntimeModel)rootElement).getPeerNode());
					}
				}
			};

			if (Protocol.isDispatchThread()) runnable.run();
			else Protocol.invokeAndWait(runnable);
		}
		else if (rootElement != null) {
			node.set(rootElement instanceof IAdaptable ? (IPeerNode)((IAdaptable)rootElement).getAdapter(IPeerNode.class) : (IPeerNode)Platform.getAdapterManager().getAdapter(rootElement, IPeerNode.class));
		}

		String label = Messages.getStringDelegated(node.get(), "ProcessSearchable_SearchTitle"); //$NON-NLS-1$
	    return label != null ? label : Messages.ProcessSearchable_SearchTitle;
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#getSearchMessage(java.lang.Object)
	 */
	@Override
    public String getSearchMessage(final Object rootElement) {
		if (rootElement == null || rootElement instanceof IRuntimeModel) {
			final AtomicReference<IPeerNode> node = new AtomicReference<IPeerNode>();

			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					if (rootElement instanceof IRuntimeModel) {
						node.set(((IRuntimeModel)rootElement).getPeerNode());
					}
				}
			};

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

			String label = Messages.getStringDelegated(node.get(), "ProcessSearchable_PromptFindInProcessList"); //$NON-NLS-1$
			return label != null ? label : Messages.ProcessSearchable_PromptFindInProcessList;
		}

		IPeerNode node = rootElement instanceof IAdaptable ? (IPeerNode)((IAdaptable)rootElement).getAdapter(IPeerNode.class) : (IPeerNode)Platform.getAdapterManager().getAdapter(rootElement, IPeerNode.class);
		String label = Messages.getStringDelegated(node, "ProcessSearchable_PromptFindUnderProcess"); //$NON-NLS-1$
		String message = label != null ? label : Messages.ProcessSearchable_PromptFindUnderProcess;
		String rootName = "\"" + getElementName(rootElement) + "\""; //$NON-NLS-1$//$NON-NLS-2$
		message = NLS.bind(message, rootName);
		return message;
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#getCustomMessage(java.lang.Object, java.lang.String)
	 */
	@Override
	public String getCustomMessage(final Object rootElement, final String key) {
		final AtomicReference<IPeerNode> node = new AtomicReference<IPeerNode>();

		if (rootElement instanceof IRuntimeModel) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					if (rootElement instanceof IRuntimeModel) {
						node.set(((IRuntimeModel)rootElement).getPeerNode());
					}
				}
			};

			if (Protocol.isDispatchThread()) runnable.run();
			else Protocol.invokeAndWait(runnable);
		}
		else if (rootElement != null) {
			node.set(rootElement instanceof IAdaptable ? (IPeerNode)((IAdaptable)rootElement).getAdapter(IPeerNode.class) : (IPeerNode)Platform.getAdapterManager().getAdapter(rootElement, IPeerNode.class));
		}

		String message = Messages.getStringDelegated(node.get(), key);
	    return message;
 	}

	/**
	 * Get a name representation for each process node.
	 *
	 * @param rootElement The root element whose name is being retrieved.
	 * @return The node's name.
	 */
	private String getElementName(final Object rootElement) {
		if (rootElement == null || rootElement instanceof IRuntimeModel) {
			final AtomicReference<IPeerNode> node = new AtomicReference<IPeerNode>();

			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					node.set(((IRuntimeModel)rootElement).getPeerNode());
				}
			};

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

			String label = Messages.getStringDelegated(node.get(), "ProcessSearchable_ProcessList"); //$NON-NLS-1$
			return label != null ? label : Messages.ProcessSearchable_ProcessList;
		}
		return labelProvider.getText(rootElement);
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.ISearchable#getElementText(java.lang.Object)
	 */
	@Override
    public String getElementText(Object element) {
	    return getElementName(element);
    }
}

Back to the top