Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef2005524f9ebea617ba563d89ba2e5bd07b1212 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * 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:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.internal.console;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.m2e.core.core.MavenLogger;
import org.eclipse.m2e.core.internal.Messages;
import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants;
import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleListener;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.ULocale;


/**
 * Maven Console implementation
 * 
 * @author Dmitri Maximovich
 */
public class MavenConsoleImpl extends IOConsole implements IPropertyChangeListener {

  private boolean initialized = false;

  // console is visible in the Console view
  private boolean visible = false;

  private ConsoleDocument consoleDocument;

  // created colors for each line type - must be disposed at shutdown
  private Color commandColor;

  private Color messageColor;

  private Color errorColor;

  // streams for each command type - each stream has its own color
  private IOConsoleOutputStream commandStream;

  private IOConsoleOutputStream messageStream;

  private IOConsoleOutputStream errorStream;
  private static final String TITLE = Messages.MavenConsoleImpl_title;

  private List<IMavenConsoleListener> listeners = new CopyOnWriteArrayList<IMavenConsoleListener>();

  public MavenConsoleImpl(ImageDescriptor imageDescriptor) {
    super(TITLE, imageDescriptor);
    this.setConsoleDocument(new ConsoleDocument());
  }

  protected void init() {
    super.init();

    //  Ensure that initialization occurs in the UI thread
    Display.getDefault().asyncExec(new Runnable() {
      public void run() {
        JFaceResources.getFontRegistry().addListener(MavenConsoleImpl.this);
        initializeConsoleStreams(Display.getDefault());
        dumpConsole();
      }
    });
  }

  /*
   * Initialize three streams of the console. Must be called from the UI thread, so synchronization is unnecessary.
   */
  protected void initializeConsoleStreams(Display display) {
    if(!initialized) {
      setCommandStream(newOutputStream());
      setErrorStream(newOutputStream());
      setMessageStream(newOutputStream());

      // TODO convert this to use themes
      // install colors
      commandColor = new Color(display, new RGB(0, 0, 0));
      messageColor = new Color(display, new RGB(0, 0, 255));
      errorColor = new Color(display, new RGB(255, 0, 0));

      getCommandStream().setColor(commandColor);
      getMessageStream().setColor(messageColor);
      getErrorStream().setColor(errorColor);

      // install font
      setFont(JFaceResources.getFontRegistry().get("pref_console_font")); //$NON-NLS-1$

      initialized = true;
    }
  }

  /**
   * Is always called from main thread, so synchronization not necessary
   */
  protected void dumpConsole() {
    setVisible(true);
    ConsoleDocument.ConsoleLine[] lines = getConsoleDocument().getLines();
    for(int i = 0; i < lines.length; i++ ) {
      ConsoleDocument.ConsoleLine line = lines[i];
      appendLine(line.type, line.line);
    }
    getConsoleDocument().clear();
  }

  private void appendLine(final int type, final String line) {
    show(false);
    //the synchronization here caused a deadlock. since the writes are simply appending to the output stream
    //or the document, just doing it on the main thread to avoid deadlocks and or corruption of the 
    //document or output stream
    Display.getDefault().asyncExec(new Runnable(){
      public void run(){
        if(isVisible()) {
          try {
            switch(type) {
              case ConsoleDocument.COMMAND:
                getCommandStream().write(line);
                getCommandStream().write('\n');
                break;
              case ConsoleDocument.MESSAGE:
                getMessageStream().write(line);
                getMessageStream().write('\n');
                break;
              case ConsoleDocument.ERROR:
                getErrorStream().write(line);
                getErrorStream().write('\n');
                break;
            }
          } catch(IOException ex) {
            MavenLogger.log("Console error", ex);
          }
        } else {
          getConsoleDocument().appendConsoleLine(type, line);
        }
      }
    });
  }

