creatorextension/com.nokia.s60tools.creator/src/com/nokia/s60tools/creator/wizards/CreatorScriptNewWizard.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
permissions -rw-r--r--
initial EPL conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
package com.nokia.s60tools.creator.wizards;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
import java.io.ByteArrayInputStream;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
import java.io.IOException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
import java.io.InputStream;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
import java.io.UnsupportedEncodingException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
import java.lang.reflect.InvocationTargetException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
import org.eclipse.core.resources.IContainer;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import org.eclipse.core.resources.IFile;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
import org.eclipse.core.resources.IProject;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
import org.eclipse.core.resources.IResource;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
import org.eclipse.core.resources.IWorkspaceRoot;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
import org.eclipse.core.resources.ResourcesPlugin;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
import org.eclipse.core.runtime.CoreException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
import org.eclipse.core.runtime.IProgressMonitor;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
import org.eclipse.core.runtime.IStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
import org.eclipse.core.runtime.Path;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
import org.eclipse.core.runtime.Status;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
import org.eclipse.jface.dialogs.MessageDialog;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
import org.eclipse.jface.operation.IRunnableWithProgress;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
import org.eclipse.jface.viewers.ISelection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
import org.eclipse.jface.viewers.IStructuredSelection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
import org.eclipse.jface.wizard.Wizard;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
import org.eclipse.swt.widgets.Shell;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
import org.eclipse.ui.INewWizard;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
import org.eclipse.ui.IWorkbench;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
import org.eclipse.ui.IWorkbenchPage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
import org.eclipse.ui.IWorkbenchWizard;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
import org.eclipse.ui.PartInitException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
import org.eclipse.ui.PlatformUI;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
import org.eclipse.ui.ide.IDE;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
import com.nokia.s60tools.creator.CreatorActivator;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
import com.nokia.s60tools.creator.xml.CreatorXML;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
import com.nokia.s60tools.util.resource.FileUtils;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
 * This is a new wizard for Creator Script. Its role is to create a new file 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
 * resource in the provided container. If the container resource
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
 * (a folder or a project) is selected in the workspace 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
 * when the wizard is opened, it will accept it as the target
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
 * container. The wizard creates one file with the extension
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
 * "creatorxml". 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
public class CreatorScriptNewWizard extends Wizard implements INewWizard {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	private CreatorScriptNewWizardPage page;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	private ISelection selection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
	 * Constructor for CreatorScriptNewWizard.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
	 * @throws Exception 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
	public CreatorScriptNewWizard() throws Exception {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
		super();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
		IProject projects [] = ResourcesPlugin.getWorkspace().getRoot().getProjects();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
		if(projects.length < 1){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
			String title = "Creator Script Editor Error" ; 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
			String msg = "No existing projects in workspace! Please create a project before creating scripts!";
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
			Shell sh = CreatorActivator.getCurrentlyActiveWbWindowShell();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
			MessageDialog.openError(sh, title, msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
			//This is need to be done because wizard can be launched also from File -> New -> Other...
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
			throw new Exception(msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
		setNeedsProgressMonitor(true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
	 * Adding the page to the wizard.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
	public void addPages() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
		page = new CreatorScriptNewWizardPage(selection);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
		addPage(page);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    96
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    97
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    98
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    99
	 * This method is called when 'Finish' button is pressed in
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   100
	 * the wizard. We will create an operation and run it
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   101
	 * using wizard as execution context.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   102
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   103
	public boolean performFinish() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   104
		final String containerName = page.getContainerName();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   105
		final String fileName = page.getFileName();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   106
		IRunnableWithProgress op = new IRunnableWithProgress() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   107
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   108
				try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   109
					doFinish(containerName, fileName, monitor);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   110
				} catch (CoreException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
					throw new InvocationTargetException(e);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
				} finally {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
					monitor.done();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
		};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
			getContainer().run(true, false, op);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
		} catch (InterruptedException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
		} catch (InvocationTargetException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
			Throwable realException = e.getTargetException();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
		return true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
	 * The worker method. It will find the container, create the
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
	 * file if missing or just replace its contents, and open
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
	 * the editor on the newly created file.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
	private void doFinish(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
		String containerName,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
		String fileName,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
		IProgressMonitor monitor)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
		throws CoreException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
		// create a sample file
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
		monitor.beginTask("Creating " + fileName, 2);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   142
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   143
		IResource resource = root.findMember(new Path(containerName));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   144
		if (!resource.exists() || !(resource instanceof IContainer)) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   145
			throwCoreException("Container \"" + containerName + "\" does not exist.");
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   146
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
		IContainer container = (IContainer) resource;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   148
		final IFile file = container.getFile(new Path(fileName));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   149
		//File should not be possible to exist, because wizard page is chekking that
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   150
		//Not creating file really in here, save file from editor will create file		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   151
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   152
			InputStream stream = openContentStream(fileName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   153
			file.create(stream, true, monitor);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   154
			file.setCharset(FileUtils.ENCODING_TYPE_UTF_8, monitor);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   155
			stream.close();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   156
		} catch (IOException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   157
			e.printStackTrace();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   158
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   159
		monitor.worked(1);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   160
		monitor.setTaskName("Opening file for editing...");
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   161
		getShell().getDisplay().asyncExec(new Runnable() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   162
			public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   163
				IWorkbenchPage page =
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   164
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   165
				try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   166
					IDE.openEditor(page, file, true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   167
				} catch (PartInitException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   168
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   169
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   170
		});
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   171
		monitor.worked(1);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   172
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   173
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   174
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   175
	 * We will initialize file contents with a sample text.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   176
	 * @throws UnsupportedEncodingException 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   177
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   178
	private InputStream openContentStream(String fileName) throws UnsupportedEncodingException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   179
		String contents =  CreatorXML.NEW_API_CREATOR_FILE_UID + fileName ;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   180
		return new ByteArrayInputStream(contents.getBytes(FileUtils.ENCODING_TYPE_UTF_8));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   181
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   182
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   183
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   184
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   185
	 * set image to wizard
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   186
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   187
	private void setImage() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   188
		setDefaultPageImageDescriptor(CreatorActivator.getWizardImage());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   189
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   190
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   191
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   192
	 * Throws exception with error message
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   193
	 * @param message
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   194
	 * @throws CoreException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   195
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   196
	private void throwCoreException(String message) throws CoreException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   197
		IStatus status =
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   198
			new Status(IStatus.ERROR, CreatorActivator.PLUGIN_ID, IStatus.OK, message, null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   199
		throw new CoreException(status);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   200
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   201
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   202
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   203
	 * We will accept the selection in the workbench to see if
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   204
	 * we can initialize from it.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   205
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   206
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   207
	public void init(IWorkbench workbench, IStructuredSelection selection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   208
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   209
		this.selection = selection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   210
		setImage();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   211
		setWindowTitle("New Creator Script File");
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   212
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   213
}