001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.editors.reportwizard.wizards;
014:
015: import java.lang.reflect.InvocationTargetException;
016:
017: import org.eclipse.core.resources.IContainer;
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.core.runtime.IProgressMonitor;
024: import org.eclipse.core.runtime.IStatus;
025: import org.eclipse.core.runtime.Path;
026: import org.eclipse.core.runtime.Status;
027: import org.eclipse.jface.dialogs.MessageDialog;
028: import org.eclipse.jface.operation.IRunnableWithProgress;
029: import org.eclipse.jface.resource.ImageDescriptor;
030: import org.eclipse.jface.viewers.ISelection;
031: import org.eclipse.jface.viewers.IStructuredSelection;
032: import org.eclipse.jface.wizard.Wizard;
033: import org.eclipse.ui.INewWizard;
034: import org.eclipse.ui.IWorkbench;
035: import org.eclipse.ui.IWorkbenchPage;
036: import org.eclipse.ui.IWorkbenchWizard;
037: import org.eclipse.ui.PartInitException;
038: import org.eclipse.ui.PlatformUI;
039: import org.eclipse.ui.ide.IDE;
040: import org.pentaho.designstudio.editors.reportwizard.ReportWizardPlugin;
041: import org.pentaho.designstudio.editors.reportwizard.messages.Messages;
042: import org.pentaho.jfreereport.castormodel.reportspec.ReportSpec;
043: import org.pentaho.jfreereport.wizard.utility.CastorUtility;
044:
045: /**
046: * This is a sample new wizard. Its role is to create a new file
047: * resource in the provided container. If the container resource
048: * (a folder or a project) is selected in the workspace
049: * when the wizard is opened, it will accept it as the target
050: * container. The wizard creates one file with the extension
051: * "action_xml". If a sample multi-page editor (also available
052: * as a template) is registered for the same extension, it will
053: * be able to open it.
054: */
055:
056: public class ReportSpecNewWizard extends Wizard implements INewWizard {
057: private ReportSpecNewWizardPage page;
058: private ISelection selection;
059:
060: /**
061: * Constructor for ActionSequenceNewWizard.
062: */
063: public ReportSpecNewWizard() {
064: super ();
065: setNeedsProgressMonitor(true);
066: }
067:
068: /**
069: * Adding the page to the wizard.
070: */
071:
072: public void addPages() {
073: page = new ReportSpecNewWizardPage(selection);
074: addPage(page);
075:
076: }
077:
078: /**
079: * This method is called when 'Finish' button is pressed in
080: * the wizard. We will create an operation and run it
081: * using wizard as execution context.
082: */
083: public boolean performFinish() {
084: final String containerName = page.getContainerName();
085: String tmpFileName = page.getFileName();
086: if (tmpFileName.indexOf(".") == -1) { //$NON-NLS-1$
087: tmpFileName = tmpFileName
088: + "." + ReportSpecNewWizardPage.JFREE_REPORT_WIZ_FILE_EXTENSION; //$NON-NLS-1$
089: }
090: final String fileName = tmpFileName;
091: IRunnableWithProgress op = new IRunnableWithProgress() {
092: public void run(IProgressMonitor monitor)
093: throws InvocationTargetException {
094: try {
095: doFinish(containerName, fileName, monitor);
096: } catch (CoreException e) {
097: throw new InvocationTargetException(e);
098: } finally {
099: monitor.done();
100: }
101: }
102: };
103: try {
104: getContainer().run(true, false, op);
105: } catch (InterruptedException e) {
106: return false;
107: } catch (InvocationTargetException e) {
108: System.out.println("Parent Exception");
109: e.printStackTrace();
110: Throwable realException = e.getTargetException();
111: System.out.println("Child Exception");
112: realException.printStackTrace();
113: MessageDialog
114: .openError(
115: getShell(),
116: Messages
117: .getString("ActionSequenceNewWizard.UI_WIZ_ERROR_DLG_TITLE"), realException.getMessage()); //$NON-NLS-1$
118: return false;
119: }
120: return true;
121: }
122:
123: /**
124: * The worker method. It will find the container, create the
125: * file if missing or just replace its contents, and open
126: * the editor on the newly created file.
127: */
128:
129: private void doFinish(String containerName, String fileName,
130: IProgressMonitor monitor) throws CoreException {
131: // create a sample file
132: monitor
133: .beginTask(
134: Messages
135: .getString(
136: "ActionSequenceNewWizard.UI_WIZ_CREATING_MSG", fileName), 2); //$NON-NLS-1$
137: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
138: IResource resource = root.findMember(new Path(containerName));
139: if (!resource.exists() || !(resource instanceof IContainer)) {
140: throwCoreException(Messages
141: .getString(
142: "ActionSequenceNewWizard.UI_WIZ_CREATING_ERROR_MSG", containerName)); //$NON-NLS-1$
143: }
144: IContainer container = (IContainer) resource;
145: ReportSpec reportSpec = new ReportSpec();
146: final IFile file = container.getFile(new Path(fileName));
147: CastorUtility.getInstance().writeCastorObject(reportSpec,
148: file.getRawLocation().toString());
149: // get resource
150: // IResource myresource =
151: // ResourcesPlugin.getWorkspace().getRoot().findMember(myFilenameString);
152: //
153: // then just refresh it
154:
155: resource.refreshLocal(IResource.DEPTH_INFINITE, null /*or progress
156: monitor*/);
157:
158: monitor.worked(1);
159: monitor
160: .setTaskName(Messages
161: .getString("ActionSequenceNewWizard.UI_WIZ_OPENING_MSG")); //$NON-NLS-1$
162: getShell().getDisplay().asyncExec(new Runnable() {
163: public void run() {
164: IWorkbenchPage page = PlatformUI.getWorkbench()
165: .getActiveWorkbenchWindow().getActivePage();
166: try {
167: IDE.openEditor(page, file, true);
168: } catch (PartInitException e) {
169: }
170: }
171: });
172: monitor.worked(1);
173: }
174:
175: private void throwCoreException(String message)
176: throws CoreException {
177: IStatus status = new Status(
178: IStatus.ERROR,
179: "org.pentaho.designstudio.editors.actionsequence", IStatus.OK, message, null); //$NON-NLS-1$
180: throw new CoreException(status);
181: }
182:
183: /**
184: * We will accept the selection in the workbench to see if
185: * we can initialize from it.
186: * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
187: */
188: public void init(IWorkbench workbench,
189: IStructuredSelection selection) {
190: this .selection = selection;
191: setWindowTitle("Pentaho"); //$NON-NLS-1$
192: ImageDescriptor image = ReportWizardPlugin
193: .getImageDescriptor(Messages
194: .getString("ActionSequenceNewWizard.ICON_NEW_ACTION_SEQUENCE")); //$NON-NLS-1$
195: setDefaultPageImageDescriptor(image);
196: }
197: }
|