KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > IDE


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.ui.ide;
12
13 import java.net.URI JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.osgi.util.NLS;
20
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.core.filesystem.EFS;
25 import org.eclipse.core.filesystem.IFileStore;
26
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IAdaptable;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.MultiStatus;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.core.runtime.QualifiedName;
33 import org.eclipse.core.runtime.SafeRunner;
34 import org.eclipse.core.runtime.content.IContentDescription;
35 import org.eclipse.core.runtime.content.IContentType;
36 import org.eclipse.core.runtime.content.IContentTypeMatcher;
37
38 import org.eclipse.core.resources.IFile;
39 import org.eclipse.core.resources.IMarker;
40 import org.eclipse.core.resources.IResource;
41 import org.eclipse.core.resources.IResourceDelta;
42 import org.eclipse.core.resources.IWorkspaceRoot;
43 import org.eclipse.core.resources.ResourcesPlugin;
44 import org.eclipse.core.resources.mapping.IModelProviderDescriptor;
45 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
46 import org.eclipse.core.resources.mapping.ModelProvider;
47 import org.eclipse.core.resources.mapping.ModelStatus;
48 import org.eclipse.core.resources.mapping.ResourceChangeValidator;
49 import org.eclipse.core.resources.mapping.ResourceMapping;
50 import org.eclipse.core.resources.mapping.ResourceMappingContext;
51 import org.eclipse.core.resources.mapping.ResourceTraversal;
52
53 import org.eclipse.jface.dialogs.ErrorDialog;
54 import org.eclipse.jface.dialogs.IDialogConstants;
55 import org.eclipse.jface.util.SafeRunnable;
56 import org.eclipse.jface.viewers.IStructuredSelection;
57
58 import org.eclipse.ui.IEditorDescriptor;
59 import org.eclipse.ui.IEditorInput;
60 import org.eclipse.ui.IEditorPart;
61 import org.eclipse.ui.IEditorRegistry;
62 import org.eclipse.ui.IMarkerHelpRegistry;
63 import org.eclipse.ui.ISaveableFilter;
64 import org.eclipse.ui.IWorkbenchPage;
65 import org.eclipse.ui.IWorkbenchPart;
66 import org.eclipse.ui.IWorkbenchWindow;
67 import org.eclipse.ui.PartInitException;
68 import org.eclipse.ui.PlatformUI;
69 import org.eclipse.ui.Saveable;
70 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
71 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
72 import org.eclipse.ui.internal.ide.registry.MarkerHelpRegistry;
73 import org.eclipse.ui.internal.ide.registry.MarkerHelpRegistryReader;
74 import org.eclipse.ui.internal.misc.UIStats;
75 import org.eclipse.ui.part.FileEditorInput;
76
77 /**
78  * Collection of IDE-specific APIs factored out of existing workbench. This
79  * class cannot be instantiated; all functionality is provided by static methods
80  * and fields.
81  *
82  * @since 3.0
83  */

