Package: DebugConsoleManager$1

DebugConsoleManager$1

nameinstructionbranchcomplexitylinemethod
run()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui;
23:
24: import java.util.Collections;
25: import java.util.HashMap;
26: import java.util.Map;
27:
28: import org.eclipse.core.resources.ResourcesPlugin;
29: import org.eclipse.core.runtime.CoreException;
30: import org.eclipse.core.runtime.IConfigurationElement;
31: import org.eclipse.core.runtime.IExtensionPoint;
32: import org.eclipse.core.runtime.Platform;
33: import org.eclipse.debug.core.DebugPlugin;
34: import org.eclipse.debug.core.ILaunch;
35: import org.eclipse.debug.core.ILaunchConfiguration;
36: import org.eclipse.debug.core.ILaunchManager;
37: import org.eclipse.debug.core.ILaunchesListener2;
38: import org.eclipse.debug.core.model.IProcess;
39: import org.eclipse.debug.ui.DebugUITools;
40: import org.eclipse.debug.ui.IDebugUIConstants;
41: import org.eclipse.debug.ui.console.ConsoleColorProvider;
42: import org.eclipse.debug.ui.console.IConsoleColorProvider;
43: import org.eclipse.osgi.util.NLS;
44: import org.eclipse.ui.IViewPart;
45: import org.eclipse.ui.IWorkbenchPage;
46: import org.eclipse.ui.IWorkbenchWindow;
47: import org.eclipse.ui.PlatformUI;
48: import org.eclipse.ui.console.ConsolePlugin;
49: import org.eclipse.ui.console.IConsole;
50: import org.eclipse.ui.console.IConsoleConstants;
51: import org.eclipse.ui.console.IConsoleManager;
52: import org.eclipse.ui.console.IConsoleView;
53: import org.eclipse.ui.console.IOConsole;
54: import org.overture.ide.debug.core.IDebugConstants;
55: import org.overture.ide.debug.core.VdmDebugPlugin;
56: import org.overture.ide.debug.core.model.IVdmDebugTarget;
57:
58: import com.ibm.icu.text.MessageFormat;
59:
60: public class DebugConsoleManager implements ILaunchesListener2
61: {
62:
63:         private static DebugConsoleManager instance;
64:
65:         public static synchronized DebugConsoleManager getInstance()
66:         {
67:                 if (instance == null)
68:                 {
69:                         instance = new DebugConsoleManager();
70:                 }
71:                 return instance;
72:         }
73:
74:         private final Map<ILaunch, VdmDebugConsole> launchToConsoleMap = Collections.synchronizedMap(new HashMap<ILaunch, VdmDebugConsole>());
75:
76:         protected boolean acceptLaunch(ILaunch launch)
77:         {
78:                 if (launch == null)
79:                 {
80:                         return false;
81:                 }
82:                 // TODO we allow both debug and run here...
83:                 if (!(ILaunchManager.DEBUG_MODE.equals(launch.getLaunchMode()) || ILaunchManager.RUN_MODE.equals(launch.getLaunchMode())))
84:                 {
85:                         return false;
86:                 }
87:                 return launch.getProcesses().length != 0
88:                                 && true // DLTKDebugLaunchConstants.isDebugConsole(launch)
89:                                 || launch.getDebugTarget() instanceof IVdmDebugTarget
90:                                 && ((IVdmDebugTarget) launch.getDebugTarget()).isRemote();
91:         }
92:
93:         /**
94:          * @since 2.0
95:          */
96:         protected VdmDebugConsole createConsole(ILaunch launch)
97:         {
98:                 final String encoding = selectEncoding(launch);
99:                 final IProcess[] processes = launch.getProcesses();
100:                 final IProcess process = processes.length != 0 ? processes[0] : null;
101:                 final IConsoleColorProvider colorProvider = getColorProvider(process != null ? process.getAttribute(IProcess.ATTR_PROCESS_TYPE)
102:                                 : null);
103:                 final VdmDebugConsole console = new VdmDebugConsole(launch, computeName(launch), null, encoding, colorProvider);
104:
105:                 DebugPlugin.getDefault().addDebugEventListener(console);
106:
107:                 if (process != null)
108:                 {
109:                         console.setAttribute(IDebugUIConstants.ATTR_CONSOLE_PROCESS, process);
110:                         // if (process instanceof IProcess) {
111:                         // console.connect((IProcess) process);
112:                         // }
113:                 }
114:                 final IConsoleManager manager = getConsoleManager();
115:                 manager.addConsoles(new IConsole[] { console });
116:                 console.activate();
117:
118:                 return console;
119:         }
120:
121:         public void displayConsoleView(boolean pin)
122:         {
123:                 IWorkbenchWindow[] activeWorkbenchWindow = PlatformUI.getWorkbench().getWorkbenchWindows();
124:                 if (activeWorkbenchWindow.length > 0)
125:                 {
126:                         IWorkbenchPage activePage = activeWorkbenchWindow[0].getActivePage();
127:                         if (activePage != null)
128:                         {
129:                                 IViewPart part = activePage.findView(IConsoleConstants.ID_CONSOLE_VIEW);
130:                                 if (part != null && part instanceof IConsoleView)
131:                                 {
132:                                         IConsoleView consoleView = (IConsoleView) part;
133:                                         consoleView.setPinned(pin);
134:                                 }
135:                         }
136:                 }
137:         }
138:
139:         private String selectEncoding(ILaunch launch)
140:         {
141:                 String encoding = launch.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING);
142:                 if (encoding != null)
143:                 {
144:                         return encoding;
145:                 }
146:                 final ILaunchConfiguration configuration = launch.getLaunchConfiguration();
147:                 if (configuration != null)
148:                 {
149:                         try
150:                         {
151:                                 return DebugPlugin.getDefault().getLaunchManager().getEncoding(configuration);
152:                         } catch (CoreException e)
153:                         {
154:                                 VdmDebugPlugin.log(e);
155:                         }
156:                 }
157:                 return ResourcesPlugin.getEncoding();
158:         }
159:
160:         protected void destroyConsole(IOConsole console)
161:         {
162:                 getConsoleManager().removeConsoles(new IConsole[] { console });
163:         }
164:
165:         private IConsoleManager getConsoleManager()
166:         {
167:                 return ConsolePlugin.getDefault().getConsoleManager();
168:         }
169:
170:         protected DebugConsoleManager()
171:         {
172:         }
173:
174:         /**
175:          * @since 2.0
176:          */
177:         protected String computeName(ILaunch launch)
178:         {
179:                 final IProcess[] processes = launch.getProcesses();
180:                 String consoleName;
181:                 if (processes.length != 0)
182:                 {
183:                         final IProcess process = processes[0];
184:                         ILaunchConfiguration config = process.getLaunch().getLaunchConfiguration();
185:                         consoleName = process.getAttribute(IProcess.ATTR_PROCESS_LABEL);
186:                         if (consoleName == null)
187:                         {
188:                                 if (config == null || DebugUITools.isPrivate(config))
189:                                 {
190:                                         // No config or PRIVATE
191:                                         consoleName = process.getLabel();
192:                                 } else
193:                                 {
194:                                         consoleName = computeName(config, process);
195:                                 }
196:                         }
197:                 } else
198:                 {
199:                         final ILaunchConfiguration config = launch.getLaunchConfiguration();
200:                         if (config != null)
201:                         {
202:                                 consoleName = computeName(config, null);
203:                         } else
204:                         {
205:                                 consoleName = "";// Util.EMPTY_STRING;
206:                         }
207:                 }
208:                 consoleName = Messages.DebugConsoleManager_debugConsole
209:                                 + " " + consoleName; //$NON-NLS-1$
210:                 if (launch.isTerminated())
211:                 {
212:                         consoleName = NLS.bind(Messages.DebugConsoleManager_terminated, consoleName);
213:                 }
214:                 return consoleName;
215:         }
216:
217:         /**
218:          * @since 2.0
219:          */
220:         protected String computeName(ILaunchConfiguration config, IProcess process)
221:         {
222:                 String type = null;
223:                 try
224:                 {
225:                         type = config.getType().getName();
226:                 } catch (CoreException e)
227:                 {
228:                 }
229:                 StringBuffer buffer = new StringBuffer();
230:                 buffer.append(config.getName());
231:                 if (type != null)
232:                 {
233:                         buffer.append(" ["); //$NON-NLS-1$
234:                         buffer.append(type);
235:                         buffer.append("]"); //$NON-NLS-1$
236:                 }
237:                 if (process != null)
238:                 {
239:                         buffer.append(" "); //$NON-NLS-1$
240:                         buffer.append(process.getLabel());
241:                 }
242:                 return buffer.toString();
243:         }
244:
245:         /**
246:          * @since 2.0
247:          */
248:         public void launchesAdded(ILaunch[] launches)
249:         {
250:                 launchesChanged(launches);
251:         }
252:
253:         /**
254:          * @since 2.0
255:          */
256:         public void launchesChanged(ILaunch[] launches)
257:         {
258:                 for (ILaunch launch : launches)
259:                 {
260:                         if (acceptLaunch(launch))
261:                         {
262:                                 VdmDebugConsole console = launchToConsoleMap.get(launch);
263:                                 if (console == null)
264:                                 {
265:                                         console = createConsole(launch);
266:                                         launchToConsoleMap.put(launch, console);
267:                                 }
268:                                 final IProcess[] processes = launch.getProcesses();
269:                                 if (processes.length != 0 && processes[0] instanceof IProcess)
270:                                 {
271:                                         console.connect(processes[0]);
272:                                 }
273:                                 if (launch.getDebugTarget() instanceof IVdmDebugTarget)
274:                                 {
275:                                         IVdmDebugTarget target = (IVdmDebugTarget) launch.getDebugTarget();
276:                                         if (target != null && target.getStreamProxy() == null)
277:                                         {
278:                                                 boolean interactiveConsoleMode = false;
279:                                                 try
280:                                                 {
281:                                                         interactiveConsoleMode = launch.getLaunchConfiguration().getAttribute(IDebugConstants.VDM_LAUNCH_CONFIG_CONSOLE_ENTRY, false);
282:                                                 } catch (CoreException e)
283:                                                 {
284:                                                         e.printStackTrace();
285:                                                 }
286:                                                 target.setStreamProxy(new VdmStreamProxy(console, interactiveConsoleMode));
287:                                         }
288:                                 }
289:                         }
290:                 }
291:         }
292:
293:         /**
294:          * @since 2.0
295:          */
296:         public void launchesRemoved(ILaunch[] launches)
297:         {
298:                 for (ILaunch launch : launches)
299:                 {
300:                         final VdmDebugConsole console = launchToConsoleMap.get(launch);
301:                         if (console != null)
302:                         {
303:                                 destroyConsole(console);
304:                                 launchToConsoleMap.remove(launch);
305:                         }
306:                 }
307:         }
308:
309:         /**
310:          * @since 2.0
311:          */
312:         public void launchesTerminated(ILaunch[] launches)
313:         {
314:                 for (ILaunch launch : launches)
315:                 {
316:                         final VdmDebugConsole console = launchToConsoleMap.get(launch);
317:                         if (console != null)
318:                         {
319:                                 final String newName = computeName(launch);
320:                                 if (!newName.equals(console.getName()))
321:                                 {
322:                                         final Runnable r = new Runnable()
323:                                         {
324:                                                 public void run()
325:                                                 {
326:                                                         console.setName(newName);
327:                                                 }
328:                                         };
329:                                         VdmDebugPlugin.getStandardDisplay().asyncExec(r);
330:                                 }
331:                         }
332:                 }
333:         }
334:
335:         /**
336:          * Console document content provider extensions, keyed by extension id
337:          */
338:         private Map<String, IConfigurationElement> fColorProviders = null;
339:
340:         /**
341:          * The default color provider. Used if no color provider is contributed for the given process type.
342:          */
343:         private IConsoleColorProvider fDefaultColorProvider;
344:
345:         /**
346:          * Returns a new console document color provider extension for the given process type, or <code>null</code> if none.
347:          *
348:          * @param type
349:          * corresponds to <code>IProcess.ATTR_PROCESS_TYPE</code>
350:          * @return IConsoleColorProvider
351:          */
352:         private IConsoleColorProvider getColorProvider(String type)
353:         {
354:                 if (fColorProviders == null)
355:                 {
356:                         fColorProviders = new HashMap<String, IConfigurationElement>();
357:                         IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.EXTENSION_POINT_CONSOLE_COLOR_PROVIDERS);
358:                         IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
359:                         for (int i = 0; i < elements.length; i++)
360:                         {
361:                                 IConfigurationElement extension = elements[i];
362:                                 fColorProviders.put(extension.getAttribute("processType"), extension); //$NON-NLS-1$
363:                         }
364:                 }
365:                 IConfigurationElement extension = fColorProviders.get(type);
366:                 if (extension != null)
367:                 {
368:                         try
369:                         {
370:                                 Object colorProvider = extension.createExecutableExtension("class"); //$NON-NLS-1$
371:                                 if (colorProvider instanceof IConsoleColorProvider)
372:                                 {
373:                                         return (IConsoleColorProvider) colorProvider;
374:                                 }
375:                                 VdmDebugPlugin.logError(MessageFormat.format("Extension {0} must specify an instanceof IConsoleColorProvider for class attribute.", //$NON-NLS-1$
376:                                                 new Object[] { extension.getDeclaringExtension().getUniqueIdentifier() }));
377:                         } catch (CoreException e)
378:                         {
379:                                 VdmDebugPlugin.log(e);
380:                         }
381:                 }
382:                 // no color provider found of specified type, return default color
383:                 // provider.
384:                 if (fDefaultColorProvider == null)
385:                 {
386:                         fDefaultColorProvider = new ConsoleColorProvider();
387:                 }
388:                 return fDefaultColorProvider;
389:         }
390:
391:
392:
393: }