Search in sources :

Example 1 with WzrdDashboard

use of com.ge.research.osate.verdict.gui.WzrdDashboard in project VERDICT by ge-high-assurance.

the class WzrdHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    if (isRunningNow) {
        MessageDialog.openError(window.getShell(), "VERDICT Wizard Launcher", "Cannot launch multiple sessions of Wizard simultaneously. Aborting..");
        return null;
    } else {
        isRunningNow = true;
    }
    try {
        // set auto-refresh of eclipse editor "ON"
        IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode("org.eclipse.core.resources");
        prefs.putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, true);
        try {
            prefs.flush();
        } catch (Exception e) {
            System.out.println("Error in setting auto-refresh");
            System.out.println(e.getStackTrace());
        }
        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        // save the invoking .aadl editor if it has unsaved content
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorPart openEditor = page.getActiveEditor();
        if (openEditor != null) {
            boolean response = page.saveEditor(openEditor, true);
            if (!response) {
                isRunningNow = false;
                return null;
            } else if (openEditor.isDirty()) {
                MessageDialog.openError(window.getShell(), "VERDICT Wizard Launcher", "Cannot launch Wizard with unsaved content on the editor. Aborting..");
                isRunningNow = false;
                return null;
            }
        }
        // Skip the dashboard when Wizard is invoked from a valid text selection
        if (HandlerUtil.getCurrentSelection(event) instanceof TextSelection) {
            XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor();
            TextSelection ts = (TextSelection) xtextEditor.getSelectionProvider().getSelection();
            xtextEditor.getDocument().readOnly(resource -> {
                EObject e = new EObjectAtOffsetHelper().resolveContainedElementAt(resource, ts.getOffset());
                if (e instanceof SystemTypeImpl) {
                    SystemTypeImpl selectedSys = (SystemTypeImpl) e;
                    setModelPath(e);
                    StatementEditor editor = new StatementEditor(selectedSys, fileModel.getFullPath(), shell, window.getShell().getBounds(), "osate");
                    if (editor.isValid()) {
                        editor.run();
                    }
                } else {
                    MessageDialog.openError(window.getShell(), "VERDICT Wizard Launcher", "Selected object must be a SystemTypeImpl to launch cyber-property editor Wizard.");
                }
                return EcoreUtil.getURI(e);
            });
            isRunningNow = false;
            return null;
        }
        IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
        // Launch dashboard when invoked from a valid .aadl file
        if (selection.getFirstElement() instanceof IOutlineNode) {
            IOutlineNode node = (IOutlineNode) selection.getFirstElement();
            node.readOnly(state -> {
                EObject selectedObject = state;
                setModelPath(selectedObject);
                if (selectedObject instanceof SystemTypeImpl) {
                    SystemTypeImpl selectedSys = (SystemTypeImpl) selectedObject;
                    setModelPath(selectedObject);
                    StatementEditor editor = new StatementEditor(selectedSys, fileModel.getFullPath(), shell, window.getShell().getBounds(), "osate");
                    if (editor.isValid()) {
                        editor.run();
                    }
                } else {
                    MessageDialog.openError(window.getShell(), "VERDICT Wizard Launcher", "Selected object must be a SystemTypeImpl to launch Cyber Requirement Wizard. The currently selected element is:" + selectedObject.getClass().toString());
                }
                isRunningNow = false;
                return null;
            });
        } else if (selection.getFirstElement() instanceof IFile) {
            fileModel = (IFile) selection.getFirstElement();
            if (!fileModel.getFileExtension().equals("aadl")) {
                MessageDialog.openError(window.getShell(), "VERDICT Wizard Launcher", "Wizard can be launched from files only with .aadl extension.");
                return null;
            }
            IFile file = (IFile) selection.getFirstElement();
            URI uri = URI.createPlatformResourceURI(file.getFullPath().toPortableString(), true);
            ResourceSet rs = new ResourceSetImpl();
            Resource resource = rs.getResource(uri, true);
            try {
                resource.load(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // the following code loads all the cross-referred .aadl resources in the project
            // IFile fileRoot = WorkspaceSynchronizer.getFile(resource);
            // IResource parent = fileRoot.getParent();
            // IResource[] children = parent.getProject().members();
            // 
            // for (int i = 0; i < children.length; i++) {
            // if (children[i].getFileExtension().equals("aadl") && children[i] != file) {
            // URI uriXRefered = URI.createPlatformResourceURI(children[i].getFullPath().toPortableString(),
            // true);
            // Resource resourceXRefered = rs.getResource(uriXRefered, true);
            // System.out.println(
            // resourceXRefered.toString() + "----loading status: " + resourceXRefered.isLoaded());
            // }
            // }
            WzrdDashboard dashboard = new WzrdDashboard(resource, shell, fileModel.getFullPath());
            if (dashboard.isValid()) {
                dashboard.run();
            }
        }
        isRunningNow = false;
    } catch (Exception e) {
        System.out.println("Error in Wizard!!");
        e.printStackTrace();
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) TextSelection(org.eclipse.jface.text.TextSelection) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) Resource(org.eclipse.emf.ecore.resource.Resource) IEditorPart(org.eclipse.ui.IEditorPart) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) WzrdDashboard(com.ge.research.osate.verdict.gui.WzrdDashboard) URI(org.eclipse.emf.common.util.URI) ExecutionException(org.eclipse.core.commands.ExecutionException) SystemTypeImpl(org.osate.aadl2.impl.SystemTypeImpl) Shell(org.eclipse.swt.widgets.Shell) EObject(org.eclipse.emf.ecore.EObject) EObjectAtOffsetHelper(org.eclipse.xtext.resource.EObjectAtOffsetHelper) StatementEditor(com.ge.research.osate.verdict.gui.StatementEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Aggregations

StatementEditor (com.ge.research.osate.verdict.gui.StatementEditor)1 WzrdDashboard (com.ge.research.osate.verdict.gui.WzrdDashboard)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 TextSelection (org.eclipse.jface.text.TextSelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 Shell (org.eclipse.swt.widgets.Shell)1 IEditorPart (org.eclipse.ui.IEditorPart)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 EObjectAtOffsetHelper (org.eclipse.xtext.resource.EObjectAtOffsetHelper)1 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)1 IOutlineNode (org.eclipse.xtext.ui.editor.outline.IOutlineNode)1 SystemTypeImpl (org.osate.aadl2.impl.SystemTypeImpl)1