84 public final class IDE {
85     /**
86      * The persistent property key used on IFile resources to contain the
87      * preferred editor ID to use.
88      * <p>
89      * Example of retrieving the persisted editor id:
90      *
91      * <pre><code>
92      * IFile file = ...
93      * IEditorDescriptor editorDesc = null;
94      * try {
95      * String editorID = file.getPersistentProperty(EDITOR_KEY);
96      * if (editorID != null) {
97      * editorDesc = editorReg.findEditor(editorID);
98      * }
99      * } catch (CoreException e) {
100      * // handle problem accessing persistent property here
101      * }
102      * </code></pre>
103      *
104      * </p>
105      * <p>
106      * Example of persisting the editor id:
107      *
108      * <pre><code>
109      * IFile file = ...
110      * try {
111      * file.setPersistentProperty(EDITOR_KEY, editorDesc.getId());
112      * } catch (CoreException e) {
113      * // handle problem setting persistent property here
114      * }
115      * </code></pre>
116      *
117      * </p>
118      */

119     public static final QualifiedName EDITOR_KEY = new QualifiedName(
120             "org.eclipse.ui.internal.registry.ResourceEditorRegistry", "EditorProperty");//$NON-NLS-2$//$NON-NLS-1$
121

122     /**
123      * An optional attribute within a workspace marker (<code>IMarker</code>)
124      * which identifies the preferred editor type to be opened.
125      */

126     public static final String JavaDoc EDITOR_ID_ATTR = "org.eclipse.ui.editorID"; //$NON-NLS-1$
127

128     /**
129      * The resource based perspective identifier.
130      */

131     public static final String JavaDoc RESOURCE_PERSPECTIVE_ID = "org.eclipse.ui.resourcePerspective"; //$NON-NLS-1$
132

133     /**
134      * Marker help registry mapping markers to help context ids and resolutions;
135      * lazily initialized on fist access.
136      */

137     private static MarkerHelpRegistry markerHelpRegistry = null;
138
139     /**
140      * Standard shared images defined by the IDE. These are over and above the
141      * standard workbench images declared in {@link org.eclipse.ui.ISharedImages
142      * ISharedImages}.
143      * <p>
144      * This interface is not intended to be implemented by clients.
145      * </p>
146      *
147      * @see org.eclipse.ui.ISharedImages
148      */

149     public interface SharedImages {
150         /**
151          * Identifies a project image.
152          */

153         public final static String JavaDoc IMG_OBJ_PROJECT = "IMG_OBJ_PROJECT"; //$NON-NLS-1$
154

155         /**
156          * Identifies a closed project image.
157          */

158         public final static String JavaDoc IMG_OBJ_PROJECT_CLOSED = "IMG_OBJ_PROJECT_CLOSED"; //$NON-NLS-1$
159

160         /**
161          * Identifies the image used for "open marker".
162          */

163         public final static String JavaDoc IMG_OPEN_MARKER = "IMG_OPEN_MARKER"; //$NON-NLS-1$
164

165         /**
166          * Identifies the default image used to indicate a task.
167          */

168         public final static String JavaDoc IMG_OBJS_TASK_TSK = "IMG_OBJS_TASK_TSK"; //$NON-NLS-1$
169

170         /**
171          * Identifies the default image used to indicate a bookmark.
172          */

173         public final static String JavaDoc IMG_OBJS_BKMRK_TSK = "IMG_OBJS_BKMRK_TSK"; //$NON-NLS-1$
174
}
175
176     /**
177      * Preferences defined by the IDE workbench.
178      * <p>
179      * This interface is not intended to be implemented by clients.
180      * </p>
181      */

182     public interface Preferences {
183
184         /**
185          * A named preference for how a new perspective should be opened when a
186          * new project is created.
187          * <p>
188          * Value is of type <code>String</code>. The possible values are
189          * defined by the constants
190          * <code>OPEN_PERSPECTIVE_WINDOW, OPEN_PERSPECTIVE_PAGE,
191          * OPEN_PERSPECTIVE_REPLACE, and NO_NEW_PERSPECTIVE</code>.
192          * </p>
193          *
194          * @see org.eclipse.ui.IWorkbenchPreferenceConstants#OPEN_PERSPECTIVE_WINDOW
195          * @see org.eclipse.ui.IWorkbenchPreferenceConstants#OPEN_PERSPECTIVE_PAGE
196          * @see org.eclipse.ui.IWorkbenchPreferenceConstants#OPEN_PERSPECTIVE_REPLACE
197          * @see org.eclipse.ui.IWorkbenchPreferenceConstants#NO_NEW_PERSPECTIVE
198          */

199         public static final String JavaDoc PROJECT_OPEN_NEW_PERSPECTIVE = "PROJECT_OPEN_NEW_PERSPECTIVE"; //$NON-NLS-1$
200

201         /**
202          * <p>
203          * Specifies whether or not the workspace selection dialog should be
204          * shown on startup.
205          * </p>
206          * <p>
207          * The default value for this preference is <code>true</code>.
208          * </p>
209          *
210          * @since 3.1
211          */

212         public static final String JavaDoc SHOW_WORKSPACE_SELECTION_DIALOG = "SHOW_WORKSPACE_SELECTION_DIALOG"; //$NON-NLS-1$
213

214         /**
215          * <p>
216          * Stores the maximum number of workspaces that should be displayed in
217          * the ChooseWorkspaceDialog.
218          * </p>
219          *
220          * @since 3.1
221          */

222         public static final String JavaDoc MAX_RECENT_WORKSPACES = "MAX_RECENT_WORKSPACES"; //$NON-NLS-1$
223

224         /**
225          * <p>
226          * Stores a comma separated list of the recently used workspace paths.
227          * </p>
228          *
229          * @since 3.1
230          */

231         public static final String JavaDoc RECENT_WORKSPACES = "RECENT_WORKSPACES"; //$NON-NLS-1$
232

233         /**
234          * <p>
235          * Stores the version of the protocol used to decode/encode the list of
236          * recent workspaces.
237          * </p>
238          *
239          * @since 3.1
240          */

241         public static final String JavaDoc RECENT_WORKSPACES_PROTOCOL = "RECENT_WORKSPACES_PROTOCOL"; //$NON-NLS-1$
242

243     }
244
245     /**
246      * A saveable filter that selects savables that contain resources that
247      * are descendants of the roots of the filter.
248      * @since 3.3
249      *
250      */

251     private static class SaveFilter implements ISaveableFilter {
252         private final IResource[] roots;
253
254         /**
255          * Create the filter
256          * @param roots the save roots
257          */

258         public SaveFilter(IResource[] roots) {
259             this.roots = roots;
260         }
261         
262         /* (non-Javadoc)
263          * @see org.eclipse.ui.ISaveableFilter#select(org.eclipse.ui.Saveable, org.eclipse.ui.IWorkbenchPart[])
264          */

265         public boolean select(Saveable saveable,
266                 IWorkbenchPart[] containingParts) {
267             if (isDescendantOfRoots(saveable)) {
268                 return true;
269             }
270             // For backwards compatibility, we need to check the parts
271
for (int i = 0; i < containingParts.length; i++) {
272                 IWorkbenchPart workbenchPart = containingParts[i];
273                 if (workbenchPart instanceof IEditorPart) {
274                     IEditorPart editorPart = (IEditorPart) workbenchPart;
275                     if (isEditingDescendantOf(editorPart)) {
276                         return true;
277                     }
278                 }
279             }
280             return false;
281         }
282         
283         /**
284          * Return whether the given saveable contains any resources that
285          * are descendants of the root resources.
286          * @param saveable the saveable
287          * @return whether the given saveable contains any resources that
288          * are descendants of the root resources
289          */

290         private boolean isDescendantOfRoots(Saveable saveable) {
291             // First, try and adapt the saveable to a resource mapping.
292
ResourceMapping mapping = ResourceUtil.getResourceMapping(saveable);
293             if (mapping != null) {
294                 try {
295                     ResourceTraversal[] traversals = mapping.getTraversals(
296                             ResourceMappingContext.LOCAL_CONTEXT, null);
297                     for (int i = 0; i < traversals.length; i++) {
298                         ResourceTraversal traversal = traversals[i];
299                         IResource[] resources = traversal.getResources();
300                         for (int j = 0; j < resources.length; j++) {
301                             IResource resource = resources[j];
302                             if (isDescendantOfRoots(resource)) {
303                                 return true;
304                             }
305                         }
306                     }
307                 } catch (CoreException e) {
308                     IDEWorkbenchPlugin
309                             .log(
310                                     NLS
311                                             .bind(
312                                                     "An internal error occurred while determining the resources for {0}", saveable.getName()), e); //$NON-NLS-1$
313
}
314             } else {
315                 // If there is no mapping, try to adapt to a resource or file directly
316
IFile file = ResourceUtil.getFile(saveable);
317                 if (file != null) {
318                     return isDescendantOfRoots(file);
319                 }
320             }
321             return false;
322         }
323
324         /**
325          * Return whether the given resource is either equal to or a descendant of
326          * one of the given roots.
327          *
328          * @param roots the root resources
329          * @param resource the resource to be tested
330          * @return whether the given resource is either equal to or a descendant of
331          * one of the given roots
332          */

333         private boolean isDescendantOfRoots(IResource resource) {
334             for (int l = 0; l < roots.length; l++) {
335                 IResource root = roots[l];
336                 if (root.getFullPath().isPrefixOf(resource.getFullPath())) {
337                     return true;
338                 }
339             }
340             return false;
341         }
342         
343         /**
344          * Return whether the given dirty editor part is editing resources that are
345          * descendants of the given roots.
346          *
347          * @param roots the root resources
348          * @param part the dirty editor part
349          * @return whether the given dirty editor part is editing resources that are
350          * descendants of the given roots
351          */

352         private boolean isEditingDescendantOf(IEditorPart part) {
353             IFile file = ResourceUtil.getFile(part.getEditorInput());
354             if (file != null) {
355                 return isDescendantOfRoots(file);
356             }
357             return false;
358         }
359         
360     }
361     
362     /**
363      * Block instantiation.
364      */

365     private IDE() {
366         // do nothing
367
}
368
369     /**
370      * Returns the marker help registry for the workbench.
371      *
372      * @return the marker help registry
373      */

374     public static IMarkerHelpRegistry getMarkerHelpRegistry() {
375         if (markerHelpRegistry == null) {
376             markerHelpRegistry = new MarkerHelpRegistry();
377             new MarkerHelpRegistryReader().addHelp(markerHelpRegistry);
378         }
379         return markerHelpRegistry;
380     }
381
382     /**
383      * Sets the cursor and selection state for the given editor to reveal the
384      * position of the given marker. This is done on a best effort basis. If the
385      * editor does not provide an <code>IGotoMarker</code> interface (either
386      * directly or via <code>IAdaptable.getAdapter</code>), this has no
387      * effect.
388      *
389      * @param editor
390      * the editor
391      * @param marker
392      * the marker
393      */

394     public static void gotoMarker(IEditorPart editor, IMarker marker) {
395         IGotoMarker gotoMarker = null;
396         if (editor instanceof IGotoMarker) {
397             gotoMarker = (IGotoMarker) editor;
398         } else {
399             gotoMarker = (IGotoMarker) editor.getAdapter(IGotoMarker.class);
400         }
401         if (gotoMarker != null) {
402             gotoMarker.gotoMarker(marker);
403         }
404     }
405
406     /**
407      * Opens an editor on the given object.
408      * <p>
409      * If the page already has an editor open on the target object then that
410      * editor is brought to front; otherwise, a new editor is opened.
411      * <p>
412      *
413      * @param page
414      * the page in which the editor will be opened
415      * @param input
416      * the editor input
417      * @param editorId
418      * the id of the editor extension to use
419      * @return an open editor or <code>null</code> if an external editor was
420      * opened
421      * @exception PartInitException
422      * if the editor could not be initialized
423      * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String)
424      */

425     public static IEditorPart openEditor(IWorkbenchPage page,
426             IEditorInput input, String JavaDoc editorId) throws PartInitException {
427         // sanity checks
428
if (page == null) {
429             throw new IllegalArgumentException JavaDoc();
430         }
431
432         // open the editor on the file
433
return page.openEditor(input, editorId);
434     }
435
436     /**
437      * Opens an editor on the given IFileStore object.
438      * <p>
439      * Unlike the other <code>openEditor</code> methods, this one can be used
440      * to open files that reside outside the workspace resource set.
441      * </p>
442      * <p>
443      * If the page already has an editor open on the target object then that
444      * editor is brought to front; otherwise, a new editor is opened.
445      * </p>
446      *
447      * @param page
448      * the page in which the editor will be opened
449      * @param uri
450      * the URI of the file store representing the file to open
451      * @param editorId
452      * the id of the editor extension to use
453      * @param activate
454      * if <code>true</code> the editor will be activated opened
455      * @return an open editor or <code>null</code> if an external editor was
456      * @exception PartInitException
457      * if the editor could not be initialized
458      *
459      * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String)
460      * @see EFS#getStore(URI)
461      *
462      * @since 3.3
463      */

464     public static IEditorPart openEditor(IWorkbenchPage page, URI JavaDoc uri,
465             String JavaDoc editorId, boolean activate) throws PartInitException {
466         // sanity checks
467
if (page == null) {
468             throw new IllegalArgumentException JavaDoc();
469         }
470
471         IFileStore fileStore;
472         try {
473             fileStore = EFS.getStore(uri);
474         } catch (CoreException e) {
475             throw new PartInitException(
476                     IDEWorkbenchMessages.IDE_coreExceptionFileStore, e);
477         }
478
479         IEditorInput input = getEditorInput(fileStore);
480
481         // open the editor on the file
482
return page.openEditor(input, editorId, activate);
483     }
484
485     /**
486      * Create the Editor Input appropriate for the given <code>IFileStore</code>.
487      * The result is a normal file editor input if the file exists in the
488      * workspace and, if not, we create a wrapper capable of managing an
489      * 'external' file using its <code>IFileStore</code>.
490      *
491      * @param fileStore
492      * The file store to provide the editor input for
493      * @return The editor input associated with the given file store
494      */

495     private static IEditorInput getEditorInput(IFileStore fileStore) {
496         IFile workspaceFile = getWorkspaceFile(fileStore);
497         if (workspaceFile != null)
498             return new FileEditorInput(workspaceFile);
499         return new FileStoreEditorInput(fileStore);
500     }
501
502     /**
503      * Determine whether or not the <code>IFileStore</code> represents a file
504      * currently in the workspace.
505      *
506      * @param fileStore
507      * The <code>IFileStore</code> to test
508      * @return The workspace's <code>IFile</code> if it exists or
509      * <code>null</code> if not
510      */

511     private static IFile getWorkspaceFile(IFileStore fileStore) {
512         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
513         IFile[] files = root.findFilesForLocationURI(fileStore.toURI());
514         files = filterNonExistentFiles(files);
515         if (files == null || files.length == 0)
516             return null;
517
518         // for now only return the first file
519
return files[0];
520     }
521
522     /**
523      * Filter the incoming array of <code>IFile</code> elements by removing
524      * any that do not currently exist in the workspace.
525      *
526      * @param files
527      * The array of <code>IFile</code> elements
528      * @return The filtered array
529      */

530     private static IFile[] filterNonExistentFiles(IFile[] files) {
531         if (files == null)
532             return null;
533
534         int length = files.length;
535         ArrayList JavaDoc existentFiles = new ArrayList JavaDoc(length);
536         for (int i = 0; i < length; i++) {
537             if (files[i].exists())
538                 existentFiles.add(files[i]);
539         }
540         return (IFile[]) existentFiles.toArray(new IFile[existentFiles.size()]);
541     }
542
543     /**
544      * Opens an editor on the given object.
545      * <p>
546      * If the page already has an editor open on the target object then that
547      * editor is brought to front; otherwise, a new editor is opened. If
548      * <code>activate == true</code> the editor will be activated.
549      * <p>
550      *
551      * @param page
552      * the page in which the editor will be opened
553      * @param input
554      * the editor input
555      * @param editorId
556      * the id of the editor extension to use
557      * @param activate
558      * if <code>true</code> the editor will be activated
559      * @return an open editor or <code>null</code> if an external editor was
560      * opened
561      * @exception PartInitException
562      * if the editor could not be initialized
563      * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String,
564      * boolean)
565      */

566     public static IEditorPart openEditor(IWorkbenchPage page,
567             IEditorInput input, String JavaDoc editorId, boolean activate)
568             throws PartInitException {
569         // sanity checks
570
if (page == null) {
571             throw new IllegalArgumentException JavaDoc();
572         }
573
574         // open the editor on the file
575
return page.openEditor(input, editorId, activate);
576     }
577
578     /**
579      * Opens an editor on the given file resource. This method will attempt to
580      * resolve the editor based on content-type bindings as well as traditional
581      * name/extension bindings.
582      * <p>
583      * If the page already has an editor open on the target object then that
584      * editor is brought to front; otherwise, a new editor is opened. If
585      * <code>activate == true</code> the editor will be activated.
586      * <p>
587      *
588      * @param page
589      * the page in which the editor will be opened
590      * @param input
591      * the editor input
592      * @param activate
593      * if <code>true</code> the editor will be activated
594      * @return an open editor or <code>null</code> if an external editor was
595      * opened
596      * @exception PartInitException
597      * if the editor could not be initialized
598      * @see org.eclipse.ui.IWorkbenchPage#openEditor(org.eclipse.ui.IEditorInput,
599      * String, boolean)
600      */

601     public static IEditorPart openEditor(IWorkbenchPage page, IFile input,
602             boolean activate) throws PartInitException {
603         return openEditor(page, input, activate, true);
604     }
605
606     /**
607      * Opens an editor on the given file resource. This method will attempt to
608      * resolve the editor based on content-type bindings as well as traditional
609      * name/extension bindings if <code>determineContentType</code> is
610      * <code>true</code>.
611      * <p>
612      * If the page already has an editor open on the target object then that
613      * editor is brought to front; otherwise, a new editor is opened. If
614      * <code>activate == true</code> the editor will be activated.
615      * <p>
616      *
617      * @param page
618      * the page in which the editor will be opened
619      * @param input
620      * the editor input
621      * @param activate
622      * if <code>true</code> the editor will be activated
623      * @param determineContentType
624      * attempt to resolve the content type for this file
625      * @return an open editor or <code>null</code> if an external editor was
626      * opened
627      * @exception PartInitException
628      * if the editor could not be initialized
629      * @see org.eclipse.ui.IWorkbenchPage#openEditor(org.eclipse.ui.IEditorInput,
630      * String, boolean)
631      * @since 3.1
632      */

633     public static IEditorPart openEditor(IWorkbenchPage page, IFile input,
634             boolean activate, boolean determineContentType)
635             throws PartInitException {
636         // sanity checks
637
if (page == null) {
638             throw new IllegalArgumentException JavaDoc();
639         }
640
641         // open the editor on the file
642
IEditorDescriptor editorDesc = getEditorDescriptor(input,
643                 determineContentType);
644         return page.openEditor(new FileEditorInput(input), editorDesc.getId(),
645                 activate);
646     }
647
648     /**
649      * Opens an editor on the given file resource. This method will attempt to
650      * resolve the editor based on content-type bindings as well as traditional
651      * name/extension bindings.
652      * <p>
653      * If the page already has an editor open on the target object then that
654      * editor is brought to front; otherwise, a new editor is opened.
655      * <p>
656      *
657      * @param page
658      * the page in which the editor will be opened
659      * @param input
660      * the editor input
661      * @return an open editor or <code>null</code> if an external editor was
662      * opened
663      * @exception PartInitException
664      * if the editor could not be initialized
665      * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String)
666      */

667     public static IEditorPart openEditor(IWorkbenchPage page, IFile input)
668             throws PartInitException {
669         // sanity checks
670
if (page == null) {
671             throw new IllegalArgumentException JavaDoc();
672         }
673
674         // open the editor on the file
675
IEditorDescriptor editorDesc = getEditorDescriptor(input);
676         return page.openEditor(new FileEditorInput(input), editorDesc.getId());
677     }
678
679     /**
680      * Opens an editor on the given file resource.
681      * <p>
682      * If the page already has an editor open on the target object then that
683      * editor is brought to front; otherwise, a new editor is opened.
684      * <p>
685      *
686      * @param page
687      * the page in which the editor will be opened
688      * @param input
689      * the editor input
690      * @param editorId
691      * the id of the editor extension to use
692      * @return an open editor or <code>null</code> if an external editor was
693      * opened
694      * @exception PartInitException
695      * if the editor could not be initialized
696      * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String)
697      */

698     public static IEditorPart openEditor(IWorkbenchPage page, IFile input,
699             String JavaDoc editorId) throws PartInitException {
700         // sanity checks
701
if (page == null) {
702             throw new IllegalArgumentException JavaDoc();
703         }
704
705         // open the editor on the file
706
return page.openEditor(new FileEditorInput(input), editorId);
707     }
708
709     /**
710      * Opens an editor on the given file resource.
711      * <p>
712      * If the page already has an editor open on the target object then that
713      * editor is brought to front; otherwise, a new editor is opened. If
714      * <code>activate == true</code> the editor will be activated.
715      * <p>
716      *
717      * @param page
718      * the page in which the editor will be opened
719      * @param input
720      * the editor input
721      * @param editorId
722      * the id of the editor extension to use
723      * @param activate
724      * if <code>true</code> the editor will be activated
725      * @return an open editor or <code>null</code> if an external editor was
726      * opened
727      * @exception PartInitException
728      * if the editor could not be initialized
729      * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String,
730      * boolean)
731      */

732     public static IEditorPart openEditor(IWorkbenchPage page, IFile input,
733             String JavaDoc editorId, boolean activate) throws PartInitException {
734         // sanity checks
735
if (page == null) {
736             throw new IllegalArgumentException JavaDoc();
737         }
738
739         // open the editor on the file
740
return page.openEditor(new FileEditorInput(input), editorId, activate);
741     }
742
743     /**
744      * Returns an editor descriptor appropriate for opening the given file
745      * resource.
746      * <p>
747      * The editor descriptor is determined using a multi-step process. This
748      * method will attempt to resolve the editor based on content-type bindings
749      * as well as traditional name/extension bindings.
750      * </p>
751      * <ol>
752      * <li>The file is consulted for a persistent property named
753      * <code>IDE.EDITOR_KEY</code> containing the preferred editor id to be
754      * used.</li>
755      * <li>The workbench editor registry is consulted to determine if an editor
756      * extension has been registered for the file type. If so, an instance of
757      * the editor extension is opened on the file. See
758      * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li>
759      * <li>The operating system is consulted to determine if an in-place
760      * component editor is available (e.g. OLE editor on Win32 platforms).</li>
761      * <li>The operating system is consulted to determine if an external editor
762      * is available.</li>
763      * </ol>
764      * </p>
765      *
766      * @param file
767      * the file
768      * @return an editor descriptor, appropriate for opening the file
769      * @throws PartInitException
770      * if no editor can be found
771      */

772     public static IEditorDescriptor getEditorDescriptor(IFile file)
773             throws PartInitException {
774         return getEditorDescriptor(file, true);
775     }
776
777     /**
778      * Returns an editor descriptor appropriate for opening the given file
779      * resource.
780      * <p>
781      * The editor descriptor is determined using a multi-step process. This
782      * method will attempt to resolve the editor based on content-type bindings
783      * as well as traditional name/extension bindings if
784      * <code>determineContentType</code>is <code>true</code>.
785      * </p>
786      * <ol>
787      * <li>The file is consulted for a persistent property named
788      * <code>IDE.EDITOR_KEY</code> containing the preferred editor id to be
789      * used.</li>
790      * <li>The workbench editor registry is consulted to determine if an editor
791      * extension has been registered for the file type. If so, an instance of
792      * the editor extension is opened on the file. See
793      * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li>
794      * <li>The operating system is consulted to determine if an in-place
795      * component editor is available (e.g. OLE editor on Win32 platforms).</li>
796      * <li>The operating system is consulted to determine if an external editor
797      * is available.</li>
798      * </ol>
799      * </p>
800      *
801      * @param file
802      * the file
803      * @param determineContentType
804      * query the content type system for the content type of the file
805      * @return an editor descriptor, appropriate for opening the file
806      * @throws PartInitException
807      * if no editor can be found
808      * @since 3.1
809      */

810     public static IEditorDescriptor getEditorDescriptor(IFile file,
811             boolean determineContentType) throws PartInitException {
812
813         if (file == null) {
814             throw new IllegalArgumentException JavaDoc();
815         }
816
817         return getEditorDescriptor(file.getName(), PlatformUI.getWorkbench()
818                 .getEditorRegistry(), getDefaultEditor(file,
819                 determineContentType));
820     }
821
822     /**
823      * Returns an editor descriptor appropriate for opening a file resource with
824      * the given name.
825      * <p>
826      * The editor descriptor is determined using a multi-step process. This
827      * method will attempt to infer content type from the file name.
828      * </p>
829      * <ol>
830      * <li>The file is consulted for a persistent property named
831      * <code>IDE.EDITOR_KEY</code> containing the preferred editor id to be
832      * used.</li>
833      * <li>The workbench editor registry is consulted to determine if an editor
834      * extension has been registered for the file type. If so, an instance of
835      * the editor extension is opened on the file. See
836      * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li>
837      * <li>The operating system is consulted to determine if an in-place
838      * component editor is available (e.g. OLE editor on Win32 platforms).</li>
839      * <li>The operating system is consulted to determine if an external editor
840      * is available.</li>
841      * </ol>
842      * </p>
843      *
844      * @param name
845      * the file name
846      * @return an editor descriptor, appropriate for opening the file
847      * @throws PartInitException
848      * if no editor can be found
849      * @since 3.1
850      */

851     public static IEditorDescriptor getEditorDescriptor(String JavaDoc name)
852             throws PartInitException {
853         return getEditorDescriptor(name, true);
854     }
855
856     /**
857      * Returns an editor descriptor appropriate for opening a file resource with
858      * the given name.
859      * <p>
860      * The editor descriptor is determined using a multi-step process. This
861      * method will attempt to infer the content type of the file if
862      * <code>inferContentType</code> is <code>true</code>.
863      * </p>
864      * <ol>
865      * <li>The file is consulted for a persistent property named
866      * <code>IDE.EDITOR_KEY</code> containing the preferred editor id to be
867      * used.</li>
868      * <li>The workbench editor registry is consulted to determine if an editor
869      * extension has been registered for the file type. If so, an instance of
870      * the editor extension is opened on the file. See
871      * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li>
872      * <li>The operating system is consulted to determine if an in-place
873      * component editor is available (e.g. OLE editor on Win32 platforms).</li>
874      * <li>The operating system is consulted to determine if an external editor
875      * is available.</li>
876      * </ol>
877      * </p>
878      *
879      * @param name
880      * the file name
881      * @param inferContentType
882      * attempt to infer the content type from the file name if this
883      * is <code>true</code>
884      * @return an editor descriptor, appropriate for opening the file
885      * @throws PartInitException
886      * if no editor can be found
887      * @since 3.1
888      */

889     public static IEditorDescriptor getEditorDescriptor(String JavaDoc name,
890             boolean inferContentType) throws PartInitException {
891
892         if (name == null) {
893             throw new IllegalArgumentException JavaDoc();
894         }
895
896         IContentType contentType = inferContentType ? Platform
897                 .getContentTypeManager().findContentTypeFor(name) : null;
898         IEditorRegistry editorReg = PlatformUI.getWorkbench()
899                 .getEditorRegistry();
900
901         return getEditorDescriptor(name, editorReg, editorReg.getDefaultEditor(
902                 name, contentType));
903     }
904
905     /**
906      * Get the editor descriptor for a given name using the editorDescriptor
907      * passed in as a default as a starting point.
908      *
909      * @param name
910      * The name of the element to open.
911      * @param editorReg
912      * The editor registry to do the lookups from.
913      * @param defaultDescriptor
914      * IEditorDescriptor or <code>null</code>
915      * @return IEditorDescriptor
916      * @throws PartInitException
917      * if no valid editor can be found
918      *
919      * @since 3.1
920      */

921     private static IEditorDescriptor getEditorDescriptor(String JavaDoc name,
922             IEditorRegistry editorReg, IEditorDescriptor defaultDescriptor)
923             throws PartInitException {
924
925         if (defaultDescriptor != null) {
926             return defaultDescriptor;
927         }
928
929         IEditorDescriptor editorDesc = defaultDescriptor;
930
931         // next check the OS for in-place editor (OLE on Win32)
932
if (editorReg.isSystemInPlaceEditorAvailable(name)) {
933             editorDesc = editorReg
934                     .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID);
935         }
936
937         // next check with the OS for an external editor
938
if (editorDesc == null
939                 && editorReg.isSystemExternalEditorAvailable(name)) {
940             editorDesc = editorReg
941                     .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
942         }
943
944         // next lookup the default text editor
945
if (editorDesc == null) {
946             editorDesc = editorReg
947                     .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID);
948         }
949
950         // if no valid editor found, bail out
951
if (editorDesc == null) {
952             throw new PartInitException(
953                     IDEWorkbenchMessages.IDE_noFileEditorFound);
954         }
955
956         return editorDesc;
957     }
958
959     /**
960      * Opens an editor on the file resource of the given marker.
961      * <p>
962      * If this page already has an editor open on the marker resource file that
963      * editor is brought to front; otherwise, a new editor is opened.The cursor
964      * and selection state of the editor are then updated from information
965      * recorded in the marker.
966      * </p>
967      * <p>
968      * If the marker contains an <code>EDITOR_ID_ATTR</code> attribute the
969      * attribute value will be used to determine the editor type to be opened.
970      * If not, the registered editor for the marker resource file will be used.
971      * </p>
972      *
973      * @param page
974      * the workbench page to open the editor in
975      * @param marker
976      * the marker to open
977      * @return an open editor or <code>null</code> not possible
978      * @exception PartInitException
979      * if the editor could not be initialized
980      * @see #openEditor(org.eclipse.ui.IWorkbenchPage,
981      * org.eclipse.core.resources.IMarker, boolean)
982      */

