View Javadoc

1   /* ======================================
2    * Copyright (c) 2004-2005 Achim Nierbeck
3    * All rights reserved.
4    *
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.gnu.org/licenses/lgpl.html
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * ======================================
16   */
17  
18  /*
19   * $Revision: 1.6 $
20   * Date 14.12.2005
21   *  
22   * $Author: nierbeck $ 
23   * $Date: 2006/02/03 22:34:55 $ 
24   */
25  package de.nierbeck.timeTrack;
26  
27  import java.util.MissingResourceException;
28  import java.util.ResourceBundle;
29  
30  import org.eclipse.jface.resource.ImageDescriptor;
31  import org.eclipse.ui.plugin.AbstractUIPlugin;
32  import org.osgi.framework.BundleContext;
33  
34  /***
35   * The main plugin class to be used in the desktop.
36   * 
37   * @author Achim
38   */
39  public class TimeTrackPlugin extends AbstractUIPlugin {
40  	// The shared instance.
41  	private static TimeTrackPlugin plugin;
42  
43  	private static TimeTrackEntriesManager timeTrackEntriesManager;
44  
45  	// Resource bundle.
46  	private static ResourceBundle resourceBundle;
47  
48  	/***
49  	 * Static field PI_RUNTIME contains the Classname
50  	 */
51  	public static final String PI_RUNTIME = "de.nierbeck.TimeTrackPlugin";
52  	
53  	/***
54  	 * Static field for calling the Resource Bundle 
55  	 */
56  	public static final String PI_RESOURCE = "de.nierbeck.timeTrack.TimeTrackPluginResources";
57  
58  	/***
59  	 * The constructor.
60  	 */
61  	public TimeTrackPlugin() {
62  		super();
63  		plugin = this;
64  	}
65  
66  	/***
67  	 * This method is called upon plug-in activation
68  	 * 
69  	 * @param context - the BundleContext
70  	 * 
71  	 * @throws Exception - can be thrown by the super.start
72  	 */
73  	public final void start(BundleContext context) throws Exception {
74  		super.start(context);
75  		// TimeTrackEntriesManager.getTimeTrackEntriesManager().restoreState();
76  		timeTrackEntriesManager = new TimeTrackEntriesManager();
77  		timeTrackEntriesManager.restoreState();
78  	}
79  
80  	/***
81  	 * This method is called when the plug-in is stopped
82  	 * 
83  	 * @param context - the BundleContext of this Plugin
84  	 * 
85  	 * @throws Exception - propably by the super method
86  	 */
87  	public final void stop(BundleContext context) throws Exception {
88  		// TimeTrackEntriesManager.getTimeTrackEntriesManager().saveState();
89  		timeTrackEntriesManager.saveState();
90  		super.stop(context);
91  		plugin = null;
92  		resourceBundle = null;
93  	}
94  
95  	/***
96  	 * Returns the shared instance.
97  	 * 
98  	 * @return the shared instance
99  	 */
100 	public static TimeTrackPlugin getDefault() {
101 		return plugin;
102 	}
103 
104 	/***
105 	 * Returns the string from the plugin's resource bundle, or 'key' if not
106 	 * found.
107 	 * 
108 	 * @param key - the key to look for in the Resource bundle
109 	 * 
110 	 * @return a resource string connected to the given key
111 	 */
112 	public final static String getResourceString(String key) {
113 		ResourceBundle bundle = TimeTrackPlugin.getDefault()
114 				.getResourceBundle();
115 		String rc = key;
116 		try {
117 			rc = (bundle != null) ? bundle.getString(key) : key;
118 		} catch (MissingResourceException e) {
119 			TimeTrackLog.logError("No resource found", e);
120 		}
121 		
122 		return rc;
123 	}
124 
125 	/***
126 	 * Returns the plugin's resource bundle,
127 	 * 
128 	 * @return the ResourceBundle
129 	 */
130 	public final ResourceBundle getResourceBundle() {
131 		try {
132 			if (resourceBundle == null) {
133 				resourceBundle = ResourceBundle
134 						.getBundle(TimeTrackPlugin.PI_RESOURCE);
135 			}
136 		} catch (MissingResourceException x) {
137 			TimeTrackLog.logError("Trouble loading Resource", x);
138 			resourceBundle = null;
139 		}
140 		return resourceBundle;
141 	}
142 
143 	/***
144 	 * Returns an image descriptor for the image file at the given plug-in
145 	 * relative path.
146 	 * 
147 	 * @param path
148 	 *            the path
149 	 * @return the image descriptor
150 	 */
151 	public static ImageDescriptor getImageDescriptor(String path) {
152 		return AbstractUIPlugin.imageDescriptorFromPlugin(TimeTrackPlugin.PI_RUNTIME, path);
153 	}
154 
155 	/***
156 	 * returns the TimeTrackEntriesManager instance and creates it
157 	 * if it is not already created (Singleton)
158 	 * 
159 	 * @return the timeTrackManager instance
160 	 */
161 	public static TimeTrackEntriesManager getTimeTrackManager() {
162 		if (timeTrackEntriesManager == null) {
163 			timeTrackEntriesManager = new TimeTrackEntriesManager();
164 		}
165 		return timeTrackEntriesManager;
166 	}
167 
168 }