  /**
   * Show the console.
   * 
   * @param showNoMatterWhat ignore preferences if <code>true</code>
   */
  public void show(boolean showNoMatterWhat) {
    if(showNoMatterWhat) {
      if(!isVisible()) {
        showConsole();
      } else {
        ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this);
      }
    }
  }
  
  public void showConsole() {
    boolean exists = false;
    IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
    for(IConsole element : manager.getConsoles()) {
      if(this == element) {
        exists = true;
      }
    }
    if(!exists) {
      manager.addConsoles(new IConsole[] {this});
    }
    manager.showConsoleView(this);
  }
  
  public void closeConsole() {
    IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
    manager.removeConsoles(new IConsole[] {this});
    ConsolePlugin.getDefault().getConsoleManager().addConsoleListener(this.newLifecycle());
  }
  

  public void propertyChange(PropertyChangeEvent event) {
    // font changed
    setFont(JFaceResources.getFontRegistry().get("pref_console_font")); //$NON-NLS-1$
  }

  private void bringConsoleToFront() {
    if(PlatformUI.isWorkbenchRunning()) {
      IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
      if(!isVisible()) {
        manager.addConsoles(new IConsole[] {this});   
      }
      manager.showConsoleView(this);
    }
  }

  // Called when console is removed from the console view
  protected void dispose() {
    // Here we can't call super.dispose() because we actually want the partitioner to remain
    // connected, but we won't show lines until the console is added to the console manager
    // again.
    Display.getDefault().asyncExec(new Runnable(){
      public void run(){
        setVisible(false);
        JFaceResources.getFontRegistry().removeListener(MavenConsoleImpl.this);
      }
    });
  }

  public void shutdown() {
    // Call super dispose because we want the partitioner to be
    // disconnected.
    super.dispose();
    if(commandColor != null) {
      commandColor.dispose();
    }
    if(messageColor != null) {
      messageColor.dispose();
    }
    if(errorColor != null) {
      errorColor.dispose();
    }
  }

  private DateFormat getDateFormat() {
    return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, ULocale.getDefault());
  }

  // MavenConsole

  public void logMessage(String message) {
    if(showConsoleOnOutput()){
      bringConsoleToFront();
    }
    appendLine(ConsoleDocument.MESSAGE, getDateFormat().format(new Date()) + ": " + message); 

    for(IMavenConsoleListener listener : listeners) {
      try {
        listener.loggingMessage(message);
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  }
  
  public void logError(String message) {
    if(showConsoleOnError()){
      bringConsoleToFront();
    }
    appendLine(ConsoleDocument.ERROR, getDateFormat().format(new Date()) + ": " + message); //$NON-NLS-1$

    for(IMavenConsoleListener listener : listeners) {
      try {
        listener.loggingError(message);
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  }

  public boolean showConsoleOnError(){
    return M2EUIPluginActivator.getDefault().getPreferenceStore().getBoolean(MavenPreferenceConstants.P_SHOW_CONSOLE_ON_ERR);
  }
  
  public boolean showConsoleOnOutput(){
    return M2EUIPluginActivator.getDefault().getPreferenceStore().getBoolean(MavenPreferenceConstants.P_SHOW_CONSOLE_ON_OUTPUT);
  }
  public IConsoleListener newLifecycle() {
    return new MavenConsoleLifecycle();
  }

  /**
   * @param commandStream The commandStream to set.
   */
  protected void setCommandStream(IOConsoleOutputStream commandStream) {
    this.commandStream = commandStream;
  }

  /**
   * @return Returns the commandStream.
   */
  protected IOConsoleOutputStream getCommandStream() {
    return commandStream;
  }

  /**
   * @param messageStream The messageStream to set.
   */
  protected void setMessageStream(IOConsoleOutputStream messageStream) {
    this.messageStream = messageStream;
  }

  /**
   * @return Returns the messageStream.
   */
  protected IOConsoleOutputStream getMessageStream() {
    return messageStream;
  }

  /**
   * @param errorStream The errorStream to set.
   */
  protected void setErrorStream(IOConsoleOutputStream errorStream) {
    this.errorStream = errorStream;
  }

  /**
   * @return Returns the errorStream.
   */
  protected IOConsoleOutputStream getErrorStream() {
    return errorStream;
  }

  /**
   * @param visible The visible to set.
   */
  protected void setVisible(boolean visible) {
    this.visible = visible;
  }

  /**
   * @return Returns the visible.
   */
  protected boolean isVisible() {
    return visible;
  }

  /**
   * @param consoleDocument The consoleDocument to set.
   */
  private void setConsoleDocument(ConsoleDocument consoleDocument) {
    this.consoleDocument = consoleDocument;
  }

  /**
   * @return Returns the consoleDocument.
   */
  protected ConsoleDocument getConsoleDocument() {
    return consoleDocument;
  }

  /**
   * Used to notify this console of lifecycle methods <code>init()</code> and <code>dispose()</code>.
   */
  public class MavenConsoleLifecycle implements org.eclipse.ui.console.IConsoleListener {

    public void consolesAdded(IConsole[] consoles) {
      for(int i = 0; i < consoles.length; i++ ) {
        IConsole console = consoles[i];
        if(console == MavenConsoleImpl.this) {
          init();
        }
      }

    }

    public void consolesRemoved(IConsole[] consoles) {
      for(int i = 0; i < consoles.length; i++ ) {
        IConsole console = consoles[i];
        if(console == MavenConsoleImpl.this) {
          ConsolePlugin.getDefault().getConsoleManager().removeConsoleListener(this);
          dispose();
        }
      }
    }

  }

  public void addMavenConsoleListener(IMavenConsoleListener listener) {
    listeners.remove(listener);
    listeners.add(listener);
  }

  public void removeMavenConsoleListener(IMavenConsoleListener listener) {
    listeners.remove(listener);
  }

}

Back to the top