1 package org.simantics.scl.ui.console;
3 import java.util.ArrayList;
5 import org.eclipse.core.runtime.IProgressMonitor;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.core.runtime.jobs.Job;
9 import org.eclipse.jface.bindings.keys.KeyStroke;
10 import org.eclipse.jface.bindings.keys.ParseException;
11 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
12 import org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.widgets.Composite;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.ServiceReference;
16 import org.osgi.util.tracker.ServiceTracker;
17 import org.simantics.scl.compiler.commands.CommandSession;
18 import org.simantics.scl.compiler.commands.SCLConsoleListener;
19 import org.simantics.scl.compiler.errors.CompilationError;
20 import org.simantics.scl.compiler.errors.Locations;
21 import org.simantics.scl.osgi.SCLOsgi;
22 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
23 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
24 import org.simantics.scl.ui.Activator;
25 import org.simantics.scl.ui.assist.SCLContentProposalProvider;
26 import org.simantics.scl.ui.assist.StyledTextContentAdapter;
28 import gnu.trove.set.hash.THashSet;
31 * An SCL console with input and output area that can be embedded
32 * into any editor or view.
33 * @author Hannu Niemistö
35 public class SCLConsole extends AbstractCommandConsole {
36 public static final String JOB_NAME = "org.simantics.scl.console.job";
37 public static final long TERMINATE_GRACE_PERIOD = 1000L;
39 private THashSet<Job> currentJobs = new THashSet<Job>();
40 private final IdentitySchedulingRule schedulingRule = new IdentitySchedulingRule();
41 private ArrayList<SCLConsoleListener> listeners = new ArrayList<SCLConsoleListener>(2);
42 private boolean consoleIsEmpty = true;
44 SCLReportingHandler handler = new AbstractSCLReportingHandler() {
46 public void print(String text) {
47 appendOutput(text + "\n", null, null);
50 public void printError(String error) {
51 appendOutput(error + "\n", redColor, null);
54 public void printCommand(String command) {
55 appendOutput("> " + command.replace("\n", "\n ") + "\n", greenColor, null);
59 CommandSession session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler);
60 ContentProposalAdapter contentProposalAdapter;
62 public SCLConsole(Composite parent, int style) {
63 this(parent, style, 0);
66 public SCLConsole(Composite parent, int style, int options) {
67 super(parent, style, options);
68 createContentProposalAdapter();
69 addContributedListeners();
72 protected void createContentProposalAdapter() {
76 StyledTextContentAdapter styledTextContentAdapter = new StyledTextContentAdapter();
77 SCLContentProposalProvider contentProvider = new SCLContentProposalProvider(session);
79 contentProposalAdapter = new ContentProposalAdapter(
81 styledTextContentAdapter,
83 KeyStroke.getInstance("Ctrl+Space"),
85 contentProposalAdapter.setAutoActivationDelay(200);
86 } catch (ParseException e) {
87 // No content assist then.
92 protected boolean canExecuteCommand() {
93 return contentProposalAdapter == null || !contentProposalAdapter.isProposalPopupOpen();
97 public ErrorAnnotation[] validate(String command) {
99 return ErrorAnnotation.EMPTY_ARRAY;
101 CompilationError[] errors = session.validate(command);
102 if(errors.length == 0)
103 return ErrorAnnotation.EMPTY_ARRAY;
105 ErrorAnnotation[] annotations = new ErrorAnnotation[errors.length];
106 for(int i=0;i<errors.length;++i) {
107 CompilationError error = errors[i];
108 int begin = Locations.beginOf(error.location);
109 if(begin == Integer.MAX_VALUE)
111 int end = Locations.endOf(error.location);
112 if(end == Integer.MIN_VALUE)
113 end = command.length();
121 annotations[i] = new ErrorAnnotation(begin, end, error.description);
127 private String jobNameFromCommand(String command) {
128 return command.split("\n")[0];
132 public void execute(final String command) {
133 Job job = new Job(jobNameFromCommand(command)) {
135 protected IStatus run(IProgressMonitor monitor) {
137 session.execute(command, handler);
139 synchronized(currentJobs) {
140 currentJobs.remove(this);
141 if(currentJobs.isEmpty())
142 for(SCLConsoleListener listener : listeners)
143 listener.finishedExecution();
146 return Status.OK_STATUS;
150 protected void canceling() {
151 Thread thread = getThread();
156 Thread.sleep(TERMINATE_GRACE_PERIOD);
157 } catch (InterruptedException e) {
162 thread = getThread();
167 job.setRule(schedulingRule);
168 synchronized(currentJobs) {
169 boolean firstJob = currentJobs.isEmpty();
170 currentJobs.add(job);
172 synchronized(listeners) {
173 for(SCLConsoleListener listener : listeners)
174 listener.startedExecution();
181 public CommandSession getSession() {
185 public SCLReportingHandler getHandler() {
189 public void interruptCurrentCommands() {
190 synchronized(currentJobs) {
191 for(Job job : currentJobs)
197 public void addListener(SCLConsoleListener listener) {
198 synchronized (listeners) {
199 listeners.add(listener);
203 public void removeListener(SCLConsoleListener listener) {
204 synchronized (listeners) {
205 listeners.remove(listener);
210 public void appendOutput(String text, Color foreground, Color background) {
211 super.appendOutput(text, foreground, background);
213 consoleIsEmpty = false;
214 synchronized (listeners) {
215 for(SCLConsoleListener listener : listeners)
216 listener.consoleIsNotEmptyAnymore();
222 public void clear() {
224 consoleIsEmpty = true;
227 protected void addContributedListeners() {
228 final BundleContext context = Activator.getInstance().getBundle().getBundleContext();
229 new ServiceTracker<SCLConsoleListener, SCLConsoleListener>(context,
230 SCLConsoleListener.class, null) {
232 public SCLConsoleListener addingService(
233 ServiceReference<SCLConsoleListener> reference) {
234 SCLConsoleListener listener = context.getService(reference);
235 addListener(listener);
240 public void modifiedService(
241 ServiceReference<SCLConsoleListener> reference,
242 SCLConsoleListener service) {
246 public void removedService(
247 ServiceReference<SCLConsoleListener> reference,
248 SCLConsoleListener service) {
249 removeListener(service);