983     public static IEditorPart openEditor(IWorkbenchPage page, IMarker marker)
984             throws PartInitException {
985         return openEditor(page, marker, true);
986     }
987
988     /**
989      * Opens an editor on the file resource of the given marker.
990      * <p>
991      * If this page already has an editor open on the marker resource file that
992      * editor is brought to front; otherwise, a new editor is opened. If
993      * <code>activate == true</code> the editor will be activated. The cursor
994      * and selection state of the editor are then updated from information
995      * recorded in the marker.
996      * </p>
997      * <p>
998      * If the marker contains an <code>EDITOR_ID_ATTR</code> attribute the
999      * attribute value will be used to determine the editor type to be opened.
1000     * If not, the registered editor for the marker resource file will be used.
1001     * </p>
1002     *
1003     * @param page
1004     * the workbench page to open the editor in
1005     * @param marker
1006     * the marker to open
1007     * @param activate
1008     * if <code>true</code> the editor will be activated
1009     * @return an open editor or <code>null</code> not possible
1010     * @exception PartInitException
1011     * if the editor could not be initialized
1012     */

1013    public static IEditorPart openEditor(IWorkbenchPage page, IMarker marker,
1014            boolean activate) throws PartInitException {
1015        // sanity checks
1016
if (page == null || marker == null) {
1017            throw new IllegalArgumentException JavaDoc();
1018        }
1019
1020        // get the marker resource file
1021
if (!(marker.getResource() instanceof IFile)) {
1022            IDEWorkbenchPlugin
1023                    .log("Open editor on marker failed; marker resource not an IFile"); //$NON-NLS-1$
1024
return null;
1025        }
1026        IFile file = (IFile) marker.getResource();
1027
1028        // get the preferred editor id from the marker
1029
IEditorRegistry editorReg = PlatformUI.getWorkbench()
1030                .getEditorRegistry();
1031        IEditorDescriptor editorDesc = null;
1032        try {
1033            String JavaDoc editorID = (String JavaDoc) marker.getAttribute(EDITOR_ID_ATTR);
1034            if (editorID != null) {
1035                editorDesc = editorReg.findEditor(editorID);
1036            }
1037        } catch (CoreException e) {
1038            // ignore this
1039
}
1040
1041        // open the editor on the marker resource file
1042
IEditorPart editor = null;
1043        if (editorDesc == null) {
1044            editor = openEditor(page, file, activate);
1045        } else {
1046            editor = page.openEditor(new FileEditorInput(file), editorDesc
1047                    .getId(), activate);
1048        }
1049
1050        // get the editor to update its position based on the marker
1051
if (editor != null) {
1052            gotoMarker(editor, marker);
1053        }
1054
1055        return editor;
1056    }
1057
1058    /**
1059     * Opens an editor on the given IFileStore object.
1060     * <p>
1061     * Unlike the other <code>openEditor</code> methods, this one
1062     * can be used to open files that reside outside the workspace
1063     * resource set.
1064     * </p>
1065     * <p>
1066     * If the page already has an editor open on the target object then that
1067     * editor is brought to front; otherwise, a new editor is opened.
1068     * </p>
1069     *
1070     * @param page
1071     * the page in which the editor will be opened
1072     * @param fileStore
1073     * the IFileStore representing the file to open
1074     * @return an open editor or <code>null</code> if an external editor was opened
1075     * @exception PartInitException
1076     * if the editor could not be initialized
1077     * @see org.eclipse.ui.IWorkbenchPage#openEditor(IEditorInput, String)
1078     * @since 3.3
1079     */

