Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.team.internal.ui.actions; |
12 |
|
13 |
import java.util.*; |
14 |
|
15 |
import org.eclipse.core.resources.IFile; |
16 |
import org.eclipse.core.resources.IStorage; |
17 |
import org.eclipse.core.runtime.*; |
18 |
import org.eclipse.jface.action.ContributionItem; |
19 |
import org.eclipse.jface.resource.ImageDescriptor; |
20 |
import org.eclipse.jface.viewers.IStructuredSelection; |
21 |
import org.eclipse.jface.viewers.StructuredSelection; |
22 |
import org.eclipse.jface.window.Window; |
23 |
import org.eclipse.osgi.util.NLS; |
24 |
import org.eclipse.swt.SWT; |
25 |
import org.eclipse.swt.graphics.Image; |
26 |
import org.eclipse.swt.widgets.*; |
27 |
import org.eclipse.team.core.history.IFileRevision; |
28 |
import org.eclipse.team.internal.ui.TeamUIMessages; |
29 |
import org.eclipse.team.internal.ui.Utils; |
30 |
import org.eclipse.team.internal.ui.history.AbstractHistoryCategory; |
31 |
import org.eclipse.team.internal.ui.history.FileRevisionEditorInput; |
32 |
import org.eclipse.team.ui.history.HistoryPage; |
33 |
import org.eclipse.ui.*; |
34 |
import org.eclipse.ui.dialogs.EditorSelectionDialog; |
35 |
import org.eclipse.ui.internal.WorkbenchPage; |
36 |
import org.eclipse.ui.part.FileEditorInput; |
37 |
import org.eclipse.ui.statushandlers.*; |
38 |
|
39 |
import com.ibm.icu.text.Collator; |
40 |
|
41 |
/** |
42 |
* A menu for opening file revisions in the workbench. |
43 |
* <p> |
44 |
* An <code>OpenWithMenu</code> is used to populate a menu with "Open With" |
45 |
* actions. One action is added for each editor which is applicable to the |
46 |
* selected file. If the user selects one of these items, the corresponding |
47 |
* editor is opened on the file. |
48 |
* </p> |
49 |
*/ |
50 |
public class OpenWithMenu extends ContributionItem { |
51 |
|
52 |
private IStructuredSelection selection; |
53 |
|
54 |
private HistoryPage page; |
55 |
|
56 |
private IEditorRegistry registry = PlatformUI.getWorkbench() |
57 |
.getEditorRegistry(); |
58 |
|
59 |
private static Hashtable imageCache = new Hashtable(11); |
60 |
|
61 |
/** |
62 |
* The id of this action. |
63 |
*/ |
64 |
public static final String ID = PlatformUI.PLUGIN_ID + ".OpenWithMenu";//$NON-NLS-1$ |
65 |
|
66 |
/** |
67 |
* Match both the input and id, so that different types of editor can be |
68 |
* opened on the same input. |
69 |
*/ |
70 |
private static final int MATCH_BOTH = IWorkbenchPage.MATCH_INPUT |
71 |
| IWorkbenchPage.MATCH_ID; |
72 |
|
73 |
/* |
74 |
* Compares the labels from two IEditorDescriptor objects |
75 |
*/ |
76 |
private static final Comparator comparer = new Comparator() { |
77 |
private Collator collator = Collator.getInstance(); |
78 |
|
79 |
public int compare(Object arg0, Object arg1) { |
80 |
String s1 = ((IEditorDescriptor) arg0).getLabel(); |
81 |
String s2 = ((IEditorDescriptor) arg1).getLabel(); |
82 |
return collator.compare(s1, s2); |
83 |
} |
84 |
}; |
85 |
|
86 |
/** |
87 |
* Constructs a new instance of <code>OpenWithMenu</code>. |
88 |
* |
89 |
* @param page |
90 |
* the page where the editor is opened if an item within the menu |
91 |
* is selected |
92 |
*/ |
93 |
public OpenWithMenu(HistoryPage page) { |
94 |
super(ID); |
95 |
this.page = page; |
96 |
} |
97 |
|
98 |
/** |
99 |
* Returns an image to show for the corresponding editor descriptor. |
100 |
* |
101 |
* @param editorDesc |
102 |
* the editor descriptor, or null for the system editor |
103 |
* @return the image or null |
104 |
*/ |
105 |
private Image getImage(IEditorDescriptor editorDesc) { |
106 |
ImageDescriptor imageDesc = getImageDescriptor(editorDesc); |
107 |
if (imageDesc == null) { |
108 |
return null; |
109 |
} |
110 |
Image image = (Image) imageCache.get(imageDesc); |
111 |
if (image == null) { |
112 |
image = imageDesc.createImage(); |
113 |
imageCache.put(imageDesc, image); |
114 |
} |
115 |
return image; |
116 |
} |
117 |
|
118 |
/** |
119 |
* Returns the image descriptor for the given editor descriptor, or null if |
120 |
* it has no image. |
121 |
*/ |
122 |
private ImageDescriptor getImageDescriptor(IEditorDescriptor editorDesc) { |
123 |
ImageDescriptor imageDesc = null; |
124 |
if (editorDesc == null) { |
125 |
imageDesc = registry |
126 |
.getImageDescriptor(getFileRevision().getName()); |
127 |
// TODO: is this case valid, and if so, what are the implications |
128 |
// for content-type editor bindings? |
129 |
} else { |
130 |
imageDesc = editorDesc.getImageDescriptor(); |
131 |
} |
132 |
if (imageDesc == null) { |
133 |
if (editorDesc.getId().equals( |
134 |
IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID)) { |
135 |
imageDesc = registry |
136 |
.getSystemExternalEditorImageDescriptor(getFileRevision() |
137 |
.getName()); |
138 |
} |
139 |
} |
140 |
return imageDesc; |
141 |
} |
142 |
|
143 |
/** |
144 |
* Creates the menu item for the editor descriptor. |
145 |
* |
146 |
* @param menu |
147 |
* the menu to add the item to |
148 |
* @param descriptor |
149 |
* the editor descriptor, or null for the system editor |
150 |
* @param preferredEditor |
151 |
* the descriptor of the preferred editor, or <code>null</code> |
152 |
*/ |
153 |
private MenuItem createMenuItem(Menu menu, |
154 |
final IEditorDescriptor descriptor, |
155 |
final IEditorDescriptor preferredEditor) { |
156 |
// XXX: Would be better to use bold here, but SWT does not support it. |
157 |
final MenuItem menuItem = new MenuItem(menu, SWT.RADIO); |
158 |
boolean isPreferred = preferredEditor != null |
159 |
&& descriptor.getId().equals(preferredEditor.getId()); |
160 |
menuItem.setSelection(isPreferred); |
161 |
menuItem.setText(descriptor.getLabel()); |
162 |
Image image = getImage(descriptor); |
163 |
if (image != null) { |
164 |
menuItem.setImage(image); |
165 |
} |
166 |
Listener listener = new Listener() { |
167 |
public void handleEvent(Event event) { |
168 |
switch (event.type) { |
169 |
case SWT.Selection: |
170 |
if (menuItem.getSelection()) { |
171 |
openEditor(descriptor, false); |
172 |
} |
173 |
break; |
174 |
} |
175 |
} |
176 |
}; |
177 |
menuItem.addListener(SWT.Selection, listener); |
178 |
return menuItem; |
179 |
} |
180 |
|
181 |
/** |
182 |
* Creates the Other... menu item |
183 |
* |
184 |
* @param menu |
185 |
* the menu to add the item to |
186 |
*/ |
187 |
private void createOtherMenuItem(final Menu menu) { |
188 |
final IFileRevision fileResource = getFileRevision(); |
189 |
if (fileResource == null) { |
190 |
return; |
191 |
} |
192 |
new MenuItem(menu, SWT.SEPARATOR); |
193 |
final MenuItem menuItem = new MenuItem(menu, SWT.PUSH); |
194 |
menuItem.setText(TeamUIMessages.LocalHistoryPage_OpenWithMenu_Other); |
195 |
Listener listener = new Listener() { |
196 |
public void handleEvent(Event event) { |
197 |
switch (event.type) { |
198 |
case SWT.Selection: |
199 |
EditorSelectionDialog dialog = new EditorSelectionDialog( |
200 |
menu.getShell()); |
201 |
dialog |
202 |
.setMessage(NLS |
203 |
.bind( |
204 |
TeamUIMessages.LocalHistoryPage_OpenWithMenu_OtherDialogDescription, |
205 |
fileResource.getName())); |
206 |
if (dialog.open() == Window.OK) { |
207 |
IEditorDescriptor editor = dialog.getSelectedEditor(); |
208 |
if (editor != null) { |
209 |
openEditor(editor, editor.isOpenExternal()); |
210 |
} |
211 |
} |
212 |
break; |
213 |
} |
214 |
} |
215 |
}; |
216 |
menuItem.addListener(SWT.Selection, listener); |
217 |
} |
218 |
|
219 |
public void fill(Menu menu, int index) { |
220 |
final IFileRevision fileRevision = getFileRevision(); |
221 |
if (fileRevision == null) { |
222 |
return; |
223 |
} |
224 |
|
225 |
IEditorDescriptor defaultTextEditor = registry |
226 |
.findEditor("org.eclipse.ui.DefaultTextEditor"); //$NON-NLS-1$ |
227 |
IEditorDescriptor preferredEditor = Utils |
228 |
.getDefaultEditor(fileRevision); |
229 |
|
230 |
Object[] editors = Utils.getEditors(fileRevision); |
231 |
Collections.sort(Arrays.asList(editors), comparer); |
232 |
boolean defaultFound = false; |
233 |
|
234 |
// Check that we don't add it twice. This is possible |
235 |
// if the same editor goes to two mappings. |
236 |
ArrayList alreadyMapped = new ArrayList(); |
237 |
|
238 |
for (int i = 0; i < editors.length; i++) { |
239 |
IEditorDescriptor editor = (IEditorDescriptor) editors[i]; |
240 |
if (!alreadyMapped.contains(editor)) { |
241 |
createMenuItem(menu, editor, preferredEditor); |
242 |
if (defaultTextEditor != null |
243 |
&& editor.getId().equals(defaultTextEditor.getId())) { |
244 |
defaultFound = true; |
245 |
} |
246 |
alreadyMapped.add(editor); |
247 |
} |
248 |
} |
249 |
|
250 |
// Only add a separator if there is something to separate |
251 |
if (editors.length > 0) { |
252 |
new MenuItem(menu, SWT.SEPARATOR); |
253 |
} |
254 |
|
255 |
// Add default editor. Check it if it is saved as the preference. |
256 |
if (!defaultFound && defaultTextEditor != null) { |
257 |
createMenuItem(menu, defaultTextEditor, preferredEditor); |
258 |
} |
259 |
|
260 |
// TODO : We might perhaps enable inplace and system external editors menu items |
261 |
/*// Add system editor |
262 |
IEditorDescriptor descriptor = registry |
263 |
.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); |
264 |
final MenuItem systemEditorMenuItem = createMenuItem(menu, descriptor, |
265 |
preferredEditor); |
266 |
systemEditorMenuItem.setEnabled(false); |
267 |
|
268 |
// Add system in-place editor |
269 |
descriptor = registry |
270 |
.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); |
271 |
|
272 |
final MenuItem inPlaceEditorMenuItem = (descriptor != null) ? createMenuItem( |
273 |
menu, descriptor, preferredEditor) |
274 |
: null; |
275 |
if (inPlaceEditorMenuItem != null) |
276 |
inPlaceEditorMenuItem.setEnabled(false); |
277 |
|
278 |
Job job = new Job("updateOpenWithMenu") { //$NON-NLS-1$ |
279 |
protected IStatus run(IProgressMonitor monitor) { |
280 |
try { |
281 |
final boolean isFile = fileRevision.getStorage(monitor) instanceof IFile; |
282 |
Display.getDefault().asyncExec(new Runnable() { |
283 |
public void run() { |
284 |
if (inPlaceEditorMenuItem != null |
285 |
&& !inPlaceEditorMenuItem.isDisposed()) |
286 |
inPlaceEditorMenuItem.setEnabled(isFile); |
287 |
if (!systemEditorMenuItem.isDisposed()) |
288 |
systemEditorMenuItem.setEnabled(isFile); |
289 |
} |
290 |
}); |
291 |
return Status.OK_STATUS; |
292 |
} catch (CoreException e) { |
293 |
return new Status(IStatus.WARNING, TeamUIPlugin.ID, null, e); |
294 |
} |
295 |
}; |
296 |
}; |
297 |
job.setSystem(true); |
298 |
job.schedule();*/ |
299 |
|
300 |
createDefaultMenuItem(menu, fileRevision); |
301 |
|
302 |
// add Other... menu item |
303 |
createOtherMenuItem(menu); |
304 |
} |
305 |
|
306 |
public void createDefaultMenuItem(Menu menu, final IFileRevision revision) { |
307 |
final MenuItem menuItem = new MenuItem(menu, SWT.RADIO); |
308 |
menuItem.setSelection(Utils.getDefaultEditor(revision) == null); |
309 |
menuItem |
310 |
.setText(TeamUIMessages.LocalHistoryPage_OpenWithMenu_DefaultEditorDescription); |
311 |
|
312 |
Listener listener = new Listener() { |
313 |
public void handleEvent(Event event) { |
314 |
switch (event.type) { |
315 |
case SWT.Selection: |
316 |
if (menuItem.getSelection()) { |
317 |
openEditor(Utils.getDefaultEditor(revision), false); |
318 |
} |
319 |
break; |
320 |
} |
321 |
} |
322 |
}; |
323 |
|
324 |
menuItem.addListener(SWT.Selection, listener); |
325 |
} |
326 |
|
327 |
/** |
328 |
* Opens the given editor on the selected file revision. |
329 |
*/ |
330 |
protected void openEditor(IEditorDescriptor editorDescriptor, |
331 |
boolean openUsingDescriptor) { |
332 |
IFileRevision fileRevision = getFileRevision(); |
333 |
if (fileRevision == null) { |
334 |
return; |
335 |
} |
336 |
try { |
337 |
IProgressMonitor monitor = new NullProgressMonitor(); |
338 |
IStorage storage = fileRevision.getStorage(monitor); |
339 |
boolean isFile = storage instanceof IFile; |
340 |
|
341 |
if (openUsingDescriptor) { |
342 |
// discouraged access to open system editors |
343 |
((WorkbenchPage) (page.getSite().getPage())) |
344 |
.openEditorFromDescriptor(isFile ? new FileEditorInput( |
345 |
(IFile) storage) |
346 |
: (IEditorInput) FileRevisionEditorInput |
347 |
.createEditorInputFor(fileRevision, |
348 |
monitor), editorDescriptor, |
349 |
true, null); |
350 |
} else { |
351 |
String editorId = editorDescriptor == null ? IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID |
352 |
: editorDescriptor.getId(); |
353 |
page.getSite().getPage().openEditor( |
354 |
isFile ? new FileEditorInput((IFile) storage) |
355 |
: (IEditorInput) FileRevisionEditorInput |
356 |
.createEditorInputFor(fileRevision, |
357 |
monitor), editorId, true, |
358 |
MATCH_BOTH); |
359 |
} |
360 |
} catch (PartInitException e) { |
361 |
StatusAdapter sA = new StatusAdapter(e.getStatus()); |
362 |
sA.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, |
363 |
TeamUIMessages.LocalHistoryPage_OpenEditorError); |
364 |
StatusManager.getManager().handle(sA, StatusManager.SHOW); |
365 |
} catch (CoreException e) { |
366 |
StatusAdapter sA = new StatusAdapter(e.getStatus()); |
367 |
sA.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, |
368 |
TeamUIMessages.LocalHistoryPage_OpenEditorError); |
369 |
StatusManager.getManager().handle(sA, StatusManager.LOG); |
370 |
} |
371 |
} |
372 |
|
373 |
private IFileRevision getFileRevision() { |
374 |
IStructuredSelection structSel = selection; |
375 |
|
376 |
IFileRevision revision = null; |
377 |
|
378 |
if (structSel == null) |
379 |
return null; |
380 |
|
381 |
Object[] objArray = structSel.toArray(); |
382 |
|
383 |
for (int i = 0; i < objArray.length; i++) { |
384 |
Object tempRevision = objArray[i]; |
385 |
// If not a revision, don't try opening |
386 |
if (tempRevision instanceof AbstractHistoryCategory) |
387 |
continue; |
388 |
|
389 |
revision = (IFileRevision) tempRevision; |
390 |
} |
391 |
return revision; |
392 |
} |
393 |
|
394 |
/* |
395 |
* (non-Javadoc) Returns whether this menu is dynamic. |
396 |
*/ |
397 |
public boolean isDynamic() { |
398 |
return true; |
399 |
} |
400 |
|
401 |
public void selectionChanged(IStructuredSelection selection) { |
402 |
if (selection instanceof IStructuredSelection) { |
403 |
this.selection = selection; |
404 |
} else { |
405 |
this.selection = StructuredSelection.EMPTY; |
406 |
} |
407 |
} |
408 |
} |