summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor.ui/src/rba/tool/editor/ui/wizard/RBAModelNewProjectWizardEx.java
blob: 47cda5fa977142a90f0446e472f27a9051f51a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package rba.tool.editor.ui.wizard;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.internal.events.BuildCommand;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.xtext.ui.wizard.IProjectCreator;

import com.google.inject.Inject;

public class RBAModelNewProjectWizardEx extends RBAModelNewProjectWizard {

    private static final String DEFAULT_TEMPLATE_PATH = "RBATool\\Project"; //$NON-NLS-1$

    @Inject
    public RBAModelNewProjectWizardEx(IProjectCreator projectCreator) {
        super(projectCreator);
    }

    @Override
    public boolean performFinish() {

        boolean performFinish = super.performFinish();

        String projectName = getMainPage().getProjectName();
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

        try {
            setProjectSettings(project, projectName);
            copyResources(project);

            String pathStr = project.getName() + "/model";
            IFolder ifolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(pathStr));

            if (ifolder.exists()) {
                IResource[] members = ifolder.members();
                if (members.length > 0 && members[0] instanceof IFile) {
                    IFile ifile = (IFile) members[0];
                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
                    try {
                        IDE.openEditor(window.getActivePage(), ifile);
                    } catch (PartInitException e) {
                        e.printStackTrace();
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (CoreException e) {
            e.printStackTrace();
        }

        return performFinish;
    }

    @SuppressWarnings("restriction")
    private void setProjectSettings(IProject project, String projectName) throws CoreException {

        IProjectDescription description = ResourcesPlugin.getWorkspace().getRoot().getWorkspace().newProjectDescription(projectName);
        BuildCommand rbacommand = new BuildCommand();
        rbacommand.setBuilderName("rba.tool.RBAToolBuilder"); //$NON-NLS-1$
        BuildCommand xcommand = new BuildCommand();
        xcommand.setBuilderName("org.eclipse.xtext.ui.shared.xtextBuilder"); //$NON-NLS-1$
        ICommand[] commands = new BuildCommand[] { rbacommand, xcommand };
        description.setBuildSpec(commands);

        String[] natures = new String[] { "org.eclipse.xtext.ui.shared.xtextNature" }; //$NON-NLS-1$ //$NON-NLS-2$
        description.setNatureIds(natures);

        project.setDescription(description, new NullProgressMonitor());
    }

    protected void copyResources(IProject project) throws IOException, URISyntaxException, CoreException {
        URL installLocationUrl = Platform.getInstallLocation().getURL();
        String installLocationPath = new File(installLocationUrl.getPath()).toString();

        java.nio.file.Path runtimePath = Paths.get(installLocationPath, DEFAULT_TEMPLATE_PATH);

        URI locationURI = project.getLocationURI();

        File srcRuntime = new File(runtimePath.toString());
        File dst = new File(locationURI.getPath());

        if (srcRuntime.exists()) {
            FileUtils.copyDirectory(srcRuntime, dst);
        }

        project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    }
}