1080    public static IEditorPart openEditorOnFileStore(IWorkbenchPage page, IFileStore fileStore) throws PartInitException {
1081        //sanity checks
1082
if (page == null) {
1083            throw new IllegalArgumentException JavaDoc();
1084        }
1085
1086        IEditorInput input = getEditorInput(fileStore);
1087        String JavaDoc editorId = getEditorId(fileStore);
1088        
1089        // open the editor on the file
1090
return page.openEditor(input, editorId);
1091    }
1092
1093    /**
1094     * Get the id of the editor associated with the given <code>IFileStore</code>.
1095     *
1096     * @param workbench
1097     * the Workbench to use to determine the appropriate editor's id
1098     * @param fileStore
1099     * the <code>IFileStore</code> representing the file for which the editor id is desired
1100     * @return the id of the appropriate editor
1101     * @since 3.3
1102     */

1103    private static String JavaDoc getEditorId(IFileStore fileStore) {
1104        IEditorDescriptor descriptor;
1105        try {
1106            descriptor = IDE.getEditorDescriptor(fileStore.getName());
1107        } catch (PartInitException e) {
1108            return null;
1109        }
1110        if (descriptor != null)
1111            return descriptor.getId();
1112        return null;
1113    }
1114
1115    /**
1116     * Save all dirty editors in the workbench whose editor input is a child
1117     * resource of one of the <code>IResource</code>'s provided. Opens a
1118     * dialog to prompt the user if <code>confirm</code> is true. Return true
1119     * if successful. Return false if the user has canceled the command.
1120     *
1121     * @since 3.0
1122     *
1123     * @param resourceRoots the resource roots under which editor input should
1124     * be saved, other will be left dirty
1125     * @param confirm <code>true</code> to ask the user before saving unsaved
1126     * changes (recommended), and <code>false</code> to save
1127     * unsaved changes without asking
1128     * @return <code>true</code> if the command succeeded, and
1129     * <code>false</code> if the operation was canceled by the user or
1130     * an error occurred while saving
1131     */

