Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a7a05843efa13d34558495181e078a3525f5523c (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
 * Copyright (c) 2010 Martin Fluegge (Berlin, Germany) and others.
 * 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:
 *    Martin Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.dawn.codegen.util;

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.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * @author Martin Fluegge
 */
public class ProjectCreationHelper
{

  private String name;

  private String[] natures;

  public String getName()
  {
    return name;
  }

  public IProject getProject()
  {
    return ResourcesPlugin.getWorkspace().getRoot().getProject(name);
  }

  public IProject createProject() throws CoreException
  {
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
    // List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();

    // if (project.exists())
    // {
    // try
    // {
    // project.delete(true, null);
    // }
    // catch (Exception e)
    // {
    // System.out.println("Project could not be deleted!!!");
    // }
    // }
    if (!project.exists())
    {
      project.create(null);
    }

    if (!project.isOpen())
    {
      project.open(null);
    }

    IProjectDescription description = project.getDescription();
    description.setNatureIds(natures);
    project.setDescription(description, null);

    return project;
  }

  /**
   * @param path
   * @param javaProject
   * @throws JavaModelException
   */
  public static void addJarToBuildPath(String path, IJavaProject javaProject) throws JavaModelException
  {
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(new Path(path), null, null);
    ProjectCreationHelper.addToClasspath(javaProject, newLibraryEntry);
  }

  public static void addJarToBuildPath(IPath path, IJavaProject javaProject) throws JavaModelException
  {
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(path, null, null);
    ProjectCreationHelper.addToClasspath(javaProject, newLibraryEntry);
  }

  public static void addVariableEntryToBuildPath(IPath path, IJavaProject javaProject) throws JavaModelException
  {
    IClasspathEntry newLibraryEntry = JavaCore.newVariableEntry(path, null, null);
    ProjectCreationHelper.addToClasspath(javaProject, newLibraryEntry);
  }

  /**
   * adds all jar files in this folder to the build path. Searches only on level 1
   * 
   * @param folder
   *          the folder which contains the jar files
   * @param javaProject
   * @throws CoreException
   */
  public static void addAllJarsToBuildPath(IFolder folder, IJavaProject javaProject) throws CoreException
  {
    try
    {
      folder.refreshLocal(100, new NullProgressMonitor());
      for (IResource resource : folder.members())
      {

        if (resource instanceof IFile && resource.getRawLocation().toString().endsWith(".jar"))
        {
          addJarToBuildPath(resource.getRawLocation(), javaProject);
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * @param javaProject
   * @param newEntry
   * @throws JavaModelException
   */
  public final static void addToClasspath(IJavaProject javaProject, IClasspathEntry newEntry) throws JavaModelException
  {
    if (newEntry == null)
    {
      return;
    }
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);

    newEntries[oldEntries.length] = newEntry;
    javaProject.setRawClasspath(newEntries, null);
  }

  /**
   * @param javaProject
   * @param toBeRemoved
   * @throws JavaModelException
   */
  public final static void removeFromClasspath(IJavaProject javaProject, IPath toBeRemoved) throws JavaModelException
  {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();

    for (IClasspathEntry classpathEntry : oldEntries)
    {
      if (!classpathEntry.getPath().equals(toBeRemoved))
      {
        newEntries.add(classpathEntry);
      }
    }
    // IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    // System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);

    // newEntries[oldEntries.length] = newEntry;
    IClasspathEntry[] newEntriesArray = new IClasspathEntry[newEntries.size()];
    javaProject.setRawClasspath(newEntries.toArray(newEntriesArray), null);
  }

  /**
   * @return the newly created java project
   * @throws CoreException
   */
  public IJavaProject createJavaProject() throws CoreException
  {
    IProject project = createProject();
    return createJavaProject(project);
  }

  /**
   * Creates a JavaProject prom the given project
   * 
   * @param project
   * @return the created JavaProject
   */
  public IJavaProject createJavaProject(IProject project)
  {
    try
    {
      // addJavaNature(project);
      IJavaProject javaProject = JavaCore.create(project);
      if (!javaProject.isOpen())
      {
        javaProject.open(null);
      }
      createBinFolder(project, javaProject);
      clearSourcePath(javaProject);

      IClasspathEntry sourceFolder = createSourceFolder("src", project);

      addToClasspath(javaProject, sourceFolder);
      addJREContainerToProject(javaProject);
      return javaProject;
    }
    catch (CoreException e)
    {
      e.printStackTrace();
    }
    return null;

  }

  /**
   * @param javaProject
   * @throws JavaModelException
   */
  private void addJREContainerToProject(IJavaProject javaProject) throws JavaModelException
  {
    addToClasspath(javaProject, JavaRuntime.getDefaultJREContainerEntry());
  }

  /**
   * @param javaProject
   * @throws JavaModelException
   */
  private void clearSourcePath(IJavaProject javaProject) throws JavaModelException
  {
    javaProject.setRawClasspath(new IClasspathEntry[] {}, new NullProgressMonitor()); // clean classpath, means remove
                                                                                      // Project root from classpath
  }

  /**
   * @param project
   * @throws CoreException
   */
  public void addJavaNature(IProject project) throws CoreException
  {
    IProjectDescription description = project.getDescription();
    String[] natures = description.getNatureIds();
    String[] newNatures = new String[natures.length + 1];
    System.arraycopy(natures, 0, newNatures, 0, natures.length);
    newNatures[natures.length] = JavaCore.NATURE_ID;
    description.setNatureIds(newNatures);
    project.setDescription(description, new NullProgressMonitor());
  }

  /**
   * @param path
   * @param project
   * @return
   * @throws CoreException
   */
  private IClasspathEntry createSourceFolder(String path, IProject project) throws CoreException
  {
    IFolder srcFolder = project.getFolder(new Path(path));
    if (!srcFolder.exists())
    {
      srcFolder.create(false, true, null);
      IPath sourceFolderPath = srcFolder.getFullPath();
      IClasspathEntry entry = JavaCore.newSourceEntry(sourceFolderPath);

      return entry;
    }
    return null;
  }

  /**
   * @param path
   * @param project
   * @param javaProject
   * @return the created source folder
   * @throws CoreException
   */
  public IFolder createSourceFolder(String path, IProject project, IJavaProject javaProject) throws CoreException
  {
    IFolder srcFolder = project.getFolder(new Path(path));
    if (!srcFolder.exists())
    {
      srcFolder.create(false, true, null);
      IPath sourceFolderPath = srcFolder.getFullPath();
      IClasspathEntry entry = JavaCore.newSourceEntry(sourceFolderPath);
      addToClasspath(javaProject, entry);
      return srcFolder;
    }
    return null;
  }

  /**
   * @param project
   * @param javaProject
   * @throws CoreException
   * @throws JavaModelException
   */
  private void createBinFolder(IProject project, IJavaProject javaProject) throws CoreException, JavaModelException
  {
    createOutputFolder("bin", project, javaProject);
  }

  /**
   * @param project
   * @param javaProject
   * @throws CoreException
   * @throws JavaModelException
   */
  private void createOutputFolder(String path, IProject project, IJavaProject javaProject) throws CoreException,
      JavaModelException
  {
    try
    {
      IFolder binFolder = project.getFolder(new Path(path));
      if (!binFolder.exists())
      {
        binFolder.create(true, true, null);
        javaProject.setOutputLocation(binFolder.getFullPath(), null);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * @param name
   * @param project
   * @return the created folder
   */
  public IFolder createFolder(String name, IProject project)
  {
    IFolder folder = project.getFolder(name);

    if (!folder.exists())
    {
      try
      {
        folder.create(IResource.NONE, true, null);
      }
      catch (CoreException e)
      {
        e.printStackTrace();
      }
    }
    return folder;
  }

  public IFolder getFolder(String name, IProject project)
  {
    IFolder folder = project.getFolder(name);

    return folder;
  }

  /**
   * @param name
   * @param folder
   * @param content
   * @return the created file
   */
  public IFile createFile(String name, IFolder folder, String content)
  {
    IFile file = folder.getFile("web.xml");

    try
    {

      if (!file.exists())
      {
        byte[] bytes = content.getBytes();
        InputStream source = new ByteArrayInputStream(bytes);
        file.create(source, IResource.NONE, null);

      }
    }
    catch (CoreException e)
    {
      e.printStackTrace();
    }
    return file;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public String[] getNatures()
  {
    return natures;
  }

  public void setNatures(String[] natures)
  {
    this.natures = natures;
  }

  public ProjectCreationHelper()
  {
  }

  public static void refreshProject(IResource resource, IProgressMonitor monitor) throws InvocationTargetException,
      InterruptedException
  {
    IRunnableWithProgress op = new WorkspaceModifyOperation(null)
    {
      @Override
      protected void execute(IProgressMonitor monitor) throws CoreException, InterruptedException
      {
        try
        {
          IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
          root.refreshLocal(IResource.DEPTH_INFINITE, monitor);

        }
        catch (CoreException e)
        {

          e.printStackTrace();
        }
      }
    };
    op.run(monitor);
  }
}

Back to the top