package at.bestsolution.efxclipse.tooling.converter;
import java.io.ByteArrayInputStream;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISources;
public abstract class AbstractConverterHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IEvaluationContext context = (IEvaluationContext) event.getApplicationContext();
ISelection s = (ISelection) context.getVariable(ISources.ACTIVE_MENU_SELECTION_NAME);
if( s instanceof IStructuredSelection ) {
Object o = ((IStructuredSelection) s).getFirstElement();
if( o instanceof IFile ) {
IFile f = (IFile) o;
String content = convert(f);
IContainer folder = f.getParent();
IFile outFile = folder.getFile(new Path(f.getName().substring(0,f.getName().length()-f.getFileExtension().length()-1)+".fxml"));
if( ! outFile.exists() ) {
try {
outFile.create(new ByteArrayInputStream(content.getBytes()), true, new NullProgressMonitor());
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
outFile.setContents(new ByteArrayInputStream(content.getBytes()), IResource.KEEP_HISTORY|IResource.FORCE, new NullProgressMonitor());
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
protected abstract String convert(IFile file) throws ExecutionException;
}