blob: 71037bacdc92da344c511b4923e77f2d9ad631aa [file] [log] [blame]
/*********************************************************************
* Copyright (c) 2005, 2019 SAP SE
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* SAP SE - initial API, implementation and documentation
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipse.graphiti.examples.common.handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.graphiti.examples.common.Messages;
import org.eclipse.graphiti.ui.editor.DiagramEditor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
/**
* Creates sample data
*/
public final class OpenDiagramEditorFromFileHandler extends AbstractHandler {
public Object execute(final ExecutionEvent event) throws ExecutionException {
// Get the current selection
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (!(selection instanceof IStructuredSelection)) {
return null;
}
// Operation must be started on IProject node -> cancel if not
Object first = ((IStructuredSelection) selection).getFirstElement();
if (!(first instanceof IProject)) {
return null;
}
// Ask for the file to open
FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
dialog.setFilterExtensions(new String[] { "*.diagram", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
dialog.setFilterIndex(0);
dialog.setText(Messages.OpenDiagramFromFileHandler_DialogTitle);
String fileName = dialog.open();
if (fileName == null) {
// Cancelled
return null;
}
// Create URI and editor input
URI fileURI = URI.createFileURI(fileName);
URIEditorInput editorInput = new URIEditorInput(fileURI);
// Open the editor
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(editorInput, DiagramEditor.DIAGRAM_EDITOR_ID);
} catch (PartInitException e) {
IStatus status = new Status(IStatus.ERROR, "org.eclipse.graphiti.examples.tutorial", e.getMessage(), e); //$NON-NLS-1$
ErrorDialog.openError(Display.getCurrent().getActiveShell(), Messages.OpenDiagramFromFileHandler_OpenEditorError,
e.getMessage(), status);
return null;
}
return null;
}
}