Accessing Models

RMP uses the EMF Transactions component to manage its models. This implies that every access to models in RMP must be performed according to the EMF transaction protocol. This protocol requires that read-only access to models be performed within a Runnable whose execution is controlled by EMF Transactions' TransactionalEditingDomain. The Runnable is executed by passing it to org.eclipse.emf.transaction.TransactionalEditingDomain.runExclusive() from any thread. The TransactionalEditingDomain used by RMP's UMLModeler is accessible from UMLModeler.getEditingDomain().

The following example demonstrates the use of the EMF Transaction protocol by opening a model using UMLModeler.openModel() and log its root model element.


	public void plugletmain(String[] args) {

		try {
			UMLModeler.getEditingDomain().runExclusive(new Runnable() {

				public void run() {

					Model model = null;
					try {
						model = UMLModeler.openModel(URI.createURI("platform:/resource/MyProject/model.emx"));
						out.println(model.getName());
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						if (model != null) {
							UMLModeler.closeModel(model);
						}
					}
				}
			});
		} catch (InterruptedException e) {
			out.println("The operation was interrupted");
		}
	}

Sometimes, it is useful to access the models that are already opened. This is accomplished using UMLModeler.getOpenedModels(). The following example prints the name of each opened models.


	public void plugletmain(String[] args) {

		try {
			UMLModeler.getEditingDomain().runExclusive(new Runnable() {

				public void run() {

					Collection models = UMLModeler.getOpenedModels();

					for (Iterator iter = models.iterator(); iter.hasNext();) {
						Model model = (Model) iter.next();
						out.println(model.getName());
					}
				}
			});
		} catch (InterruptedException e) {
			out.println("The operation was interrupted");
		}
	}


The UMLModeler class is UML-centric. getOpenedModels only returns the UML models that are loaded in the ResourceSet of the UML Modeler. Accessing non-UML models already opened in the UML Modeler can be achieved by leveraging its ResourceSet directly. The code snippet below demonstrates how to list the resources contained in the UML Modeler resource set and logs their root elements.


	public void plugletmain(String[] args) {

		try {
			UMLModeler.getEditingDomain().runExclusive(new Runnable() {

				public void run() {

					ResourceSet resourceSet = UMLModeler.getEditingDomain().getResourceSet();

					for (Iterator iter = resourceSet.getResources().iterator(); iter.hasNext();) {
						Resource resource = (Resource) iter.next();
						out.println(resource.getURI());

						for (Iterator iterRoots = resource.getContents().iterator(); iterRoots.hasNext();) {
							EObject eObject = (EObject) iterRoots.next();
							out.print("\t");
							out.println(eObject);
						}
					}
				}
			});
		} catch (InterruptedException e) {
			out.println("The operation was interrupted");
		}
	}

Another way of accessing models is through selection. UMLModeler provides a utility class, IUMLUIHelper, to access the selected UML elements. IUMLUIHelper.getSelectedElements() will return the selected UML elements from the Project Explorer or from the active diagram, whichever has focus. The pluglet code snippet below shows how to get the selection and log information about each selected object.


	public void plugletmain(String[] args) {

		try {
			UMLModeler.getEditingDomain().runExclusive(new Runnable() {

				public void run() {

					List selectedElements = UMLModeler.getUMLUIHelper().getSelectedElements();

					for (Iterator iter = selectedElements.iterator(); iter.hasNext();) {
						
						EObject eObject = (EObject) iter.next();
						String eClassName = eObject.eClass().getName();
						out.print(eClassName + " : ");

						if (eObject instanceof Diagram) {
							out.println(((Diagram) eObject).getName());

						} else if (eObject instanceof View) {
							View view = (View) eObject;
							String viewType = view.getType();
							if (viewType.trim().length() > 0) {
								out.print("(" + view.getType() + ")");
							}

							EObject element = view.getElement();
							if (null != element) {
								out.print(" of " + element);
							}
							out.println();

						} else if (eObject instanceof Element) {
							if (eObject instanceof NamedElement) {
								out.println(((NamedElement) eObject).getName());
							} else {
								out.println(eObject);
							}
						}
					}
				}
			});
		} catch (InterruptedException e) {
			out.println("The operation was interrupted");
		}
	}

It is possible to get the current selection from the Project Explorer even if it has not the focus by calling IUMLUIHelper.getSelectedElements(String viewId) and passing the view ID of the Project Explorer. Alternatively, to get the selected element in a diagram regardless of whether it has the focus, we can use IUMLUIHelper.getSelectedElements(Diagram diagram). The following example demonstrates how to get the selection from the active diagram regardless of whether it has the focus:


	public void plugletmain(String[] args) {

		try {
			UMLModeler.getEditingDomain().runExclusive(new Runnable() {

				public void run() {


					IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
							.getActiveEditor();
					IEditorInput editorInput = editorPart.getEditorInput();

					if (editorInput instanceof IDiagramEditorInput) {

						Diagram diagram = ((IDiagramEditorInput) editorInput).getDiagram();

						List selectedElements = UMLModeler.getUMLUIHelper().getSelectedElements(diagram);

						for (Iterator iter = selectedElements.iterator(); iter.hasNext();) {

							EObject eObject = (EObject) iter.next();
							String eClassName = eObject.eClass().getName();
							out.print(eClassName + " : ");

							if (eObject instanceof Diagram) {
								out.println(((Diagram) eObject).getName());

							} else if (eObject instanceof View) {
								View view = (View) eObject;
								String viewType = view.getType();
								if (viewType.trim().length() > 0) {
									out.print("(" + view.getType() + ")");
								}

								EObject element = view.getElement();
								if (null != element) {
									out.print(" of " + element);
								}
								out.println();

							} else if (eObject instanceof Element) {
								if (eObject instanceof NamedElement) {
									out.println(((NamedElement) eObject).getName());
								} else {
									out.println(eObject);
								}
							}
						}
					}
				}
			});
		} catch (InterruptedException e) {
			out.println("The operation was interrupted");
		}
	}


Legal notices