Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / NewTmfProjectWizard.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2011 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15 import java.net.URI;
16
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IProjectDescription;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.IWorkspaceRoot;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExecutableExtension;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.wizard.Wizard;
30 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
31 import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
32 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
34 import org.eclipse.ui.INewWizard;
35 import org.eclipse.ui.IWorkbench;
36 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
37
38 /**
39 * <b><u>NewTmfProjectWizard</u></b>
40 * <p>
41 */
42 public class NewTmfProjectWizard extends Wizard implements INewWizard, IExecutableExtension {
43
44 private final String fTtitle;
45 private final String fDescription;
46
47 protected NewTmfProjectMainWizardPage fMainPage;
48 protected String fProjectName;
49 protected URI fProjectLocation;
50 protected IConfigurationElement fConfigElement;
51
52 protected IProject fProject;
53
54 /**
55 *
56 */
57 public NewTmfProjectWizard() {
58 this(Messages.NewProjectWizard_DialogHeader, Messages.NewProjectWizard_DialogMessage);
59 }
60
61 /**
62 * @param title
63 * @param desc
64 */
65 public NewTmfProjectWizard(String title, String desc) {
66 super();
67 setDialogSettings(TmfUiPlugin.getDefault().getDialogSettings());
68 setNeedsProgressMonitor(true);
69 setForcePreviousAndNextButtons(true);
70 setWindowTitle(title);
71 fTtitle = title;
72 fDescription = desc;
73 }
74
75 /*
76 * (non-Javadoc)
77 *
78 * @see org.eclipse.jface.wizard.Wizard#addPages()
79 */
80 @Override
81 public void addPages() {
82 fMainPage = new NewTmfProjectMainWizardPage(Messages.NewProjectWizard_DialogHeader);
83 fMainPage.setTitle(fTtitle);
84 fMainPage.setDescription(fDescription);
85 addPage(fMainPage);
86 }
87
88 /*
89 * (non-Javadoc)
90 *
91 * @see org.eclipse.jface.wizard.Wizard#performCancel()
92 */
93 @Override
94 public boolean performCancel() {
95 return true;
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see org.eclipse.jface.wizard.Wizard#performFinish()
102 */
103 @Override
104 public boolean performFinish() {
105 fProjectName = fMainPage.getProjectName();
106 fProjectLocation = fMainPage.useDefaults() ? null : fMainPage.getLocationURI();
107 fProject = createProject(fProjectName, fProjectLocation, new NullProgressMonitor());
108 BasicNewProjectResourceWizard.updatePerspective(fConfigElement);
109 return true;
110 }
111
112 /**
113 * @param projectName
114 * @param projectLocation
115 * @param monitor
116 * @return
117 */
118 private IProject createProject(String projectName, URI projectLocation, IProgressMonitor monitor) {
119
120 IWorkspace workspace = ResourcesPlugin.getWorkspace();
121 IWorkspaceRoot root = workspace.getRoot();
122 IProject project = root.getProject(projectName);
123
124 try {
125 if (!project.exists()) {
126 IProjectDescription description = workspace.newProjectDescription(project.getName());
127 if (projectLocation != null)
128 description.setLocationURI(projectLocation);
129 project.create(description, monitor);
130 }
131
132 if (!project.isOpen())
133 project.open(monitor);
134
135 IProjectDescription description = project.getDescription();
136 description.setNatureIds(new String[] { TmfProjectNature.ID });
137 project.setDescription(description, null);
138
139 IFolder folder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
140 if (!folder.exists())
141 folder.create(true, true, null);
142
143 folder = project.getFolder(TmfExperimentFolder.EXPER_FOLDER_NAME);
144 if (!folder.exists())
145 folder.create(true, true, null);
146
147 return project;
148 } catch (CoreException e) {
149 e.printStackTrace();
150 }
151 return null;
152 }
153
154 // ------------------------------------------------------------------------
155 // INewWizard
156 // ------------------------------------------------------------------------
157
158 /* (non-Javadoc)
159 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
160 */
161 @Override
162 public void init(IWorkbench iworkbench, IStructuredSelection istructuredselection) {
163 }
164
165 // ------------------------------------------------------------------------
166 // IExecutableExtension
167 // ------------------------------------------------------------------------
168
169 @Override
170 public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
171 fConfigElement = config;
172 }
173
174 }
This page took 0.053221 seconds and 6 git commands to generate.