package es.iiia.sgi.handlers;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.services.IEvaluationService;
import es.iiia.sgi.ApplicationWorkbenchAdvisor;
import es.iiia.sgi.editors.RenderingEditor;
import es.iiia.sgi.views.RuleView;
import es.iiia.sgi.views.ShapeView;
import es.iiia.shapeeditor.ShapeGrammarInput;
import es.iiia.shapegrammar.grammar.ShapeGrammarParser;
import es.iiia.shapegrammar.model.ShapeGrammarModel;
public class LoadHandler extends AbstractHandler implements IHandler, PropertyChangeListener {
public static final String recentFilesKey = "recent_files.key";
public static final String fileMenuID = "mainMenuFile";
public static final String recentGrammarsMenuID = "recentGrammars";
public static final String recentGrammarsMenuLabel = "Recent Grammars";
protected IWorkbenchWindow workbenchWindow;
public static void addRecentFile(String path) {
// rebuild recent files
ApplicationWorkbenchAdvisor.tools.addRecentFile(path,
LoadHandler.fileMenuID,
LoadHandler.recentGrammarsMenuID,
LoadHandler.recentFilesKey,
LoadHandler.recentGrammarsMenuLabel,
new LoadHandler());
}
public Object execute(ExecutionEvent event) throws ExecutionException {
workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
boolean canContinue = workbenchWindow.getActivePage().closeAllEditors(
true);
if (canContinue) {
String path = openFileDialog(workbenchWindow);
if (path != null) {
// create input
ShapeGrammarModel model = initModel(path);
if (model != null) {
// load file
loadFile(workbenchWindow, model);
// rebuild recent files
addRecentFile(path, this);
}
}
}
return null;
}
public ShapeGrammarInput loadFile(IWorkbenchWindow workbenchWindow,
ShapeGrammarModel model) {
ShapeGrammarInput input = new ShapeGrammarInput(model);
// get views
ShapeView shapes = (ShapeView) workbenchWindow.getActivePage()
.findView(ShapeView.ID);
RuleView rules = (RuleView) workbenchWindow.getActivePage().findView(
RuleView.ID);
// set view content
shapes.setContent(input);
rules.setContent(input);
// recreate menu
IEvaluationService service = (IEvaluationService) PlatformUI
.getWorkbench().getService(IEvaluationService.class);
service.requestEvaluation("es.iiia.sgi.propertytesters.isFileOpen");
// open rendering editor
openRenderingEditor(workbenchWindow, input);
return input;
}
public static void addRecentFile(String path, LoadHandler handler) {
ApplicationWorkbenchAdvisor.tools.addRecentFile(path,
LoadHandler.fileMenuID,
LoadHandler.recentGrammarsMenuID,
LoadHandler.recentFilesKey,
LoadHandler.recentGrammarsMenuLabel,
handler);
}
public ShapeGrammarModel initModel(String path) {
ShapeGrammarParser parser = new ShapeGrammarParser(null);
String type = parser.getGrammarType(path);
if (type.length() > 0) {
MessageDialog.openError(workbenchWindow.getShell(), "Load Error",
"This is specific shape grammar. Please use proper loader");
return null;
}
return new ShapeGrammarModel(path);
}
private static String openFileDialog(IWorkbenchWindow workbenchWindow) {
FileDialog dialog = new FileDialog(workbenchWindow.getShell(), SWT.OPEN);
dialog.setText("Open grammar");
dialog.setFilterExtensions(new String[] { "*.xml" });
return dialog.open();
}
protected void openRenderingEditor(IWorkbenchWindow workbenchWindow,
ShapeGrammarInput input) {
try {
workbenchWindow.getActivePage().openEditor(input,
RenderingEditor.ID);
RenderingEditor.CURRENT_INPUT = input;
} catch (PartInitException e) {
e.printStackTrace();
}
}
public void propertyChange(PropertyChangeEvent evt) {
workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ShapeGrammarModel model = initModel(evt.getNewValue().toString());
if (model != null) {
this.loadFile(workbenchWindow, model);
}
}
}