Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af580427691980614edff3d8de14f5c53c755914 (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
244
245
246
/*******************************************************************************
 * Copyright (c) 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.handler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistableNodeProperties;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerUtil;
import org.eclipse.tcf.te.runtime.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.core.peers.Peer;
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.interfaces.services.IPeerModelRefreshService;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.tcf.locator.nodes.PeerRedirector;
import org.eclipse.tcf.te.tcf.ui.help.IContextHelpIds;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.dialogs.RenameDialog;
import org.eclipse.tcf.te.ui.views.ViewsUtil;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Rename handler implementation.
 */
public class RenameHandler extends AbstractHandler {
	// Remember the shell from the execution event
	private Shell shell = null;

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// Get the shell
		shell = HandlerUtil.getActiveShell(event);
		// Get the current selection
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		// Delete the selection
		if (selection != null) {
			rename(selection, new Callback() {
				@Override
				protected void internalDone(Object caller, IStatus status) {
					// Refresh the view
					ViewsUtil.refresh(IUIConstants.ID_EXPLORER);
				}
			});
		}
		// Reset the shell
		shell = null;

		return null;
	}

	/**
	 * Renames all elements from the given selection and invokes the
	 * given callback once done.
	 *
	 * @param selection The selection. Must not be <code>null</code>.
	 * @param callback The callback. Must not be <code>null</code>.
	 */
	public void rename(final ISelection selection, final ICallback callback) {
		Assert.isNotNull(selection);
		Assert.isNotNull(callback);

		// The callback needs to be invoked in any case. However, if called
		// from an asynchronous callback, set this flag to false.
		boolean invokeCallback = true;

		// The selection must be a structured selection and must not be empty
		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			Iterator<?> iterator = ((IStructuredSelection)selection).iterator();
			while (iterator.hasNext()) {
				Object element = iterator.next();
				Assert.isTrue(element instanceof IPeerNode);
				final IPeerNode node = (IPeerNode)element;

				RenameDialog dialog = createRenameDialog(shell, node);
				int ok = dialog.open();
				if (ok == Window.OK) {
					// Do the renaming.
					final String newName = dialog.getNewName();

					Runnable runnable = new Runnable() {
						@Override
						public void run() {
							if (newName != null && !"".equals(newName) && !newName.equals(node.getPeer().getName())) { //$NON-NLS-1$
								try {
									// Get the persistence service
									IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
									if (uRIPersistenceService == null) {
										throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
									}

									// To update the peer attributes, the peer needs to be recreated
									IPeer oldPeer = node.getPeer();
									// Create a write able copy of the peer attributes
									Map<String, String> attributes = new HashMap<String, String>(oldPeer.getAttributes());
									// Update the name
									attributes.put(IPeer.ATTR_NAME, newName);
									// Remove the persistence storage URI (if set)
									attributes.remove(IPersistableNodeProperties.PROPERTY_URI);
									// Create the new peer
									IPeer newPeer = oldPeer instanceof PeerRedirector ? new PeerRedirector(((PeerRedirector)oldPeer).getParent(), attributes) : new Peer(attributes);
									// Update the peer node instance (silently)
									boolean changed = node.setChangeEventsEnabled(false);
									node.setProperty(IPeerNodeProperties.PROP_INSTANCE, newPeer);
									if (changed) {
										node.setChangeEventsEnabled(true);
									}

									// Remove the old persisted peer
									uRIPersistenceService.delete(oldPeer, null);
									// Save the peer node to the new persistence storage
									uRIPersistenceService.write(newPeer, null);

									Protocol.invokeLater(new Runnable() {
										@Override
										public void run() {
											// Trigger a change event for the node
											node.fireChangeEvent("properties", null, node.getProperties()); //$NON-NLS-1$
										}
									});

								} catch (IOException e) {
									String template = NLS.bind(Messages.RenameHandler_error_renameFailed, Messages.PossibleCause);
									StatusHandlerUtil.handleStatus(StatusHelper.getStatus(e), selection, template,
													Messages.RenameHandler_error_title, IContextHelpIds.MESSAGE_RENAME_FAILED, this, null);
								}
							}
						}
					};

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

					// Trigger a refresh of the model
					invokeCallback = false;
					Protocol.invokeLater(new Runnable() {
						@Override
						public void run() {
							final IPeerModelRefreshService service = Model.getModel().getService(IPeerModelRefreshService.class);
							// Refresh the model now (must be executed within the TCF dispatch thread)
							if (service != null) {
								service.refresh(new Callback() {
									@Override
									protected void internalDone(Object caller, IStatus status) {
										// Invoke the callback
										callback.done(RenameHandler.this, Status.OK_STATUS);
									}
								});
							}
						}
					});
				}
			}
		}

		if (invokeCallback) {
			callback.done(this, Status.OK_STATUS);
		}
	}

	/**
	 * Create a renaming dialog for the specified peer model node.
	 *
	 * @param shell The parent shell or <code>null</code>
	 * @param node The peer model. Must not be <code>null</code>.
	 *
	 * @return The renaming dialog.
	 */
	private RenameDialog createRenameDialog(final Shell shell, final IPeerNode node) {
		Assert.isNotNull(node);

		final AtomicReference<String> name = new AtomicReference<String>();
		final List<String> usedNames = new ArrayList<String>();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				name.set(node.getPeer().getName());

				IPeerModel model = Model.getModel();
				Assert.isNotNull(model);
				IPeerNode[] peers = model.getPeerNodes();
				for (IPeerNode peer : peers) {
						String name = peer.getPeer().getName();
						Assert.isNotNull(name);
						if (!"".equals(name) && !usedNames.contains(name)) { //$NON-NLS-1$
							usedNames.add(name.trim().toUpperCase());
						}
				}
			}
		};

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

		String title = NLS.bind(Messages.RenameHandler_dialog_title, name.get());
		String prompt = Messages.RenameHandler_dialog_message;
		String usedError = Messages.RenameHandler_dialog_error_nameExist;
		String label = Messages.RenameHandler_dialog_promptNewName;

		return new RenameDialog(shell, null, title, prompt, usedError, null, label, name.get(), null, usedNames.toArray(new String[usedNames.size()]), null);
	}

}

Back to the top