PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/emf-2.8.0/org.eclipse.emf.mapping/src/org/eclipse/emf/mapping/command/CreateMappingCommand.java

#
Java | 311 lines | 186 code | 34 blank | 91 comment | 23 complexity | 5e8a3803c5e2d196149e9a1b3ef7db25 MD5 | raw file
  1. /**
  2. * Copyright (c) 2002-2006 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 v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM - Initial API and implementation
  10. */
  11. package org.eclipse.emf.mapping.command;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.Collections;
  15. import org.eclipse.emf.common.command.AbstractCommand;
  16. import org.eclipse.emf.common.command.Command;
  17. import org.eclipse.emf.common.command.StrictCompoundCommand;
  18. import org.eclipse.emf.edit.command.CommandParameter;
  19. import org.eclipse.emf.mapping.Mapping;
  20. import org.eclipse.emf.mapping.MappingPlugin;
  21. import org.eclipse.emf.mapping.domain.MappingDomain;
  22. /**
  23. * The create mapping command creates a new mapping in a {@link MappingDomain}
  24. * from a set of the domain's input and output objects.
  25. */
  26. public class CreateMappingCommand extends AbstractCommand
  27. {
  28. /**
  29. * @deprecated - use MappingDomain.ENABLE_MULTIPLE_INPUTS
  30. */
  31. @Deprecated
  32. public static final int ENABLE_MULTIPLE_INPUTS = 0x0001;
  33. /**
  34. * @deprecated - use MappingDomain.ENABLE_MULTIPLE_OUTPUTS
  35. */
  36. @Deprecated
  37. public static final int ENABLE_MULTIPLE_OUTPUTS = 0x0002;
  38. /**
  39. * @deprecated - use MappingDomain.ENABLE_MULTIPLE_INPUT_MAPPINGS
  40. */
  41. @Deprecated
  42. public static final int ENABLE_MAPPED_INPUTS = 0x0004;
  43. /**
  44. * @deprecated - use MappingDomain.ENABLE_MULTIPLE_OUTPUT_MAPPINGS
  45. */
  46. @Deprecated
  47. public static final int ENABLE_MAPPED_OUTPUTS = 0x0008;
  48. /**
  49. * @deprecated - use MappingDomain.ENABLE_INCOMPATIBLE_METAOBJECTS
  50. */
  51. @Deprecated
  52. public static final int ENABLE_INCOMPATIBLE_METAOBJECTS = 0x0010;
  53. /**
  54. * @deprecated - use MappingDomain.ENABLE_INCOMPATIBLE_TYPE_CLASSIFIERS
  55. */
  56. @Deprecated
  57. public static final int ENABLE_INCOMPATIBLE_TYPE_CLASSIFIERS = 0x0020;
  58. /**
  59. * @deprecated - use MappingDomain.ENABLE_EMPTY_INPUTS
  60. */
  61. @Deprecated
  62. public static final int ENABLE_EMPTY_INPUTS = 0x0040;
  63. /**
  64. * @deprecated - use MappingDomain.ENABLE_EMPTY_OUTPUTS
  65. */
  66. @Deprecated
  67. public static final int ENABLE_EMPTY_OUTPUTS = 0x0080;
  68. /**
  69. * @deprecated - use MappingDomain.ENABLE_UNMAPPED_PARENTS
  70. */
  71. @Deprecated
  72. public static final int ENABLE_UNMAPPED_PARENTS = 0x0100;
  73. /**
  74. * @deprecated - use MappingDomain.ENABLE_ALL
  75. */
  76. @Deprecated
  77. public static final int ENABLE_ALL = 0xFFFF;
  78. /**
  79. * This creates a command that creates a new mapping involving the given domain's collection of input and output objects.
  80. */
  81. public static Command create(MappingDomain domain, Collection<?> collection)
  82. {
  83. return
  84. domain.createCommand
  85. (CreateMappingCommand.class,
  86. new CommandParameter(domain.getMappingRoot(), null, collection));
  87. }
  88. /**
  89. * This creates a command that creates a new mapping between the given input and output.
  90. */
  91. public static Command create(MappingDomain domain, Object input, Object output)
  92. {
  93. Collection<Object> collection = new ArrayList<Object>();
  94. collection.add(input);
  95. collection.add(output);
  96. return create(domain, collection);
  97. }
  98. /**
  99. * This creates a command that creates a new mapping with the given collections of inputs and outputs.
  100. */
  101. public static Command create(MappingDomain domain, Collection<?> inputs, Collection<?> outputs)
  102. {
  103. Collection<Object> collection = new ArrayList<Object>();
  104. collection.addAll(inputs);
  105. collection.addAll(outputs);
  106. return create(domain, collection);
  107. }
  108. /**
  109. * This creates a command that creates a new mapping with the given collection of inputs and output.
  110. */
  111. public static Command create(MappingDomain domain, Collection<?> inputs, Object output)
  112. {
  113. Collection<Object> collection = new ArrayList<Object>();
  114. collection.addAll(inputs);
  115. collection.add(output);
  116. return create(domain, collection);
  117. }
  118. /**
  119. * This creates a command that creates a new mapping with the given input and collection of outputs.
  120. */
  121. public static Command create(MappingDomain domain, Object input, Collection<?> outputs)
  122. {
  123. Collection<Object> collection = new ArrayList<Object>();
  124. collection.add(input);
  125. collection.addAll(outputs);
  126. return create(domain, collection);
  127. }
  128. /**
  129. * This caches the label.
  130. */
  131. protected static final String LABEL = MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_label");
  132. /**
  133. * This cachaes the description.
  134. */
  135. protected static final String DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_description");
  136. /**
  137. * This keeps track of the mapping domain in which the command operates.
  138. */
  139. protected MappingDomain domain;
  140. /**
  141. * This keeps track of the input objects that are to be mapped.
  142. */
  143. protected Collection<?> inputs;
  144. /**
  145. * This keeps track of the output objects that are to be mapped.
  146. */
  147. protected Collection<?> outputs;
  148. /**
  149. * This is set during {@link #execute} to record the new mapping that is created.
  150. */
  151. protected Mapping newMapping;
  152. /**
  153. * This is set during {@link #execute} to record the command used to add the newly created mapping to the mapping root.
  154. */
  155. protected Command subcommand;
  156. /**
  157. * @deprecated
  158. */
  159. @Deprecated
  160. public CreateMappingCommand(MappingDomain domain, Collection<?> collection, int enablementFlags)
  161. {
  162. this(domain, collection);
  163. }
  164. /**
  165. * This creates a command that creates a new mapping involving the given domain's collection of input and output objects.
  166. */
  167. public CreateMappingCommand(MappingDomain domain, Collection<?> collection)
  168. {
  169. super(LABEL, DESCRIPTION);
  170. this.domain = domain;
  171. ArrayList<Object> newInputs = new ArrayList<Object>();
  172. ArrayList<Object> newOutputs = new ArrayList<Object>();
  173. inputs = newInputs;
  174. outputs = newOutputs;
  175. for (Object object : collection)
  176. {
  177. if (domain.getMappingRoot().isInputObject(object))
  178. {
  179. newInputs.add(object);
  180. }
  181. else if (domain.getMappingRoot().isOutputObject(object))
  182. {
  183. newOutputs.add(object);
  184. }
  185. else
  186. {
  187. inputs = outputs = null;
  188. break;
  189. }
  190. }
  191. }
  192. @Override
  193. protected boolean prepare()
  194. {
  195. boolean result =
  196. domain != null &&
  197. inputs != null &&
  198. outputs != null &&
  199. domain.getMappingRoot().canCreateMapping(inputs, outputs, null);
  200. return result;
  201. }
  202. public void execute()
  203. {
  204. newMapping = domain.getMappingRoot().createMapping(inputs, outputs);
  205. StrictCompoundCommand subcommands = new StrictCompoundCommand();
  206. subcommands.appendAndExecute(AddMappingCommand.create(domain, newMapping));
  207. subcommand = subcommands.unwrap();
  208. }
  209. @Override
  210. public void undo()
  211. {
  212. //domain.getMappingRoot().removeMapping(newMapping);
  213. subcommand.undo();
  214. }
  215. public void redo()
  216. {
  217. subcommand.redo();
  218. }
  219. @Override
  220. public Collection<?> getResult()
  221. {
  222. return Collections.singleton(newMapping);
  223. }
  224. @Override
  225. public Collection<?> getAffectedObjects()
  226. {
  227. return Collections.singleton(newMapping);
  228. }
  229. @Override
  230. public void dispose()
  231. {
  232. if (subcommand != null)
  233. {
  234. subcommand.dispose();
  235. }
  236. super.dispose();
  237. }
  238. @Override
  239. public String getLabel()
  240. {
  241. if (inputs == null || inputs.isEmpty() || outputs == null || outputs.isEmpty())
  242. {
  243. return MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_onesided_label");
  244. }
  245. else
  246. {
  247. return super.getLabel();
  248. }
  249. }
  250. @Override
  251. public String getDescription()
  252. {
  253. if (inputs == null || inputs.isEmpty() || outputs == null || outputs.isEmpty())
  254. {
  255. return MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_onesided_description");
  256. }
  257. else
  258. {
  259. return super.getDescription();
  260. }
  261. }
  262. /**
  263. * This gives an abbreviated name using this object's own class' name, without package qualification,
  264. * followed by a space separated list of <tt>field:value</tt> pairs.
  265. */
  266. @Override
  267. public String toString()
  268. {
  269. StringBuffer result = new StringBuffer(super.toString());
  270. result.append(" (domain: " + domain + ")");
  271. result.append(" (inputs: " + inputs + ")");
  272. result.append(" (outputs: " + outputs + ")");
  273. result.append(" (newMapping: " + newMapping + ")");
  274. result.append(" (subcommand: " + subcommand + ")");
  275. return result.toString();
  276. }
  277. }