2
|
1 |
/*
|
|
2 |
* Copyright (c) 2005 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.sdt.sourcegen;
|
|
19 |
|
|
20 |
import com.nokia.sdt.datamodel.IDesignerDataModel;
|
|
21 |
import com.nokia.sdt.editor.EditorServices;
|
|
22 |
import com.nokia.sdt.editor.IDesignerDataModelEditor;
|
|
23 |
import com.nokia.sdt.editor.EditorServices.EditorListener;
|
|
24 |
import com.nokia.sdt.editor.IDesignerDataModelEditor.SaveListener;
|
|
25 |
import com.nokia.sdt.sourcegen.core.Messages;
|
|
26 |
import com.nokia.cpp.internal.api.utils.core.Logging;
|
|
27 |
import com.nokia.cpp.internal.api.utils.core.TextUtils;
|
|
28 |
import com.nokia.cpp.internal.api.utils.ui.UITaskUtils;
|
|
29 |
|
|
30 |
import org.eclipse.core.resources.*;
|
|
31 |
import org.eclipse.core.runtime.*;
|
|
32 |
import org.eclipse.jface.dialogs.IDialogConstants;
|
|
33 |
import org.eclipse.jface.dialogs.MessageDialog;
|
|
34 |
import org.eclipse.jface.operation.IRunnableWithProgress;
|
|
35 |
import org.eclipse.jface.resource.ImageDescriptor;
|
|
36 |
import org.eclipse.jface.util.SafeRunnable;
|
|
37 |
import org.eclipse.swt.widgets.Shell;
|
|
38 |
import org.eclipse.ui.*;
|
|
39 |
import org.eclipse.ui.dialogs.ListSelectionDialog;
|
|
40 |
import org.eclipse.ui.model.*;
|
|
41 |
import org.eclipse.ui.plugin.AbstractUIPlugin;
|
|
42 |
import org.osgi.framework.BundleContext;
|
|
43 |
|
|
44 |
import java.lang.reflect.InvocationTargetException;
|
|
45 |
import java.text.MessageFormat;
|
|
46 |
import java.util.*;
|
|
47 |
import java.util.regex.Pattern;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* The main plugin class to be used in the desktop (auto-generated)
|
|
51 |
*/
|
|
52 |
public class SourceGenPlugin extends AbstractUIPlugin implements SaveListener, EditorListener {
|
|
53 |
//The shared instance.
|
|
54 |
private static SourceGenPlugin plugin;
|
|
55 |
//Resource bundle.
|
|
56 |
private ResourceBundle resourceBundle;
|
|
57 |
|
|
58 |
// Pattern matcher for files which might be written by sourcegen
|
|
59 |
static final Pattern sourcePattern = Pattern.compile(
|
|
60 |
".*\\.(c|cpp|h|hpp|ra|rss|rssi|loc|l[0-9]{2}|rls)$"); //$NON-NLS-1$
|
|
61 |
|
|
62 |
|
|
63 |
/**
|
|
64 |
* The constructor.
|
|
65 |
*/
|
|
66 |
public SourceGenPlugin() {
|
|
67 |
super();
|
|
68 |
plugin = this;
|
|
69 |
|
|
70 |
// attach to any open editors
|
|
71 |
IEditorPart[] editors = EditorServices.getEditors();
|
|
72 |
for (int i = 0; i < editors.length; i++) {
|
|
73 |
editorAdded(editors[i]);
|
|
74 |
}
|
|
75 |
EditorServices.addListener(this);
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* This method is called upon plug-in activation
|
|
80 |
*/
|
|
81 |
public void start(BundleContext context) throws Exception {
|
|
82 |
super.start(context);
|
|
83 |
EditorServices.addListener(this);
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* This method is called when the plug-in is stopped
|
|
88 |
*/
|
|
89 |
public void stop(BundleContext context) throws Exception {
|
|
90 |
super.stop(context);
|
|
91 |
EditorServices.removeListener(this);
|
|
92 |
plugin = null;
|
|
93 |
resourceBundle = null;
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns the shared instance.
|
|
98 |
*/
|
|
99 |
public static SourceGenPlugin getDefault() {
|
|
100 |
return plugin;
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Returns the string from the plugin's resource bundle,
|
|
105 |
* or 'key' if not found.
|
|
106 |
*/
|
|
107 |
public static String getResourceString(String key) {
|
|
108 |
ResourceBundle bundle = SourceGenPlugin.getDefault().getResourceBundle();
|
|
109 |
try {
|
|
110 |
return (bundle != null) ? bundle.getString(key) : key;
|
|
111 |
} catch (MissingResourceException e) {
|
|
112 |
return key;
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Returns the plugin's resource bundle,
|
|
118 |
*/
|
|
119 |
public ResourceBundle getResourceBundle() {
|
|
120 |
try {
|
|
121 |
if (resourceBundle == null)
|
|
122 |
resourceBundle = ResourceBundle.getBundle("com.nokia.sdt.sourcegen.SourceGenPluginResources"); //$NON-NLS-1$
|
|
123 |
} catch (MissingResourceException x) {
|
|
124 |
resourceBundle = null;
|
|
125 |
}
|
|
126 |
return resourceBundle;
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Returns an image descriptor for the image file at the given
|
|
131 |
* plug-in relative path.
|
|
132 |
*
|
|
133 |
* @param path the path
|
|
134 |
* @return the image descriptor
|
|
135 |
*/
|
|
136 |
public static ImageDescriptor getImageDescriptor(String path) {
|
|
137 |
return AbstractUIPlugin.imageDescriptorFromPlugin("com.nokia.sdt.sourcegen", path); //$NON-NLS-1$
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
*/
|
|
142 |
public void log(Throwable t) {
|
|
143 |
IStatus status = Logging.newSimpleStatus(0, t);
|
|
144 |
Logging.log(this, status);
|
|
145 |
}
|
|
146 |
|
|
147 |
public IStatus createStatus(int severity, String message, Throwable t) {
|
|
148 |
return Logging.newStatus(this, severity, message, t);
|
|
149 |
}
|
|
150 |
|
|
151 |
static interface IEditorPartFilter {
|
|
152 |
public boolean accept(IEditorPart part);
|
|
153 |
}
|
|
154 |
|
|
155 |
/**
|
|
156 |
* Save editors that appear to be source-related.
|
|
157 |
*/
|
|
158 |
public boolean saveAllSourceEditors(final IEditorPartFilter filter, boolean confirm) {
|
|
159 |
final IWorkbench workbench = PlatformUI.getWorkbench();
|
|
160 |
final boolean finalConfirm = confirm;
|
|
161 |
final boolean[] result = new boolean[1];
|
|
162 |
result[0] = true;
|
|
163 |
|
|
164 |
SafeRunner.run(new SafeRunnable() {
|
|
165 |
public void run() {
|
|
166 |
// Collect unsaved parts
|
|
167 |
ArrayList dirtyParts = new ArrayList();
|
|
168 |
ArrayList dirtyEditorsInput = new ArrayList();
|
|
169 |
IWorkbenchWindow windows[] = workbench.getWorkbenchWindows();
|
|
170 |
for (int i = 0; i < windows.length; i++) {
|
|
171 |
IWorkbenchPage pages[] = windows[i].getPages();
|
|
172 |
for (int j = 0; j < pages.length; j++) {
|
|
173 |
IWorkbenchPage page = (IWorkbenchPage) pages[j];
|
|
174 |
|
|
175 |
ISaveablePart[] parts = page.getDirtyEditors();
|
|
176 |
|
|
177 |
for (int k = 0; k < parts.length; k++) {
|
|
178 |
ISaveablePart part = parts[k];
|
|
179 |
|
|
180 |
if (part instanceof IEditorPart) {
|
|
181 |
IEditorPart editor = (IEditorPart)part;
|
|
182 |
if (editor.isSaveOnCloseNeeded() && filter.accept(editor)) {
|
|
183 |
if (!dirtyEditorsInput.contains(editor
|
|
184 |
.getEditorInput())) {
|
|
185 |
dirtyParts.add(editor);
|
|
186 |
dirtyEditorsInput.add(editor
|
|
187 |
.getEditorInput());
|
|
188 |
}
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
if (dirtyParts.size() > 0) {
|
|
196 |
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
|
|
197 |
if (window == null)
|
|
198 |
window = windows[0];
|
|
199 |
result[0] = confirmAndSaveAll(dirtyParts, finalConfirm, window);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
});
|
|
203 |
return result[0];
|
|
204 |
}
|
|
205 |
|
|
206 |
/**
|
|
207 |
* Put up a dialog showing possible sources that will be
|
|
208 |
* modified and allow the user to select items to save,
|
|
209 |
* and save the selected items.
|
|
210 |
* <p>
|
|
211 |
* This is a ripoff of the Eclipse EditorManager#saveAll()
|
|
212 |
* method, which is internal.
|
|
213 |
* @param dirtyParts
|
|
214 |
* @return true: saving confirmed, false: save canceled
|
|
215 |
*/
|
|
216 |
private boolean confirmAndSaveAll(List dirtyParts, boolean confirm, IWorkbenchWindow window) {
|
|
217 |
// If the editor list is empty return.
|
|
218 |
if (dirtyParts.isEmpty())
|
|
219 |
return true;
|
|
220 |
|
|
221 |
// Convert the list into an element collection.
|
|
222 |
AdaptableList input = new AdaptableList(dirtyParts);
|
|
223 |
|
|
224 |
ListSelectionDialog dlg = new ListSelectionDialog(
|
|
225 |
window.getShell(), input,
|
|
226 |
new BaseWorkbenchContentProvider(),
|
|
227 |
new WorkbenchPartLabelProvider(),
|
|
228 |
Messages.getString("SourceGenPlugin.SelectResources")); //$NON-NLS-1$
|
|
229 |
|
|
230 |
dlg.setInitialSelections(dirtyParts
|
|
231 |
.toArray(new Object[dirtyParts.size()]));
|
|
232 |
dlg.setTitle(Messages.getString("SourceGenPlugin.SaveSourcesTitle")); //$NON-NLS-1$
|
|
233 |
int result = dlg.open();
|
|
234 |
|
|
235 |
if (result == IDialogConstants.CANCEL_ID)
|
|
236 |
return false;
|
|
237 |
|
|
238 |
final List savingDirtyParts = Arrays.asList(dlg.getResult());
|
|
239 |
|
|
240 |
final boolean[] saveSucceeded = new boolean[1];
|
|
241 |
|
|
242 |
saveSucceeded[0] = true;
|
|
243 |
UITaskUtils.runImmediately(new IRunnableWithProgress() {
|
|
244 |
|
|
245 |
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
|
246 |
monitor.beginTask(Messages.getString("SourceGenPlugin.SavingEditors"), savingDirtyParts.size()); //$NON-NLS-1$
|
|
247 |
for (Iterator iter = savingDirtyParts.iterator(); iter.hasNext();) {
|
|
248 |
if (monitor.isCanceled())
|
|
249 |
saveSucceeded[0] = false;
|
|
250 |
IEditorPart editor = (IEditorPart) iter.next();
|
|
251 |
editor.doSave(monitor);
|
|
252 |
monitor.worked(1);
|
|
253 |
}
|
|
254 |
monitor.done();
|
|
255 |
}
|
|
256 |
|
|
257 |
});
|
|
258 |
|
|
259 |
return saveSucceeded[0];
|
|
260 |
}
|
|
261 |
|
|
262 |
/**
|
|
263 |
* Check that the workspace is synchronized and in a
|
|
264 |
* sane state to save sources.
|
|
265 |
* @return true: everything is up-to-date, false: cancel the save
|
|
266 |
*/
|
|
267 |
private boolean checkSynchronizedWorkspace(IDesignerDataModel dataModel) {
|
|
268 |
// ask to save any source editors
|
|
269 |
if (dataModel.getProjectContext() == null)
|
|
270 |
return true;
|
|
271 |
final IProject theProject = dataModel.getProjectContext().getProject();
|
|
272 |
if (theProject == null)
|
|
273 |
return true;
|
|
274 |
|
|
275 |
IEditorPartFilter filter = new IEditorPartFilter() {
|
|
276 |
|
|
277 |
public boolean accept(IEditorPart part) {
|
|
278 |
// only look at stuff in this project
|
|
279 |
IResource rsrc = (IResource) part.getEditorInput().getAdapter(IResource.class);
|
|
280 |
if (rsrc != null) {
|
|
281 |
IProject project = rsrc.getProject();
|
|
282 |
if (project == null || !project.equals(theProject))
|
|
283 |
return false;
|
|
284 |
}
|
|
285 |
String name = part.getEditorInput().getName();
|
|
286 |
return sourcePattern.matcher(name).matches();
|
|
287 |
}
|
|
288 |
};
|
|
289 |
if (!saveAllSourceEditors(filter, true))
|
|
290 |
return false;
|
|
291 |
|
|
292 |
// check for changes made outside workspace
|
|
293 |
IProject project = null;
|
|
294 |
if (dataModel.getProjectContext() != null
|
|
295 |
&& (project = dataModel.getProjectContext().getProject()) != null) {
|
|
296 |
|
|
297 |
final List<IPath> unsynchronizedResources = new ArrayList<IPath>();
|
|
298 |
|
|
299 |
IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
|
|
300 |
public boolean visit(IResourceProxy resourceProxy) throws CoreException {
|
|
301 |
String name = resourceProxy.getName();
|
|
302 |
boolean looksLikeSource = sourcePattern.matcher(name).matches();
|
|
303 |
if (looksLikeSource || resourceProxy.getType() == IResource.FOLDER) {
|
|
304 |
IResource resource = resourceProxy.requestResource();
|
|
305 |
if (resource != null && !resource.isSynchronized(IResource.DEPTH_ZERO))
|
|
306 |
unsynchronizedResources.add(resource.getProjectRelativePath());
|
|
307 |
}
|
|
308 |
return resourceProxy.getType() == IResource.FOLDER ||
|
|
309 |
resourceProxy.getType() == IResource.PROJECT;
|
|
310 |
}
|
|
311 |
};
|
|
312 |
|
|
313 |
try {
|
|
314 |
project.accept(visitor, 0);
|
|
315 |
if (unsynchronizedResources.size() == 0)
|
|
316 |
return true;
|
|
317 |
} catch (CoreException e1) {
|
|
318 |
SourceGenPlugin.getDefault().log(e1);
|
|
319 |
// fall through, something bad happened
|
|
320 |
}
|
|
321 |
|
|
322 |
Shell shell = null;
|
|
323 |
boolean doSync = MessageDialog.openConfirm(shell,
|
|
324 |
Messages.getString("SourceGenProvider.ProjectOutOfSync"), //$NON-NLS-1$
|
|
325 |
MessageFormat.format(
|
|
326 |
Messages.getString("SourceGenProvider.ProjectOutOfSyncMessage"), //$NON-NLS-1$
|
|
327 |
new Object[] { project.getName(),
|
|
328 |
TextUtils.formatTabbedList(unsynchronizedResources) }));
|
|
329 |
if (doSync) {
|
|
330 |
try {
|
|
331 |
project.refreshLocal(IResource.DEPTH_INFINITE, null);
|
|
332 |
return true;
|
|
333 |
} catch (CoreException e) {
|
|
334 |
SourceGenPlugin.getDefault().log(e);
|
|
335 |
Logging.showErrorDialog(Messages.getString("SourceGenProvider.Error"), //$NON-NLS-1$
|
|
336 |
null, e.getStatus());
|
|
337 |
return false;
|
|
338 |
}
|
|
339 |
} else {
|
|
340 |
return false;
|
|
341 |
}
|
|
342 |
}
|
|
343 |
return true;
|
|
344 |
}
|
|
345 |
|
|
346 |
|
|
347 |
/* (non-Javadoc)
|
|
348 |
* @see com.nokia.sdt.ui.IDesignerEditorBase.SaveListener#queryAboutToSave(com.nokia.sdt.ui.IDesignerEditorBase)
|
|
349 |
*/
|
|
350 |
public boolean queryAboutToSave(IDesignerDataModelEditor editor) {
|
|
351 |
return checkSynchronizedWorkspace(editor.getDataModel());
|
|
352 |
}
|
|
353 |
|
|
354 |
/* (non-Javadoc)
|
|
355 |
* @see com.nokia.sdt.ui.IDesignerEditorBase.SaveListener#preSaveNotify(com.nokia.sdt.ui.IDesignerEditorBase)
|
|
356 |
*/
|
|
357 |
public void preSaveNotify(IDesignerDataModelEditor editor, IProgressMonitor monitor) {
|
|
358 |
}
|
|
359 |
|
|
360 |
/* (non-Javadoc)
|
|
361 |
* @see com.nokia.sdt.ui.IDesignerEditorBase.SaveListener#postSaveNotify(com.nokia.sdt.ui.IDesignerEditorBase)
|
|
362 |
*/
|
|
363 |
public void postSaveNotify(IDesignerDataModelEditor editor, IProgressMonitor monitor) {
|
|
364 |
}
|
|
365 |
|
|
366 |
/* (non-Javadoc)
|
|
367 |
* @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
|
|
368 |
*/
|
|
369 |
public void editorAdded(IEditorPart part) {
|
|
370 |
IDesignerDataModelEditor editor = (IDesignerDataModelEditor) part.getAdapter(IDesignerDataModelEditor.class);
|
|
371 |
if (editor != null)
|
|
372 |
editor.addSaveListener(SourceGenPlugin.this);
|
|
373 |
}
|
|
374 |
|
|
375 |
public void editorRemoved(IEditorPart part) {
|
|
376 |
IDesignerDataModelEditor editor = (IDesignerDataModelEditor) part.getAdapter(IDesignerDataModelEditor.class);
|
|
377 |
if (editor != null)
|
|
378 |
editor.removeSaveListener(SourceGenPlugin.this);
|
|
379 |
}
|
|
380 |
}
|