summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java266
1 files changed, 266 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java
new file mode 100644
index 0000000..505a254
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java
@@ -0,0 +1,266 @@
1/*****************************************************************************
2 * Copyright (c) 2009 Ken Gilmer
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Ken Gilmer - initial API and implementation
10 *******************************************************************************/
11package org.yocto.bc.ui;
12
13import java.io.File;
14import java.io.IOException;
15import java.io.Writer;
16import java.lang.reflect.InvocationTargetException;
17import java.util.Hashtable;
18import java.util.Iterator;
19import java.util.Map;
20
21import org.eclipse.core.resources.IProject;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.IResourceChangeEvent;
24import org.eclipse.core.resources.IResourceChangeListener;
25import org.eclipse.core.resources.ResourcesPlugin;
26import org.eclipse.core.runtime.CoreException;
27import org.eclipse.jface.resource.ImageDescriptor;
28import org.eclipse.jface.resource.ImageRegistry;
29import org.eclipse.ui.plugin.AbstractUIPlugin;
30import org.osgi.framework.BundleContext;
31
32import org.yocto.bc.bitbake.BBRecipe;
33import org.yocto.bc.bitbake.BBSession;
34import org.yocto.bc.bitbake.ProjectInfoHelper;
35import org.yocto.bc.bitbake.ShellSession;
36import org.yocto.bc.ui.model.ProjectInfo;
37import org.yocto.bc.ui.wizards.newproject.CreateBBCProjectOperation;
38
39public class Activator extends AbstractUIPlugin {
40
41 // The plug-in ID
42 public static final String PLUGIN_ID = "org.yocto.bc.ui";
43 public static final String IMAGE_VARIABLE = "IMAGE_VARIABLE";
44 public static final String IMAGE_FUNCTION = "IMAGE_FUNCTION";
45
46 // The shared instance
47 private static Activator plugin;
48 private static Map shellMap;
49 private static Map projInfoMap;
50 private static Hashtable bbSessionMap;
51 private static Hashtable bbRecipeMap;
52
53 private IResourceChangeListener listener = new BCResourceChangeListener();
54
55 public static BBRecipe getBBRecipe(BBSession session, String filePath) throws IOException {
56 if (bbRecipeMap == null) {
57 bbRecipeMap = new Hashtable();
58 }
59
60 String key = session.getProjInfoRoot() + filePath;
61 BBRecipe recipe = (BBRecipe) bbRecipeMap.get(key);
62 if (recipe == null) {
63 recipe = new BBRecipe(session,filePath);
64 bbRecipeMap.put(key, recipe);
65 }
66
67 return recipe;
68 }
69
70 /**
71 * Get or create a BitBake session passing in ProjectInfo
72 * @param pinfo
73 * @return
74 * @throws IOException
75 */
76 public static BBSession getBBSession(String projectRoot, Writer out) throws IOException {
77 if (bbSessionMap == null) {
78 bbSessionMap = new Hashtable();
79 }
80
81 BBSession bbs = (BBSession) bbSessionMap.get(projectRoot);
82
83 if (bbs == null) {
84 bbs = new BBSession(getShellSession(projectRoot, out), projectRoot);
85 bbSessionMap.put(projectRoot, bbs);
86 }
87
88 return bbs;
89 }
90
91 /**
92 * Get or create a BitBake session passing in ProjectInfo
93 * @param pinfo
94 * @return
95 * @throws IOException
96 */
97 public static BBSession getBBSession(String projectRoot) throws IOException {
98 if (bbSessionMap == null) {
99 bbSessionMap = new Hashtable();
100 }
101
102 BBSession bbs = (BBSession) bbSessionMap.get(projectRoot);
103
104 if (bbs == null) {
105 bbs = new BBSession(getShellSession(projectRoot, null), projectRoot);
106 bbSessionMap.put(projectRoot, bbs);
107 }
108
109 return bbs;
110 }
111
112 /**
113 * Returns the shared instance
114 *
115 * @return the shared instance
116 */
117 public static Activator getDefault() {
118 return plugin;
119 }
120
121 /**
122 * Returns an image descriptor for the image file at the given
123 * plug-in relative path
124 *
125 * @param path the path
126 * @return the image descriptor
127 */
128 public static ImageDescriptor getImageDescriptor(String path) {
129 return imageDescriptorFromPlugin(PLUGIN_ID, path);
130 }
131
132 public static ProjectInfo getProjInfo(String location) throws CoreException, InvocationTargetException, InterruptedException {
133 if (projInfoMap == null) {
134 projInfoMap = new Hashtable();
135 }
136
137 ProjectInfo pi = (ProjectInfo) projInfoMap.get(location);
138
139 if (pi == null) {
140 pi = new ProjectInfo();
141 pi.setLocation(location);
142 try {
143 pi.setInitScriptPath(ProjectInfoHelper.getInitScriptPath(location));
144 } catch (IOException e) {
145 throw new InvocationTargetException(e);
146 }
147 }
148
149 return pi;
150 }
151
152 public static void notifyAllBBSession(IResource[] added, IResource[] removed, IResource[] changed) {
153 Iterator iter;
154 if(bbRecipeMap != null) {
155 iter = bbRecipeMap.values().iterator();
156 while(iter.hasNext()) {
157 BBRecipe p = (BBRecipe)iter.next();
158 p.changeNotified(added, removed, changed);
159 }
160 }
161
162 if(bbSessionMap != null) {
163 iter= bbSessionMap.values().iterator();
164 while(iter.hasNext()) {
165 BBSession p = (BBSession)iter.next();
166 p.changeNotified(added, removed, changed);
167 }
168 }
169 }
170
171 /**
172 * @param absolutePath
173 * @return a cached shell session for a given project root.
174 * @throws IOException
175 */
176 private static ShellSession getShellSession(String absolutePath, Writer out) throws IOException {
177 if (shellMap == null) {
178 shellMap = new Hashtable();
179 }
180
181 ShellSession ss = (ShellSession) shellMap.get(absolutePath);
182
183 if (ss == null) {
184 ss = new ShellSession(ShellSession.SHELL_TYPE_BASH, new File(absolutePath), ProjectInfoHelper.getInitScriptPath(absolutePath), out);
185 }
186
187 return ss;
188 }
189
190 private static String loadInit(String absolutePath) throws CoreException {
191 IProject [] prjs = ResourcesPlugin.getWorkspace().getRoot().getProjects();
192 IProject foundPrj = null;
193
194 for (int i = 0; i < prjs.length; ++i) {
195 IProject p = prjs[i];
196
197 System.out
198 .println(p.getDescription().getLocationURI().getPath());
199
200 if (p.getDescription().getLocationURI().getPath().equals(absolutePath)) {
201 foundPrj = p;
202 break;
203 }
204 }
205
206 if (foundPrj == null) {
207 throw new RuntimeException("Unable to find project associated with path! " + absolutePath);
208 }
209
210 return foundPrj.getPersistentProperty(CreateBBCProjectOperation.BBC_PROJECT_INIT);
211 }
212
213 public static void putProjInfo(String location, ProjectInfo pinfo) {
214 if (projInfoMap == null) {
215 projInfoMap = new Hashtable();
216 }
217
218
219
220 projInfoMap.put(location, pinfo);
221 }
222
223 /**
224 * The constructor
225 */
226 public Activator() {
227 }
228
229 /*
230 * (non-Javadoc)
231 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
232 */
233 @Override
234 public void start(BundleContext context) throws Exception {
235 super.start(context);
236 plugin = this;
237 ResourcesPlugin.getWorkspace().addResourceChangeListener(
238 listener, IResourceChangeEvent.POST_CHANGE);
239 }
240
241 /*
242 * (non-Javadoc)
243 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
244 */
245 @Override
246 public void stop(BundleContext context) throws Exception {
247 ResourcesPlugin.getWorkspace().removeResourceChangeListener(
248 listener);
249 plugin = null;
250 super.stop(context);
251 }
252
253 /**
254 * Reset a configuration
255 * @param path
256 */
257 public static void resetBBSession(String path) {
258 shellMap.remove(path);
259 bbSessionMap.remove(path);
260 }
261
262 protected void initializeImageRegistry(ImageRegistry reg) {
263 reg.put(IMAGE_VARIABLE, Activator.getImageDescriptor("icons/variable.gif"));
264 reg.put(IMAGE_FUNCTION, Activator.getImageDescriptor("icons/function.gif"));
265 }
266}