001: /**
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */package com.bostechcorp.cbesb.ui.ide.customcomponent.wizard;
024:
025: import java.io.IOException;
026:
027: import java.lang.reflect.InvocationTargetException;
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.eclipse.core.resources.IContainer;
032:
033: import org.eclipse.core.resources.IFolder;
034: import org.eclipse.core.resources.IProject;
035: import org.eclipse.core.resources.IProjectDescription;
036: import org.eclipse.core.resources.IResource;
037:
038: import org.eclipse.core.runtime.CoreException;
039: import org.eclipse.core.runtime.IPath;
040: import org.eclipse.core.runtime.IProgressMonitor;
041: import org.eclipse.core.runtime.NullProgressMonitor;
042: import org.eclipse.core.runtime.Path;
043: import org.eclipse.core.runtime.Platform;
044: import org.eclipse.core.runtime.SubProgressMonitor;
045: import org.eclipse.jdt.core.IClasspathEntry;
046: import org.eclipse.jdt.core.IJavaProject;
047: import org.eclipse.jdt.core.IPackageFragment;
048: import org.eclipse.jdt.core.IPackageFragmentRoot;
049: import org.eclipse.jdt.core.JavaCore;
050: import org.eclipse.jdt.core.JavaModelException;
051: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
052: import org.eclipse.jdt.internal.ui.wizards.ClassPathDetector;
053: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
054: import org.eclipse.jdt.launching.JavaRuntime;
055: import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage;
056: import org.eclipse.jface.operation.IRunnableWithProgress;
057: import com.bostechcorp.cbesb.common.i18n.I18N;
058: import com.bostechcorp.cbesb.common.i18n.Messages;
059: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
060: import com.bostechcorp.cbesb.ui.ide.Activator;
061: import com.bostechcorp.cbesb.ui.ide.classpath.CBJBISAClasspathContainer;
062: import com.bostechcorp.cbesb.ui.ide.classpath.ClassPathContainerRepository;
063: import com.bostechcorp.cbesb.ui.ide.util.JavaProjectUtil;
064:
065: /**
066: * @author LPS
067: *
068: */
069: public class CBCCJavaPage extends JavaCapabilityConfigurationPage {
070: private IProgressMonitor monitor;
071: /** Description of the Field */
072: protected boolean fCanRemoveContent;
073:
074: /** Description of the Field */
075: protected IProject fCurrProject;
076:
077: /** Description of the Field */
078: protected IPath fCurrProjectLocation;
079:
080: /** Description of the Field */
081: protected CBCCProjectCreationWizardPage fMainPage;
082:
083: public static final String CUSTOM_NATURE = "com.bostechcorp.cbesb.ui.ide.customproject";
084:
085: public static String IDE_PACK = "com.bostechcorp.cbesb.ui.ide";
086:
087: public static String X12_BUILD_PACK = "cbesb.build/installDir/formats/x12";
088:
089: public static String HL7_BUILD_PACK = "cbesb.build/installDir/formats/hl7";
090:
091: public static final String WST_VALIDATION_BUILDER_ID = "org.eclipse.wst.validation.validationbuilder";
092:
093: private List super Projects = null;
094:
095: /**
096: *Constructor for the ProjectWizardPage object
097: *
098: * @param mainPage Description of the Parameter
099: */
100: public CBCCJavaPage(CBCCProjectCreationWizardPage mainPage) {
101: super ();
102: this .fMainPage = mainPage;
103: this .fCurrProjectLocation = null;
104: this .fCurrProject = null;
105: this .fCanRemoveContent = false;
106: setTitle(I18N.getString(Messages.CBCC_JAVA_PAGE_TITLE));
107: setDescription(I18N
108: .getString(Messages.CBCC_JAVA_PAGE_DESCRIPTION));
109: }
110:
111: /** Called from the wizard on cancel. */
112: public void performCancel() {
113: this .removeProject();
114: }
115:
116: /**
117: * Called from the wizard on finish.
118: *
119: * @param monitor Description of the Parameter
120: * @exception CoreException Description of the Exception
121: * @exception InterruptedException Description of the Exception
122: */
123: public void performFinish(IProgressMonitor monitor)
124: throws CoreException, InterruptedException {
125: try {
126: monitor
127: .beginTask(
128: NewWizardMessages.JavaProjectWizardSecondPage_operation_create,
129: 3);//$NON-NLS-1$
130: if (fCurrProject == null) {
131: updateProject(true, new SubProgressMonitor(monitor, 1));
132: }
133: configureJavaProject(new SubProgressMonitor(monitor, 2));
134: } catch (Exception ex) {
135: ex.printStackTrace();
136: } finally {
137: monitor.done();
138: fCurrProject = null;
139: }
140: }
141:
142: /**
143: * Sets the visible attribute of the ProjectWizardPage object
144: *
145: * @param visible The new visible value
146: */
147: public void setVisible(boolean visible) {
148: if (visible) {
149: this .changeToNewProject();
150: } else {
151: //this.removeProject();
152: }
153: super .setVisible(visible);
154: }
155:
156: /** Description of the Method */
157: protected void changeToNewProject() {
158: IProject newProjectHandle = fMainPage.getProjectHandle();
159: IPath newProjectLocation = fMainPage.getLocationPath();
160:
161: if (fMainPage.useDefaults()) {
162: fCanRemoveContent = !newProjectLocation.append(
163: fMainPage.getProjectName()).toFile().exists();
164: } else {
165: fCanRemoveContent = !newProjectLocation.toFile().exists();
166: }
167:
168: final boolean initialize = !(newProjectHandle
169: .equals(fCurrProject) && newProjectLocation
170: .equals(fCurrProjectLocation));
171:
172: IRunnableWithProgress op = new IRunnableWithProgress() {
173: public void run(IProgressMonitor monitor)
174: throws InvocationTargetException,
175: InterruptedException {
176: try {
177: updateProject(initialize, monitor);
178: } catch (CoreException e) {
179: throw new InvocationTargetException(e);
180: }
181: }
182: };
183:
184: try {
185: this .getContainer().run(false, true, op);
186: } catch (InvocationTargetException e) {
187: String title = NewWizardMessages.JavaProjectWizard_op_error_title;//$NON-NLS-1$
188: String message = NewWizardMessages.JavaProjectWizard_op_error_create_message;//$NON-NLS-1$
189: ExceptionHandler.handle(e, getShell(), title, message);
190: } catch (InterruptedException e) {//TODO
191: // cancel pressed
192: }
193: }
194:
195: /**
196: * Add necessary source and libraries
197: *
198: * @param entries
199: * @return
200: * @throws CoreException
201: */
202: protected IClasspathEntry[] checkEntries(IClasspathEntry[] entries)
203: throws CoreException {
204: // entries = JavaProjectUtil.mergeClasspathEntry(entries,
205: // JavaProjectUtil.createSourceClasspathEntry(fCurrProject, "src"));
206: entries = JavaProjectUtil.mergeClasspathEntry(entries,
207: JavaRuntime.getDefaultJREContainerEntry());
208: // entries = JavaProjectUtil.mergeClasspathEntry(entries,
209: // ClassPathContainerRepository.getInstance().getEntry(
210: // J2EE14ClasspathContainer.CLASSPATH_CONTAINER));
211: // entries = JavaProjectUtil.mergeClasspathEntry(entries,
212: // ClassPathContainerRepository.getInstance().getEntry(
213: // CBCustomComponentClasspathContainer.CLASSPATH_CONTAINER));
214:
215: entries = JavaProjectUtil.mergeClasspathEntry(entries,
216: ClassPathContainerRepository.getInstance().getEntry(
217: CBJBISAClasspathContainer.CLASSPATH_CONTAINER));
218:
219: entries = JavaProjectUtil.mergeClasspathEntry(entries, JavaCore
220: .newLibraryEntry(new Path(getLibraryBaseDir()
221: + "/wsdl4j-1.6.2.jar"), null, null));
222:
223: entries = JavaProjectUtil.mergeClasspathEntry(entries, JavaCore
224: .newLibraryEntry(new Path(getLibraryBaseDir()
225: + "/com.bostechcorp.cbesb.common.util.jar"),
226: null, null));
227:
228: entries = JavaProjectUtil.mergeClasspathEntry(entries, JavaCore
229: .newLibraryEntry(new Path(getLibraryBaseDir()
230: + "/com.bostechcorp.cbesb.common.util.jar"),
231: null, null));
232: entries = JavaProjectUtil
233: .mergeClasspathEntry(
234: entries,
235: JavaCore
236: .newLibraryEntry(
237: new Path(
238: getLibraryBaseDir()
239: + "/com.bostechcorp.cbesb.runtime.ccsl-base.jar"),
240: null, null));
241: entries = JavaProjectUtil
242: .mergeClasspathEntry(
243: entries,
244: JavaCore
245: .newLibraryEntry(
246: new Path(
247: getLibraryBaseDir()
248: + "/com.bostechcorp.cbesb.runtime.component.util.jar"),
249: null, null));
250: return entries;
251: }
252:
253: protected String[] getBuilders() {
254: return new String[] { WST_VALIDATION_BUILDER_ID };
255: }
256:
257: /** Description of the Method */
258: protected void removeProject() {
259: if (fCurrProject == null || !fCurrProject.exists()) {
260: return;
261: }
262:
263: IRunnableWithProgress op = new IRunnableWithProgress() {
264: public void run(IProgressMonitor monitor)
265: throws InvocationTargetException,
266: InterruptedException {
267: boolean noProgressMonitor = Platform.getLocation()
268: .equals(fCurrProjectLocation);
269: if (monitor == null || noProgressMonitor) {
270: monitor = new NullProgressMonitor();
271: }
272: monitor
273: .beginTask(
274: NewWizardMessages.JavaProjectWizardSecondPage_operation_remove,
275: 3);//$NON-NLS-1$
276:
277: try {
278: fCurrProject.delete(fCanRemoveContent, false,
279: monitor);
280: } catch (CoreException e) {
281: throw new InvocationTargetException(e);
282: } finally {
283: monitor.done();
284: fCurrProject = null;
285: fCanRemoveContent = false;
286: }
287: }
288: };
289:
290: try {
291: this .getContainer().run(false, true, op);
292: } catch (InvocationTargetException e) {
293: String title = NewWizardMessages.JavaProjectWizardSecondPage_error_remove_title;//$NON-NLS-1$
294: String message = NewWizardMessages.JavaProjectWizardSecondPage_error_remove_message;//$NON-NLS-1$
295: ExceptionHandler.handle(e, getShell(), title, message);
296: } catch (InterruptedException e) {//TODO
297: // cancel pressed
298: }
299: }
300:
301: /**
302: * Description of the Method
303: *
304: * @param initialize Description of the Parameter
305: * @param monitor Description of the Parameter
306: * @exception CoreException Description of the Exception
307: * @exception InterruptedException Description of the Exception
308: */
309: protected void updateProject(boolean initialize,
310: IProgressMonitor monitor) throws CoreException,
311: InterruptedException {
312: fCurrProject = fMainPage.getProjectHandle();
313: fCurrProjectLocation = fMainPage.getLocationPath();
314: boolean noProgressMonitor = !initialize && fCanRemoveContent;
315:
316: if (monitor == null || noProgressMonitor) {
317: monitor = new NullProgressMonitor();
318: }
319: try {
320: monitor
321: .beginTask(
322: NewWizardMessages.JavaProjectWizardSecondPage_operation_initialize,
323: 2);//$NON-NLS-1$
324:
325: createProject(fCurrProject, fCurrProjectLocation,
326: new SubProgressMonitor(monitor, 1));
327:
328: if (initialize) {
329: IClasspathEntry[] entries = null;
330: IPath outputPath = null;
331: IJavaProject javaProject = JavaCore
332: .create(fCurrProject);
333:
334: if (fCurrProjectLocation.toFile().exists()
335: && !Platform.getLocation().equals(
336: fCurrProjectLocation)) {
337: // detect classpath
338: if (!fCurrProject.getFile(".classpath").exists()//$NON-NLS-1$
339: ) {
340: // if .classpath exists noneed to look for files
341: ClassPathDetector detector = new ClassPathDetector(
342: fCurrProject.getProject(), monitor);
343: entries = detector.getClasspath();
344: outputPath = detector.getOutputLocation();
345: }
346: }
347:
348: entries = this .checkEntries(entries);
349: // make "bin" as output Location
350: IFolder outputFolder = javaProject.getProject()
351: .getFolder("/bin");
352: if (!outputFolder.exists()) {
353: outputFolder.create(true, true, null);
354: }
355: outputPath = outputFolder.getFullPath();
356:
357: ArrayList cpEntries = new ArrayList();
358: ArrayList srcEntries = customizePreDefinedResourceStructure(
359: fCurrProject,
360: new SubProgressMonitor(monitor, 1));
361: if (srcEntries != null)
362: cpEntries.addAll(srcEntries);
363: for (int i = 0; i < entries.length; i++) {
364: IClasspathEntry entry = entries[i];
365: cpEntries.add(entry);
366: }
367: IClasspathEntry[] cpe = (IClasspathEntry[]) cpEntries
368: .toArray(new IClasspathEntry[cpEntries.size()]);
369: //
370: init(javaProject, outputPath, cpe, false);
371: //
372: createFiles(fCurrProject);
373: fCurrProject.refreshLocal(2, new SubProgressMonitor(
374: monitor, 1));
375: }
376: monitor.worked(1);
377: } finally {
378: monitor.done();
379: }
380: }
381:
382: /*
383: * To customize your own page behavor
384: *
385: * @param monitor parent's monitor.
386: * @exception CoreException Description of the Exception
387: * @exception InterruptedException Description of the Exception
388: */
389: protected ArrayList customizePreDefinedResourceStructure(
390: IProject javaProject, IProgressMonitor monitor)
391: throws CoreException, InterruptedException {
392:
393: //
394: this .monitor = monitor;
395: ArrayList entries = new ArrayList();
396: createproject(javaProject, entries);
397: return entries;
398: }
399:
400: private void createproject(IProject project, ArrayList entries) {
401: try {
402: addSourceFolders(project, entries);
403: createInitialContent(project, monitor);
404: setJavaNature(project);
405: project.refreshLocal(2, new SubProgressMonitor(monitor, 1));
406: } catch (JavaModelException e) {
407: //"Problem when creating java project"
408: //ErrorDialog.openError(getShell(),
409: // I18N.getString(Messages.IDE_ERR_CREATE_PROJECT), null, e.getStatus());
410: e.printStackTrace();
411: } catch (CoreException e) {
412: //ErrorDialog.openError(getShell(),
413: // I18N.getString(Messages.IDE_ERR_CREATE_PROJECT), null, e.getStatus());
414: e.printStackTrace();
415: } catch (IOException e) {
416: //ErrorDialog.openError(getShell(),
417: // I18N.getString(Messages.IDE_ERR_CREATE_PROJECT), null, null);
418: e.printStackTrace();
419: }
420: }
421:
422: private void createFiles(IProject project) throws CoreException {
423: //Creting the packages
424: IJavaProject jProject = JavaCore.create(project);
425: IFolder folder = project.getFolder("/src/java");
426: IPackageFragmentRoot root = jProject
427: .getPackageFragmentRoot(folder);
428:
429: IPackageFragment name = root.createPackageFragment("com."
430: + getVendorPackageForm() + ".component."
431: + getNamePackageForm(), false, null);
432: IPackageFragment nameWsdl = root.createPackageFragment("com."
433: + getVendorPackageForm() + ".component."
434: + getNamePackageForm() + ".wsdl", false, null);
435: IPackageFragment nameUi = root.createPackageFragment("com."
436: + getVendorPackageForm() + ".component."
437: + getNamePackageForm() + ".ui", false, null);
438: IPackageFragment nameProcessors = root.createPackageFragment(
439: "com." + getVendorPackageForm() + ".component."
440: + getNamePackageForm() + ".processors", false,
441: null);
442:
443: try {
444: //Class in charge to create files after
445: //the directory structure was created
446: ProviderEditorWizardPage ppwp = (ProviderEditorWizardPage) getWizard()
447: .getPage("ProviderPage");
448: ConsumerEditorWizardPage cpwp = (ConsumerEditorWizardPage) getWizard()
449: .getPage("ConsumerPage");
450:
451: CBCCStructureGenerator cbccsg = new CBCCStructureGenerator(
452: project.getLocation(), name, nameProcessors,
453: nameWsdl, nameUi, fMainPage.getProperties(), cpwp
454: .getPages(), ppwp.getPages());
455: cbccsg.createAllFiles();
456: } catch (IOException e) {
457: e.printStackTrace();
458: } catch (Exception e) {
459: e.printStackTrace();
460: }
461:
462: }
463:
464: private String getNamePackageForm() {
465: return fMainPage.getProperties().get(
466: ComponentPropertiesComposite.NAME).trim().toLowerCase();
467: }
468:
469: private String getVendorPackageForm() {
470: String vendor = fMainPage.getProperties().get(
471: ComponentPropertiesComposite.VENDOR).toLowerCase();
472: while (vendor.startsWith(" ")) {
473: vendor = vendor.substring(1);
474: }
475: while (vendor.endsWith(" ")) {
476: vendor = vendor.substring(0, vendor.length() - 1);
477: }
478: vendor = vendor.replaceAll(" ", ".");
479: return vendor;
480: }
481:
482: private void addSourceFolders(IProject project, ArrayList entries)
483: throws JavaModelException, CoreException {
484: addSourceFolder(project, entries, "src/java");
485: addSourceFolder(project, entries, "src/resources");
486: //addSourceFolder(project, entries, "scripts");
487: }
488:
489: private void setJavaNature(IProject project) throws CoreException {
490: IProjectDescription description = project.getDescription();
491: description.setNatureIds(new String[] { CUSTOM_NATURE });
492: project.setDescription(description, null);
493: }
494:
495: protected void addSourceFolder(IProject project, ArrayList entries,
496: String srcPath) throws CoreException {
497: IFolder folder = project.getProject().getFolder(srcPath);
498: createFolder(folder);
499: //IPackageFragmentRoot root=javaProject.getPackageFragmentRoot(folder);
500: entries.add(JavaCore.newSourceEntry(project.getFullPath()
501: .append(srcPath)));
502: }
503:
504: /**
505: * Creating the Tree structure
506: * @param project
507: * @param monitor
508: * @throws CoreException
509: * @throws JavaModelException
510: * @throws IOException
511: */
512: protected void createInitialContent(IProject project,
513: IProgressMonitor monitor) throws CoreException,
514: JavaModelException, IOException {
515: // createJBIXMLfile(project);
516: //createFolder(project.getFolder("/src/java"));
517: //createFolder(project.getFolder("/src/resources"));
518: createFolder(project.getFolder("/src/resources/META-INF"));
519: createFolder(project.getFolder("/scripts"));
520: createFolder(project.getFolder("/lib/"));
521: createFolder(project.getFolder("/lib/generated"));
522: createFolder(project.getFolder("/lib/optional"));
523: // createFolder(project.getFolder("/src/java/ucm"));
524: //createBuildMapFile(project,monitor);
525: //createBuildUcmFile(project,monitor);
526: //createBuildMdlFile(project,monitor);
527: //createBuildFile(project,monitor);
528: // createX12zipFile(project,monitor);
529: project.refreshLocal(IResource.DEPTH_INFINITE,
530: new SubProgressMonitor(monitor, 1));
531: }
532:
533: protected void createFolder(IFolder folder) throws CoreException {
534: IContainer parent = folder.getParent();
535: if (parent != null && !parent.exists()
536: && parent instanceof IFolder) {
537: createFolder((IFolder) parent);
538: }
539: folder.create(true, true, null);
540: }
541:
542: private String getLibraryBaseDir() {
543: String baseDir = EsbPathHelper.getCbesbHomeDir();
544: if (baseDir != null) {
545: baseDir = baseDir + "/lib";
546: } else {
547: baseDir = Activator.getDefault().getBaseDir();
548: }
549: return baseDir;
550: }
551: }
|