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
|
/*******************************************************************************
* 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.internal.adapters;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
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.concurrent.util.ExecutorsUtil;
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.tcf.core.peers.Peer;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelLookupService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelRefreshService;
import org.eclipse.tcf.te.tcf.locator.model.ModelManager;
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.IEditorSaveAsAdapter;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PlatformUI;
/**
* EditorSaveAsAdapter
*/
public class EditorSaveAsAdapter implements IEditorSaveAsAdapter {
/**
* Constructor.
*/
public EditorSaveAsAdapter() {
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.IEditorSaveAsAdapter#isSaveAsAllowed(org.eclipse.ui.IEditorInput)
*/
@Override
public boolean isSaveAsAllowed(IEditorInput input) {
IPeerNode peerNode = (IPeerNode)input.getAdapter(IPeerNode.class);
if (peerNode != null) return true;
return false;
}
/* (non-Javadoc)
* @see org.eclipse.tcf.te.ui.views.interfaces.IEditorSaveAsAdapter#doSaveAs(org.eclipse.ui.IEditorInput)
*/
@Override
public Object doSaveAs(IEditorInput input) {
IPeerNode model = (IPeerNode)input.getAdapter(IPeerNode.class);
if (model != null) {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
// Create the peer attributes
final Map<String, String> attrs = new HashMap<String, String>(model.getPeer().getAttributes());
attrs.put(IPeer.ATTR_ID, UUID.randomUUID().toString());
final List<String> usedNames = getUsedNameList();
String title = Messages.EditorSaveAsAdapter_title;
String prompt = Messages.EditorSaveAsAdapter_message;
String usedError = Messages.EditorSaveAsAdapter_nameInUse_error;
String label = Messages.EditorSaveAsAdapter_label;
RenameDialog dialog = new RenameDialog(shell, title, null, prompt, usedError, null, label, attrs.get(IPeer.ATTR_NAME), null, usedNames.toArray(new String[usedNames.size()]), null);
if (dialog.open() != Window.OK) {
return null;
}
attrs.put(IPeer.ATTR_NAME, dialog.getNewName());
attrs.remove(IPersistableNodeProperties.PROPERTY_URI);
try {
// Save the new peer
IURIPersistenceService persistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
if (persistenceService == null) {
throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
}
persistenceService.write(new Peer(attrs), null);
final AtomicReference<IPeerNode> newPeer = new AtomicReference<IPeerNode>();
final Callback cb = new Callback() {
@Override
protected void internalDone(Object caller, IStatus status) {
// Get the peer model node from the model and select it in the tree
IPeerNode peerNode = ModelManager.getPeerModel().getService(IPeerModelLookupService.class).lkupPeerModelById(attrs.get(IPeer.ATTR_ID));
newPeer.set(peerNode);
if (peerNode != null) {
// Refresh the viewer
ViewsUtil.refresh(IUIConstants.ID_EXPLORER);
// Create the selection
ISelection selection = new StructuredSelection(peerNode);
// Set the selection
ViewsUtil.setSelection(IUIConstants.ID_EXPLORER, selection);
// And open the properties on the selection
}
}
};
// Trigger a refresh of the model to read in the newly created static peer
Protocol.invokeLater(new Runnable() {
@Override
public void run() {
IPeerModelRefreshService service = ModelManager.getPeerModel().getService(IPeerModelRefreshService.class);
// Refresh the model now (must be executed within the TCF dispatch thread)
if (service != null) {
service.refresh(cb);
}
}
});
ExecutorsUtil.waitAndExecute(0, cb.getDoneConditionTester(null));
return newPeer.get();
} catch (IOException e) {
}
}
return null;
}
/**
* Initialize the used name list.
*/
protected List<String> getUsedNameList() {
final List<String> usedNames = new ArrayList<String>();
Runnable runnable = new Runnable() {
@Override
public void run() {
// Get all peer model objects
IPeerNode[] peers = ModelManager.getPeerModel().getPeerNodes();
// Loop them and find the ones which are of our handled types
for (IPeerNode peerNode : peers) {
String name = peerNode.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);
}
return usedNames;
}
}
|