1 package org.simantics.scl.ui.console;
3 import gnu.trove.set.hash.THashSet;
5 import java.util.ArrayList;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.eclipse.jface.bindings.keys.KeyStroke;
12 import org.eclipse.jface.bindings.keys.ParseException;
13 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.widgets.Composite;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.util.tracker.ServiceTracker;
19 import org.simantics.scl.compiler.commands.CommandSession;
20 import org.simantics.scl.compiler.commands.SCLConsoleListener;
21 import org.simantics.scl.compiler.errors.CompilationError;
22 import org.simantics.scl.compiler.errors.Locations;
23 import org.simantics.scl.osgi.SCLOsgi;
24 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
25 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
26 import org.simantics.scl.ui.Activator;
27 import org.simantics.scl.ui.assist.SCLContentProposalProvider;
28 import org.simantics.scl.ui.assist.StyledTextContentAdapter;
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";
38 private THashSet<Job> currentJobs = new THashSet<Job>();
39 private Thread currentThread;
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) {
65 StyledTextContentAdapter styledTextContentAdapter = new StyledTextContentAdapter();
66 SCLContentProposalProvider contentProvider = new SCLContentProposalProvider(session);
69 contentProposalAdapter = new ContentProposalAdapter(
71 styledTextContentAdapter,
73 KeyStroke.getInstance("Ctrl+Space"),
75 contentProposalAdapter.setAutoActivationDelay(200);
76 } catch (ParseException e) {
77 // No content assist then.
80 addContributedListeners();
84 protected boolean canExecuteCommand() {
85 return !contentProposalAdapter.isProposalPopupOpen();
89 public ErrorAnnotation[] validate(String command) {
91 return ErrorAnnotation.EMPTY_ARRAY;
93 CompilationError[] errors = session.validate(command);
94 if(errors.length == 0)
95 return ErrorAnnotation.EMPTY_ARRAY;
97 ErrorAnnotation[] annotations = new ErrorAnnotation[errors.length];
98 for(int i=0;i<errors.length;++i) {
99 CompilationError error = errors[i];
100 int begin = Locations.beginOf(error.location);
101 if(begin == Integer.MAX_VALUE)
103 int end = Locations.endOf(error.location);
104 if(end == Integer.MIN_VALUE)
105 end = command.length();
113 annotations[i] = new ErrorAnnotation(begin, end, error.description);
119 private String jobNameFromCommand(String command) {
120 return command.split("\n")[0];
124 public void execute(final String command) {
125 Job job = new Job(jobNameFromCommand(command)) {
127 protected IStatus run(IProgressMonitor monitor) {
129 synchronized(currentJobs) {
130 currentJobs.remove(this);
131 currentThread = Thread.currentThread();
133 session.execute(command, handler);
135 synchronized(currentJobs) {
136 currentThread = null;
137 if(currentJobs.isEmpty())
138 for(SCLConsoleListener listener : listeners)
139 listener.finishedExecution();
142 return Status.OK_STATUS;
145 job.setRule(schedulingRule);
146 synchronized(currentJobs) {
147 boolean firstJob = currentJobs.isEmpty();
148 currentJobs.add(job);
150 synchronized(listeners) {
151 for(SCLConsoleListener listener : listeners)
152 listener.startedExecution();
159 public CommandSession getSession() {
162 public void interruptCurrentCommands() {
163 synchronized(currentJobs) {
164 for(Job job : currentJobs)
167 if(currentThread != null)
168 currentThread.interrupt();
172 public void addListener(SCLConsoleListener listener) {
173 synchronized (listeners) {
174 listeners.add(listener);
178 public void removeListener(SCLConsoleListener listener) {
179 synchronized (listeners) {
180 listeners.remove(listener);
185 public void appendOutput(String text, Color foreground, Color background) {
186 super.appendOutput(text, foreground, background);
188 consoleIsEmpty = false;
189 synchronized (listeners) {
190 for(SCLConsoleListener listener : listeners)
191 listener.consoleIsNotEmptyAnymore();
197 public void clear() {
199 consoleIsEmpty = true;
202 private void addContributedListeners() {
203 final BundleContext context = Activator.getInstance().getBundle().getBundleContext();
204 new ServiceTracker<SCLConsoleListener, SCLConsoleListener>(context,
205 SCLConsoleListener.class, null) {
207 public SCLConsoleListener addingService(
208 ServiceReference<SCLConsoleListener> reference) {
209 SCLConsoleListener listener = context.getService(reference);
210 addListener(listener);
215 public void modifiedService(
216 ServiceReference<SCLConsoleListener> reference,
217 SCLConsoleListener service) {
221 public void removedService(
222 ServiceReference<SCLConsoleListener> reference,
223 SCLConsoleListener service) {
224 removeListener(service);