001: package com.bostechcorp.cbesb.ui.ide.wizards;
002:
003: import java.io.File;
004: import java.util.Iterator;
005:
006: import org.eclipse.core.resources.IContainer;
007: import org.eclipse.core.resources.IFile;
008: import org.eclipse.core.resources.IFolder;
009: import org.eclipse.core.resources.IProject;
010: import org.eclipse.core.resources.ResourcesPlugin;
011: import org.eclipse.core.runtime.CoreException;
012: import org.eclipse.core.runtime.IPath;
013: import org.eclipse.core.runtime.Path;
014: import org.eclipse.jdt.core.IJavaElement;
015: import org.eclipse.jdt.core.IJavaProject;
016: import org.eclipse.jdt.core.IPackageFragment;
017: import org.eclipse.jdt.core.IPackageFragmentRoot;
018: import org.eclipse.jdt.core.JavaCore;
019: import org.eclipse.jdt.core.JavaModelException;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.wizard.Wizard;
022: import org.eclipse.ui.IEditorInput;
023: import org.eclipse.ui.INewWizard;
024: import org.eclipse.ui.IWorkbench;
025: import org.eclipse.ui.IWorkbenchPage;
026: import org.eclipse.ui.IWorkbenchWindow;
027: import org.eclipse.ui.IWorkbenchWizard;
028: import org.eclipse.ui.PartInitException;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.part.FileEditorInput;
031:
032: import com.bostechcorp.cbesb.common.util.custom.code.AbstractCodeGenerator;
033: import com.bostechcorp.cbesb.common.util.custom.code.CustomCodeConstants;
034: import com.bostechcorp.cbesb.common.util.custom.code.GeneratorFactory;
035: import com.bostechcorp.cbesb.ui.ide.Activator;
036: import com.bostechcorp.cbesb.ui.util.ProjectUtil;
037:
038: public class CustomCodeWizard extends Wizard implements INewWizard {
039:
040: private IStructuredSelection selection;
041:
042: private IWorkbench workbench;
043:
044: private CustomCodeWizardMainPage mainPage;
045:
046: private CustomCodeWizardPojoPage secondPage;
047:
048: private CustomCodeWizardGroovyPage thirdPage;
049:
050: public CustomCodeWizard() {
051: super ();
052: // setNeedsProgressMonitor(true);
053: }
054:
055: /**
056: * Adding the page to the wizard.
057: */
058: public void addPages() {
059: mainPage = new CustomCodeWizardMainPage(workbench, selection);
060: addPage(mainPage);
061: secondPage = new CustomCodeWizardPojoPage(workbench, selection);
062: addPage(secondPage);
063: thirdPage = new CustomCodeWizardGroovyPage(workbench, selection);
064: addPage(thirdPage);
065: }
066:
067: /**
068: * We will accept the selection in the workbench to see if we can initialize
069: * from it.
070: *
071: * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
072: */
073: public void init(IWorkbench workbench,
074: IStructuredSelection selection) {
075: this .workbench = workbench;
076: this .selection = selection;
077: setWindowTitle("Custom Code");
078: setDefaultPageImageDescriptor(Activator
079: .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
080: "icons/full/wizban/openMessageFormatView.gif"));
081: }
082:
083: // public boolean canFinish(){
084: //
085: // }
086: /**
087: * (non-Javadoc) Method declared on IWizard
088: */
089: public boolean performFinish() {
090: try {
091: doFinish();
092: } catch (Exception ex) {
093: ex.printStackTrace();
094: }
095: return true;// thiredPage.finish();
096: }
097:
098: private void doFinish() throws CoreException {
099: // String sourceRoot=thiredPage.getCombo().getText();
100: // String targetRoot=thiredPage.getCombo_1().getText();
101: // mainPage.finish(secondPage.getSourceFile(),
102: // secondPage.getTargetFile(),sourceRoot,targetRoot,secondPage.getSourceType(),secondPage.getTargetType());
103: if (mainPage.getLanguageCombo().getText().equalsIgnoreCase(
104: "java")) {
105: String path = secondPage.getPath() + File.separator
106: + secondPage.getName() + ".java";
107: String className = secondPage.getName();
108: String methodName = secondPage.getMethod();
109: String packageName = secondPage.getPackageName();
110: try {
111: createFile("Java", path, className, methodName,
112: packageName, secondPage.getProjectName());
113: openFileInIdeEditor(path); // required for bug 173
114: } catch (Exception e) {
115: e.printStackTrace();
116: }
117:
118: } else if (mainPage.getLanguageCombo().getText()
119: .equalsIgnoreCase("groovy")) {
120: String path = thirdPage.getPath() + File.separator
121: + thirdPage.getName() + ".groovy";
122: String className = thirdPage.getName();
123: String methodName = thirdPage.getMethod();
124: try {
125: createFile("Groovy", path, className, methodName, "",
126: thirdPage.getProjectName());
127: openFileInIdeEditor(path); // required for bug 173
128: } catch (Exception e) {
129: e.printStackTrace();
130: }
131:
132: }
133: }
134:
135: private void openFileInIdeEditor(String path)
136: throws PartInitException { // this
137: // method
138: // resolve
139: // bug
140: // 173
141: // -
142: // auto
143: // open
144: // generated
145: // file
146: IPath filePath = null;
147: if (path.startsWith(ResourcesPlugin.getWorkspace().getRoot()
148: .getLocation().toOSString())) {
149: filePath = new Path(path.substring(ResourcesPlugin
150: .getWorkspace().getRoot().getLocation()
151: .toOSString().length()));
152: } else if (path.startsWith(ResourcesPlugin.getWorkspace()
153: .getRoot().getLocation().toString())) {
154: filePath = new Path(path.substring(ResourcesPlugin
155: .getWorkspace().getRoot().getLocation().toString()
156: .length()));
157: } else {
158: filePath = new Path(path);
159: }
160:
161: if (filePath != null) {
162: IFile newFile = ResourcesPlugin.getWorkspace().getRoot()
163: .getFile(filePath);
164: IEditorInput editorInput = new FileEditorInput(newFile);
165: IWorkbenchWindow window = PlatformUI.getWorkbench()
166: .getActiveWorkbenchWindow();
167: IWorkbenchPage page = window.getActivePage();
168: page.openEditor(editorInput,
169: "org.eclipse.ui.DefaultTextEditor");
170: } else {
171: System.err.println("Something is Wrong with Path");
172: }
173: }
174:
175: @Override
176: public boolean canFinish() {
177:
178: if (!(this .getContainer().getCurrentPage() instanceof CustomCodeWizardMainPage)) {
179: if ((mainPage.isPageComplete() && secondPage
180: .isPageComplete())
181: || (mainPage.isPageComplete() && thirdPage
182: .isPageComplete()))
183: return true;
184: }
185: return false;
186: }
187:
188: public CustomCodeWizardMainPage getMainPage() {
189: return mainPage;
190: }
191:
192: public CustomCodeWizardPojoPage getPojoPage() {
193: return secondPage;
194: }
195:
196: public CustomCodeWizardGroovyPage getGroovyPage() {
197: return thirdPage;
198: }
199:
200: protected void createFile(String language, String path,
201: String className, String methodName, String packageName,
202: String projectName) throws Exception {
203: String type = mainPage.getTypeCombo().getText();
204: GeneratorFactory factory = new GeneratorFactory();
205: AbstractCodeGenerator generator = null;
206: if (language.equalsIgnoreCase("java")) {
207: if (!packageName.equals("")) {
208: IJavaProject jProject = JavaCore.create(ResourcesPlugin
209: .getWorkspace().getRoot().getProject(
210: projectName));
211: IPath srcJavaPath = jProject.getPath().append(
212: "/src/java");
213: try {
214: IPackageFragmentRoot pkgFragmentRoot = jProject
215: .findPackageFragmentRoot(srcJavaPath);
216: IPackageFragment pkgFragment = pkgFragmentRoot
217: .createPackageFragment(packageName, true,
218: null);
219: } catch (JavaModelException e) {
220: //TODO handle Exception
221: System.out.println(e.getMessage());
222: }
223: }
224: try {
225: if (type
226: .equalsIgnoreCase(CustomCodeConstants.MAP_FILTER)) {
227: generator = factory.getGenerator(
228: GeneratorFactory.JAVA_MAP_FILTER, path);
229: } else if (type
230: .equalsIgnoreCase(CustomCodeConstants.MAP_USER_OPERATION)) {
231: generator = factory.getGenerator(
232: GeneratorFactory.JAVA_MAP_USER_OPERATION,
233: path);
234: generator.setMethodName(methodName);
235: } else if (type
236: .equalsIgnoreCase(CustomCodeConstants.TRX_ID)) {
237: generator = factory.getGenerator(
238: GeneratorFactory.JAVA_TRX_ID, path);
239: } else if (type
240: .equalsIgnoreCase(CustomCodeConstants.UPOC)) {
241: generator = factory.getGenerator(
242: GeneratorFactory.JAVA_UPOC, path);
243: generator.setMethodName(methodName);
244: } else if (type
245: .equalsIgnoreCase(CustomCodeConstants.SCRIPT_COMPONENT)) {
246: generator = factory.getGenerator(
247: GeneratorFactory.JAVA_SCRIPT_COMPONENT,
248: path);
249: } else if (type
250: .equalsIgnoreCase(CustomCodeConstants.ETL_ERROR_HANDLER)) {
251: generator = factory.getGenerator(
252: GeneratorFactory.JAVA_ETL_ERROR_HANDLER,
253: path);
254: }
255: } catch (Exception e) {
256: e.printStackTrace();
257: }
258:
259: } else if (language.equalsIgnoreCase("groovy")) {
260: try {
261: if (type.equalsIgnoreCase(CustomCodeConstants.TRX_ID)) {
262: generator = factory.getGenerator(
263: GeneratorFactory.GROOVY_TRX_ID, path);
264: } else if (type
265: .equalsIgnoreCase(CustomCodeConstants.UPOC)) {
266: generator = factory.getGenerator(
267: GeneratorFactory.GROOVY_UPOC, path);
268: generator.setMethodName(methodName);
269: } else if (type
270: .equalsIgnoreCase(CustomCodeConstants.SCRIPT_COMPONENT)) {
271: generator = factory.getGenerator(
272: GeneratorFactory.GROOVY_SCRIPT, path);
273: }
274: } catch (Exception e) {
275: e.printStackTrace();
276: }
277: }
278: if (generator != null) {
279: generator.setPackageName(packageName);
280: generator.setClassName(className);
281: generator.setMethodName(methodName);
282: generator.generateFile();
283: ProjectUtil.refreshProject(projectName);
284: }
285: }
286:
287: public String getProjectFromSelection() {
288: if (!(selection instanceof IStructuredSelection))
289: return "";
290: // Find the first plugin.xml file.
291: Iterator iter = ((IStructuredSelection) selection).iterator();
292: while (iter.hasNext()) {
293: Object item = (Object) iter.next();
294: if (item instanceof IJavaElement) {
295: IJavaElement javaElem = (IJavaElement) item;
296: if (javaElem.getPath().toOSString().lastIndexOf(
297: File.separatorChar) != 0) {
298: return javaElem.getPath().toOSString().substring(
299: 1,
300: javaElem.getPath().toOSString()
301: .substring(1).indexOf(
302: File.separatorChar) + 1);
303: }
304:
305: return javaElem.getElementName();
306: }
307: if (item instanceof IFile) {
308: IFile file = (IFile) item;
309: return file.getProject().getName();
310: }
311: if (item instanceof IFolder) {
312: IFolder folder = (IFolder) item;
313: return folder.getProject().getName();
314: }
315: if (item instanceof IProject) {
316: IProject project = (IProject) item;
317: return project.getName();
318: }
319: }
320:
321: return "";
322: }
323:
324: protected void createFolder(IFolder folder) throws CoreException {
325: IContainer parent = folder.getParent();
326: if (parent != null && !parent.exists()
327: && parent instanceof IFolder) {
328: createFolder((IFolder) parent);
329: }
330: folder.create(true, true, null);
331: }
332:
333: }
|