1 package org.apache.directory.ldapstudio.ldifeditor; 2 3 4 import java.io.IOException ; 5 import java.net.URL ; 6 import java.util.MissingResourceException ; 7 import java.util.ResourceBundle ; 8 9 import org.eclipse.core.runtime.FileLocator; 10 import org.eclipse.core.runtime.Path; 11 import org.eclipse.jface.resource.ColorRegistry; 12 import org.eclipse.jface.resource.ImageDescriptor; 13 import org.eclipse.jface.text.templates.ContextTypeRegistry; 14 import org.eclipse.jface.text.templates.GlobalTemplateVariables; 15 import org.eclipse.jface.text.templates.persistence.TemplateStore; 16 import org.eclipse.swt.graphics.Color; 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.swt.graphics.RGB; 19 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry; 20 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore; 21 import org.eclipse.ui.plugin.AbstractUIPlugin; 22 import org.osgi.framework.BundleContext; 23 24 25 28 public class LdifEditorActivator extends AbstractUIPlugin 29 { 30 31 32 public static final String PLUGIN_ID = "org.apache.directory.ldapstudio.ldifeditor"; 33 34 35 private static LdifEditorActivator plugin; 36 37 38 private ResourceBundle resourceBundle; 39 40 41 private ColorRegistry colorRegistry; 42 43 44 private ContributionTemplateStore ldifTemplateStore; 45 46 47 private ContributionContextTypeRegistry ldifTemplateContextTypeRegistry; 48 49 50 53 public LdifEditorActivator() 54 { 55 plugin = this; 56 57 try 58 { 59 resourceBundle = ResourceBundle.getBundle( "org.apache.directory.ldapstudio.ldifeditor.messages" ); 60 } 61 catch ( MissingResourceException x ) 62 { 63 resourceBundle = null; 64 } 65 } 66 67 68 71 public void start( BundleContext context ) throws Exception 72 { 73 super.start( context ); 74 75 if ( colorRegistry == null ) 76 { 77 colorRegistry = new ColorRegistry( getWorkbench().getDisplay() ); 78 } 79 80 if ( ldifTemplateContextTypeRegistry == null ) 81 { 82 ldifTemplateContextTypeRegistry = new ContributionContextTypeRegistry(); 83 84 ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_FILE_TEMPLATE_ID ); 85 ldifTemplateContextTypeRegistry.getContextType( LdifEditorConstants.LDIF_FILE_TEMPLATE_ID ).addResolver( 86 new GlobalTemplateVariables.Cursor() ); 87 88 ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_ATTR_VAL_RECORD_TEMPLATE_ID ); 89 ldifTemplateContextTypeRegistry.getContextType( LdifEditorConstants.LDIF_ATTR_VAL_RECORD_TEMPLATE_ID ) 90 .addResolver( new GlobalTemplateVariables.Cursor() ); 91 92 ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_MODIFICATION_RECORD_TEMPLATE_ID ); 93 ldifTemplateContextTypeRegistry.getContextType( LdifEditorConstants.LDIF_MODIFICATION_RECORD_TEMPLATE_ID ) 94 .addResolver( new GlobalTemplateVariables.Cursor() ); 95 96 ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_MODIFICATION_ITEM_TEMPLATE_ID ); 97 98 ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_MODDN_RECORD_TEMPLATE_ID ); 99 } 100 101 if ( ldifTemplateStore == null ) 102 { 103 ldifTemplateStore = new ContributionTemplateStore( getLdifTemplateContextTypeRegistry(), 104 getPreferenceStore(), "templates" ); 105 try 106 { 107 ldifTemplateStore.load(); 108 } 109 catch ( IOException e ) 110 { 111 e.printStackTrace(); 112 } 113 } 114 } 115 116 117 120 public void stop( BundleContext context ) throws Exception 121 { 122 plugin = null; 123 super.stop( context ); 124 125 if ( colorRegistry != null ) 126 { 127 colorRegistry = null; 128 } 129 130 if ( ldifTemplateContextTypeRegistry != null ) 131 { 132 ldifTemplateContextTypeRegistry = null; 133 } 134 135 if ( ldifTemplateStore != null ) 136 { 137 try 138 { 139 ldifTemplateStore.save(); 140 } 141 catch ( IOException e ) 142 { 143 e.printStackTrace(); 144 } 145 ldifTemplateStore = null; 146 } 147 } 148 149 150 155 public static LdifEditorActivator getDefault() 156 { 157 return plugin; 158 } 159 160 161 172 public Color getColor( RGB rgb ) 173 { 174 if ( !colorRegistry.hasValueFor( rgb.toString() ) ) 175 { 176 colorRegistry.put( rgb.toString(), rgb ); 177 } 178 179 return colorRegistry.get( rgb.toString() ); 180 } 181 182 183 191 public ImageDescriptor getImageDescriptor( String key ) 192 { 193 if ( key != null ) 194 { 195 URL url = FileLocator.find( getBundle(), new Path( key ), null ); 196 if ( url != null ) 197 return ImageDescriptor.createFromURL( url ); 198 else 199 return null; 200 } 201 else 202 { 203 return null; 204 } 205 } 206 207 208 221 public Image getImage( String key ) 222 { 223 Image image = getImageRegistry().get( key ); 224 if ( image == null ) 225 { 226 ImageDescriptor id = getImageDescriptor( key ); 227 if ( id != null ) 228 { 229 image = id.createImage(); 230 getImageRegistry().put( key, image ); 231 } 232 } 233 return image; 234 } 235 236 237 241 public ContextTypeRegistry getLdifTemplateContextTypeRegistry() 242 { 243 return ldifTemplateContextTypeRegistry; 244 } 245 246 247 251 public TemplateStore getLdifTemplateStore() 252 { 253 return ldifTemplateStore; 254 } 255 256 257 262 public ResourceBundle getResourceBundle() 263 { 264 return resourceBundle; 265 } 266 267 } 268 | Popular Tags |