Moved to test plugin to eliminate dependency upon junit plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / SQLLogView.java
1 package net.sourceforge.phpdt.sql.view;
2
3 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
4
5 import org.eclipse.jface.action.Action;
6 import org.eclipse.jface.action.IToolBarManager;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.custom.StyleRange;
9 import org.eclipse.swt.custom.StyledText;
10 import org.eclipse.swt.custom.StyledTextContent;
11 import org.eclipse.swt.events.DisposeEvent;
12 import org.eclipse.swt.events.DisposeListener;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.IActionBars;
16 import org.eclipse.ui.IWorkbenchActionConstants;
17 import org.eclipse.ui.part.ViewPart;
18
19 public class SQLLogView extends ViewPart implements LogConstants {
20         private Color QUERY_COLOR;
21         private Color WARNING_COLOR;
22         private Color DEFAULT_COLOR;
23         private Color ERROR_COLOR;
24         private Color RESULTS_COLOR;
25         private StyledText widget;
26         private static SQLLogView instance = null;
27         private static final String newLine = "\n";
28         public static SQLLogView getInstance() {
29                 return instance;
30         }
31
32         public void createPartControl(Composite parent) {
33                 instance = this;
34                 QUERY_COLOR = new Color(parent.getShell().getDisplay(), 0, 255, 0);
35                 ERROR_COLOR = new Color(parent.getShell().getDisplay(), 255, 0, 0);
36                 RESULTS_COLOR = new Color(parent.getShell().getDisplay(), 0, 0, 255);
37                 DEFAULT_COLOR = new Color(parent.getShell().getDisplay(), 0, 0, 0);
38                 WARNING_COLOR = new Color(parent.getShell().getDisplay(), 255, 127, 0);
39                 widget =  new StyledText(parent, SWT.H_SCROLL | SWT.V_SCROLL);
40                 IActionBars bars = this.getViewSite().getActionBars();
41                 bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
42                 bars.setGlobalActionHandler(IWorkbenchActionConstants.SELECT_ALL, selectAllAction);
43
44                 IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
45                 clearAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("clear.gif"));
46                 clearAction.setToolTipText("Clear Log");
47                 toolBar.add(clearAction);
48
49                 widget.setEditable(false);
50                 
51                 widget.addDisposeListener(new DisposeListener() {
52                         public void widgetDisposed(DisposeEvent e) {
53                                 instance = null;
54                         }
55                 });
56         }
57         
58     public void addText(int style, String text) {
59         text = text + newLine;
60         int start = widget.getText().length();
61                 StyleRange styleRange = new StyleRange();
62                 styleRange.start = start;
63                 styleRange.length = text.length();
64                 if (style == QUERY) {
65                         styleRange.foreground = QUERY_COLOR;
66                 } else if (style == ERROR) {
67                         styleRange.foreground = ERROR_COLOR;
68                 } else if (style == RESULTS) {
69                         styleRange.foreground = RESULTS_COLOR;
70                 } else if (style == WARNING) {
71                         styleRange.foreground = WARNING_COLOR;
72                 } else {
73                         styleRange.foreground = DEFAULT_COLOR;
74                 }
75                 widget.append(text);
76                 widget.setStyleRange(styleRange);
77                 revealEndOfDocument();
78     }
79
80         protected void revealEndOfDocument() {
81                 StyledTextContent doc= widget.getContent();
82                 int docLength= doc.getCharCount();
83                 if (docLength > 0) {
84                         widget.setCaretOffset(docLength);
85                         widget.showSelection();
86                 }
87         }
88
89         public void setFocus() {
90                 widget.setFocus();
91         }
92
93         private Action copyAction = new Action() {
94                 public void run() {
95                         widget.copy();
96                 }
97         };
98         private Action selectAllAction = new Action() {
99                 public void run() {
100                         widget.selectAll();
101                 }
102         };
103         private Action clearAction = new Action() {
104                 public void run() {
105                         widget.setText("");
106                 }
107         };
108 }