package com.quantum.view;

import com.quantum.ImageStore;
import com.quantum.Messages;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.StyledTextContent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.ViewPart;

public class SQLLogView extends ViewPart implements LogConstants {
	private Color QUERY_COLOR;
	private Color WARNING_COLOR;
	private Color DEFAULT_COLOR;
	private Color ERROR_COLOR;
	private Color RESULTS_COLOR;
	private StyledText widget;
	private static SQLLogView instance = null;
	private static final String newLine = "\n"; //$NON-NLS-1$
	public static SQLLogView getInstance() {
		return instance;
	}

	public void createPartControl(Composite parent) {
	   	instance = this;
	   	QUERY_COLOR = new Color(parent.getShell().getDisplay(), 0, 255, 0);
	   	ERROR_COLOR = new Color(parent.getShell().getDisplay(), 255, 0, 0);
	   	RESULTS_COLOR = new Color(parent.getShell().getDisplay(), 0, 0, 255);
	   	DEFAULT_COLOR = new Color(parent.getShell().getDisplay(), 0, 0, 0);
	   	WARNING_COLOR = new Color(parent.getShell().getDisplay(), 255, 127, 0);
		widget =  new StyledText(parent, SWT.H_SCROLL | SWT.V_SCROLL);
		IActionBars bars = this.getViewSite().getActionBars();
		bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
		bars.setGlobalActionHandler(IWorkbenchActionConstants.SELECT_ALL, selectAllAction);

		IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
		clearAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.CLEAR));
		clearAction.setToolTipText(Messages.getString("SQLLogView.ClearLog")); //$NON-NLS-1$
		toolBar.add(clearAction);

 		widget.setEditable(false);
 		
		widget.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				instance = null;
			}
		});
	}
	
    public void addText(int style, String text) {
    	text = text + newLine;
    	int start = widget.getText().length();
		StyleRange styleRange = new StyleRange();
		styleRange.start = start;
		styleRange.length = text.length();
		if (style == QUERY) {
			styleRange.foreground = QUERY_COLOR;
		} else if (style == ERROR) {
			styleRange.foreground = ERROR_COLOR;
		} else if (style == RESULTS) {
			styleRange.foreground = RESULTS_COLOR;
		} else if (style == WARNING) {
			styleRange.foreground = WARNING_COLOR;
		} else {
			styleRange.foreground = DEFAULT_COLOR;
		}
		widget.append(text);
		widget.setStyleRange(styleRange);
		revealEndOfDocument();
    }

	protected void revealEndOfDocument() {
		StyledTextContent doc= widget.getContent();
		int docLength= doc.getCharCount();
		if (docLength > 0) {
			widget.setCaretOffset(docLength);
			widget.showSelection();
		}
	}

	public void setFocus() {
		widget.setFocus();
	}

	private Action copyAction = new Action() {
		public void run() {
			widget.copy();
		}
	};
	private Action selectAllAction = new Action() {
		public void run() {
			widget.selectAll();
		}
	};
	private Action clearAction = new Action() {
		public void run() {
			widget.setText(""); //$NON-NLS-1$
		}
	};
}