Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Import/Export Perspectives
Import/Export Perspectives [message #1731944] Wed, 11 May 2016 08:33 Go to next message
Alexander Bunkowski is currently offline Alexander BunkowskiFriend
Messages: 29
Registered: February 2014
Junior Member
Hey,

is there any chance to import and export single perspectives as a String?
Looking at the application e4xmi, it should be not that difficult since it is already defined as text there:

<children xsi:type="advanced:Perspective" xmi:id="_8uNpQI2KEeO1LthTjtmapA" elementId="perspectiveId" label="testPerspective">
          <children xsi:type="basic:Part" xmi:id="_Dph1kI2LEeO1LthTjtmapA" elementId="partId" contributionURI="uriHere" label="testPart">
          </children>
</children>


Anyone done something like that or has an idea how to accomplish that?
Re: Import/Export Perspectives [message #1731976 is a reply to message #1731944] Wed, 11 May 2016 11:43 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
Since the application model is an ecore model (EMF), you would need to know how to do this using ecore/EMF facilities.
Try searching for "EMF serialize (deserialze) model".
Re: Import/Export Perspectives [message #1732057 is a reply to message #1731976] Thu, 12 May 2016 10:00 Go to previous message
Alexander Bunkowski is currently offline Alexander BunkowskiFriend
Messages: 29
Registered: February 2014
Junior Member
Thx for the hint. I encountered two bugs in eclipse on the way but found workarounds.

Solution:
	public void exportPerspective(MPerspective activePerspective) {

		//clone the perspective and replace the placeholder ref with element ids of their content
		MPerspective clone = clonePerspectiveWithWorkaround(modelService, activePerspective);				
		List<MPlaceholder> placeholderClones = modelService.findElements(clone, null, MPlaceholder.class, null, EModelService.IN_ANY_PERSPECTIVE);
		for (MPlaceholder placeholder : placeholderClones) {
			MUIElement ref = placeholder.getRef();
			placeholder.getTags().add(0, ref.getElementId());
			placeholder.setRef(null);
		}

		
		Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		Map<String, Object> m = reg.getExtensionToFactoryMap();
		m.put("perspective", new E4XMIResourceFactory());
		ResourceSet resSet = new ResourceSetImpl();
		Resource resource = resSet.createResource(URI.createURI("perspectives/my.perspective"));
		resource.getContents().add((EObject) clone);

		try {
			resource.save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void importPerspective() {
		Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		Map<String, Object> m = reg.getExtensionToFactoryMap();
		m.put("perspective", new E4XMIResourceFactory());
		ResourceSet resSet = new ResourceSetImpl();
		Resource resource = resSet.getResource(URI.createURI("perspectives/my.perspective"), true);
		MPerspective perspective = (MPerspective) resource.getContents().get(0);

		List<MPlaceholder> placeholderClones = modelService.findElements(perspective, null, MPlaceholder.class, null, EModelService.IN_ANY_PERSPECTIVE);
		for (MPlaceholder placeholder : placeholderClones) {
			String id = placeholder.getTags().get(0);
			List<MPart> findElements = modelService.findElements(application, id, MPart.class, null);
			if (findElements.size() != 1) {
				throw new IllegalStateException(placeholder.getElementId() + " does not define id of its content");
			} else{
				placeholder.setRef(findElements.get(0));
			}
		}

		MSnippetContainer snippetContainer = new MSnippetContainer() {
			@Override
			public List<MUIElement> getSnippets() {
				List<MUIElement> snippets = new ArrayList<MUIElement>();
				snippets.add(perspective);
				return snippets;
			}
		};
		MPerspective cloneSnippet = (MPerspective) modelService.cloneSnippet(snippetContainer, perspective.getElementId(), window);

		orphanPerspectiveWorkaround(cloneSnippet, cloneSnippet.getLocalizedLabel());

		perspectives.add(cloneSnippet);
		switchPerspective(cloneSnippet);
	}



The workarounds for known bugs:
	/**
	 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=481996
	 * Set the references of the placeholder to the corresponding parts again in the copy.
	 */
	private static MPerspective clonePerspectiveWithWorkaround(EModelService modelService, MPerspective original) {
		MPerspective clone = (MPerspective) modelService.cloneElement(original, null);
		clone.setElementId(original.getElementId());

		List<MPlaceholder> placeholderClones = modelService.findElements(clone, null, MPlaceholder.class, null, EModelService.IN_ANY_PERSPECTIVE);
		// For each placeholder in the new perspective, set the reference value to the one from the old perspective
		for (MPlaceholder placeholderClone : placeholderClones) {
			// Search for the corresponding placeholder in the "old" perspective
			List<MPlaceholder> placeholderOriginal = modelService.findElements(original, placeholderClone.getElementId(), MPlaceholder.class, null,
					EModelService.IN_ANY_PERSPECTIVE);
			if (placeholderOriginal.size() == 1) {
				// We found only one corresponding placeholder element. Set reference of old element to the new element
				placeholderClone.setRef((placeholderOriginal.get(0).getRef()));
			} else if (placeholderOriginal.isEmpty()) {
				logger.error(Messages.PerspectiveController_err_no_placeholder);
			} else {
				logger.error(Messages.PerspectiveController_err_more_than_one_placeholder + " " + placeholderOriginal.toString()); //$NON-NLS-1$
				placeholderClone.setRef((placeholderOriginal.get(0).getRef()));
			}
		}
		return clone;
	}

	/**
	 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=489816
	 * 
	 * Fix orphan perspectives
	 */
	@SuppressWarnings("restriction")
	private static void orphanPerspectiveWorkaround(MPerspective newPerspective, String newPerspectiveName) {
		PerspectiveRegistry reg = (PerspectiveRegistry) PlatformUI.getWorkbench().getPerspectiveRegistry();
		String preLabel = newPerspective.getElementId().substring(0, newPerspective.getElementId().lastIndexOf(".")); //$NON-NLS-1$
		PerspectiveDescriptor pd = new PerspectiveDescriptor(preLabel, newPerspectiveName, null);
		pd = reg.createPerspective(preLabel, pd);
		newPerspective.setElementId(pd.getId());
	}

Previous Topic:Toolbar Contributions in a pure e4 app
Next Topic:placeholders and references to parts
Goto Forum:
  


Current Time: Thu Apr 18 08:33:25 GMT 2024

Powered by FUDForum. Page generated in 0.01468 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top