1132    public static boolean saveAllEditors(final IResource[] resourceRoots,
1133            final boolean confirm) {
1134
1135        if (resourceRoots.length == 0) {
1136            return true;
1137        }
1138
1139        final boolean[] result = new boolean[] { true };
1140        SafeRunner.run(new SafeRunnable(IDEWorkbenchMessages.ErrorOnSaveAll) {
1141            public void run() {
1142                IWorkbenchWindow w = PlatformUI.getWorkbench()
1143                        .getActiveWorkbenchWindow();
1144                if (w == null) {
1145                    IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
1146                            .getWorkbenchWindows();
1147                    if (windows.length > 0)
1148                        w = windows[0];
1149                }
1150                if (w != null) {
1151                    result[0] = PlatformUI.getWorkbench().saveAll(w, w,
1152                            new SaveFilter(resourceRoots), confirm);
1153                }
1154            }
1155        });
1156        return result[0];
1157    }
1158
1159    /**
1160     * Sets the default editor id for a given file. This value will be used to
1161     * determine the default editor descriptor for the file in future calls to
1162     * <code>getDefaultEditor(IFile)</code>.
1163     *
1164     * @param file
1165     * the file
1166     * @param editorID
1167     * the editor id
1168     */

1169    public static void setDefaultEditor(IFile file, String JavaDoc editorID) {
1170        try {
1171            file.setPersistentProperty(EDITOR_KEY, editorID);
1172        } catch (CoreException e) {
1173            // do nothing
1174
}
1175    }
1176
1177    /**
1178     * Returns the default editor for a given file. This method will attempt to
1179     * resolve the editor based on content-type bindings as well as traditional
1180     * name/extension bindings.
1181     * <p>
1182     * A default editor id may be registered for a specific file using
1183     * <code>setDefaultEditor</code>. If the given file has a registered
1184     * default editor id the default editor will derived from it. If not, the
1185     * default editor is determined by taking the file name for the file and
1186     * obtaining the default editor for that name.
1187     * </p>
1188     *
1189     * @param file
1190     * the file
1191     * @return the descriptor of the default editor, or <code>null</code> if
1192     * not found
1193     */

