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