]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / SCLConsole.java
1 package org.simantics.scl.ui.console;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.util.ArrayList;
6
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;
29
30 /**
31  * An SCL console with input and output area that can be embedded
32  * into any editor or view.
33  * @author Hannu Niemistö
34  */
35 public class SCLConsole extends AbstractCommandConsole {
36         public static final String JOB_NAME = "org.simantics.scl.console.job";
37         
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;
43
44         SCLReportingHandler handler = new AbstractSCLReportingHandler() {
45         @Override
46         public void print(String text) {
47             appendOutput(text + "\n", null, null);
48         }
49         @Override
50         public void printError(String error) {
51             appendOutput(error + "\n", redColor, null);
52         }
53         @Override
54         public void printCommand(String command) {
55             appendOutput("> " + command.replace("\n", "\n  ") + "\n", greenColor, null);
56         }
57     };
58
59     CommandSession session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler);
60     ContentProposalAdapter contentProposalAdapter;
61     
62     public SCLConsole(Composite parent, int style) {
63         super(parent, style);
64         
65         StyledTextContentAdapter styledTextContentAdapter = new StyledTextContentAdapter();
66         SCLContentProposalProvider contentProvider = new SCLContentProposalProvider(session);
67         
68         try {
69             contentProposalAdapter = new ContentProposalAdapter(
70                     input, 
71                     styledTextContentAdapter, 
72                     contentProvider, 
73                     KeyStroke.getInstance("Ctrl+Space"), 
74                     null);
75             contentProposalAdapter.setAutoActivationDelay(200);
76         } catch (ParseException e) {
77             // No content assist then.
78         }
79         
80         addContributedListeners();
81     }
82
83     @Override
84     protected boolean canExecuteCommand() {
85         return !contentProposalAdapter.isProposalPopupOpen();
86     }
87     
88     @Override
89     public ErrorAnnotation[] validate(String command) {
90         if(command.isEmpty())
91             return ErrorAnnotation.EMPTY_ARRAY;
92         
93         CompilationError[] errors = session.validate(command);
94         if(errors.length == 0)
95             return ErrorAnnotation.EMPTY_ARRAY;
96         
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)
102                 begin = 0;
103             int end = Locations.endOf(error.location);
104             if(end == Integer.MIN_VALUE)
105                 end = command.length();
106             if(begin == end) {
107                 if(begin > 0)
108                     --begin;
109                 else
110                     ++end;
111             }
112             
113             annotations[i] = new ErrorAnnotation(begin, end, error.description);
114         }
115         
116         return annotations;
117     }
118     
119     private String jobNameFromCommand(String command) {
120         return command.split("\n")[0];
121     }
122
123     @Override
124     public void execute(final String command) {
125         Job job = new Job(jobNameFromCommand(command)) {
126             @Override
127             protected IStatus run(IProgressMonitor monitor) {
128                 try {
129                     synchronized(currentJobs) {
130                         currentJobs.remove(this);
131                         currentThread = Thread.currentThread();
132                     }
133                     session.execute(command, handler);
134                 } finally {
135                     synchronized(currentJobs) {
136                         currentThread = null;
137                         if(currentJobs.isEmpty())
138                             for(SCLConsoleListener listener : listeners)
139                                 listener.finishedExecution();
140                     }
141                 }
142                 return Status.OK_STATUS;
143             }
144         };
145         job.setRule(schedulingRule);
146         synchronized(currentJobs) {
147             boolean firstJob = currentJobs.isEmpty();
148             currentJobs.add(job);
149             if(firstJob) {
150                 synchronized(listeners) {
151                     for(SCLConsoleListener listener : listeners)
152                         listener.startedExecution();
153                 }
154             }
155         }
156         job.schedule();
157     }
158
159     public CommandSession getSession() {
160         return session;
161     }
162     public void interruptCurrentCommands() {
163         synchronized(currentJobs) {
164             for(Job job : currentJobs)
165                 job.cancel();
166             currentJobs.clear();
167             if(currentThread != null)
168                 currentThread.interrupt();
169         }
170     }
171     
172     public void addListener(SCLConsoleListener listener) {
173         synchronized (listeners) {
174             listeners.add(listener);
175         }
176     }
177     
178     public void removeListener(SCLConsoleListener listener) {
179         synchronized (listeners) {
180             listeners.remove(listener);
181         }
182     }
183     
184     @Override
185     public void appendOutput(String text, Color foreground, Color background) {
186         super.appendOutput(text, foreground, background);
187         if(consoleIsEmpty) {
188             consoleIsEmpty = false;
189             synchronized (listeners) {
190                 for(SCLConsoleListener listener : listeners)
191                     listener.consoleIsNotEmptyAnymore();
192             }
193         }
194     }
195     
196     @Override
197     public void clear() {
198         super.clear();
199         consoleIsEmpty = true;
200     }
201
202     private void addContributedListeners() {
203         final BundleContext context = Activator.getInstance().getBundle().getBundleContext();
204         new ServiceTracker<SCLConsoleListener, SCLConsoleListener>(context,
205                 SCLConsoleListener.class, null) {
206                     @Override
207                     public SCLConsoleListener addingService(
208                             ServiceReference<SCLConsoleListener> reference) {
209                         SCLConsoleListener listener = context.getService(reference);
210                         addListener(listener);
211                         return listener;
212                     }
213
214                     @Override
215                     public void modifiedService(
216                             ServiceReference<SCLConsoleListener> reference,
217                             SCLConsoleListener service) {
218                     }
219
220                     @Override
221                     public void removedService(
222                             ServiceReference<SCLConsoleListener> reference,
223                             SCLConsoleListener service) {
224                         removeListener(service);
225                     }
226                 }.open();
227     }
228 }