001: package org.enhydra.kelp.eclipse.enhydraWizard;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.Iterator;
009: import java.util.List;
010: import org.eclipse.ui.IEditorReference;
011: import org.eclipse.ui.IViewPart;
012: import org.eclipse.ui.IViewReference;
013: import org.eclipse.ui.IWorkbench;
014: import org.eclipse.ui.IWorkbenchPage;
015: import org.eclipse.ui.IWorkbenchPart;
016: import org.eclipse.ui.IWorkbenchWindow;
017: import org.eclipse.core.resources.ICommand;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IProjectDescription;
020: import org.eclipse.core.resources.IResource;
021: import org.eclipse.core.runtime.CoreException;
022: import org.eclipse.core.runtime.IConfigurationElement;
023: import org.eclipse.core.runtime.IPath;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.jface.resource.ImageDescriptor;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.StructuredSelection;
030: import org.eclipse.jface.wizard.IWizardPage;
031: import org.eclipse.jface.wizard.Wizard;
032: import org.eclipse.ui.INewWizard;
033: import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
034: import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
035: import org.eclipse.ui.part.ISetSelectionTarget;
036: import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
037: import org.enhydra.kelp.eclipse.codegen.wizard.EclipseCodeGenWizard;
038: import org.enhydra.tool.ToolBoxInfo;
039: import org.enhydra.tool.codegen.GeneratorException;
040: import org.enhydra.tool.common.SwingUtil;
041: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
042: import org.eclipse.debug.ui.RefreshTab;
043: import org.eclipse.debug.ui.IDebugUIConstants;
044: import org.eclipse.debug.core.DebugPlugin;
045: import org.eclipse.debug.core.ILaunchConfiguration;
046: import org.eclipse.debug.core.ILaunchManager;
047: import org.eclipse.ui.externaltools.internal.model.BuilderUtils;
048: import KelpEclipse.KelpEclipsePlugin;
049:
050: /**
051: * @see Wizard
052: */
053: public class NewEnhydraProjectWizard extends Wizard implements
054: INewWizard {
055:
056: private NewEnhydraWizardPage mainPage;
057: private NewEnhydraWizardSecondPage javaPage;
058:
059: private IWorkbench workbench;
060: private IConfigurationElement fConfigElement;
061:
062: /**
063: */
064: public NewEnhydraProjectWizard() {
065: super ();
066:
067: setDialogSettings(KelpEclipsePlugin.getDefault()
068: .getDialogSettings());
069: setWindowTitle(NewEnhydraWizardMessages
070: .getString("NewEnhydraProjectWizard.title"));
071: }
072:
073: /**
074: * Adds a new page to this wizard. The page is inserted at the
075: * end of the page list.
076: *
077: * @param page the new page
078: */
079: public void addPages() {
080: super .addPages();
081:
082: mainPage = new NewEnhydraWizardPage("NewEnhydraProjectWizard");
083: mainPage
084: .setDescription(NewEnhydraWizardMessages
085: .getString("NewEnhydraProjectWizard.MainPage.description"));
086: mainPage.setTitle(NewEnhydraWizardMessages
087: .getString("NewEnhydraProjectWizard.MainPage.title"));
088: addPage(mainPage);
089:
090: javaPage = new NewEnhydraWizardSecondPage(mainPage);
091: addPage((IWizardPage) javaPage);
092: }
093:
094: /**
095: * @see Wizard#performFinish
096: */
097: public boolean performFinish() {
098: IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(
099: javaPage.getRunnable());
100: try {
101: getContainer().run(false, true, op);
102: } catch (InvocationTargetException e) {
103: e.printStackTrace();
104: String message = NewEnhydraWizardMessages
105: .getString("NewProjectCreationWizard.op_error.message"); //$NON-NLS-1$
106: /*
107: * ExceptionHandler.handle(e, getShell(), title, message);
108: */
109: MessageDialog.openError(getShell(), "Error", message);
110: return false;
111: } catch (InterruptedException e) {
112: e.printStackTrace();
113: return false;
114: }
115: BasicNewProjectResourceWizard.updatePerspective(fConfigElement);
116: selectAndReveal(javaPage.getJavaProject().getProject());
117: createExternalBuilders();
118:
119: runEnhydraWizard();
120:
121: return true;
122: }
123:
124: /**
125: * @see Wizard#init
126: */
127: public void init(IWorkbench workbench,
128: IStructuredSelection selection) {
129: this .workbench = workbench;
130:
131: setWindowTitle(NewEnhydraWizardMessages
132: .getString("NewEnhydraProjectWizard.MainPage.title"));
133: setDefaultPageImageDescriptor(createImageDescriptor("EnhydraBig.gif"));
134: setNeedsProgressMonitor(true);
135: }
136:
137: /*
138: * Stores the configuration element for the wizard. The config element will be used
139: * in <code>performFinish</code> to set the result perspective.
140: */
141: public void setInitializationData(IConfigurationElement cfig,
142: String propertyName, Object data) {
143: fConfigElement = cfig;
144: }
145:
146: /**
147: */
148: public boolean canFinish() {
149: if (javaPage.getJavaProject() == null) {
150: return false;
151: }
152: return true;
153: }
154:
155: /**
156: * Selects and reveals the newly added resource in all parts
157: * of the active workbench window's active page.
158: *
159: * @see ISetSelectionTarget
160: */
161: protected void selectAndReveal(IResource newResource) {
162: selectAndReveal(newResource, workbench
163: .getActiveWorkbenchWindow());
164: }
165:
166: /**
167: * Attempts to select and reveal the specified resource in all
168: * parts within the supplied workbench window's active page.
169: * <p>
170: * Checks all parts in the active page to see if they implement <code>ISetSelectionTarget</code>,
171: * either directly or as an adapter. If so, tells the part to select and reveal the
172: * specified resource.
173: * </p>
174: *
175: * @param resource the resource to be selected and revealed
176: * @param window the workbench window to select and reveal the resource
177: *
178: * @see ISetSelectionTarget
179: */
180: public static void selectAndReveal(IResource resource,
181: IWorkbenchWindow window) {
182: // validate the input
183: if (window == null || resource == null)
184: return;
185: IWorkbenchPage page = window.getActivePage();
186: if (page == null)
187: return;
188:
189: // get all the view and editor parts
190: List parts = new ArrayList();
191: /**/
192: IViewReference[] viewRefs = page.getViewReferences();
193: IViewPart[] viewParts = new IViewPart[viewRefs.length];
194: for (int i = 0; i < viewRefs.length; i++) {
195: viewParts[i] = viewRefs[i].getView(true);
196: }
197: /**/
198: parts.addAll(Arrays.asList(viewParts));
199: IEditorReference refs[] = page.getEditorReferences();
200: for (int i = 0; i < refs.length; i++) {
201: if (refs[i].getPart(false) != null)
202: parts.add(refs[i].getPart(false));
203: }
204:
205: final ISelection selection = new StructuredSelection(resource);
206: Iterator enumer = parts.iterator();
207: while (enumer.hasNext()) {
208: IWorkbenchPart part = (IWorkbenchPart) enumer.next();
209:
210: // get the part's ISetSelectionTarget implementation
211: ISetSelectionTarget target = null;
212: if (part instanceof ISetSelectionTarget)
213: target = (ISetSelectionTarget) part;
214: else
215: target = (ISetSelectionTarget) part
216: .getAdapter(ISetSelectionTarget.class);
217:
218: if (target != null) {
219: // select and reveal resource
220: final ISetSelectionTarget finalTarget = target;
221: window.getShell().getDisplay().asyncExec(
222: new Runnable() {
223: public void run() {
224: finalTarget.selectReveal(selection);
225: }
226: });
227: }
228: }
229: }
230:
231: /**
232: * Utility method to create an <code>ImageDescriptor</code>
233: * from a path to a file.
234: */
235: private ImageDescriptor createImageDescriptor(String path) {
236: URL baseURL = KelpEclipsePlugin.getDefault().getDescriptor()
237: .getInstallURL();
238: String iconPath = "icons/";
239: try {
240: URL url = new URL(baseURL, iconPath + path);
241: return ImageDescriptor.createFromURL(url);
242: } catch (MalformedURLException e) {
243: }
244: return ImageDescriptor.getMissingImageDescriptor();
245: }
246:
247: private void runEnhydraWizard() {
248: EclipseCodeGenWizard wizard;
249: IProject project = javaPage.getJavaProject().getProject();
250:
251: String prjName = javaPage.getJavaProject().getProject()
252: .getName();
253: SwingUtil.setLookAndFeelToSystem(); //Dacha add 25.02.2003
254:
255: try {
256: IPath projPath = javaPage.getJavaProject().getProject()
257: .getLocation();
258: String a = projPath
259: .uptoSegment(projPath.segmentCount() - 1)
260: .toString();
261:
262: wizard = new EclipseCodeGenWizard(prjName, a);
263:
264: wizard.getInnerPanel().initPreferredSize();
265: wizard.showDialog(null);
266:
267: try {
268: project.refreshLocal(IResource.DEPTH_INFINITE, null);
269: } catch (CoreException e) {
270: System.err.println(prjName);
271: }
272: } catch (GeneratorException e) {
273: System.err.println(e.getMessage());
274: }
275: }
276:
277: /**
278: * Creates External tool builder and make apropriate build order
279: * todo: add DODS in rebuild
280: */
281: private void createExternalBuilders() {
282:
283: IProject project = javaPage.getJavaProject().getProject();
284: IProjectDescription desc;
285: ICommand[] commands = new ICommand[0];
286: try {
287: desc = project.getDescription();
288: commands = desc.getBuildSpec();
289:
290: ICommand javaBuildCommand = commands[0]; //default for Java Project
291:
292: String location = ToolBoxInfo.getEnhydraRoot()
293: + "/bin/enhydra-ant";
294: if (System.getProperty("os.name").toLowerCase().startsWith(
295: "windows")) {
296: location += ".bat";
297: }
298:
299: //add external builders to project
300: ILaunchManager manager = DebugPlugin.getDefault()
301: .getLaunchManager();
302:
303: //xmlc
304: ICommand antXMLCCommand = desc.newCommand();
305: ILaunchConfigurationWorkingCopy xmlcConfig = manager
306: .getLaunchConfigurationType(
307: IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE)
308: .newInstance(null, "enhydra-ant xmlc");
309: ;
310: xmlcConfig.setAttribute(
311: IExternalToolConstants.ATTR_TOOL_ARGUMENTS, "xmlc");
312:
313: xmlcConfig.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT,
314: false);
315: xmlcConfig.setAttribute(
316: IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
317: xmlcConfig.setAttribute(
318: IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
319: xmlcConfig.setAttribute(
320: IExternalToolConstants.ATTR_PROMPT_FOR_ARGUMENTS,
321: false);
322:
323: xmlcConfig.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE,
324: "${project}");
325: xmlcConfig.setAttribute(RefreshTab.ATTR_REFRESH_RECURSIVE,
326: true);
327: xmlcConfig.setAttribute(
328: IExternalToolConstants.ATTR_RUN_BUILD_KINDS,
329: IExternalToolConstants.BUILD_TYPE_AUTO);
330: xmlcConfig.setAttribute(
331: IExternalToolConstants.ATTR_WORKING_DIRECTORY,
332: "${workspace_loc:/" + project.getName() + "}");
333: xmlcConfig.setAttribute(
334: IExternalToolConstants.ATTR_LOCATION, location);
335:
336: ILaunchConfiguration launchXMLCConfig = BuilderUtils
337: .migrateBuilderConfiguration(project, xmlcConfig);
338: antXMLCCommand = BuilderUtils.toBuildCommand(project,
339: launchXMLCConfig, antXMLCCommand);
340:
341: // dods
342: ICommand antDODSCommand = desc.newCommand();
343: ILaunchConfigurationWorkingCopy dodsConfig = manager
344: .getLaunchConfigurationType(
345: IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE)
346: .newInstance(null, "enhydra-ant dods");
347: ;
348: dodsConfig.setAttribute(
349: IExternalToolConstants.ATTR_TOOL_ARGUMENTS, "dods");
350:
351: dodsConfig.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT,
352: false);
353: dodsConfig.setAttribute(
354: IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
355: dodsConfig.setAttribute(
356: IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
357: dodsConfig.setAttribute(
358: IExternalToolConstants.ATTR_PROMPT_FOR_ARGUMENTS,
359: false);
360:
361: dodsConfig.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE,
362: "${project}");
363: dodsConfig.setAttribute(RefreshTab.ATTR_REFRESH_RECURSIVE,
364: true);
365: dodsConfig.setAttribute(
366: IExternalToolConstants.ATTR_RUN_BUILD_KINDS,
367: IExternalToolConstants.BUILD_TYPE_INCREMENTAL);
368: dodsConfig.setAttribute(
369: IExternalToolConstants.ATTR_WORKING_DIRECTORY,
370: "${workspace_loc:/" + project.getName() + "}");
371: dodsConfig.setAttribute(
372: IExternalToolConstants.ATTR_LOCATION, location);
373:
374: ILaunchConfiguration launchDODSConfig = BuilderUtils
375: .migrateBuilderConfiguration(project, dodsConfig);
376: antDODSCommand = BuilderUtils.toBuildCommand(project,
377: launchDODSConfig, antDODSCommand);
378:
379: // deploy
380: ICommand antDeployCommand = desc.newCommand();
381: ILaunchConfigurationWorkingCopy deployConfig = manager
382: .getLaunchConfigurationType(
383: IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE)
384: .newInstance(null, "enhydra-ant deploy");
385: ;
386: deployConfig.setAttribute(
387: IExternalToolConstants.ATTR_TOOL_ARGUMENTS,
388: "deploy");
389:
390: deployConfig.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT,
391: false);
392: deployConfig.setAttribute(
393: IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
394: deployConfig.setAttribute(
395: IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
396: deployConfig.setAttribute(
397: IExternalToolConstants.ATTR_PROMPT_FOR_ARGUMENTS,
398: false);
399:
400: deployConfig.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE,
401: "${project}");
402: deployConfig.setAttribute(
403: RefreshTab.ATTR_REFRESH_RECURSIVE, true);
404: deployConfig.setAttribute(
405: IExternalToolConstants.ATTR_RUN_BUILD_KINDS,
406: IExternalToolConstants.BUILD_TYPE_INCREMENTAL);
407: deployConfig.setAttribute(
408: IExternalToolConstants.ATTR_WORKING_DIRECTORY,
409: "${workspace_loc:/" + project.getName() + "}");
410: deployConfig.setAttribute(
411: IExternalToolConstants.ATTR_LOCATION, location);
412:
413: ILaunchConfiguration launchDeployConfig = BuilderUtils
414: .migrateBuilderConfiguration(project, deployConfig);
415: antDeployCommand = BuilderUtils.toBuildCommand(project,
416: launchDeployConfig, antDeployCommand);
417:
418: // Add it before other builders.
419: //Set new builds order
420: commands = new ICommand[4];
421: commands[0] = javaBuildCommand;
422: commands[1] = antXMLCCommand;
423: commands[2] = antDODSCommand;
424: commands[3] = antDeployCommand;
425:
426: desc.setBuildSpec(commands);
427: project.setDescription(desc, null);
428:
429: } catch (CoreException e) {
430: e.printStackTrace();
431: }
432:
433: }
434:
435: }
|