1 11 12 package org.eclipse.ui.internal.ide.filesystem; 13 14 import java.io.File ; 15 import java.net.URI ; 16 import java.util.Collection ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 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 42 public class FileSystemSupportRegistry implements IExtensionChangeHandler { 43 44 private static final String FILESYSTEM_SUPPORT = "filesystemSupport"; 46 protected static final String ATT_CLASS = "class"; 48 private static final String LABEL = "label"; 50 private static final String SCHEME = "scheme"; 52 private static FileSystemSupportRegistry singleton; 53 54 59 public static FileSystemSupportRegistry getInstance() { 60 if (singleton == null) { 61 singleton = new FileSystemSupportRegistry(); 62 } 63 return singleton; 64 } 65 66 private Collection registeredContributions = new HashSet (0); 67 68 FileSystemConfiguration defaultConfiguration = new FileSystemConfiguration( 69 FileSystemMessages.DefaultFileSystem_name, new FileSystemContributor() { 70 76 public URI browseFileSystem(String 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 selectedDirectory = dialog.open(); 91 if (selectedDirectory == null) { 92 return null; 93 } 94 return new File (selectedDirectory).toURI(); 95 96 } 97 }, null); 98 99 private FileSystemConfiguration[] allConfigurations; 100 101 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 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 131 public void addExtension(IExtensionTracker tracker, IExtension extension) { 132 processExtension(tracker, extension); 133 allConfigurations = null; } 135 136 142 public void removeExtension(IExtension extension, Object [] objects) { 143 for (int i = 0; i < objects.length; i++) { 144 registeredContributions.remove(objects[i]); 145 } 146 allConfigurations = null; 148 } 149 150 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 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 196 public void handleException(Throwable e) { 197 } 199 }); 200 201 if (exceptions[0] != null) { 202 return null; 203 } 204 String name = element.getAttribute(LABEL); 205 String fileSystem = element.getAttribute(SCHEME); 206 FileSystemConfiguration config = new FileSystemConfiguration(name, 207 contributors[0], fileSystem); 208 209 return config; 210 211 } 212 213 218 public FileSystemConfiguration[] getConfigurations() { 219 if (allConfigurations == null) { 220 allConfigurations = new FileSystemConfiguration[registeredContributions 221 .size() + 1]; 222 allConfigurations[0] = defaultConfiguration; 223 224 Iterator 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 240 public FileSystemConfiguration getDefaultConfiguration() { 241 return defaultConfiguration; 242 } 243 244 249 public boolean hasOneFileSystem() { 250 return registeredContributions.size() == 0; 251 } 252 } 253 | Popular Tags |