Skip to main content
summaryrefslogtreecommitdiffstats
blob: d57e7c9b4fdde012bb8ca954089f3ebcba15dc22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.plugin.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.framework.jdk.core.util.IConsoleInputListener;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.plugin.internal.OseePluginUiActivator;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.swt.SWT;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;

/**
 * Creates an Eclipse Console instance and allows writing output, errors and prompt messages There should be at one
 * instance of a ConsoleWriter per plugin that needs to output messages and errors. *
 * 
 * @author Donald G. Dunne
 */
public class OseeConsole {
   private IOConsoleOutputStream streamOut = null;

   private IOConsoleOutputStream streamErr = null;

   private IOConsoleOutputStream streamPrompt = null;

   private final IOConsole console;

   private final HandleInput inputHandler;

   private final boolean time;

   private final Thread thread;

   public OseeConsole(String title) {
      this(title, true);
   }

   private OseeConsole(String title, boolean time) {
      console = new IOConsole(title, null);
      this.time = time;
      this.inputHandler = new HandleInput();

      thread = new Thread(inputHandler);
      thread.setName("Osee console input handler");
      ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {console});
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            streamOut = console.newOutputStream();// newMessageStream();
            streamOut.setColor(Displays.getSystemColor(SWT.COLOR_BLACK));
            streamOut.setActivateOnWrite(false);
            streamErr = console.newOutputStream();
            streamErr.setColor(Displays.getSystemColor(SWT.COLOR_RED));
            streamErr.setActivateOnWrite(false);
            streamPrompt = console.newOutputStream();
            streamPrompt.setColor(Displays.getSystemColor(SWT.COLOR_BLUE));
            streamPrompt.setActivateOnWrite(false);
         }
      });
      thread.start();
   }

   public PrintStream getPrintStream() {
      return new PrintStream(streamOut);
   }

   public void shutdown() {
      thread.interrupt();
      try {
         thread.join(5000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      } finally {
         ConsolePlugin.getDefault().getConsoleManager().removeConsoles(new IConsole[] {console});
      }
   }

   public static final int CONSOLE_ERROR = 0;

   public static final int CONSOLE_OUT = 1;

   public static final int CONSOLE_PROMPT = 2;

   /**
    * Writes string to console without popping console forward
    */
   public void write(String str) {
      write(str, false);
   }

   /**
    * Writes string to console without popping console forward
    */
   public void writeError(String str) {
      write(str, CONSOLE_ERROR, true);
   }

   /**
    * Writes string to console
    * 
    * @param popup bring console window forward
    */
   public void write(String str, boolean popup) {
      write(str, CONSOLE_OUT, popup);
   }

   /**
    * Write string to console
    * 
    * @param type CONSOLE_ERROR, CONSOLE_OUT, CONSOLE_PROMPT
    */
   public void write(String str, int type) {
      write(str, type, false);
   }

   /**
    * Write string to console
    * 
    * @param type CONSOLE_ERROR, CONSOLE_OUT, CONSOLE_PROMPT
    * @param popup bring console window forward
    */
   public void write(String str, int type, boolean popup) {
      String time = "";
      if (this.time) {
         Calendar cal = Calendar.getInstance();
         cal.setTime(new Date());

         if (cal.get(Calendar.HOUR) == 0) {
            time = "12";
         } else {
            time = "" + cal.get(Calendar.HOUR);
         }
         time = Lib.padLeading(time, '0', 2);
         String minute = "" + cal.get(Calendar.MINUTE);
         minute = Lib.padLeading(minute, '0', 2);
         time += ":" + minute + " => ";
      }
      try {
         sendToStreams(type, time);
         if (str.length() > 100000) {
            int i = 0;

            while (i < str.length()) {
               int endIndex = i + 100000;
               endIndex = endIndex > str.length() ? str.length() : endIndex;
               String chunk = str.substring(i, endIndex);
               sendToStreams(type, chunk);
               i = endIndex;
            }
         } else {
            sendToStreams(type, str);
         }

         sendToStreams(type, "\n");
         if (popup) {
            popup();
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }

   public void prompt(String str) throws IOException {
      sendToStreams(CONSOLE_PROMPT, str);
   }

   private void sendToStreams(int type, String str) throws IOException {
      if (type == CONSOLE_ERROR && streamErr != null) {
         streamErr.write(str);
      }
      if (type == CONSOLE_PROMPT && streamPrompt != null) {
         streamPrompt.write(str);
      }
      if (type == CONSOLE_OUT && streamOut != null) {
         streamOut.write(str);
      }
   }

   public void popup() {
      ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
   }

   public void addInputListener(IConsoleInputListener listener) {
      inputHandler.addListener(listener);
   }

   public void removeInputListener(IConsoleInputListener listener) {
      inputHandler.removeListener(listener);
   }

   private class HandleInput implements Runnable {

      private final CopyOnWriteArrayList<IConsoleInputListener> listeners;

      public HandleInput() {
         listeners = new CopyOnWriteArrayList<IConsoleInputListener>();
      }

      public void addListener(IConsoleInputListener listener) {
         listeners.add(listener);
      }

      public void removeListener(IConsoleInputListener listener) {
         listeners.remove(listener);
      }

      @Override
      public void run() {
         BufferedReader input = new BufferedReader(new InputStreamReader(console.getInputStream()));
         try {
            String line = null;
            while ((line = input.readLine()) != null) {
               for (IConsoleInputListener listener : listeners) {
                  listener.lineRead(line);
               }
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
         OseeLog.log(OseePluginUiActivator.class, Level.INFO, "done with the handling of input");
      }

   }
}

Back to the top