1194    public static IEditorDescriptor getDefaultEditor(IFile file) {
1195        return getDefaultEditor(file, true);
1196    }
1197
1198    /**
1199     * Returns the default editor for a given file. This method will attempt to
1200     * resolve the editor based on content-type bindings as well as traditional
1201     * name/extension bindings if <code>determineContentType</code> is
1202     * <code>true</code>.
1203     * <p>
1204     * A default editor id may be registered for a specific file using
1205     * <code>setDefaultEditor</code>. If the given file has a registered
1206     * default editor id the default editor will derived from it. If not, the
1207     * default editor is determined by taking the file name for the file and
1208     * obtaining the default editor for that name.
1209     * </p>
1210     *
1211     * @param file
1212     * the file
1213     * @param determineContentType
1214     * determine the content type for the given file
1215     * @return the descriptor of the default editor, or <code>null</code> if
1216     * not found
1217     * @since 3.1
1218     */

1219    public static IEditorDescriptor getDefaultEditor(IFile file,
1220            boolean determineContentType) {
1221        // Try file specific editor.
1222
IEditorRegistry editorReg = PlatformUI.getWorkbench()
1223                .getEditorRegistry();
1224        try {
1225            String JavaDoc editorID = file.getPersistentProperty(EDITOR_KEY);
1226            if (editorID != null) {
1227                IEditorDescriptor desc = editorReg.findEditor(editorID);
1228                if (desc != null) {
1229                    return desc;
1230                }
1231            }
1232        } catch (CoreException e) {
1233            // do nothing
1234
}
1235
1236        IContentType contentType = null;
1237        if (determineContentType) {
1238            contentType = getContentType(file);
1239        }
1240        // Try lookup with filename
1241
return editorReg.getDefaultEditor(file.getName(), contentType);
1242    }
1243
1244    /**
1245     * Extracts and returns the <code>IResource</code>s in the given
1246     * selection or the resource objects they adapts to.
1247     *
1248     * @param originalSelection
1249     * the original selection, possibly empty
1250     * @return list of resources (element type: <code>IResource</code>),
1251     * possibly empty
1252     */

