Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/action/ControlAction.java

raw
1 /**
2 * Copyright (c) 2006-2012 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.action;
12
13
14 import java.util.Collection;
15 import java.util.List;
16
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.PlatformUI;
23
24 import org.eclipse.emf.common.command.Command;
25 import org.eclipse.emf.common.command.CommandWrapper;
26 import org.eclipse.emf.common.ui.dialogs.ResourceDialog;
27 import org.eclipse.emf.common.util.URI;
28 import org.eclipse.emf.ecore.EObject;
29 import org.eclipse.emf.ecore.resource.Resource;
30 import org.eclipse.emf.ecore.resource.ResourceSet;
31 import org.eclipse.emf.ecore.util.EcoreUtil;
32 import org.eclipse.emf.edit.command.AddCommand;
33 import org.eclipse.emf.edit.command.RemoveCommand;
34 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
35 import org.eclipse.emf.edit.domain.EditingDomain;
36 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
37 import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
38
39
40 /**
41 * A control action moves a contained object into a new resource or restores it to its containers' resource.
42 * It is implemented by creating a {@link AddCommand} or {@link RemoveCommand}.
43 * @since 2.2.0
44 */
45 public class ControlAction extends CommandActionHandler
46 {
47 protected IStructuredSelection selection = null;
48 protected EObject eObject = null;
49
50 public ControlAction(EditingDomain domain)
51 {
52 super(domain, EMFEditUIPlugin.INSTANCE.getString("_UI_Control_menu_item"));
53 }
54
55 public ControlAction()
56 {
57 this(null);
58 }
59
60 // We can create the RemoveCommand for an uncontrol, but we must defer creating an AddCommand
61 // to control until run(), when the user specifies a target resource.
62 //
63 @Override
64 public boolean updateSelection(IStructuredSelection selection)
65 {
66 this.selection = selection;
67 if (selection.size() != 1) return false;
68
69 Object object = AdapterFactoryEditingDomain.unwrap(selection.getFirstElement());
70 boolean result = domain.isControllable(object);
71 eObject = result ? (EObject)object : null;
72
73 if (!AdapterFactoryEditingDomain.isControlled(object))
74 {
75 setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Control_menu_item"));
76 setDescription("_UI_Control_menu_item_description");
77 command = null;
78 }
79 else
80 {
81 setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Uncontrol_menu_item"));
82 setDescription("_UI_Uncontrol_menu_item_description");
83 if (result)
84 {
85 command = new RemoveCommand(domain, eObject.eResource().getContents(), eObject);
86 command = new SelfAffectingCommand(EMFEditUIPlugin.INSTANCE.getString("_UI_UncontrolCommand_label"), command);
87 result = command.canExecute();
88 }
89 }
90 return result;
91 }
92
93 protected class SelfAffectingCommand extends CommandWrapper
94 {
95 SelfAffectingCommand(String label, Command command)
96 {
97 super(label, command);
98 }
99
100 @Override
101 public Collection<?> getResult()
102 {
103 return selection.toList();
104 }
105
106 @Override
107 public Collection<?> getAffectedObjects()
108 {
109 return selection.toList();
110 }
111 }
112
113 // For the control case, we need a dialog to ask for the URI and obtain the resource, then we create the command.
114 //
115 @Override
116 public void run()
117 {
118 if (command == null)
119 {
120 if (eObject == null) return;
121
122 Resource resource = getResource();
123 if (resource == null) return;
124
125 command = new AddCommand(domain, resource.getContents(), eObject);
126 command = new SelfAffectingCommand(EMFEditUIPlugin.INSTANCE.getString("_UI_ControlCommand_label"), command);
127 }
128
129 // Ensure that all proxies are resolved so that references into the controlled object will be saved to reference the new resource.
130 //
131 EcoreUtil.resolveAll(domain.getResourceSet());
132
133 super.run();
134 }
135
136 protected Resource getResource()
137 {
138 ControlResourceDialog dialog = new ControlResourceDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), domain, eObject.eResource());
139 dialog.open();
140 return dialog.getResource();
141 }
142
143 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
144 {
145 if (workbenchPart instanceof IEditingDomainProvider)
146 {
147 domain = ((IEditingDomainProvider)workbenchPart).getEditingDomain();
148 }
149 }
150
151 /**
152 * A save-type {@link ResourceDialog resource dialog} that attempts to create the specified resource and
153 * load it, if it already exists.
154 */
155 protected static class ControlResourceDialog extends ResourceDialog
156 {
157 protected EditingDomain domain;
158 protected Resource resource;
159 protected Resource currentResource;
160
161 public ControlResourceDialog(Shell parent, EditingDomain domain, Resource currentResource)
162 {
163 super(parent, EMFEditUIPlugin.INSTANCE.getString("_UI_ControlDialog_title"), SWT.SAVE, getContextURI(domain));
164 this.domain = domain;
165 this.currentResource = currentResource;
166 }
167
168 private static URI getContextURI(EditingDomain domain)
169 {
170 if (domain != null)
171 {
172 List<Resource> resources = domain.getResourceSet().getResources();
173 if (!resources.isEmpty())
174 {
175 return resources.get(0).getURI();
176 }
177 }
178 return null;
179 }
180
181 /**
182 * Creates and, if it already exists, loads the specified resource. This implementation verifies that a
183 * resource can be opened for that URI, that the resource is not the object's current container, and that
184 * it is not read-only in the editing domain. If there is an existing resource with that URI, it prompts
185 * before overriding or adding to it.
186 */
187 @Override
188 protected boolean processResources()
189 {
190 List<URI> uris = getURIs();
191 if (uris.isEmpty()) return false;
192
193 URI uri = uris.get(0);
194 ResourceSet resourceSet = domain.getResourceSet();
195 Resource resource = resourceSet.getResource(uri, false);
196 boolean resourceInSet = resource != null;
197
198 if (resource == currentResource)
199 {
200 MessageDialog.openError(getShell(), EMFEditUIPlugin.INSTANCE.getString("_UI_InvalidURI_label"), EMFEditUIPlugin.INSTANCE.getString("_WARN_AlreadyInResource"));
201 return false;
202 }
203 if (resource != null && domain.isReadOnly(resource))
204 {
205 MessageDialog.openError(getShell(), EMFEditUIPlugin.INSTANCE.getString("_UI_InvalidURI_label"), EMFEditUIPlugin.INSTANCE.getString("_WARN_ReadOnlyResource"));
206 return false;
207 }
208
209 boolean resourceExists = resourceSet.getURIConverter().exists(uri, null);
210
211 boolean resourceBad = false;
212 if (!resourceInSet)
213 {
214 resource = resourceSet.createResource(uri);
215 if (resource == null)
216 {
217 MessageDialog.openError(getShell(), EMFEditUIPlugin.INSTANCE.getString("_UI_InvalidURI_label"), EMFEditUIPlugin.INSTANCE.getString("_WARN_CannotCreateResource"));
218 return false;
219 }
220
221 if (resourceExists)
222 {
223 try
224 {
225 resource = resourceSet.getResource(uri, true);
226 }
227 catch (RuntimeException exception)
228 {
229 EMFEditUIPlugin.INSTANCE.log(exception);
230 resourceBad = resource.getContents().isEmpty();
231 }
232 }
233 }
234
235 boolean result = true;
236 if (resourceBad)
237 {
238 result = MessageDialog.openQuestion(getShell(), EMFEditUIPlugin.INSTANCE.getString("_UI_ExistingResource_label"), EMFEditUIPlugin.INSTANCE.getString("_WARN_ReplaceResource"));
239 }
240 else if (resourceExists)
241 {
242 result = MessageDialog.openQuestion(getShell(), EMFEditUIPlugin.INSTANCE.getString("_UI_ExistingResource_label"), EMFEditUIPlugin.INSTANCE.getString("_WARN_AddToResource"));
243 }
244
245 if (!result && !resourceInSet && resource != null)
246 {
247 resource.unload();
248 resourceSet.getResources().remove(resource);
249 }
250 else
251 {
252 this.resource = resource;
253 }
254 return result;
255 }
256
257 public Resource getResource()
258 {
259 return resource;
260 }
261 }
262 }