/**
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.eclipse.flow.common.editor.action;
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.drools.eclipse.flow.common.editor.GenericModelEditor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
/**
* Action for exporting an image of a RuleFlow.
*
* @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
*/
public class ExportImageAction extends ActionDelegate implements IEditorActionDelegate {
private IEditorPart editor;
public void run(IAction action) {
execute();
}
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
editor = targetEditor;
}
private void execute() {
ExportImageDialog dialog = new ExportImageDialog(editor.getSite().getWorkbenchWindow().getShell());
dialog.setOriginalFile(((IFileEditorInput) editor.getEditorInput()).getFile());
dialog.open();
IPath path = dialog.getResult();
if (path == null) {
return;
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IFile file = workspace.getRoot().getFile(path);
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
public void execute(final IProgressMonitor monitor)
throws CoreException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
((GenericModelEditor) editor).createImage(out, SWT.IMAGE_PNG);
file.create(new ByteArrayInputStream(out.toByteArray()), true, monitor);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
try {
new ProgressMonitorDialog(editor.getSite().getWorkbenchWindow().getShell()).run(false, true, op);
} catch (Exception e) {
e.printStackTrace();
}
}
}