Package: VdmStreamProxy$1

VdmStreamProxy$1

nameinstructionbranchcomplexitylinemethod
run()
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 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.io.IOException;
25: import java.io.InputStream;
26: import java.io.OutputStream;
27:
28: import org.eclipse.debug.internal.ui.DebugUIPlugin;
29: import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants;
30: import org.eclipse.jface.preference.IPreferenceStore;
31: import org.eclipse.jface.preference.PreferenceConverter;
32: import org.eclipse.swt.widgets.Display;
33: import org.eclipse.ui.PlatformUI;
34: import org.eclipse.ui.WorkbenchEncoding;
35: import org.eclipse.ui.console.IOConsole;
36: import org.eclipse.ui.console.IOConsoleInputStream;
37: import org.eclipse.ui.console.IOConsoleOutputStream;
38: import org.overture.ide.debug.core.VdmDebugPlugin;
39: import org.overture.ide.debug.core.model.internal.IVdmStreamProxy;
40:
41: @SuppressWarnings("restriction")
42: public class VdmStreamProxy implements IVdmStreamProxy
43: {
44:         private IOConsoleInputStream input;
45:         private IOConsoleOutputStream stdOut;
46:         private IOConsoleOutputStream stdErr;
47:
48:         private boolean closed = false;
49:         private boolean interactiveMode = false;
50:
51:         public VdmStreamProxy(IOConsole console, boolean interactiveMode)
52:         {
53:                 input = console.getInputStream();
54:                 stdOut = console.newOutputStream();
55:                 stdErr = console.newOutputStream();
56:                 this.interactiveMode = interactiveMode;
57:
58:                 // TODO is there a better way to access these internal preferences??
59:                 final IPreferenceStore debugUIStore = DebugUIPlugin.getDefault().getPreferenceStore();
60:                 stdOut.setActivateOnWrite(debugUIStore.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT));
61:                 stdErr.setActivateOnWrite(debugUIStore.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR));
62:
63:                 getDisplay().asyncExec(new Runnable()
64:                 {
65:                         public void run()
66:                         {
67:                                 final VdmDebugPlugin colors = VdmDebugPlugin.getDefault();
68:                                 stdOut.setColor(colors.getColor(PreferenceConverter.getColor(debugUIStore, IDebugPreferenceConstants.CONSOLE_SYS_OUT_COLOR)));
69:                                 stdErr.setColor(colors.getColor(PreferenceConverter.getColor(debugUIStore, IDebugPreferenceConstants.CONSOLE_SYS_ERR_COLOR)));
70:                         }
71:                 });
72:         }
73:
74:         private Display getDisplay()
75:         {
76:                 // If we are in the UI Thread use that
77:                 if (Display.getCurrent() != null)
78:                 {
79:                         return Display.getCurrent();
80:                 }
81:
82:                 if (PlatformUI.isWorkbenchRunning())
83:                 {
84:                         return PlatformUI.getWorkbench().getDisplay();
85:                 }
86:
87:                 return Display.getDefault();
88:         }
89:
90:         public OutputStream getStderr()
91:         {
92:                 return stdErr;
93:         }
94:
95:         public OutputStream getStdout()
96:         {
97:                 return stdOut;
98:         }
99:
100:         public InputStream getStdin()
101:         {
102:                 return input;
103:         }
104:
105:         public synchronized void close()
106:         {
107:                 if (!closed)
108:                 {
109:                         try
110:                         {
111:                                 stdOut.close();
112:                                 stdErr.close();
113:                                 input.close();
114:                                 closed = true;
115:                         } catch (IOException e)
116:                         {
117:                                 if (VdmDebugPlugin.DEBUG)
118:                                 {
119:                                         e.printStackTrace();
120:                                 }
121:                         }
122:                 }
123:         }
124:
125:         private boolean needsEncoding = false;
126:         private String encoding = null;
127:
128:         public String getEncoding()
129:         {
130:                 return encoding;
131:         }
132:
133:         public void setEncoding(String encoding)
134:         {
135:                 this.encoding = encoding;
136:                 needsEncoding = encoding != null
137:                                 && !encoding.equals(WorkbenchEncoding.getWorkbenchDefaultEncoding());
138:         }
139:
140:         public void writeStdout(String value)
141:         {
142:                 if (interactiveMode)
143:                 {
144:                         if (value != null && value.startsWith("= "))
145:                         {
146:                                 try
147:                                 {
148:                                         value = value.substring(value.indexOf("= ") + 2);
149:                                 } catch (Exception e)
150:                                 {
151:                                         // Don't care
152:                                 }
153:                         }
154:                 }
155:                 write(stdOut, value);
156:
157:         }
158:
159:         public void writeStderr(String value)
160:         {
161:                 write(stdErr, value);
162:         }
163:
164:         private void write(IOConsoleOutputStream stream, String value)
165:         {
166:                 try
167:                 {
168:                         if (needsEncoding)
169:                         {
170:                                 stream.write(value.getBytes(encoding));
171:                         } else
172:                         {
173:                                 stream.write(value);
174:                         }
175:                         stream.flush();
176:                 } catch (IOException e)
177:                 {
178:                         if (VdmDebugPlugin.DEBUG)
179:                         {
180:                                 e.printStackTrace();
181:                         }
182:                         VdmDebugPlugin.log(e);
183:                 }
184:         }
185:
186:         @Override
187:         public void printPrompt() {
188:                 if (interactiveMode)
189:                         write(stdOut,"> ");
190:                 
191:         }
192:
193: }