at.spardat.xma.guidesign.presentation.action.GenerateAction.java Source code

Java tutorial

Introduction

Here is the source code for at.spardat.xma.guidesign.presentation.action.GenerateAction.java

Source

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * 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:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * @(#)
 *
 *
 *
 *
 *
 */
package at.spardat.xma.guidesign.presentation.action;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import at.spardat.xma.guidesign.XMAComponent;
import at.spardat.xma.guidesign.plugin.GUIDesignerPlugin;
import at.spardat.xma.guidesign.presentation.GuidesignEditor;
import at.spardat.xma.guidesign.presentation.ValidationAndGenerationExecutor;
import at.spardat.xma.guidesign.util.ProjectClassLoaderFactory;

/**
 *
 *
 * @author s1462
 * @since guidesign.editor
 */
public class GenerateAction implements IEditorActionDelegate, IObjectActionDelegate {

    private IEditorPart editorPart;

    /**
     *
     */
    public GenerateAction() {
        super();
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
     */
    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
        editorPart = targetEditor;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    public void run(IAction action) {
        if (editorPart instanceof GuidesignEditor) {
            final GuidesignEditor editor = (GuidesignEditor) editorPart;
            final XMAComponent component = editor.getComponent();
            final IFile file = editor.getResourceFile();
            WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
                protected void execute(IProgressMonitor monitor) throws CoreException {
                    editor.doSave(monitor);
                    ProjectClassLoaderFactory.setEclipseProjectClassLoader(file.getProject());
                    ValidationAndGenerationExecutor.generate(file, component, monitor, true);
                }
            };
            try {
                new ProgressMonitorDialog(editor.getSite().getShell()).run(false, false, operation);
            } catch (Exception exc) {
                GUIDesignerPlugin.INSTANCE.log(exc);
                if (exc instanceof InvocationTargetException) {
                    Throwable cause = exc.getCause();
                    if (cause instanceof CoreException) {
                        ErrorDialog.openError(editorPart.getEditorSite().getShell(), "Error in generation!",
                                cause.getMessage(), ((CoreException) cause).getStatus());
                    } else {
                        MessageDialog.openError(editorPart.getEditorSite().getShell(), "Unexpected exception",
                                cause.getMessage());
                    }
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        if (targetPart instanceof IEditorPart) {
            editorPart = (IEditorPart) targetPart;
        }
    }

}