001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.studio.apacheds;
021    
022    
023    import java.io.IOException;
024    import java.net.URL;
025    import java.util.PropertyResourceBundle;
026    
027    import org.apache.directory.studio.apacheds.model.ServersHandler;
028    import org.eclipse.core.runtime.FileLocator;
029    import org.eclipse.core.runtime.Path;
030    import org.eclipse.core.runtime.Status;
031    import org.eclipse.jface.resource.ImageDescriptor;
032    import org.eclipse.swt.graphics.Image;
033    import org.eclipse.ui.plugin.AbstractUIPlugin;
034    import org.osgi.framework.BundleContext;
035    
036    
037    /**
038     * The activator class controls the plug-in life cycle
039     */
040    public class ApacheDsPlugin extends AbstractUIPlugin
041    {
042        /** The shared instance */
043        private static ApacheDsPlugin plugin;
044    
045        /** The servers handler */
046        private ServersHandler serversHandler;
047    
048        /** The plugin properties */
049        private PropertyResourceBundle properties;
050    
051    
052        /**
053         * The constructor
054         */
055        public ApacheDsPlugin()
056        {
057        }
058    
059    
060        /**
061         * Returns the shared instance
062         *
063         * @return the shared instance
064         */
065        public static ApacheDsPlugin getDefault()
066        {
067            return plugin;
068        }
069    
070    
071        /*
072         * (non-Javadoc)
073         * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
074         */
075        public void start( BundleContext context ) throws Exception
076        {
077            super.start( context );
078            plugin = this;
079    
080            // Creating the servers handler
081            serversHandler = ServersHandler.getDefault();
082    
083            // Initializing the servers from the store
084            serversHandler.loadServersFromStore();
085        }
086    
087    
088        /**
089         * (non-Javadoc)
090         * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
091         */
092        public void stop( BundleContext context ) throws Exception
093        {
094            plugin = null;
095            super.stop( context );
096        }
097    
098    
099        /**
100         * Use this method to get SWT images. Use the IMG_ constants from
101         * PluginConstants for the key.
102         *
103         * @param key
104         *                The key (relative path to the image in filesystem)
105         * @return The image descriptor or null
106         */
107        public ImageDescriptor getImageDescriptor( String key )
108        {
109            if ( key != null )
110            {
111                URL url = FileLocator.find( getBundle(), new Path( key ), null );
112                if ( url != null )
113                    return ImageDescriptor.createFromURL( url );
114                else
115                    return null;
116            }
117            else
118            {
119                return null;
120            }
121        }
122    
123    
124        /**
125         * Use this method to get SWT images. Use the IMG_ constants from
126         * PluginConstants for the key. A ImageRegistry is used to manage the
127         * the key->Image mapping.
128         * <p>
129         * Note: Don't dispose the returned SWT Image. It is disposed
130         * automatically when the plugin is stopped.
131         *
132         * @param key
133         *                The key (relative path to the image in filesystem)
134         * @return The SWT Image or null
135         */
136        public Image getImage( String key )
137        {
138            Image image = getImageRegistry().get( key );
139            if ( image == null )
140            {
141                ImageDescriptor id = getImageDescriptor( key );
142                if ( id != null )
143                {
144                    image = id.createImage();
145                    getImageRegistry().put( key, image );
146                }
147            }
148            return image;
149        }
150    
151    
152        /**
153         * Gets the plugin properties.
154         *
155         * @return
156         *      the plugin properties
157         */
158        public PropertyResourceBundle getPluginProperties()
159        {
160            if ( properties == null )
161            {
162                try
163                {
164                    properties = new PropertyResourceBundle( FileLocator.openStream( this.getBundle(), new Path(
165                        "plugin.properties" ), false ) );
166                }
167                catch ( IOException e )
168                {
169                    // We can't use the PLUGIN_ID constant since loading the plugin.properties file has failed,
170                    // So we're using a default plugin id.
171                    getLog().log(
172                        new Status( Status.ERROR, "org.apache.directory.studio.apacheds", Status.OK,
173                            "Unable to get the plugin properties.", e ) );
174                }
175            }
176    
177            return properties;
178        }
179    }