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