/******************************************************************************* | |
* Copyright (c) 2004-2008 Istvan Rath and Daniel Varro | |
* 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: | |
* Istvan Rath - initial API and implementation | |
*******************************************************************************/ | |
package org.eclipse.viatra2.treeeditor.actions; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.Iterator; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.widgets.FileDialog; | |
import org.eclipse.ui.ISharedImages; | |
import org.eclipse.ui.IWorkbenchPart; | |
import org.eclipse.ui.PlatformUI; | |
import org.eclipse.viatra2.core.IEntity; | |
import org.eclipse.viatra2.core.IModelElement; | |
import org.eclipse.viatra2.core.IModelSpace; | |
import org.eclipse.viatra2.exports.VTMLExporterSimple; | |
import org.eclipse.viatra2.treeeditor.ViatraTreeEditor; | |
public class ExportToVTMLAction extends ViatraTreeEditorSelectionAction { | |
public static final String ID = "ViatraTreeEditor.Actions.ExportToVTMLAction"; | |
private ViatraTreeEditor iVTE; | |
public ExportToVTMLAction(IWorkbenchPart part) { | |
super(part); | |
iVTE = (ViatraTreeEditor) part; | |
setId(ID); | |
setText("Export as VTML"); | |
setToolTipText("Export selected model element as VTML"); | |
setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() | |
.getImageDescriptor(ISharedImages.IMG_OBJ_FILE)); | |
} | |
protected boolean calculateEnabled() { | |
return true; | |
} | |
public void run() { | |
FileDialog fop = new FileDialog(iVTE.getEditorSite().getShell(), | |
SWT.SAVE); | |
fop.setText("Choose a file to export to"); | |
fop.setFilterNames(new String[] { "Viatra VTML files" }); | |
String fn = fop.open(); | |
if (fn != null) { | |
Iterator it = getSelectedObjects().iterator(); | |
FileWriter fw; | |
try { | |
fw = new FileWriter(fn, false); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
PrintWriter pw = new PrintWriter(fw); | |
IModelSpace ms = iVTE.getFramework().getTopmodel(); | |
while (it.hasNext()) { | |
try { | |
IModelElement me = (IModelElement) it.next(); | |
VTMLExporterSimple.exportToVTML(pw, ms, me); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
pw.close(); | |
try { | |
fw.close(); | |
} catch (IOException ioex) { | |
ioex.printStackTrace(); | |
} | |
iVTE.showMessage("Export to " + fn + " successful"); | |
} | |
} | |
@Override | |
public void updateSelf() { | |
if (getSelectedObjects().size() == 1 && getSelectedObjects().getFirstElement() instanceof IEntity) | |
setEnabled(true); | |
else | |
setEnabled(false); | |
} | |
} |