/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc.blockdiagram.editor.features.custom; | |
import java.io.IOException; | |
import java.util.Collections; | |
import org.eclipse.core.internal.resources.File; | |
import org.eclipse.core.resources.ResourcesPlugin; | |
import org.eclipse.core.runtime.IStatus; | |
import org.eclipse.core.runtime.Status; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.fmc.blockdiagram.editor.features.add.BlockDiagramAddFeature; | |
import org.eclipse.fmc.blockdiagram.editor.wizards.BlockDiagramFilePage; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.ICustomContext; | |
import org.eclipse.graphiti.features.context.impl.AddContext; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.Diagram; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal; | |
import org.eclipse.jface.dialogs.Dialog; | |
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; | |
import org.eclipse.ui.dialogs.ISelectionStatusValidator; | |
import org.eclipse.ui.model.WorkbenchContentProvider; | |
import org.eclipse.ui.model.WorkbenchLabelProvider; | |
/** | |
* This feature imports all shapes from another block diagram to the diagram. | |
* All elements are copied. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
@SuppressWarnings("restriction") | |
public class ImportBlockDiagramCustomFeature extends FMCCustomFeature { | |
/** | |
* Default constructor. | |
* | |
* @param featureProvider | |
* The feature provider | |
*/ | |
public ImportBlockDiagramCustomFeature(IFeatureProvider featureProvider) { | |
super(featureProvider); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.features.custom.ICustomFeature#execute(org.eclipse | |
* .graphiti.features.context.ICustomContext) | |
*/ | |
@Override | |
public void execute(ICustomContext context) { | |
String path = showImportDialog(); | |
if (path != null) { | |
PictogramElement pictogramElement = context.getPictogramElements()[0]; | |
Resource resource = context.getPictogramElements()[0].eResource(); | |
Resource diagResource = resource.getResourceSet().createResource( | |
URI.createPlatformResourceURI(path, true)); | |
try { | |
diagResource.load(Collections.EMPTY_MAP); | |
Diagram diagram = (Diagram) diagResource.getContents().get(0); | |
AddContext ctx = new AddContext(); | |
ctx.putProperty(BlockDiagramAddFeature.DIAGRAM_PROP, diagram); | |
ctx.setTargetContainer((ContainerShape) pictogramElement); | |
new BlockDiagramAddFeature(getFeatureProvider()).execute(ctx); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
/** | |
* Displays the import dialog. | |
* | |
* @return The full path to the selected diagram file. | |
*/ | |
private String showImportDialog() { | |
ElementTreeSelectionDialog fDialog = new ElementTreeSelectionDialog( | |
GraphitiUiInternal.getWorkbenchService().getShell(), | |
new WorkbenchLabelProvider(), new WorkbenchContentProvider()); | |
fDialog.setInput(ResourcesPlugin.getWorkspace().getRoot()); | |
fDialog.setAllowMultiple(false); | |
fDialog.setValidator(new ISelectionStatusValidator() { | |
@Override | |
public IStatus validate(Object[] selection) { | |
if (selection.length == 1 | |
&& selection[0] instanceof File | |
&& ((File) selection[0]).getFullPath().toOSString() | |
.endsWith(BlockDiagramFilePage.DIAGRAM_EXTENSION)) | |
return new Status(IStatus.OK, "Blockdiagram Editor", ""); | |
return new Status(IStatus.ERROR, "Blockdiagram", ""); | |
} | |
}); | |
if (fDialog.open() == Dialog.OK) { | |
return ((File) fDialog.getResult()[0]).getFullPath().toOSString(); | |
} else | |
return null; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.eclipse.graphiti.features.impl.AbstractFeature#getName() | |
*/ | |
@Override | |
public String getName() { | |
return "Import FMC Blockdiagram"; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.features.custom.AbstractCustomFeature#canExecute | |
* (org.eclipse.graphiti.features.context.ICustomContext) | |
*/ | |
@Override | |
public boolean canExecute(ICustomContext context) { | |
return context.getPictogramElements().length > 0 | |
&& context.getPictogramElements()[0] instanceof ContainerShape; | |
} | |
} |