SQL Plugin copied from Quantum plugin and refactored for PHPEclipse
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / SQLLogView.java
diff --git a/archive/net.sourceforge.phpeclipse.sql/src/net/sourceforge/phpdt/sql/view/SQLLogView.java b/archive/net.sourceforge.phpeclipse.sql/src/net/sourceforge/phpdt/sql/view/SQLLogView.java
new file mode 100644 (file)
index 0000000..66552d9
--- /dev/null
@@ -0,0 +1,108 @@
+package net.sourceforge.phpdt.sql.view;
+
+import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
+
+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";
+       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(PHPEclipseSQLPlugin.getImageDescriptor("clear.gif"));
+               clearAction.setToolTipText("Clear Log");
+               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("");
+               }
+       };
+}