summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/AbstractBitbakeCommandAction.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/AbstractBitbakeCommandAction.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/AbstractBitbakeCommandAction.java199
1 files changed, 199 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/AbstractBitbakeCommandAction.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/AbstractBitbakeCommandAction.java
new file mode 100644
index 0000000..41d5c73
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/AbstractBitbakeCommandAction.java
@@ -0,0 +1,199 @@
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.actions;
12
13import java.io.IOException;
14
15import org.eclipse.core.resources.IFile;
16import org.eclipse.core.resources.IProject;
17import org.eclipse.core.runtime.CoreException;
18import org.eclipse.core.runtime.IProgressMonitor;
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.Status;
21import org.eclipse.core.runtime.jobs.Job;
22import org.eclipse.jface.action.IAction;
23import org.eclipse.jface.preference.JFacePreferences;
24import org.eclipse.jface.resource.JFaceResources;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.IStructuredSelection;
27import org.eclipse.swt.graphics.Color;
28import org.eclipse.ui.IWorkbenchWindow;
29import org.eclipse.ui.IWorkbenchWindowActionDelegate;
30import org.eclipse.ui.console.MessageConsole;
31import org.eclipse.ui.console.MessageConsoleStream;
32
33import org.yocto.bc.bitbake.BBLanguageHelper;
34import org.yocto.bc.bitbake.BBSession;
35import org.yocto.bc.bitbake.ICommandResponseHandler;
36import org.yocto.bc.ui.Activator;
37import org.yocto.bc.ui.builder.BitbakeCommanderNature;
38
39public abstract class AbstractBitbakeCommandAction implements IWorkbenchWindowActionDelegate {
40
41 private class CommandJob extends Job {
42
43 public CommandJob() {
44 super(getJobTitle());
45 }
46
47 @Override
48 protected IStatus run(IProgressMonitor monitor) {
49 String cmds[] = getCommands();
50 return execCommands(cmds, monitor);
51 }
52
53 }
54 protected IAction action;
55 protected IFile recipe;
56 protected BBSession bbs;
57
58 private Color commandColor, responseColor, errorColor;
59 private boolean errorOccurred = false;
60
61 public AbstractBitbakeCommandAction() {
62 commandColor = JFaceResources.getColorRegistry().get(JFacePreferences.ACTIVE_HYPERLINK_COLOR);
63 responseColor = JFaceResources.getColorRegistry().get(JFacePreferences.HYPERLINK_COLOR);
64 errorColor = JFaceResources.getColorRegistry().get(JFacePreferences.ERROR_COLOR);
65 }
66
67 private void checkEnabled(IFile file) {
68 try {
69 if (file.getFileExtension() == null || !file.getFileExtension().equals(BBLanguageHelper.BITBAKE_RECIPE_FILE_EXTENSION)) {
70 action.setEnabled(false);
71 return;
72 }
73
74 IProject project = file.getProject();
75 if (!(project.hasNature(BitbakeCommanderNature.NATURE_ID))) {
76 action.setEnabled(false);
77 return;
78 }
79
80 bbs = Activator.getBBSession(project.getLocationURI().getPath());
81
82 if (bbs != null) {
83 recipe = file;
84 action.setEnabled(true);
85 }
86
87 } catch (CoreException e) {
88 action.setEnabled(false);
89 e.printStackTrace();
90 } catch (Exception e) {
91 action.setEnabled(false);
92 e.printStackTrace();
93 }
94 }
95
96 public void dispose() {
97 }
98
99 /**
100 * Execute array of commands with bitbake and put output in console.
101 *
102 * @param cmds
103 * @param monitor
104 * @return
105 */
106 protected IStatus execCommands(String[] cmds, final IProgressMonitor monitor) {
107 MessageConsole mc = bbs.getConsole();
108 final MessageConsoleStream cmd = mc.newMessageStream();
109 cmd.setColor(commandColor);
110 final MessageConsoleStream out = mc.newMessageStream();
111 final MessageConsoleStream err = mc.newMessageStream();
112 err.setColor(errorColor);
113
114 try {
115 for (int i = 0; i < cmds.length; ++i) {
116 cmd.println(cmds[i]);
117 monitor.subTask(cmds[i]);
118 bbs.getShell().execute(cmds[i], new ICommandResponseHandler() {
119
120 public void response(String line, boolean isError) {
121 if (monitor.isCanceled()) {
122 cmd.println("Interrupting process by user request.");
123 bbs.getShell().interrupt();
124 }
125
126 if (isError) {
127 err.println(line);
128 errorOccurred();
129 } else if (line.startsWith("ERROR:")) {
130 err.println(line);
131 } else {
132 out.println(line);
133 }
134 }
135 });
136 }
137 } catch (IOException e) {
138 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
139 } finally {
140 try {
141 if (errorOccurred) {
142 cmd.println("At least one error occured while executing this command. Check output for more details.");
143 }
144 cmd.close();
145 out.close();
146 err.close();
147 } catch (IOException e) {
148 e.printStackTrace();
149 }
150 }
151
152 return Status.OK_STATUS;
153 }
154
155 protected void errorOccurred() {
156 errorOccurred = true;
157 }
158
159 /**
160 * Return the command to be executed.
161 *
162 * @return
163 */
164 public abstract String[] getCommands();
165
166 public Job getJob() {
167 return new CommandJob();
168 }
169
170 /**
171 * Return the title of the job.
172 *
173 * @return
174 */
175 public abstract String getJobTitle();
176
177 public void init(IWorkbenchWindow window) {
178 }
179
180 public void run(IAction action) {
181 Job job = getJob();
182 job.schedule();
183 }
184
185 public void selectionChanged(IAction action, ISelection selection) {
186 this.action = action;
187 if (selection instanceof IStructuredSelection) {
188 Object sel = ((IStructuredSelection) selection).getFirstElement();
189
190 if (sel instanceof IFile) {
191 checkEnabled((IFile) sel);
192 return;
193 }
194 }
195
196 action.setEnabled(false);
197 }
198
199} \ No newline at end of file