Debian Codegraph @

doc/org.eclipse.emf.cheatsheets/src/org/eclipse/emf/cheatsheets/actions/NewProjectAction.java

raw
1 /**
2 * Copyright (c) 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.cheatsheets.actions;
12
13 import java.lang.reflect.InvocationTargetException;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.ui.actions.WorkspaceModifyOperation;
26 import org.eclipse.ui.cheatsheets.ICheatSheetAction;
27 import org.eclipse.ui.cheatsheets.ICheatSheetManager;
28
29 import org.eclipse.emf.cheatsheets.CheatSheetsPlugin;
30 import org.eclipse.emf.common.util.BasicMonitor;
31
32
33 /**
34 * <p>Action that creates a new general project in workspace.</p>
35 * @since 2.2.0
36 */
37 public class NewProjectAction extends Action implements ICheatSheetAction
38 {
39 protected String projectName;
40
41 /**
42 * Execution of the action
43 * @param params Array of parameters - index 0: projectName
44 * @param manager Cheatsheet Manager
45 */
46 public void run(String[] params, ICheatSheetManager manager)
47 {
48 projectName = params[0];
49 run();
50 }
51
52 /**
53 * Run the action
54 */
55 @Override
56 public void run()
57 {
58 if (projectName != null)
59 {
60 WorkspaceModifyOperation operation = new WorkspaceModifyOperation()
61 {
62 @Override
63 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException
64 {
65 createProject(projectName, monitor);
66 }
67 };
68
69 try
70 {
71 runWithProgress(operation);
72 notifyResult(ResourcesPlugin.getWorkspace().getRoot().exists(new Path(projectName)));
73 return;
74 }
75 catch (Exception e)
76 {
77 CheatSheetsPlugin.INSTANCE.log(e);
78 }
79 }
80
81 notifyResult(false);
82 }
83
84 /**
85 * Run with progress
86 * @param runnable
87 * @throws InvocationTargetException
88 * @throws InterruptedException
89 */
90 protected void runWithProgress(IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException
91 {
92 new ProgressMonitorDialog(Display.getCurrent().getActiveShell()).run(false, false, runnable);
93 }
94
95 /**
96 * Creates a new project
97 * @param projectName Name of the project
98 * @return Project
99 * @throws CoreException
100 * @throws CoreException
101 */
102 protected IProject createProject(String projectName, IProgressMonitor monitor) throws CoreException
103 {
104 monitor.beginTask(CheatSheetsPlugin.INSTANCE.getString("_UI_CreateProject_message", new String []{ projectName }), 2);
105 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
106 IProject project = workspaceRoot.getProject(projectName);
107 if (!project.exists())
108 {
109 project.create(BasicMonitor.subProgress(monitor, 1));
110 }
111 project.open(BasicMonitor.subProgress(monitor, 1));
112 monitor.done();
113 return project;
114 }
115 }