|
|
Re: Import/Export Perspectives [message #1732057 is a reply to message #1731976] |
Thu, 12 May 2016 10:00 |
Alexander Bunkowski 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());
}
|
|
|
Powered by
FUDForum. Page generated in 0.03607 seconds