1253    public static List JavaDoc computeSelectedResources(
1254            IStructuredSelection originalSelection) {
1255        List JavaDoc resources = null;
1256        for (Iterator JavaDoc e = originalSelection.iterator(); e.hasNext();) {
1257            Object JavaDoc next = e.next();
1258            Object JavaDoc resource = null;
1259            if (next instanceof IResource) {
1260                resource = next;
1261            } else if (next instanceof IAdaptable) {
1262                resource = ((IAdaptable) next).getAdapter(IResource.class);
1263            }
1264            if (resource != null) {
1265                if (resources == null) {
1266                    // lazy init to avoid creating empty lists
1267
// assume selection contains mostly resources most times
1268
resources = new ArrayList JavaDoc(originalSelection.size());
1269                }
1270                resources.add(resource);
1271            }
1272        }
1273        if (resources == null) {
1274            return Collections.EMPTY_LIST;
1275        }
1276        return resources;
1277
1278    }
1279
1280    /**
1281     * Return the content type for the given file.
1282     *
1283     * @param file
1284     * the file to test
1285     * @return the content type, or <code>null</code> if it cannot be
1286     * determined.
1287     * @since 3.1
1288     */

1289    public static IContentType getContentType(IFile file) {
1290        try {
1291            UIStats.start(UIStats.CONTENT_TYPE_LOOKUP, file.getName());
1292            IContentDescription contentDescription = file
1293                    .getContentDescription();
1294            if (contentDescription == null) {
1295                return null;
1296            }
1297            return contentDescription.getContentType();
1298        } catch (CoreException e) {
1299            return null;
1300        } finally {
1301            UIStats.end(UIStats.CONTENT_TYPE_LOOKUP, file, file.getName());
1302        }
1303    }
1304
1305    /**
1306     * Guess at the content type of the given file based on the filename.
1307     *
1308     * @param file
1309     * the file to test
1310     * @return the content type, or <code>null</code> if it cannot be
1311     * determined.
1312     * @since 3.2
1313     */

