Package: VdmDebugPlugin$DbgpServicePreferenceUpdater

VdmDebugPlugin$DbgpServicePreferenceUpdater

nameinstructionbranchcomplexitylinemethod
VdmDebugPlugin.DbgpServicePreferenceUpdater(VdmDebugPlugin)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
propertyChange(Preferences.PropertyChangeEvent)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 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.core;
23:
24: import java.util.HashMap;
25: import java.util.Map;
26:
27: import org.eclipse.core.runtime.IStatus;
28: import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
29: import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
30: import org.eclipse.core.runtime.Status;
31: import org.eclipse.debug.core.DebugException;
32: import org.eclipse.debug.core.DebugPlugin;
33: import org.eclipse.debug.core.ILaunchManager;
34: import org.eclipse.debug.core.model.IDebugTarget;
35: import org.eclipse.jface.preference.IPreferenceStore;
36: import org.eclipse.swt.graphics.Color;
37: import org.eclipse.swt.graphics.RGB;
38: import org.eclipse.swt.widgets.Display;
39: import org.eclipse.swt.widgets.Shell;
40: import org.eclipse.ui.IWorkbenchPage;
41: import org.eclipse.ui.IWorkbenchWindow;
42: import org.eclipse.ui.plugin.AbstractUIPlugin;
43: import org.osgi.framework.BundleContext;
44: import org.overture.ide.debug.core.model.ISourceOffsetLookup;
45: import org.overture.ide.debug.core.model.internal.DbgpService;
46: import org.overture.ide.debug.core.model.internal.VdmDebugTarget;
47: import org.overture.ide.debug.ui.DebugConsoleManager;
48: import org.overture.ide.debug.ui.VdmEvaluationContextManager;
49: import org.overture.ide.debug.ui.log.VdmDebugLogManager;
50:
51: @SuppressWarnings("deprecation")
52: public class VdmDebugPlugin extends AbstractUIPlugin
53: {
54:
55:         private static VdmDebugPlugin plugin;
56:         protected Map<RGB, Color> fColorTable = new HashMap<RGB, Color>(10);
57:         public static final int INTERNAL_ERROR = 120;
58:
59:         public static final String PLUGIN_ID = "org.overture.ide.debug.core";
60:
61:         public static boolean DEBUG = true;
62:
63:         //        private static final String LOCALHOST = "127.0.0.1"; //$NON-NLS-1$
64:
65:         public static IWorkbenchPage getActivePage()
66:         {
67:                 IWorkbenchWindow w = getActiveWorkbenchWindow();
68:                 if (w != null)
69:                 {
70:                         return w.getActivePage();
71:                 }
72:                 return null;
73:         }
74:
75:         public void start(BundleContext context) throws Exception
76:         {
77:                 super.start(context);
78:                 ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
79:                 launchManager.addLaunchListener(DebugConsoleManager.getInstance());
80:                 launchManager.addLaunchListener(VdmDebugLogManager.getInstance());
81:                 plugin = this;
82:
83:                 // TODO
84:                 VdmEvaluationContextManager.startup();
85:                 // HotCodeReplaceManager.getDefault().startup();
86:         }
87:
88:         public void stop(BundleContext context) throws Exception
89:         {
90:                 plugin = null;
91:                 // HotCodeReplaceManager.getDefault().shutdown();
92:                 super.stop(context);
93:                 if (dbgpService != null)
94:                 {
95:                         dbgpService.shutdown();
96:                 }
97:
98:                 ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
99:
100:                 IDebugTarget[] targets = launchManager.getDebugTargets();
101:                 for (int i = 0; i < targets.length; i++)
102:                 {
103:                         IDebugTarget target = targets[i];
104:                         if (target instanceof VdmDebugTarget)
105:                         {
106:                                 ((VdmDebugTarget) target).shutdown();
107:                         }
108:                 }
109:         }
110:
111:         private DbgpService dbgpService;
112:
113:         public synchronized IDbgpService getDbgpService()
114:         {
115:
116:                 if (dbgpService == null)
117:                 {
118:                         dbgpService = new DbgpService(getPreferencePort());
119:                         getPluginPreferences().addPropertyChangeListener(new DbgpServicePreferenceUpdater());
120:
121:                 }
122:
123:                 return dbgpService;
124:
125:         }
126:
127:         /**
128:          * Inefficient use getDbgpService
129:          *
130:          * @param freePort
131:          * @return
132:          */
133:         public synchronized IDbgpService getDbgpService(int freePort)
134:         {
135:
136:                 // if (dbgpService == null) {
137:                 dbgpService = new DbgpService(freePort);
138:                 getPluginPreferences().addPropertyChangeListener(new DbgpServicePreferenceUpdater());
139:
140:                 // }
141:
142:                 return dbgpService;
143:
144:         }
145:
146:         private class DbgpServicePreferenceUpdater implements
147:
148:         IPropertyChangeListener
149:         {
150:
151:                 public void propertyChange(PropertyChangeEvent event)
152:                 {
153:
154:                         final String property = event.getProperty();
155:
156:•                        if (IDebugPreferenceConstants.PREF_DBGP_PORT.equals(property))
157:                         {
158:
159:•                                if (dbgpService != null)
160:                                 {
161:
162:                                         dbgpService.restart(getPreferencePort());
163:
164:                                 }
165:
166:                         }
167:
168:                 }
169:
170:         }
171:
172:         // Logging
173:
174:         public static void log(Throwable t)
175:         {
176:                 Throwable top = t;
177:                 if (t instanceof DebugException)
178:                 {
179:                         Throwable throwable = ((DebugException) t).getStatus().getException();
180:                         if (throwable != null)
181:                         {
182:                                 top = throwable;
183:                         }
184:                 }
185:                 log(new Status(IStatus.ERROR, PLUGIN_ID, INTERNAL_ERROR, "internalErrorLoggedFromVdmDebugPlugin"
186:                                 + top.getMessage(), top));
187:         }
188:
189:         public static void log(IStatus status)
190:         {
191:                 getDefault().getLog().log(status);
192:         }
193:
194:         public static void logWarning(String message)
195:         {
196:                 logWarning(message, null);
197:         }
198:
199:         public static void logWarning(String message, Throwable t)
200:         {
201:                 log(new Status(IStatus.WARNING, PLUGIN_ID, INTERNAL_ERROR, message, t));
202:         }
203:
204:         public static void logError(String message)
205:         {
206:                 logError(message, null);
207:         }
208:
209:         public static void logError(String message, Throwable t)
210:         {
211:                 Throwable top = t;
212:                 if (t instanceof DebugException)
213:                 {
214:                         Throwable throwable = ((DebugException) t).getStatus().getException();
215:                         if (throwable != null)
216:                         {
217:                                 top = throwable;
218:                         }
219:                 }
220:                 log(new Status(IStatus.ERROR, PLUGIN_ID, INTERNAL_ERROR, message, top));
221:         }
222:
223:         private int getPreferencePort()
224:         {
225:
226:                 int p = getPreferenceStore().getInt(IDebugPreferenceConstants.PREF_DBGP_PORT);
227:                 if (p == 0)
228:                 {
229:                         initializeDefaultDebugPreferences(getPreferenceStore());
230:                 }
231:                 return getPreferenceStore().getInt(IDebugPreferenceConstants.PREF_DBGP_PORT);
232:         }
233:
234:         public int getConnectionTimeout()
235:         {
236:                 int t = getDefault().getPluginPreferences().getInt(IDebugPreferenceConstants.PREF_DBGP_CONNECTION_TIMEOUT);
237:                 if (t == 0)
238:                 {
239:                         initializeDefaultDebugPreferences(getPreferenceStore());
240:                 }
241:                 return getDefault().getPluginPreferences().getInt(IDebugPreferenceConstants.PREF_DBGP_CONNECTION_TIMEOUT);
242:         }
243:
244:         private static ISourceOffsetLookup sourceOffsetLookup = null;
245:
246:         public static ISourceOffsetLookup getSourceOffsetLookup()
247:         {
248:                 return sourceOffsetLookup;
249:         }
250:
251:         public static void setSourceOffsetRetriever(ISourceOffsetLookup offsetLookup)
252:         {
253:                 sourceOffsetLookup = offsetLookup;
254:         }
255:
256:         private boolean fTrace = false;
257:
258:         public boolean isTraceMode()
259:         {
260:                 return fTrace;
261:         }
262:
263:         public static void logTraceMessage(String message)
264:         {
265:                 if (getDefault().isTraceMode())
266:                 {
267:                         IStatus s = new Status(IStatus.WARNING, IDebugConstants.PLUGIN_ID, INTERNAL_ERROR, message, null);
268:                         getDefault().getLog().log(s);
269:                 }
270:         }
271:
272:         public static VdmDebugPlugin getDefault()
273:         {
274:                 return plugin;
275:         }
276:
277:         /**
278:          * Returns the active workbench window
279:          *
280:          * @return the active workbench window
281:          */
282:         public static IWorkbenchWindow getActiveWorkbenchWindow()
283:         {
284:                 return getDefault().getWorkbench().getActiveWorkbenchWindow();
285:         }
286:
287:         /**
288:          * Returns the active workbench shell or <code>null</code> if none
289:          *
290:          * @return the active workbench shell or <code>null</code> if none
291:          */
292:         public static Shell getActiveWorkbenchShell()
293:         {
294:                 IWorkbenchWindow window = getActiveWorkbenchWindow();
295:                 if (window != null)
296:                 {
297:                         return window.getShell();
298:                 }
299:                 return null;
300:         }
301:
302:         public static void logWarning(Exception e)
303:         {
304:                 log(e);
305:
306:         }
307:
308:         public Color getColor(RGB rgb)
309:         {
310:                 Color color = fColorTable.get(rgb);
311:                 if (color == null)
312:                 {
313:                         color = new Color(Display.getCurrent(), rgb);
314:                         fColorTable.put(rgb, color);
315:                 }
316:                 return color;
317:         }
318:
319:         /**
320:          * Returns the standard display to be used. The method first checks, if the thread calling this method has an
321:          * associated display. If so, this display is returned. Otherwise the method returns the default display.
322:          */
323:         public static Display getStandardDisplay()
324:         {
325:                 Display display;
326:                 display = Display.getCurrent();
327:                 if (display == null)
328:                 {
329:                         display = Display.getDefault();
330:                 }
331:                 return display;
332:         }
333:
334:         /**
335:          * Initializes a preference store with default preference values for this plug-in.
336:          */
337:         @Override
338:         protected void initializeDefaultPluginPreferences()
339:         {
340:                 IPreferenceStore store = getPreferenceStore();
341:                 initializeDefaultDebugPreferences(store);
342:         }
343:
344:         public static void initializeDefaultDebugPreferences(IPreferenceStore store)
345:         {
346:                 store.setDefault(IDebugPreferenceConstants.PREF_DBGP_PORT, IDebugPreferenceConstants.DBGP_AVAILABLE_PORT);
347:                 store.setDefault(IDebugPreferenceConstants.PREF_DBGP_CONNECTION_TIMEOUT, IDebugPreferenceConstants.DBGP_DEFAULT_CONNECTION_TIMEOUT);
348:                 store.setDefault(IDebugPreferenceConstants.PREF_DBGP_ENABLE_EXPERIMENTAL_MODELCHECKER, false);
349:         }
350:
351: }