br.ufscar.sas.ui.handlers.UI.java Source code

Java tutorial

Introduction

Here is the source code for br.ufscar.sas.ui.handlers.UI.java

Source

/*******************************************************************************
 * Copyright (c)  2016 Daniel San Martin Santibanez.
 *  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:
 *    Daniel San Martin Santibanez - initial API and implementation and/or initial documentation
 *  
 *******************************************************************************/
package br.ufscar.sas.ui.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import br.ufscar.sas.createKDM.CreateKDM;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class UI extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        // get workbench window
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        // set selection service
        ISelectionService service = window.getSelectionService();
        // set structured selection
        IStructuredSelection structured = (IStructuredSelection) service.getSelection();
        //Progress Monitor
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(window.getShell());
        //Reset Perspective
        window.getActivePage().resetPerspective();

        try {
            dialog.run(true, true, new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor) {
                    //Creates KDM instance
                    int totalUnitsOfWork = IProgressMonitor.UNKNOWN;
                    monitor.beginTask("Performing Reverse Engineering....", totalUnitsOfWork);
                    // Call Reverse Engineering
                    String javaProjectName = "";
                    String projectName = null;
                    if (structured != null) {
                        if (structured.getFirstElement() instanceof IJavaProject) {
                            IJavaProject jProject = (IJavaProject) structured.getFirstElement();
                            projectName = jProject.getElementName();
                        }
                    }

                    javaProjectName = projectName;
                    CreateKDM ck = new CreateKDM();
                    ck.createKDMFile(javaProjectName);
                    monitor.done();
                    try {
                        refreshProjects();
                    } catch (CoreException e) {
                        e.printStackTrace();
                    }
                }
            });

            //Show main View
            window.getActivePage().showView("MainView");
            IWorkbenchPage ip = window.getActivePage();
            IViewPart myView = ip.findView("org.eclipse.jdt.ui.SourceView");
            ip.hideView(myView);
            myView = ip.findView("org.eclipse.jdt.ui.JavadocView");
            ip.hideView(myView);
            myView = ip.findView("org.eclipse.ui.views.ProblemView");
            ip.hideView(myView);
            myView = ip.findView("org.eclipse.mylyn.tasks.ui.views.tasks");
            ip.hideView(myView);
            myView = ip.findView("org.eclipse.ui.views.ContentOutline");
            ip.hideView(myView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void refreshProjects() throws CoreException {
        for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
        }
    }

}