1314    public static IContentType guessContentType(IFile file) {
1315        String JavaDoc fileName = file.getName();
1316        try {
1317            UIStats.start(UIStats.CONTENT_TYPE_LOOKUP, fileName);
1318            IContentTypeMatcher matcher = file.getProject()
1319                    .getContentTypeMatcher();
1320            return matcher.findContentTypeFor(fileName);
1321        } catch (CoreException e) {
1322            return null;
1323        } finally {
1324            UIStats.end(UIStats.CONTENT_TYPE_LOOKUP, file, fileName);
1325        }
1326    }
1327
1328    /**
1329     * Prompt the user to inform them of the possible side effects of an
1330     * operation on resources. Do not prompt for side effects from ignored model
1331     * providers. A model provider can be ignored if it is the client calling
1332     * this API. Any message from the provided model provider id or any model
1333     * providers it extends will be ignored.
1334     *
1335     * @param shell
1336     * the shell to parent the prompt dialog
1337     * @param title
1338     * the title of the dialog
1339     * @param message
1340     * the message for the dialog
1341     * @param delta
1342     * a delta built using an
1343     * {@link IResourceChangeDescriptionFactory}
1344     * @param ignoreModelProviderIds
1345     * model providers to be ignored
1346     * @param syncExec
1347     * prompt in a sync exec (required when called from a non-UI
1348     * thread)
1349     * @return whether the user chose to continue
1350     * @since 3.2
1351     */

1352    public static boolean promptToConfirm(final Shell shell,
1353            final String JavaDoc title, String JavaDoc message, IResourceDelta delta,
1354            String JavaDoc[] ignoreModelProviderIds, boolean syncExec) {
1355        IStatus status = ResourceChangeValidator.getValidator().validateChange(
1356                delta, null);
1357        if (status.isOK()) {
1358            return true;
1359        }
1360        final IStatus displayStatus;
1361        if (status.isMultiStatus()) {
1362            List JavaDoc result = new ArrayList JavaDoc();
1363            IStatus[] children = status.getChildren();
1364            for (int i = 0; i < children.length; i++) {
1365                IStatus child = children[i];
1366                if (!isIgnoredStatus(child, ignoreModelProviderIds)) {
1367                    result.add(child);
1368                }
1369            }
1370            if (result.isEmpty()) {
1371                return true;
1372            }
1373            if (result.size() == 1) {
1374                displayStatus = (IStatus) result.get(0);
1375            } else {
1376                displayStatus = new MultiStatus(status.getPlugin(), status
1377                        .getCode(), (IStatus[]) result
1378                        .toArray(new IStatus[result.size()]), status
1379                        .getMessage(), status.getException());
1380            }
1381        } else {
1382            if (isIgnoredStatus(status, ignoreModelProviderIds)) {
1383                return true;
1384            }
1385            displayStatus = status;
1386        }
1387
1388        if (message == null) {
1389            message = IDEWorkbenchMessages.IDE_sideEffectWarning;
1390        }
1391        final String JavaDoc dialogMessage = NLS.bind(
1392                IDEWorkbenchMessages.IDE_areYouSure, message);
1393
1394        final boolean[] result = new boolean[] { false };
1395        Runnable JavaDoc runnable = new Runnable JavaDoc() {
1396            public void run() {
1397                ErrorDialog dialog = new ErrorDialog(shell, title,
1398                        dialogMessage, displayStatus, IStatus.ERROR
1399                                | IStatus.WARNING | IStatus.INFO) {
1400                    protected void createButtonsForButtonBar(Composite parent) {
1401                        createButton(parent, IDialogConstants.YES_ID,
1402                                IDialogConstants.YES_LABEL, false);
1403                        createButton(parent, IDialogConstants.NO_ID,
1404                                IDialogConstants.NO_LABEL, true);
1405                        createDetailsButton(parent);
1406                    }
1407
1408                    /*
1409                     * (non-Javadoc)
1410                     *
1411                     * @see org.eclipse.jface.dialogs.ErrorDialog#buttonPressed(int)
1412                     */

1413                    protected void buttonPressed(int id) {
1414                        if (id == IDialogConstants.YES_ID) {
1415                            super.buttonPressed(IDialogConstants.OK_ID);
1416                        } else if (id == IDialogConstants.NO_ID) {
1417                            super.buttonPressed(IDialogConstants.CANCEL_ID);
1418                        }
1419                        super.buttonPressed(id);
1420                    }
1421                };
1422                int code = dialog.open();
1423                result[0] = code == 0;
1424            }
1425        };
1426        if (syncExec) {
1427            shell.getDisplay().syncExec(runnable);
1428        } else {
1429            runnable.run();
1430        }
1431        return result[0];
1432    }
1433
1434    private static boolean isIgnoredStatus(IStatus status,
1435            String JavaDoc[] ignoreModelProviderIds) {
1436        if (ignoreModelProviderIds == null) {
1437            return false;
1438        }
1439        if (status instanceof ModelStatus) {
1440            ModelStatus ms = (ModelStatus) status;
1441            for (int i = 0; i < ignoreModelProviderIds.length; i++) {
1442                String JavaDoc id = ignoreModelProviderIds[i];
1443                if (ms.getModelProviderId().equals(id)) {
1444                    return true;
1445                }
1446                IModelProviderDescriptor desc = ModelProvider
1447                        .getModelProviderDescriptor(id);
1448                String JavaDoc[] extended = desc.getExtendedModels();
1449                if (isIgnoredStatus(status, extended)) {
1450                    return true;
1451                }
1452            }
1453        }
1454        return false;
1455    }
1456}
1457
Popular Tags