diff options
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.java | 254 |
1 files changed, 254 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..f53592c --- /dev/null +++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/Activator.java | |||
@@ -0,0 +1,254 @@ | |||
1 | /***************************************************************************** | ||
2 | * Copyright (c) 2013 Ken Gilmer, Intel Corporation | ||
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 | * Ioana Grigoropol (Intel) - adapt class for remote support | ||
11 | *******************************************************************************/ | ||
12 | package org.yocto.bc.ui; | ||
13 | |||
14 | import java.io.File; | ||
15 | import java.io.IOException; | ||
16 | import java.io.Writer; | ||
17 | import java.lang.reflect.InvocationTargetException; | ||
18 | import java.net.URI; | ||
19 | import java.util.Hashtable; | ||
20 | import java.util.Iterator; | ||
21 | import java.util.Map; | ||
22 | |||
23 | import org.eclipse.core.resources.IProject; | ||
24 | import org.eclipse.core.resources.IResource; | ||
25 | import org.eclipse.core.resources.IResourceChangeEvent; | ||
26 | import org.eclipse.core.resources.IResourceChangeListener; | ||
27 | import org.eclipse.core.resources.ResourcesPlugin; | ||
28 | import org.eclipse.core.runtime.CoreException; | ||
29 | import org.eclipse.core.runtime.IProgressMonitor; | ||
30 | import org.eclipse.jface.resource.ImageDescriptor; | ||
31 | import org.eclipse.jface.resource.ImageRegistry; | ||
32 | import org.eclipse.rse.services.files.IHostFile; | ||
33 | import org.eclipse.ui.plugin.AbstractUIPlugin; | ||
34 | import org.osgi.framework.BundleContext; | ||
35 | |||
36 | import org.yocto.bc.bitbake.BBRecipe; | ||
37 | import org.yocto.bc.bitbake.BBSession; | ||
38 | import org.yocto.bc.bitbake.ProjectInfoHelper; | ||
39 | import org.yocto.bc.bitbake.ShellSession; | ||
40 | import org.yocto.bc.ui.model.ProjectInfo; | ||
41 | import org.yocto.bc.ui.wizards.newproject.CreateBBCProjectOperation; | ||
42 | import org.yocto.remote.utils.RemoteHelper; | ||
43 | |||
44 | public class Activator extends AbstractUIPlugin { | ||
45 | |||
46 | // The plug-in ID | ||
47 | public static final String PLUGIN_ID = "org.yocto.bc.ui"; | ||
48 | public static final String IMAGE_VARIABLE = "IMAGE_VARIABLE"; | ||
49 | public static final String IMAGE_FUNCTION = "IMAGE_FUNCTION"; | ||
50 | |||
51 | // The shared instance | ||
52 | private static Activator plugin; | ||
53 | private static Map shellMap; | ||
54 | private static Map<URI, ProjectInfo> projInfoMap; | ||
55 | private static Hashtable bbSessionMap; | ||
56 | private static Hashtable bbRecipeMap; | ||
57 | |||
58 | private IResourceChangeListener listener = new BCResourceChangeListener(); | ||
59 | |||
60 | public static BBRecipe getBBRecipe(BBSession session, URI fileURI) throws IOException { | ||
61 | if (bbRecipeMap == null) { | ||
62 | bbRecipeMap = new Hashtable(); | ||
63 | } | ||
64 | |||
65 | URI key = session.getProjInfoRoot(); | ||
66 | BBRecipe recipe = (BBRecipe) bbRecipeMap.get(key); | ||
67 | if (recipe == null) { | ||
68 | recipe = new BBRecipe(session, fileURI); | ||
69 | bbRecipeMap.put(key, recipe); | ||
70 | } | ||
71 | |||
72 | return recipe; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Get or create a BitBake session passing in ProjectInfo | ||
77 | * @param pinfo | ||
78 | * @return | ||
79 | * @throws IOException | ||
80 | */ | ||
81 | public static BBSession getBBSession(ProjectInfo projectInfo, Writer out, IProgressMonitor monitor) throws IOException { | ||
82 | URI projectRoot = projectInfo.getOriginalURI(); | ||
83 | if (bbSessionMap == null) { | ||
84 | bbSessionMap = new Hashtable(); | ||
85 | } | ||
86 | |||
87 | BBSession bbs = (BBSession) bbSessionMap.get(projectRoot); | ||
88 | |||
89 | if (bbs == null) { | ||
90 | bbs = new BBSession(getShellSession(projectInfo, out, monitor), projectRoot); | ||
91 | bbSessionMap.put(projectRoot, bbs); | ||
92 | } | ||
93 | |||
94 | return bbs; | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * Get or create a BitBake session passing in ProjectInfo | ||
99 | * @param pinfo | ||
100 | * @return | ||
101 | * @throws IOException | ||
102 | */ | ||
103 | public static BBSession getBBSession(ProjectInfo projectInfo, IProgressMonitor monitor) throws Exception { | ||
104 | URI projectRoot = projectInfo.getOriginalURI(); | ||
105 | if (bbSessionMap == null) { | ||
106 | bbSessionMap = new Hashtable(); | ||
107 | } | ||
108 | |||
109 | BBSession bbs = (BBSession) bbSessionMap.get(projectRoot); | ||
110 | |||
111 | if (bbs == null || bbs.getShell() == null) { | ||
112 | bbs = new BBSession(getShellSession(projectInfo, null, monitor), projectRoot); | ||
113 | bbSessionMap.put(projectRoot, bbs); | ||
114 | } | ||
115 | |||
116 | return bbs; | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * Returns the shared instance | ||
121 | * | ||
122 | * @return the shared instance | ||
123 | */ | ||
124 | public static Activator getDefault() { | ||
125 | return plugin; | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Returns an image descriptor for the image file at the given | ||
130 | * plug-in relative path | ||
131 | * | ||
132 | * @param path the path | ||
133 | * @return the image descriptor | ||
134 | */ | ||
135 | public static ImageDescriptor getImageDescriptor(String path) { | ||
136 | return imageDescriptorFromPlugin(PLUGIN_ID, path); | ||
137 | } | ||
138 | |||
139 | public static ProjectInfo getProjInfo(URI location) throws CoreException, InvocationTargetException, InterruptedException { | ||
140 | if (projInfoMap == null) { | ||
141 | projInfoMap = new Hashtable<URI, ProjectInfo>(); | ||
142 | } | ||
143 | if (location != null) { | ||
144 | ProjectInfo pi = projInfoMap.get(location); | ||
145 | if (pi == null) { | ||
146 | pi = new ProjectInfo(); | ||
147 | pi.setLocationURI(location); | ||
148 | try { | ||
149 | pi.setInitScriptPath(ProjectInfoHelper.getInitScriptPath(location)); | ||
150 | } catch (IOException e) { | ||
151 | throw new InvocationTargetException(e); | ||
152 | } | ||
153 | |||
154 | projInfoMap.put(location, pi); | ||
155 | } | ||
156 | return pi; | ||
157 | } | ||
158 | return null; | ||
159 | } | ||
160 | |||
161 | public static void notifyAllBBSession(IResource[] added, IResource[] removed, IResource[] changed) { | ||
162 | Iterator iter; | ||
163 | if(bbRecipeMap != null) { | ||
164 | iter = bbRecipeMap.values().iterator(); | ||
165 | while(iter.hasNext()) { | ||
166 | BBRecipe p = (BBRecipe)iter.next(); | ||
167 | p.changeNotified(added, removed, changed); | ||
168 | } | ||
169 | } | ||
170 | |||
171 | if(bbSessionMap != null) { | ||
172 | iter= bbSessionMap.values().iterator(); | ||
173 | while(iter.hasNext()) { | ||
174 | BBSession p = (BBSession)iter.next(); | ||
175 | p.changeNotified(added, removed, changed); | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * @param absolutePath | ||
182 | * @return a cached shell session for a given project root. | ||
183 | * @throws IOException | ||
184 | */ | ||
185 | private static ShellSession getShellSession(ProjectInfo projInfo, Writer out, IProgressMonitor monitor) throws IOException { | ||
186 | URI absolutePath = projInfo.getOriginalURI(); | ||
187 | if (shellMap == null) { | ||
188 | shellMap = new Hashtable(); | ||
189 | } | ||
190 | |||
191 | ShellSession ss = (ShellSession) shellMap.get(absolutePath); | ||
192 | |||
193 | if (ss == null && RemoteHelper.isInitialized(projInfo.getOriginalURI())) { | ||
194 | IHostFile remoteHostFile = RemoteHelper.getRemoteHostFile(projInfo.getConnection(), absolutePath.getPath(), monitor); | ||
195 | ss = new ShellSession(projInfo, remoteHostFile, ProjectInfoHelper.getInitScriptPath(absolutePath)); | ||
196 | } | ||
197 | |||
198 | return ss; | ||
199 | } | ||
200 | |||
201 | public static void putProjInfo(URI location, ProjectInfo pinfo) { | ||
202 | if (projInfoMap == null) { | ||
203 | projInfoMap = new Hashtable(); | ||
204 | } | ||
205 | |||
206 | |||
207 | |||
208 | projInfoMap.put(location, pinfo); | ||
209 | } | ||
210 | |||
211 | /** | ||
212 | * The constructor | ||
213 | */ | ||
214 | public Activator() { | ||
215 | } | ||
216 | |||
217 | /* | ||
218 | * (non-Javadoc) | ||
219 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) | ||
220 | */ | ||
221 | @Override | ||
222 | public void start(BundleContext context) throws Exception { | ||
223 | super.start(context); | ||
224 | plugin = this; | ||
225 | ResourcesPlugin.getWorkspace().addResourceChangeListener( | ||
226 | listener, IResourceChangeEvent.POST_CHANGE); | ||
227 | } | ||
228 | |||
229 | /* | ||
230 | * (non-Javadoc) | ||
231 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) | ||
232 | */ | ||
233 | @Override | ||
234 | public void stop(BundleContext context) throws Exception { | ||
235 | ResourcesPlugin.getWorkspace().removeResourceChangeListener( | ||
236 | listener); | ||
237 | plugin = null; | ||
238 | super.stop(context); | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * Reset a configuration | ||
243 | * @param path | ||
244 | */ | ||
245 | public static void resetBBSession(String path) { | ||
246 | shellMap.remove(path); | ||
247 | bbSessionMap.remove(path); | ||
248 | } | ||
249 | |||
250 | protected void initializeImageRegistry(ImageRegistry reg) { | ||
251 | reg.put(IMAGE_VARIABLE, Activator.getImageDescriptor("icons/variable.gif")); | ||
252 | reg.put(IMAGE_FUNCTION, Activator.getImageDescriptor("icons/function.gif")); | ||
253 | } | ||
254 | } | ||