1 /** |
|
2 * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of |
|
6 * the License "Eclipse Public License v1.0" |
|
7 * which accompanies this distribution, and is available |
|
8 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 * |
|
10 * Initial Contributors: |
|
11 * Symbian Foundation - initial contribution. |
|
12 * Contributors: |
|
13 * Description: |
|
14 * Overview: |
|
15 * Details: |
|
16 * Platforms/Drives/Compatibility: |
|
17 * Assumptions/Requirement/Pre-requisites: |
|
18 * Failures and causes: |
|
19 */ |
|
20 package org.symbian.tools.wrttools.jseditors.wizards; |
|
21 |
|
22 import org.eclipse.jface.viewers.IStructuredSelection; |
|
23 import org.eclipse.jface.wizard.Wizard; |
|
24 import org.eclipse.ui.INewWizard; |
|
25 import org.eclipse.ui.IWorkbench; |
|
26 import org.eclipse.core.runtime.*; |
|
27 import org.eclipse.jface.operation.*; |
|
28 import java.lang.reflect.InvocationTargetException; |
|
29 import org.eclipse.jface.dialogs.MessageDialog; |
|
30 import org.eclipse.jface.viewers.ISelection; |
|
31 import org.eclipse.core.resources.*; |
|
32 import org.eclipse.core.runtime.CoreException; |
|
33 import java.io.*; |
|
34 import org.eclipse.ui.*; |
|
35 import org.eclipse.ui.ide.IDE; |
|
36 |
|
37 /** |
|
38 * This is a sample new HTML wizard. Its role is to create a new file |
|
39 * resource in the provided container. If the container resource |
|
40 * (a folder or a project) is selected in the workspace |
|
41 * when the wizard is opened, it will accept it as the target |
|
42 * container. The wizard creates one file with the extension |
|
43 * "html". If a sample html editor (also available |
|
44 * as a template) is registered for the same extension, it will |
|
45 * be able to open it. |
|
46 */ |
|
47 |
|
48 public class NewHTMLWizard extends Wizard implements INewWizard { |
|
49 private NewHTMLWizardPage page; |
|
50 private ISelection selection; |
|
51 |
|
52 /** |
|
53 * Constructor for SampleNewWizard. |
|
54 */ |
|
55 public NewHTMLWizard() { |
|
56 super(); |
|
57 setNeedsProgressMonitor(true); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Adding the page to the wizard. |
|
62 */ |
|
63 |
|
64 public void addPages() { |
|
65 page = new NewHTMLWizardPage(selection); |
|
66 addPage(page); |
|
67 } |
|
68 |
|
69 /** |
|
70 * This method is called when 'Finish' button is pressed in |
|
71 * the wizard. We will create an operation and run it |
|
72 * using wizard as execution context. |
|
73 */ |
|
74 public boolean performFinish() { |
|
75 final String containerName = page.getContainerName(); |
|
76 final String fileName = page.getFileName(); |
|
77 IRunnableWithProgress op = new IRunnableWithProgress() { |
|
78 public void run(IProgressMonitor monitor) throws InvocationTargetException { |
|
79 try { |
|
80 doFinish(containerName, fileName, monitor); |
|
81 } catch (CoreException e) { |
|
82 throw new InvocationTargetException(e); |
|
83 } finally { |
|
84 monitor.done(); |
|
85 } |
|
86 } |
|
87 }; |
|
88 try { |
|
89 getContainer().run(true, false, op); |
|
90 } catch (InterruptedException e) { |
|
91 return false; |
|
92 } catch (InvocationTargetException e) { |
|
93 Throwable realException = e.getTargetException(); |
|
94 MessageDialog.openError(getShell(), "Error", realException.getMessage()); |
|
95 return false; |
|
96 } |
|
97 return true; |
|
98 } |
|
99 |
|
100 /** |
|
101 * The worker method. It will find the container, create the |
|
102 * file if missing or just replace its contents, and open |
|
103 * the editor on the newly created file. |
|
104 */ |
|
105 |
|
106 private void doFinish( |
|
107 String containerName, |
|
108 String fileName, |
|
109 IProgressMonitor monitor) |
|
110 throws CoreException { |
|
111 // create a sample file |
|
112 monitor.beginTask("Creating " + fileName, 2); |
|
113 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
|
114 IResource resource = root.findMember(new Path(containerName)); |
|
115 if (!resource.exists() || !(resource instanceof IContainer)) { |
|
116 throwCoreException("Container \"" + containerName + "\" does not exist."); |
|
117 } |
|
118 IContainer container = (IContainer) resource; |
|
119 final IFile file = container.getFile(new Path(fileName)); |
|
120 try { |
|
121 InputStream stream = openContentStream(); |
|
122 if (file.exists()) { |
|
123 file.setContents(stream, true, true, monitor); |
|
124 } else { |
|
125 file.create(stream, true, monitor); |
|
126 } |
|
127 stream.close(); |
|
128 } catch (IOException e) { |
|
129 } |
|
130 monitor.worked(1); |
|
131 monitor.setTaskName("Opening file for editing..."); |
|
132 getShell().getDisplay().asyncExec(new Runnable() { |
|
133 public void run() { |
|
134 IWorkbenchPage page = |
|
135 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
|
136 try { |
|
137 IDE.openEditor(page, file, true); |
|
138 } catch (PartInitException e) { |
|
139 } |
|
140 } |
|
141 }); |
|
142 monitor.worked(1); |
|
143 } |
|
144 |
|
145 /** |
|
146 * We will initialize file contents with a sample text. |
|
147 */ |
|
148 |
|
149 private InputStream openContentStream() { |
|
150 String contents = |
|
151 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" + |
|
152 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" + |
|
153 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" + |
|
154 "<head>\n" + |
|
155 "<title>Sample Widget</title>\n" + |
|
156 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" + |
|
157 "<script language=\"javascript\" type=\"text/javascript\" src=\"basic.js\"></script>\n" + |
|
158 "<link rel=\"stylesheet\" href=\"basic.css\" type=\"text/css\"/>\n" + |
|
159 "<meta name=\"Generator\" content=\"Symbian WRT IDE\" />\n" + |
|
160 "</head>\n" + |
|
161 "<body onload=\"javascript:init();\">\n" + |
|
162 "</body>\n</html>"; |
|
163 |
|
164 return new ByteArrayInputStream(contents.getBytes()); |
|
165 } |
|
166 |
|
167 private void throwCoreException(String message) throws CoreException { |
|
168 IStatus status = |
|
169 new Status(IStatus.ERROR, "org.symbian.tools.wrttools.jseditors", IStatus.OK, message, null); |
|
170 throw new CoreException(status); |
|
171 } |
|
172 |
|
173 /** |
|
174 * We will accept the selection in the workbench to see if |
|
175 * we can initialize from it. |
|
176 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) |
|
177 */ |
|
178 public void init(IWorkbench workbench, IStructuredSelection selection) { |
|
179 this.selection = selection; |
|
180 } |
|
181 } |
|