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.actionsequence.wizards;
014:
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.lang.reflect.InvocationTargetException;
018:
019: import org.eclipse.core.resources.IContainer;
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.IWorkspaceRoot;
023: import org.eclipse.core.resources.ResourcesPlugin;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.core.runtime.IStatus;
027: import org.eclipse.core.runtime.Path;
028: import org.eclipse.core.runtime.Status;
029: import org.eclipse.jface.dialogs.MessageDialog;
030: import org.eclipse.jface.operation.IRunnableWithProgress;
031: import org.eclipse.jface.resource.ImageDescriptor;
032: import org.eclipse.jface.viewers.ISelection;
033: import org.eclipse.jface.viewers.IStructuredSelection;
034: import org.eclipse.jface.wizard.Wizard;
035: import org.eclipse.ui.INewWizard;
036: import org.eclipse.ui.IWorkbench;
037: import org.eclipse.ui.IWorkbenchPage;
038: import org.eclipse.ui.IWorkbenchWizard;
039: import org.eclipse.ui.PartInitException;
040: import org.eclipse.ui.PlatformUI;
041: import org.eclipse.ui.ide.IDE;
042: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
043: import org.pentaho.designstudio.messages.Messages;
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 ActionSequenceNewWizard extends Wizard implements
057: INewWizard {
058: private ActionSequenceNewWizardPage page;
059: private ISelection selection;
060:
061: /**
062: * Constructor for ActionSequenceNewWizard.
063: */
064: public ActionSequenceNewWizard() {
065: super ();
066: setNeedsProgressMonitor(true);
067: }
068:
069: /**
070: * Adding the page to the wizard.
071: */
072:
073: public void addPages() {
074: page = new ActionSequenceNewWizardPage(selection);
075: addPage(page);
076:
077: }
078:
079: /**
080: * This method is called when 'Finish' button is pressed in
081: * the wizard. We will create an operation and run it
082: * using wizard as execution context.
083: */
084: public boolean performFinish() {
085: final String containerName = page.getContainerName();
086: String tmpFileName = page.getFileName();
087: if (tmpFileName.indexOf(".") == -1) { //$NON-NLS-1$
088: tmpFileName = tmpFileName
089: + "." + ActionSequenceNewWizardPage.ACTION_SEQUENCE_FILE_EXTENSION; //$NON-NLS-1$
090: }
091: final String fileName = tmpFileName;
092: IRunnableWithProgress op = new IRunnableWithProgress() {
093: public void run(IProgressMonitor monitor)
094: throws InvocationTargetException {
095: try {
096: doFinish(containerName, fileName, monitor);
097: } catch (CoreException e) {
098: throw new InvocationTargetException(e);
099: } finally {
100: monitor.done();
101: }
102: }
103: };
104: try {
105: getContainer().run(true, false, op);
106: } catch (InterruptedException e) {
107: return false;
108: } catch (InvocationTargetException e) {
109: Throwable realException = e.getTargetException();
110: MessageDialog
111: .openError(
112: getShell(),
113: Messages
114: .getString("ActionSequenceNewWizard.UI_WIZ_ERROR_DLG_TITLE"), realException.getMessage()); //$NON-NLS-1$
115: return false;
116: }
117: return true;
118: }
119:
120: /**
121: * The worker method. It will find the container, create the
122: * file if missing or just replace its contents, and open
123: * the editor on the newly created file.
124: */
125:
126: private void doFinish(String containerName, String fileName,
127: IProgressMonitor monitor) throws CoreException {
128: // create a sample file
129: monitor
130: .beginTask(
131: Messages
132: .getString(
133: "ActionSequenceNewWizard.UI_WIZ_CREATING_MSG", fileName), 2); //$NON-NLS-1$
134: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
135: IResource resource = root.findMember(new Path(containerName));
136: if (!resource.exists() || !(resource instanceof IContainer)) {
137: throwCoreException(Messages
138: .getString(
139: "ActionSequenceNewWizard.UI_WIZ_CREATING_ERROR_MSG", containerName)); //$NON-NLS-1$
140: }
141: IContainer container = (IContainer) resource;
142: final IFile file = container.getFile(new Path(fileName));
143: try {
144: InputStream stream = openContentStream();
145: if (file.exists()) {
146: file.setContents(stream, true, true, monitor);
147: } else {
148: file.create(stream, true, monitor);
149: }
150: stream.close();
151: } catch (IOException e) {
152: }
153: monitor.worked(1);
154: monitor
155: .setTaskName(Messages
156: .getString("ActionSequenceNewWizard.UI_WIZ_OPENING_MSG")); //$NON-NLS-1$
157: getShell().getDisplay().asyncExec(new Runnable() {
158: public void run() {
159: IWorkbenchPage page = PlatformUI.getWorkbench()
160: .getActiveWorkbenchWindow().getActivePage();
161: try {
162: IDE.openEditor(page, file, true);
163: } catch (PartInitException e) {
164: }
165: }
166: });
167: monitor.worked(1);
168: }
169:
170: /**
171: * We will initialize file contents with a sample text.
172: */
173:
174: private InputStream openContentStream() {
175: return (page.getInitialContent());
176: }
177:
178: private void throwCoreException(String message)
179: throws CoreException {
180: IStatus status = new Status(
181: IStatus.ERROR,
182: "org.pentaho.designstudio.editors.actionsequence", IStatus.OK, message, null); //$NON-NLS-1$
183: throw new CoreException(status);
184: }
185:
186: /**
187: * We will accept the selection in the workbench to see if
188: * we can initialize from it.
189: * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
190: */
191: public void init(IWorkbench workbench,
192: IStructuredSelection selection) {
193: this .selection = selection;
194: setWindowTitle("Pentaho"); //$NON-NLS-1$
195: ImageDescriptor image = ActionSequenceEditorPlugin
196: .getImageDescriptor(Messages
197: .getString("ActionSequenceNewWizard.ICON_NEW_ACTION_SEQUENCE")); //$NON-NLS-1$
198: setDefaultPageImageDescriptor(image);
199: }
200: }
|