blob: ed71f46713af07b37590acb0ad11d753ea6b7560 [file] [log] [blame]
package org.eclipse.ui.externaltools.internal.ui;
/**********************************************************************
Copyright (c) 2002 IBM Corp. and others.
All rights reserved.   This program and the accompanying materials
are made available under the terms of the Common Public License v0.5
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v05.html
 
Contributors:
**********************************************************************/
import java.util.*;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.*;
import org.eclipse.jface.util.*;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.externaltools.internal.core.*;
/**
* Holds onto messages generated by the execution on an
* external tool.
*/
public class LogConsoleDocument {
// class variables that handle the colors and the font
private static Color ERROR_COLOR;
private static Color WARN_COLOR;
private static Color INFO_COLOR;
private static Color VERBOSE_COLOR;
private static Color DEBUG_COLOR;
/*package*/ static Font ANT_FONT;
public static final int MSG_ERR = 0;
public static final int MSG_WARN = 10;
public static final int MSG_INFO = 20;
public static final int MSG_VERBOSE = 30;
public static final int MSG_DEBUG = 40;
private static final LogConsoleDocument instance = new LogConsoleDocument();
private LogPropertyChangeListener changeListener;
/*package*/ ArrayList views = new ArrayList();
private Document document;
private ArrayList styleRanges;
// Structure to store the textwidget index information
private OutputStructureElement root = null;
private OutputStructureElement currentElement = null;
private LogConsoleDocument() {
changeListener = new LogPropertyChangeListener();
document = new Document();
styleRanges = new ArrayList(5);
initializeOutputStructure();
}
public void append(String message, int priority) {
for (int i=0; i < views.size(); i++) {
((LogConsoleView)views.get(i)).append(message, priority);
}
}
private void addRangeStyle(int start, int length, Color color) {
if (styleRanges.size() != 0) {
StyleRange lastStyle = (StyleRange) styleRanges.get(styleRanges.size()-1);
if (color.equals(lastStyle.foreground))
lastStyle.length += length;
else
styleRanges.add(new StyleRange(start, length, color, null));
} else
styleRanges.add(new StyleRange(start, length, color, null));
StyleRange[] styleArray = (StyleRange[]) styleRanges.toArray(new StyleRange[styleRanges.size()]);
for (int i = 0; i < views.size(); i++) {
TextViewer tv = ((LogConsoleView)views.get(i)).getTextViewer();
if (tv != null)
tv.getTextWidget().setStyleRanges(styleArray);
}
}
public void clearOutput() {
document.set("");
styleRanges.clear();
// the tree can be null if #createPartControl has not called yet,
// i.e. if the console exists but has never been shown so far
initializeOutputStructure();
refreshTree();
}
public void refreshTree() {
for(int i=0; i<views.size(); i++) {
((LogConsoleView)views.get(i)).refreshTree();
}
}
public Display getDisplay() {
if (!hasViews())
return null;
return ((LogConsoleView)views.get(0)).getSite().getShell().getDisplay();
}
/*package*/ Document getDocument() {
return document;
}
/*package*/ ArrayList getStyleRanges() {
return styleRanges;
}
public ArrayList getViews() {
return views;
}
/*package*/ OutputStructureElement getRoot() {
return root;
}
public boolean hasViews() {
return (views.size() > 0);
}
public void initializeOutputStructure() {
// root is the first element of the structure: it is a fake so it doesn't need a real name
root = new OutputStructureElement("-- root --"); // $NON-NLS-1$
currentElement = new OutputStructureElement(ToolMessages.getString("LogConsoleDocument.externalTool"), root, 0); // $NON-NLS-1$
for (int i=0; i < views.size(); i++) {
LogConsoleView view = (LogConsoleView)views.get(i);
if (view.getTreeViewer() != null)
view.initializeTreeInput();
}
}
public void registerView(LogConsoleView view) {
if (!hasViews()) {
// first time there is an instance of this class: intantiate the colors and register the listener
ERROR_COLOR = new Color(null, PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(),IPreferenceConstants.CONSOLE_ERROR_RGB));
WARN_COLOR = new Color(null, PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(),IPreferenceConstants.CONSOLE_WARNING_RGB));
INFO_COLOR = new Color(null, PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(),IPreferenceConstants.CONSOLE_INFO_RGB));
VERBOSE_COLOR = new Color(null, PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(),IPreferenceConstants.CONSOLE_VERBOSE_RGB));
DEBUG_COLOR = new Color(null, PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(),IPreferenceConstants.CONSOLE_DEBUG_RGB));
ANT_FONT = new Font(null, PreferenceConverter.getFontData(ExternalToolsPlugin.getDefault().getPreferenceStore(),IPreferenceConstants.CONSOLE_FONT));
ExternalToolsPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(changeListener);
}
views.add(view);
}
public void unregisterView(LogConsoleView view) {
views.remove(view);
if (! hasViews()) {
// all the consoles are diposed: we can dispose the colors as well and remove the property listener
ERROR_COLOR.dispose();
WARN_COLOR.dispose();
INFO_COLOR.dispose();
VERBOSE_COLOR.dispose();
DEBUG_COLOR.dispose();
ANT_FONT.dispose();
ExternalToolsPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(changeListener);
}
}
public static LogConsoleDocument getInstance() {
return instance;
}
public OutputStructureElement getCurrentOutputStructureElement() {
return currentElement;
}
public void setCurrentOutputStructureElement(OutputStructureElement output) {
this.currentElement = output;
}
/*package*/ void setOutputLevelColor(int level, int start, int end) {
switch (level) {
case LogConsoleDocument.MSG_ERR:
addRangeStyle(start, end, LogConsoleDocument.ERROR_COLOR);
break;
case LogConsoleDocument.MSG_WARN:
addRangeStyle(start, end, LogConsoleDocument.WARN_COLOR);
break;
case LogConsoleDocument.MSG_INFO:
addRangeStyle(start, end, LogConsoleDocument.INFO_COLOR);
break;
case LogConsoleDocument.MSG_VERBOSE:
addRangeStyle(start, end, LogConsoleDocument.VERBOSE_COLOR);
break;
case LogConsoleDocument.MSG_DEBUG:
addRangeStyle(start, end, LogConsoleDocument.DEBUG_COLOR);
break;
default:
addRangeStyle(start, end, LogConsoleDocument.INFO_COLOR);
}
}
private class LogPropertyChangeListener implements IPropertyChangeListener {
// private constructor to ensure the singleton
private LogPropertyChangeListener() {
}
/**
* @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent event) {
String propertyName= event.getProperty();
if (propertyName.equals(IPreferenceConstants.CONSOLE_ERROR_RGB)) {
Color temp = LogConsoleDocument.ERROR_COLOR;
LogConsoleDocument.ERROR_COLOR = ToolsPreferencePage.getPreferenceColor(IPreferenceConstants.CONSOLE_ERROR_RGB);
temp.dispose();
clearOutput();
} else if (propertyName.equals(IPreferenceConstants.CONSOLE_WARNING_RGB)) {
Color temp = LogConsoleDocument.WARN_COLOR;
LogConsoleDocument.WARN_COLOR = ToolsPreferencePage.getPreferenceColor(IPreferenceConstants.CONSOLE_WARNING_RGB);
temp.dispose();
clearOutput();
} else if (propertyName.equals(IPreferenceConstants.CONSOLE_INFO_RGB)) {
Color temp = LogConsoleDocument.INFO_COLOR;
LogConsoleDocument.INFO_COLOR = ToolsPreferencePage.getPreferenceColor(IPreferenceConstants.CONSOLE_INFO_RGB);
temp.dispose();
clearOutput();
} else if (propertyName.equals(IPreferenceConstants.CONSOLE_VERBOSE_RGB)) {
Color temp = LogConsoleDocument.VERBOSE_COLOR;
LogConsoleDocument.VERBOSE_COLOR = ToolsPreferencePage.getPreferenceColor(IPreferenceConstants.CONSOLE_VERBOSE_RGB);
temp.dispose();
clearOutput();
} else if (propertyName.equals(IPreferenceConstants.CONSOLE_DEBUG_RGB)) {
Color temp = LogConsoleDocument.DEBUG_COLOR;
LogConsoleDocument.DEBUG_COLOR = ToolsPreferencePage.getPreferenceColor(IPreferenceConstants.CONSOLE_DEBUG_RGB);
temp.dispose();
clearOutput();
} else if (propertyName.equals(IPreferenceConstants.CONSOLE_FONT)) {
FontData data= ToolsPreferencePage.getConsoleFontData();
Font temp= LogConsoleDocument.ANT_FONT;
LogConsoleDocument.ANT_FONT = new Font(Display.getCurrent(), data);
temp.dispose();
updateFont();
} else
return;
}
/**
* Clears the output of all the consoles
*/
private void clearOutput() {
LogConsoleDocument.getInstance().clearOutput();
}
/**
* Updates teh font in all the consoles
*/
private void updateFont() {
for (Iterator iterator = LogConsoleDocument.getInstance().getViews().iterator(); iterator.hasNext();)
((LogConsoleView) iterator.next()).updateFont();
}
}
}