|
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 the License "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 |
|
18 package com.nokia.carbide.cpp.internal.pi.save; |
|
19 |
|
20 import java.io.ByteArrayInputStream; |
|
21 import java.io.IOException; |
|
22 import java.io.InputStream; |
|
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.IResource; |
|
28 import org.eclipse.core.resources.IWorkspaceRoot; |
|
29 import org.eclipse.core.resources.ResourcesPlugin; |
|
30 import org.eclipse.core.runtime.CoreException; |
|
31 import org.eclipse.core.runtime.IPath; |
|
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.Display; |
|
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.carbide.cpp.internal.pi.interfaces.ISaveTable; |
|
51 |
|
52 public class SaveTableWizard extends Wizard implements INewWizard { |
|
53 |
|
54 /** |
|
55 * This is a sample new wizard. Its role is to create a new file |
|
56 * resource in the provided container. If the container resource |
|
57 * (a folder or a project) is selected in the workspace |
|
58 * when the wizard is opened, it will accept it as the target |
|
59 * container. The wizard creates one file with the extension |
|
60 * "mpe". If a sample multi-page editor (also available |
|
61 * as a template) is registered for the same extension, it will |
|
62 * be able to open it. |
|
63 */ |
|
64 |
|
65 private SaveTableWizardPage page; |
|
66 private ISelection selection; |
|
67 private ISaveTable saveTable; |
|
68 private String saveTableFileContents; |
|
69 |
|
70 /** |
|
71 * Constructor for SampleNewWizard. |
|
72 */ |
|
73 public SaveTableWizard(ISaveTable saveTable) { |
|
74 super(); |
|
75 this.saveTable = saveTable; |
|
76 this.setWindowTitle(Messages.getString("SaveTableWizard.SavingTableData")); //$NON-NLS-1$ |
|
77 setNeedsProgressMonitor(true); |
|
78 } |
|
79 |
|
80 /** |
|
81 * Adding the page to the wizard. |
|
82 */ |
|
83 |
|
84 public void addPages() { |
|
85 page = new SaveTableWizardPage(selection); |
|
86 addPage(page); |
|
87 } |
|
88 |
|
89 /** |
|
90 * This method is called when 'Finish' button is pressed in |
|
91 * the wizard. We will create an operation and run it |
|
92 * using wizard as execution context. |
|
93 */ |
|
94 public boolean performFinish() { |
|
95 final IPath containerName = page.getContainerName(); |
|
96 final String fileName = page.getFileName(); |
|
97 IRunnableWithProgress op = new IRunnableWithProgress() { |
|
98 public void run(IProgressMonitor monitor) throws InvocationTargetException { |
|
99 try { |
|
100 doFinish(containerName, fileName, monitor); |
|
101 } catch (CoreException e) { |
|
102 throw new InvocationTargetException(e); |
|
103 } finally { |
|
104 monitor.done(); |
|
105 } |
|
106 } |
|
107 }; |
|
108 try { |
|
109 getContainer().run(true, false, op); |
|
110 } catch (InterruptedException e) { |
|
111 return false; |
|
112 } catch (InvocationTargetException e) { |
|
113 Throwable realException = e.getTargetException(); |
|
114 MessageDialog.openError(getShell(), Messages.getString("SaveTableWizard.Error"), 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( |
|
127 IPath containerName, |
|
128 String fileName, |
|
129 IProgressMonitor monitor) |
|
130 throws CoreException { |
|
131 // create a sample file |
|
132 monitor.beginTask(Messages.getString("SaveTableWizard.Creating") + fileName, 3); //$NON-NLS-1$ |
|
133 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
|
134 IResource resource = root.findMember(containerName); |
|
135 if (!resource.exists() || !(resource instanceof IContainer)) { |
|
136 throwCoreException(Messages.getString("SaveTableWizard.ProjectFolder") + containerName + Messages.getString("SaveTableWizard.doesNotExist")); //$NON-NLS-1$//$NON-NLS-2$ |
|
137 } |
|
138 monitor.worked(1); |
|
139 monitor.setTaskName(Messages.getString("SaveTableWizard.WritingTableData")); //$NON-NLS-1$ |
|
140 IContainer container = (IContainer) resource; |
|
141 final IFile file = container.getFile(new Path(fileName)); |
|
142 try { |
|
143 InputStream stream = openContentStream(); |
|
144 if (file.exists()) { |
|
145 file.setContents(stream, true, true, monitor); |
|
146 } else { |
|
147 file.create(stream, true, monitor); |
|
148 } |
|
149 stream.close(); |
|
150 } catch (IOException e) { |
|
151 } |
|
152 monitor.worked(1); |
|
153 monitor.setTaskName(Messages.getString("SaveTableWizard.OpeningFileForEditing")); //$NON-NLS-1$ |
|
154 getShell().getDisplay().asyncExec(new Runnable() { |
|
155 public void run() { |
|
156 IWorkbenchPage page = |
|
157 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
|
158 try { |
|
159 IDE.openEditor(page, file, true); |
|
160 } catch (PartInitException e) { |
|
161 } |
|
162 } |
|
163 }); |
|
164 monitor.worked(1); |
|
165 } |
|
166 |
|
167 /** |
|
168 * We will initialize file contents with a sample text. |
|
169 */ |
|
170 private InputStream openContentStream() { |
|
171 Display.getDefault().syncExec(new Runnable() { |
|
172 public void run() { |
|
173 saveTableFileContents = saveTable.getData();; |
|
174 } |
|
175 }); |
|
176 return new ByteArrayInputStream(saveTableFileContents.getBytes()); |
|
177 } |
|
178 |
|
179 private void throwCoreException(String message) throws CoreException { |
|
180 IStatus status = |
|
181 new Status(IStatus.ERROR, "junk", IStatus.OK, message, null); //$NON-NLS-1$ |
|
182 throw new CoreException(status); |
|
183 } |
|
184 |
|
185 /** |
|
186 * We will accept the selection in the workbench to see if |
|
187 * we can initialize from it. |
|
188 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) |
|
189 */ |
|
190 public void init(IWorkbench workbench, IStructuredSelection selection) { |
|
191 this.selection = selection; |
|
192 } |
|
193 } |