1 package org.apache.directory.ldapstudio.browser.common; 2 3 4 import java.io.IOException ; 5 import java.net.URL ; 6 7 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 8 import org.apache.directory.ldapstudio.browser.core.events.EventRunner; 9 import org.eclipse.core.runtime.FileLocator; 10 import org.eclipse.core.runtime.IConfigurationElement; 11 import org.eclipse.core.runtime.IExtension; 12 import org.eclipse.core.runtime.IExtensionPoint; 13 import org.eclipse.core.runtime.Path; 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.jface.resource.ColorRegistry; 16 import org.eclipse.jface.resource.FontRegistry; 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.jface.text.templates.ContextTypeRegistry; 19 import org.eclipse.jface.text.templates.GlobalTemplateVariables; 20 import org.eclipse.jface.text.templates.persistence.TemplateStore; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.Font; 23 import org.eclipse.swt.graphics.FontData; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.graphics.RGB; 26 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry; 27 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore; 28 import org.eclipse.ui.plugin.AbstractUIPlugin; 29 import org.osgi.framework.BundleContext; 30 31 32 35 public class BrowserCommonActivator extends AbstractUIPlugin 36 { 37 38 39 public static final String PLUGIN_ID = "org.apache.directory.ldapstudio.browser.common"; 40 41 42 private static BrowserCommonActivator plugin; 43 44 45 private FontRegistry fontRegistry; 46 47 48 private ColorRegistry colorRegistry; 49 50 51 private ExceptionHandler exceptionHandler; 52 53 54 private ValueEditorsPreferences valueEditorPreferences; 55 56 57 private ContributionTemplateStore filterTemplateStore; 58 59 60 private ContributionContextTypeRegistry filterTemplateContextTypeRegistry; 61 62 63 private EventRunner eventRunner; 64 65 66 69 public BrowserCommonActivator() 70 { 71 plugin = this; 72 } 73 74 75 78 public void start( BundleContext context ) throws Exception 79 { 80 super.start( context ); 81 82 if ( eventRunner == null ) 83 { 84 eventRunner = new UiThreadEventRunner(); 85 } 86 87 if ( fontRegistry == null ) 88 { 89 fontRegistry = new FontRegistry( getWorkbench().getDisplay() ); 90 } 91 92 if ( colorRegistry == null ) 93 { 94 colorRegistry = new ColorRegistry( getWorkbench().getDisplay() ); 95 } 96 97 if ( exceptionHandler == null ) 98 { 99 exceptionHandler = new ExceptionHandler(); 100 } 101 102 valueEditorPreferences = new ValueEditorsPreferences(); 103 104 if ( filterTemplateContextTypeRegistry == null ) 105 { 106 filterTemplateContextTypeRegistry = new ContributionContextTypeRegistry(); 107 filterTemplateContextTypeRegistry.addContextType( BrowserCommonConstants.FILTER_TEMPLATE_ID ); 108 filterTemplateContextTypeRegistry.getContextType( BrowserCommonConstants.FILTER_TEMPLATE_ID ).addResolver( 109 new GlobalTemplateVariables.Cursor() ); 110 } 111 112 if ( filterTemplateStore == null ) 113 { 114 filterTemplateStore = new ContributionTemplateStore( getFilterTemplateContextTypeRegistry(), 115 getPreferenceStore(), "templates" ); 116 try 117 { 118 filterTemplateStore.load(); 119 } 120 catch ( IOException e ) 121 { 122 e.printStackTrace(); 123 } 124 } 125 126 BrowserCorePlugin.getDefault().setAuthHandler( new BrowserCommonAuthHandler() ); 127 BrowserCorePlugin.getDefault().setReferralHandler( new BrowserCommonReferralHandler() ); 128 } 129 130 131 134 public void stop( BundleContext context ) throws Exception 135 { 136 plugin = null; 137 super.stop( context ); 138 139 if ( eventRunner != null ) 140 { 141 eventRunner = null; 142 } 143 144 if ( fontRegistry != null ) 145 { 146 fontRegistry = null; 147 } 148 149 if ( colorRegistry != null ) 150 { 151 colorRegistry = null; 152 } 153 154 if ( exceptionHandler != null ) 155 { 156 exceptionHandler = null; 157 } 158 159 if ( filterTemplateContextTypeRegistry != null ) 160 { 161 filterTemplateContextTypeRegistry = null; 162 } 163 164 if ( filterTemplateStore != null ) 165 { 166 try 167 { 168 filterTemplateStore.save(); 169 } 170 catch ( IOException e ) 171 { 172 e.printStackTrace(); 173 } 174 filterTemplateStore = null; 175 } 176 } 177 178 179 184 public static BrowserCommonActivator getDefault() 185 { 186 return plugin; 187 } 188 189 190 198 public ImageDescriptor getImageDescriptor( String key ) 199 { 200 if ( key != null ) 201 { 202 URL url = FileLocator.find( getBundle(), new Path( key ), null ); 203 if ( url != null ) 204 return ImageDescriptor.createFromURL( url ); 205 else 206 return null; 207 } 208 else 209 { 210 return null; 211 } 212 } 213 214 215 228 public Image getImage( String key ) 229 { 230 Image image = getImageRegistry().get( key ); 231 if ( image == null ) 232 { 233 ImageDescriptor id = getImageDescriptor( key ); 234 if ( id != null ) 235 { 236 image = id.createImage(); 237 getImageRegistry().put( key, image ); 238 } 239 } 240 return image; 241 } 242 243 244 255 256 public Font getFont( FontData[] fontData ) 257 { 258 if ( !fontRegistry.hasValueFor( fontData[0].toString() ) ) 259 { 260 fontRegistry.put( fontData[0].toString(), fontData ); 261 } 262 263 return fontRegistry.get( fontData[0].toString() ); 264 } 265 266 267 278 public Color getColor( RGB rgb ) 279 { 280 if ( !colorRegistry.hasValueFor( rgb.toString() ) ) 281 { 282 colorRegistry.put( rgb.toString(), rgb ); 283 } 284 285 return colorRegistry.get( rgb.toString() ); 286 } 287 288 289 293 public ExceptionHandler getExceptionHandler() 294 { 295 return exceptionHandler; 296 } 297 298 299 304 public ValueEditorsPreferences getValueEditorsPreferences() 305 { 306 return valueEditorPreferences; 307 } 308 309 310 314 public TemplateStore getFilterTemplateStore() 315 { 316 return filterTemplateStore; 317 } 318 319 320 324 public ContextTypeRegistry getFilterTemplateContextTypeRegistry() 325 { 326 return filterTemplateContextTypeRegistry; 327 } 328 329 330 336 public static boolean isIDEEnvironment() 337 { 338 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint( 339 "org.eclipse.ui.perspectives" ); 340 if ( extensionPoint != null ) 341 { 342 IExtension[] extensions = extensionPoint.getExtensions(); 343 if ( extensions != null ) 344 { 345 for ( int i = 0; i < extensions.length; i++ ) 346 { 347 IExtension extension = extensions[i]; 348 IConfigurationElement[] elements = extension.getConfigurationElements(); 349 for ( int j = 0; j < elements.length; j++ ) 350 { 351 IConfigurationElement element = elements[j]; 352 if ( element.getName().equals( "perspective" ) ) 353 { 354 if ( "org.eclipse.ui.resourcePerspective".equals( element.getAttribute( "id" ) ) ) 355 { 356 return true; 357 } 358 } 359 } 360 } 361 } 362 } 363 364 return false; 365 } 366 367 368 373 public EventRunner getEventRunner() 374 { 375 return eventRunner; 376 } 377 378 } 379 | Popular Tags |