KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > filesystem > FileSystemSupportRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.ui.internal.ide.filesystem;
13
14 import java.io.File JavaDoc;
15 import java.net.URI JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.eclipse.core.filesystem.IFileInfo;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtension;
24 import org.eclipse.core.runtime.IExtensionPoint;
25 import org.eclipse.core.runtime.ISafeRunnable;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
28 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
29 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
30 import org.eclipse.swt.widgets.DirectoryDialog;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.ide.fileSystem.FileSystemContributor;
34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
35 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
36 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
37
38 /**
39  * @since 3.2
40  *
41  */

42 public class FileSystemSupportRegistry implements IExtensionChangeHandler {
43
44     private static final String JavaDoc FILESYSTEM_SUPPORT = "filesystemSupport";//$NON-NLS-1$
45

46     protected static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
47

48     private static final String JavaDoc LABEL = "label";//$NON-NLS-1$
49

50     private static final String JavaDoc SCHEME = "scheme";//$NON-NLS-1$
51

52     private static FileSystemSupportRegistry singleton;
53
54     /**
55      * Get the instance of the registry.
56      *
57      * @return MarkerSupportRegistry
58      */

59     public static FileSystemSupportRegistry getInstance() {
60         if (singleton == null) {
61             singleton = new FileSystemSupportRegistry();
62         }
63         return singleton;
64     }
65
66     private Collection JavaDoc registeredContributions = new HashSet JavaDoc(0);
67
68     FileSystemConfiguration defaultConfiguration = new FileSystemConfiguration(
69             FileSystemMessages.DefaultFileSystem_name, new FileSystemContributor() {
70                 /*
71                  * (non-Javadoc)
72                  *
73                  * @see org.eclipse.ui.ide.fileSystem.FileSystemContributor#browseFileSystem(java.lang.String,
74                  * org.eclipse.swt.widgets.Shell)
75                  */

76                 public URI JavaDoc browseFileSystem(String JavaDoc initialPath, Shell shell) {
77
78                     DirectoryDialog dialog = new DirectoryDialog(shell);
79                     dialog
80                             .setMessage(IDEWorkbenchMessages.ProjectLocationSelectionDialog_directoryLabel);
81
82                     if (!initialPath.equals(IDEResourceInfoUtils.EMPTY_STRING)) {
83                         IFileInfo info = IDEResourceInfoUtils
84                                 .getFileInfo(initialPath);
85                         if (info != null && info.exists()) {
86                             dialog.setFilterPath(initialPath);
87                         }
88                     }
89
90                     String JavaDoc selectedDirectory = dialog.open();
91                     if (selectedDirectory == null) {
92                         return null;
93                     }
94                     return new File JavaDoc(selectedDirectory).toURI();
95
96                 }
97             }, null);
98
99     private FileSystemConfiguration[] allConfigurations;
100
101     /**
102      * Create a new instance of the receiver.
103      */

104     public FileSystemSupportRegistry() {
105
106         IExtensionTracker tracker = PlatformUI.getWorkbench()
107                 .getExtensionTracker();
108         IExtensionPoint point = Platform.getExtensionRegistry()
109                 .getExtensionPoint(IDEWorkbenchPlugin.IDE_WORKBENCH,
110                         FILESYSTEM_SUPPORT);
111         if (point == null) {
112             return;
113         }
114         IExtension[] extensions = point.getExtensions();
115         // initial population
116
for (int i = 0; i < extensions.length; i++) {
117             IExtension extension = extensions[i];
118             processExtension(tracker, extension);
119         }
120         tracker.registerHandler(this, ExtensionTracker
121                 .createExtensionPointFilter(point));
122
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker,
129      * org.eclipse.core.runtime.IExtension)
130      */

131     public void addExtension(IExtensionTracker tracker, IExtension extension) {
132         processExtension(tracker, extension);
133         allConfigurations = null;//Clear the cache
134
}
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension,
140      * java.lang.Object[])
141      */

142     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
143         for (int i = 0; i < objects.length; i++) {
144             registeredContributions.remove(objects[i]);
145         }
146         allConfigurations = null;//Clear the cache
147

148     }
149
150     /**
151      * Process the extension and register the result with the tracker.
152      *
153      * @param tracker
154      * @param extension
155      */

156     private void processExtension(IExtensionTracker tracker,
157             IExtension extension) {
158         IConfigurationElement[] elements = extension.getConfigurationElements();
159         for (int j = 0; j < elements.length; j++) {
160             IConfigurationElement element = elements[j];
161             FileSystemConfiguration contribution = newConfiguration(element);
162             registeredContributions.add(contribution);
163             tracker.registerObject(extension, contribution,
164                     IExtensionTracker.REF_STRONG);
165
166         }
167     }
168
169     /**
170      * Return a new FileSystemContribution.
171      *
172      * @param element
173      * @return FileSystemContribution or <code>null</code> if there is an
174      * exception.
175      */

176     private FileSystemConfiguration newConfiguration(
177             final IConfigurationElement element) {
178
179         final FileSystemContributor[] contributors = new FileSystemContributor[1];
180         final CoreException[] exceptions = new CoreException[1];
181
182         Platform.run(new ISafeRunnable() {
183             public void run() {
184                 try {
185                     contributors[0] = (FileSystemContributor) IDEWorkbenchPlugin
186                             .createExtension(element, ATT_CLASS);
187
188                 } catch (CoreException exception) {
189                     exceptions[0] = exception;
190                 }
191             }
192
193             /*
194              * (non-Javadoc) Method declared on ISafeRunnable.
195              */

196             public void handleException(Throwable JavaDoc e) {
197                 // Do nothing as Core will handle the logging
198
}
199         });
200
201         if (exceptions[0] != null) {
202             return null;
203         }
204         String JavaDoc name = element.getAttribute(LABEL);
205         String JavaDoc fileSystem = element.getAttribute(SCHEME);
206         FileSystemConfiguration config = new FileSystemConfiguration(name,
207                 contributors[0], fileSystem);
208
209         return config;
210
211     }
212
213     /**
214      * Return the FileSystemConfiguration defined in the receiver.
215      *
216      * @return FileSystemConfiguration[]
217      */

218     public FileSystemConfiguration[] getConfigurations() {
219         if (allConfigurations == null) {
220             allConfigurations = new FileSystemConfiguration[registeredContributions
221                     .size() + 1];
222             allConfigurations[0] = defaultConfiguration;
223
224             Iterator JavaDoc iterator = registeredContributions.iterator();
225             int index = 0;
226             while (iterator.hasNext()) {
227                 allConfigurations[++index] = (FileSystemConfiguration) iterator
228                         .next();
229             }
230         }
231         return allConfigurations;
232     }
233
234     /**
235      * Return the default file system configuration (the local file system
236      * extension in the ide plug-in).
237      *
238      * @return FileSystemConfiguration
239      */

240     public FileSystemConfiguration getDefaultConfiguration() {
241         return defaultConfiguration;
242     }
243
244     /**
245      * Return whether or not there is only one file system registered.
246      *
247      * @return <code>true</code> if there is only one file system.
248      */

249     public boolean hasOneFileSystem() {
250         return registeredContributions.size() == 0;
251     }
252 }
253
Popular Tags