|
Lines 9-24
Link Here
|
| 9 |
package org.eclipse.xtext.ui.util; |
9 |
package org.eclipse.xtext.ui.util; |
| 10 |
|
10 |
|
| 11 |
import java.io.ByteArrayInputStream; |
11 |
import java.io.ByteArrayInputStream; |
| 12 |
import java.io.IOException; |
|
|
| 13 |
import java.io.InputStream; |
12 |
import java.io.InputStream; |
| 14 |
import java.net.URL; |
|
|
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Iterator; |
13 |
import java.util.Iterator; |
| 17 |
import java.util.List; |
14 |
import java.util.List; |
| 18 |
import java.util.Set; |
15 |
import java.util.Set; |
| 19 |
|
16 |
|
| 20 |
import org.apache.log4j.Logger; |
17 |
import org.apache.log4j.Logger; |
| 21 |
import org.eclipse.core.resources.ICommand; |
|
|
| 22 |
import org.eclipse.core.resources.IContainer; |
18 |
import org.eclipse.core.resources.IContainer; |
| 23 |
import org.eclipse.core.resources.IFile; |
19 |
import org.eclipse.core.resources.IFile; |
| 24 |
import org.eclipse.core.resources.IFolder; |
20 |
import org.eclipse.core.resources.IFolder; |
|
Lines 27-84
Link Here
|
| 27 |
import org.eclipse.core.resources.IWorkspace; |
23 |
import org.eclipse.core.resources.IWorkspace; |
| 28 |
import org.eclipse.core.resources.ResourcesPlugin; |
24 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 29 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.core.runtime.CoreException; |
| 30 |
import org.eclipse.core.runtime.IPath; |
|
|
| 31 |
import org.eclipse.core.runtime.IProgressMonitor; |
26 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 32 |
import org.eclipse.core.runtime.NullProgressMonitor; |
27 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 33 |
import org.eclipse.core.runtime.Path; |
28 |
import org.eclipse.core.runtime.Path; |
| 34 |
import org.eclipse.core.runtime.Platform; |
|
|
| 35 |
import org.eclipse.core.runtime.SubMonitor; |
29 |
import org.eclipse.core.runtime.SubMonitor; |
| 36 |
import org.eclipse.jdt.core.IClasspathEntry; |
|
|
| 37 |
import org.eclipse.jdt.core.IJavaProject; |
| 38 |
import org.eclipse.jdt.core.JavaCore; |
| 39 |
import org.eclipse.jface.dialogs.MessageDialog; |
30 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 40 |
import org.eclipse.swt.widgets.Shell; |
31 |
import org.eclipse.swt.widgets.Shell; |
| 41 |
import org.eclipse.ui.IWorkbench; |
|
|
| 42 |
import org.eclipse.ui.IWorkbenchPage; |
32 |
import org.eclipse.ui.IWorkbenchPage; |
| 43 |
import org.eclipse.ui.IWorkingSet; |
|
|
| 44 |
import org.eclipse.ui.PartInitException; |
33 |
import org.eclipse.ui.PartInitException; |
| 45 |
import org.eclipse.ui.PlatformUI; |
34 |
import org.eclipse.ui.PlatformUI; |
| 46 |
import org.eclipse.ui.ide.IDE; |
35 |
import org.eclipse.ui.ide.IDE; |
| 47 |
|
36 |
|
| 48 |
/** |
37 |
/** |
| 49 |
* Represents a helper/util class to work with eclipse core resource |
38 |
* Represents a helper/util class to work with eclipse core resource abstractions for workspace,project,files and |
| 50 |
* abstractions for workspace,project,files and directories. For the most part |
39 |
* directories. For the most part this code was copied from the last version (4.3.1) of openArchitectureWare. |
| 51 |
* this code was copied from the last version (4.3.1) of openArchitectureWare. |
40 |
* |
| 52 |
* |
|
|
| 53 |
* @author Michael Clay |
41 |
* @author Michael Clay |
| 54 |
*/ |
42 |
*/ |
| 55 |
public class EclipseResourceUtil { |
43 |
public class EclipseResourceUtil { |
| 56 |
|
44 |
|
| 57 |
private static Logger logger = Logger.getLogger(EclipseResourceUtil.class); |
45 |
private static final Logger logger = Logger.getLogger(EclipseResourceUtil.class); |
| 58 |
|
46 |
|
| 59 |
public static final String ISO_8859_1 = "iso-8859-1"; |
47 |
public IProject createProject(final ProjectCreatorDescription projectCreatorDescription, |
| 60 |
|
48 |
final IProgressMonitor monitor, final Shell theShell) { |
| 61 |
public static IProject createProject(final String projectName, final IPath location, final List<String> srcFolders, |
|
|
| 62 |
final List<IProject> referencedProjects, final Set<String> requiredBundles, |
| 63 |
final List<String> exportedPackages, final List<String> importedPackages, final String activatorClassName, |
| 64 |
final IProgressMonitor monitor, final Shell theShell, String[] projectNatures, IWorkingSet[] workingSets, |
| 65 |
IWorkbench workbench) { |
| 66 |
|
49 |
|
| 67 |
IProject project = null; |
50 |
IProject project = null; |
| 68 |
SubMonitor subMonitor = SubMonitor.convert(monitor, 10); |
51 |
SubMonitor subMonitor = SubMonitor.convert(monitor, 10); |
| 69 |
try { |
52 |
try { |
| 70 |
subMonitor.subTask("Creating project " + projectName); |
53 |
subMonitor.subTask("Creating project " + projectCreatorDescription.getProjectName()); |
| 71 |
final IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
54 |
final IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 72 |
project = workspace.getRoot().getProject(projectName); |
55 |
project = workspace.getRoot().getProject(projectCreatorDescription.getProjectName()); |
| 73 |
|
|
|
| 74 |
// Clean up any old project information. |
56 |
// Clean up any old project information. |
| 75 |
if (project.exists()) { |
57 |
if (project.exists()) { |
| 76 |
final boolean[] result = new boolean[1]; |
58 |
final boolean[] result = new boolean[1]; |
| 77 |
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { |
59 |
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { |
| 78 |
public void run() { |
60 |
public void run() { |
| 79 |
result[0] = MessageDialog.openQuestion(theShell, "Do you want to overwrite the project " |
61 |
result[0] = MessageDialog.openQuestion(theShell, "Do you want to overwrite the project " |
| 80 |
+ projectName, "Note that everything inside the project '" + projectName |
62 |
+ projectCreatorDescription.getProjectName(), |
| 81 |
+ "' will be deleted if you confirm this dialog."); |
63 |
"Note that everything inside the project '" |
|
|
64 |
+ projectCreatorDescription.getProjectName() |
| 65 |
+ "' will be deleted if you confirm this dialog."); |
| 82 |
} |
66 |
} |
| 83 |
}); |
67 |
}); |
| 84 |
if (result[0]) { |
68 |
if (result[0]) { |
|
Lines 86-146
Link Here
|
| 86 |
} else |
70 |
} else |
| 87 |
return null; |
71 |
return null; |
| 88 |
} |
72 |
} |
| 89 |
|
73 |
IProjectDescription projectDescription = projectCreatorDescription.createProjectDescription(); |
| 90 |
final IJavaProject javaProject = JavaCore.create(project); |
|
|
| 91 |
final IProjectDescription projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription( |
| 92 |
projectName); |
| 93 |
if (location!=null && !Platform.getLocation().equals(location.removeLastSegments(1))) { |
| 94 |
projectDescription.setLocation(location); |
| 95 |
} |
| 96 |
project.create(projectDescription, subMonitor.newChild(1)); |
74 |
project.create(projectDescription, subMonitor.newChild(1)); |
| 97 |
final List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>(); |
|
|
| 98 |
if (referencedProjects.size() != 0) { |
| 99 |
projectDescription.setReferencedProjects(referencedProjects.toArray(new IProject[referencedProjects |
| 100 |
.size()])); |
| 101 |
for (final IProject referencedProject : referencedProjects) { |
| 102 |
final IClasspathEntry referencedProjectClasspathEntry = JavaCore.newProjectEntry(referencedProject |
| 103 |
.getFullPath()); |
| 104 |
classpathEntries.add(referencedProjectClasspathEntry); |
| 105 |
} |
| 106 |
} |
| 107 |
projectDescription.setNatureIds(projectNatures); |
| 108 |
|
| 109 |
final ICommand java = projectDescription.newCommand(); |
| 110 |
java.setBuilderName(JavaCore.BUILDER_ID); |
| 111 |
|
| 112 |
final ICommand manifest = projectDescription.newCommand(); |
| 113 |
manifest.setBuilderName("org.eclipse.pde.ManifestBuilder"); |
| 114 |
|
| 115 |
final ICommand schema = projectDescription.newCommand(); |
| 116 |
schema.setBuilderName("org.eclipse.pde.SchemaBuilder"); |
| 117 |
|
| 118 |
projectDescription.setBuildSpec(new ICommand[] { java, manifest, schema }); |
| 119 |
|
| 120 |
project.open(subMonitor.newChild(1)); |
75 |
project.open(subMonitor.newChild(1)); |
| 121 |
project.setDescription(projectDescription, subMonitor.newChild(1)); |
76 |
project.setDescription(projectDescription, subMonitor.newChild(1)); |
| 122 |
|
77 |
for (final String src : projectCreatorDescription.getSourceFolders()) { |
| 123 |
for (final String src : srcFolders) { |
|
|
| 124 |
final IFolder srcContainer = project.getFolder(src); |
78 |
final IFolder srcContainer = project.getFolder(src); |
| 125 |
if (!srcContainer.exists()) { |
79 |
if (!srcContainer.exists()) { |
| 126 |
srcContainer.create(false, true, subMonitor.newChild(1)); |
80 |
srcContainer.create(false, true, subMonitor.newChild(1)); |
| 127 |
} |
81 |
} |
| 128 |
final IClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(srcContainer.getFullPath()); |
|
|
| 129 |
classpathEntries.add(srcClasspathEntry); |
| 130 |
} |
82 |
} |
| 131 |
|
83 |
createManifest(projectCreatorDescription.getProjectName(), projectCreatorDescription.getRequiredBundles(), |
| 132 |
classpathEntries.add(JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER"))); |
84 |
projectCreatorDescription.getExportedPackages(), projectCreatorDescription.getImportedPackages(), |
| 133 |
classpathEntries.add(JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins"))); |
85 |
projectCreatorDescription.getActivatorClassName(), subMonitor.newChild(1), project); |
| 134 |
|
86 |
createBuildProps(subMonitor.newChild(1), project, projectCreatorDescription.getSourceFolders()); |
| 135 |
javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]), |
87 |
if (projectCreatorDescription.getWorkbench() != null && projectCreatorDescription.getWorkingSets() != null) |
| 136 |
subMonitor.newChild(1)); |
88 |
projectCreatorDescription.getWorkbench().getWorkingSetManager().addToWorkingSets(project, |
| 137 |
|
89 |
projectCreatorDescription.getWorkingSets()); |
| 138 |
javaProject.setOutputLocation(new Path("/" + projectName + "/bin"), subMonitor.newChild(1)); |
|
|
| 139 |
createManifest(projectName, requiredBundles, exportedPackages, importedPackages, activatorClassName, |
| 140 |
subMonitor.newChild(1), project); |
| 141 |
createBuildProps(subMonitor.newChild(1), project, srcFolders); |
| 142 |
if (workbench != null && workingSets != null) |
| 143 |
workbench.getWorkingSetManager().addToWorkingSets(project, workingSets); |
| 144 |
} catch (final Exception exception) { |
90 |
} catch (final Exception exception) { |
| 145 |
exception.printStackTrace(); |
91 |
exception.printStackTrace(); |
| 146 |
logger.error(exception); |
92 |
logger.error(exception); |
|
Lines 149-164
Link Here
|
| 149 |
} |
95 |
} |
| 150 |
return project; |
96 |
return project; |
| 151 |
} |
97 |
} |
| 152 |
|
|
|
| 153 |
public static IProject createProject(final String projectName, final List<String> srcFolders, |
| 154 |
final List<IProject> referencedProjects, final Set<String> requiredBundles, |
| 155 |
final List<String> exportedPackages, final List<String> importedPackages, final String activatorClassName, |
| 156 |
final IProgressMonitor progressMonitor, final Shell theShell, String[] projectNatures) { |
| 157 |
return createProject(projectName, null, srcFolders, referencedProjects, requiredBundles, exportedPackages, |
| 158 |
importedPackages, activatorClassName, progressMonitor, theShell, projectNatures, null, null); |
| 159 |
} |
| 160 |
|
98 |
|
| 161 |
public static IFile createFile(final String name, final IContainer container, final String content, |
99 |
protected IFile createFile(final String name, final IContainer container, final String content, |
| 162 |
final IProgressMonitor progressMonitor) { |
100 |
final IProgressMonitor progressMonitor) { |
| 163 |
final IFile file = container.getFile(new Path(name)); |
101 |
final IFile file = container.getFile(new Path(name)); |
| 164 |
assertExist(file.getParent()); |
102 |
assertExist(file.getParent()); |
|
Lines 179-185
Link Here
|
| 179 |
return file; |
117 |
return file; |
| 180 |
} |
118 |
} |
| 181 |
|
119 |
|
| 182 |
public static IFile createFile(final String name, final IContainer container, final String content, |
120 |
protected IFile createFile(final String name, final IContainer container, final String content, |
| 183 |
final String charSet, final IProgressMonitor progressMonitor) throws CoreException { |
121 |
final String charSet, final IProgressMonitor progressMonitor) throws CoreException { |
| 184 |
final IFile file = createFile(name, container, content, progressMonitor); |
122 |
final IFile file = createFile(name, container, content, progressMonitor); |
| 185 |
if (file != null && charSet != null) { |
123 |
if (file != null && charSet != null) { |
|
Lines 188-194
Link Here
|
| 188 |
return file; |
126 |
return file; |
| 189 |
} |
127 |
} |
| 190 |
|
128 |
|
| 191 |
private static void createBuildProps(final IProgressMonitor progressMonitor, final IProject project, |
129 |
protected void createBuildProps(final IProgressMonitor progressMonitor, final IProject project, |
| 192 |
final List<String> srcFolders) { |
130 |
final List<String> srcFolders) { |
| 193 |
final StringBuilder bpContent = new StringBuilder("source.. = "); |
131 |
final StringBuilder bpContent = new StringBuilder("source.. = "); |
| 194 |
for (final Iterator<String> iterator = srcFolders.iterator(); iterator.hasNext();) { |
132 |
for (final Iterator<String> iterator = srcFolders.iterator(); iterator.hasNext();) { |
|
Lines 205-211
Link Here
|
| 205 |
createFile("build.properties", project, bpContent.toString(), progressMonitor); |
143 |
createFile("build.properties", project, bpContent.toString(), progressMonitor); |
| 206 |
} |
144 |
} |
| 207 |
|
145 |
|
| 208 |
private static void createManifest(final String projectName, final Set<String> requiredBundles, |
146 |
protected void createManifest(final String projectName, final Set<String> requiredBundles, |
| 209 |
final List<String> exportedPackages, final List<String> importedPackages, final String activatorClassName, |
147 |
final List<String> exportedPackages, final List<String> importedPackages, final String activatorClassName, |
| 210 |
final IProgressMonitor progressMonitor, final IProject project) throws CoreException { |
148 |
final IProgressMonitor progressMonitor, final IProject project) throws CoreException { |
| 211 |
final StringBuilder mainContent = new StringBuilder("Manifest-Version: 1.0\n"); |
149 |
final StringBuilder mainContent = new StringBuilder("Manifest-Version: 1.0\n"); |
|
Lines 214-220
Link Here
|
| 214 |
mainContent.append("Bundle-Vendor: My Company\n"); |
152 |
mainContent.append("Bundle-Vendor: My Company\n"); |
| 215 |
mainContent.append("Bundle-Version: 1.0.0\n"); |
153 |
mainContent.append("Bundle-Version: 1.0.0\n"); |
| 216 |
mainContent.append("Bundle-SymbolicName: " + projectName.toLowerCase() + "; singleton:=true\n"); |
154 |
mainContent.append("Bundle-SymbolicName: " + projectName.toLowerCase() + "; singleton:=true\n"); |
| 217 |
// mainContent.append("Bundle-RequiredExecutionEnvironment: J2SE-1.5\n"); |
|
|
| 218 |
if (null != activatorClassName) { |
155 |
if (null != activatorClassName) { |
| 219 |
mainContent.append("Bundle-Activator: " + activatorClassName + "\n"); |
156 |
mainContent.append("Bundle-Activator: " + activatorClassName + "\n"); |
| 220 |
} |
157 |
} |
|
Lines 261-310
Link Here
|
| 261 |
} |
198 |
} |
| 262 |
} |
199 |
} |
| 263 |
|
200 |
|
| 264 |
/** |
201 |
protected void assertExist(final IContainer c) { |
| 265 |
* @param name |
|
|
| 266 |
* of the destination file |
| 267 |
* @param container |
| 268 |
* directory containing the the destination file |
| 269 |
* @param contentUrl |
| 270 |
* Url pointing to the src of the content |
| 271 |
* @param progressMonitor |
| 272 |
* used to interact with and show the user the current operation |
| 273 |
* status |
| 274 |
* @return |
| 275 |
*/ |
| 276 |
public static IFile createFile(final String name, final IContainer container, final URL contentUrl, |
| 277 |
final IProgressMonitor progressMonitor) { |
| 278 |
final IFile file = container.getFile(new Path(name)); |
| 279 |
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, 1); |
| 280 |
try { |
| 281 |
InputStream inputStream = null; |
| 282 |
try { |
| 283 |
inputStream = contentUrl.openStream(); |
| 284 |
if (file.exists()) { |
| 285 |
file.setContents(inputStream, true, true, subMonitor.newChild(1)); |
| 286 |
} else { |
| 287 |
file.create(inputStream, true, subMonitor.newChild(1)); |
| 288 |
} |
| 289 |
inputStream.close(); |
| 290 |
} catch (final Exception e) { |
| 291 |
logger.error(e); |
| 292 |
} finally { |
| 293 |
if (null != inputStream) { |
| 294 |
try { |
| 295 |
inputStream.close(); |
| 296 |
} catch (final IOException e) { |
| 297 |
logger.error(e); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} finally { |
| 302 |
subMonitor.done(); |
| 303 |
} |
| 304 |
return file; |
| 305 |
} |
| 306 |
|
| 307 |
private static void assertExist(final IContainer c) { |
| 308 |
if (!c.exists()) { |
202 |
if (!c.exists()) { |
| 309 |
if (!c.getParent().exists()) { |
203 |
if (!c.getParent().exists()) { |
| 310 |
assertExist(c.getParent()); |
204 |
assertExist(c.getParent()); |
|
Lines 319-325
Link Here
|
| 319 |
} |
213 |
} |
| 320 |
} |
214 |
} |
| 321 |
|
215 |
|
| 322 |
public static void openFileToEdit(final Shell s, final IFile file) { |
216 |
public void openFileToEdit(final Shell s, final IFile file) { |
| 323 |
s.getDisplay().asyncExec(new Runnable() { |
217 |
s.getDisplay().asyncExec(new Runnable() { |
| 324 |
public void run() { |
218 |
public void run() { |
| 325 |
final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
219 |
final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
|
Lines 331-352
Link Here
|
| 331 |
} |
225 |
} |
| 332 |
}); |
226 |
}); |
| 333 |
} |
227 |
} |
| 334 |
|
|
|
| 335 |
public static void createPackagesWithDummyClasses(IProject dslProject, String string, List<String> exportedPackages) |
| 336 |
throws CoreException { |
| 337 |
for (String string2 : exportedPackages) { |
| 338 |
IFolder folder = dslProject.getFolder(string + "/" + (string2.replace('.', '/'))); |
| 339 |
create(folder); |
| 340 |
IFile file = folder.getFile("Foo.java"); |
| 341 |
String contents = "package " + string2 + ";\nclass Foo {}"; |
| 342 |
file.create(new ByteArrayInputStream(contents.getBytes()), true, null); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
private static void create(IFolder folder) throws CoreException { |
| 347 |
if (!folder.getParent().exists()) |
| 348 |
create((IFolder) folder.getParent()); |
| 349 |
folder.create(true, true, null); |
| 350 |
} |
| 351 |
|
| 352 |
} |
228 |
} |