new update site
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / SQLQueryView.java
index 0b14583..a438d7f 100644 (file)
@@ -1,29 +1,15 @@
 package com.quantum.view;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.NoSuchElementException;
-import java.util.Vector;
-
-import com.quantum.ImageStore;
-import com.quantum.Messages;
-import com.quantum.QuantumPlugin;
-import com.quantum.actions.ExecuteAction;
-import com.quantum.actions.ExportQueryAction;
-import com.quantum.actions.ImportQueryAction;
-import com.quantum.model.Bookmark;
-import com.quantum.model.BookmarkCollection;
-import com.quantum.model.NotConnectedException;
-import com.quantum.sql.MultiSQLServer;
-import com.quantum.sql.SQLGrammar;
-import com.quantum.sql.parser.SQLLexx;
-import com.quantum.sql.parser.Token;
-import com.quantum.ui.dialog.ExceptionDisplayDialog;
-import com.quantum.util.versioning.VersioningHelper;
 
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.Separator;
+import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.preference.PreferenceConverter;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
@@ -42,11 +28,31 @@ import org.eclipse.swt.graphics.FontData;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.part.ViewPart;
 
+import com.quantum.ImageStore;
+import com.quantum.Messages;
+import com.quantum.PluginPreferences;
+import com.quantum.QuantumPlugin;
+import com.quantum.actions.ExecuteAction;
+import com.quantum.actions.ExportQueryAction;
+import com.quantum.actions.ImportQueryAction;
+import com.quantum.editors.ColorManager;
+import com.quantum.model.Bookmark;
+import com.quantum.model.BookmarkCollection;
+import com.quantum.model.NotConnectedException;
+import com.quantum.sql.MultiSQLServer;
+import com.quantum.sql.SQLGrammar;
+import com.quantum.sql.parser.SQLLexx;
+import com.quantum.sql.parser.Token;
+import com.quantum.ui.dialog.ExceptionDisplayDialog;
+import com.quantum.ui.dialog.SQLExceptionDialog;
+import com.quantum.util.versioning.VersioningHelper;
+
 public class SQLQueryView extends ViewPart {
        private class ClearAction extends Action {
                
@@ -87,7 +93,7 @@ public class SQLQueryView extends ViewPart {
                                                MultiSQLServer.getInstance().rollback(bookmarks[i].getConnection());
                                        }
                    } catch (SQLException e) {
-                       ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
+                       SQLExceptionDialog.openException(getSite().getShell(), bookmarks[i], e);
                    } catch (NotConnectedException e) {
                        ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
                    }
@@ -110,7 +116,7 @@ public class SQLQueryView extends ViewPart {
                                                MultiSQLServer.getInstance().commit(bookmarks[i].getConnection());
                                        }
                    } catch (SQLException e) {
-                       ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
+                       SQLExceptionDialog.openException(getSite().getShell(), bookmarks[i], e);
                    } catch (NotConnectedException e) {
                        ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
                    }
@@ -158,25 +164,32 @@ public class SQLQueryView extends ViewPart {
        private ImportQueryAction importQueryAction;
        private ExportQueryAction exportQueryAction;
        private StyledText widget;
-       private Color STRING_LITERAL;
-       private Color KEYWORD;
-       private Color COMMENT;
-       private Color NUMERIC;
-       private Color DEFAULT;
        private AutoCommitPreferenceAction autoCommitPreferenceAction;
        private RollbackAction rollbackAction;
        private CommitAction commitAction;
        private boolean autoCommitPreference = true;
+       private IPropertyChangeListener listener;
+       private ColorManager colorManager = new ColorManager();
+
+       private SyntaxHighlighter textUpdater = new SyntaxHighlighter(this.colorManager);
+       
        
        public SQLQueryView() {
                super();
-               IPropertyChangeListener listener = new IPropertyChangeListener() {
+               this.listener = new IPropertyChangeListener() {
                        public void propertyChange(PropertyChangeEvent event) {
                                setFont();
                        }
                };
                QuantumPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(listener);
        }
+       
+       public void dispose() {
+               QuantumPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this.listener);
+               this.colorManager.dispose();
+               super.dispose();
+       }
+       
        public static SQLQueryView getInstance() {
                return (SQLQueryView) QuantumPlugin.getDefault().getView("com.quantum.view.sqlqueryview");
 
@@ -214,9 +227,6 @@ public class SQLQueryView extends ViewPart {
                        this.executeAction);
        }
 
-       /**
-        * @param widget2
-        */
        private void setFont() {
                FontData font = PreferenceConverter.getFontData(
                                QuantumPlugin.getDefault().getPreferenceStore(), 
@@ -228,12 +238,12 @@ public class SQLQueryView extends ViewPart {
        /**
         * @param parent
         */
-       private void initializeColours(org.eclipse.swt.widgets.Composite parent) {
-               KEYWORD = new Color(parent.getShell().getDisplay(), 126, 0, 75);
-               STRING_LITERAL = new Color(parent.getShell().getDisplay(), 0, 0, 255);
-               COMMENT = new Color(parent.getShell().getDisplay(), 88, 148, 64);
-               NUMERIC = new Color(parent.getShell().getDisplay(), 255, 0, 0);
-           DEFAULT = new Color(parent.getShell().getDisplay(), 0, 0, 0);
+       private void initializeColours(Composite parent) {
+           IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
+
+               parent.setBackground(this.colorManager.getColor(
+                               PreferenceConverter.getColor(store, PluginPreferences.BACKGROUND_COLOR)));
+               this.textUpdater.initializeColours();
        }
        
        private void initActions() {
@@ -263,16 +273,27 @@ public class SQLQueryView extends ViewPart {
         actionBars.getMenuManager().add(this.commitAction);
 }
 
+       /**
+        * Returns the query to be executed. The query is either 1) the 
+        * text currently highlighted/selected in the editor or 2) all of 
+     * the text in the editor. 
+        * @return query string to be executed
+        */
        public String getQuery() {
-               return widget.getText();
+           String query; 
+           
+           if (widget.getSelectionText().length() > 0)
+               query = widget.getSelectionText();
+           else    
+               query = widget.getText();
+           
+               return query;
        }
        
        public void setQuery(String text) {
                widget.setText(text);
        }
        
-       SyntaxHighlighter textUpdater = new SyntaxHighlighter();
-       
        private class UpdateRequest {
                public UpdateRequest(String text, int start, int length) {
                        this.text = text;
@@ -285,21 +306,42 @@ public class SQLQueryView extends ViewPart {
        }
        
        private class SyntaxHighlighter extends Thread {
+               
+               private Color STRING_LITERAL;
+               private Color KEYWORD;
+               private Color COMMENT;
+               private Color NUMERIC;
+               private Color DEFAULT;
+               
                private boolean running = true;
                private LinkedList requests = new LinkedList();
-               public SyntaxHighlighter() {
+               private final ColorManager colorManager;
+               public SyntaxHighlighter(ColorManager colorManager) {
                        super();
+                       this.colorManager = colorManager;
+                       
                        setPriority(Thread.MIN_PRIORITY);
                        start();
                }
+               public void initializeColours() {
+                   IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
+
+                       this.DEFAULT = this.colorManager.getColor(
+                                       PreferenceConverter.getColor(store, PluginPreferences.TEXT_COLOR));
+                       this.KEYWORD = this.colorManager.getColor(
+                                       PreferenceConverter.getColor(store, PluginPreferences.KEYWORD_COLOR)); 
+                       this.STRING_LITERAL = this.colorManager.getColor(
+                                       PreferenceConverter.getColor(store, PluginPreferences.STRING_COLOR));
+                       this.COMMENT = this.colorManager.getColor(
+                                       PreferenceConverter.getColor(store, PluginPreferences.COMMENT_COLOR));
+                       this.NUMERIC = this.colorManager.getColor(
+                                       PreferenceConverter.getColor(store, PluginPreferences.NUMERIC_COLOR));
+                       
+               }
                public synchronized void updateText(String text, int start, int length) {
                        requests.add(new UpdateRequest(text, start, length));
                        notify();
                }
-               public synchronized void shutdown() {
-                       running = false;
-                       interrupt();
-               }
                public void run() {
                        while (running) {
                                try {
@@ -315,12 +357,12 @@ public class SQLQueryView extends ViewPart {
                                        //int dirtyStart = request.start;
                                        //int dirtyEnd = request.start + request.length;
                                        StyleRange styleRange;
-                                       Vector tokens = SQLLexx.parse(text);
-                                       Vector styles = new Vector();
+                                       List tokens = SQLLexx.parse(text);
+                                       List styles = new ArrayList();
                                        int min = Integer.MAX_VALUE;
                                        int max = 0;
                                        for (int i = 0; i < tokens.size(); i++) {
-                                               Token t = (Token) tokens.elementAt(i);
+                                               Token t = (Token) tokens.get(i);
                                                String value = t.getValue();
                                                int start = t.getStart();
                                                int length = t.getEnd() - t.getStart();
@@ -349,25 +391,23 @@ public class SQLQueryView extends ViewPart {
                                                                } else {
                                                                        styleRange.foreground = DEFAULT;
                                                                }
-                                                               styles.addElement(styleRange);
+                                                               styles.add(styleRange);
                                                        } else if (t.getType() == Token.COMMENT) {
                                                                styleRange.foreground = COMMENT;
-                                                               styles.addElement(styleRange);
+                                                               styles.add(styleRange);
                                                        } else if (t.getType() == Token.LITERAL) {
                                                                styleRange.foreground = STRING_LITERAL;
-                                                               styles.addElement(styleRange);
+                                                               styles.add(styleRange);
                                                        } else if (t.getType() == Token.NUMERIC) {
                                                                styleRange.foreground = NUMERIC;
-                                                               styles.addElement(styleRange);
+                                                               styles.add(styleRange);
                                                        } else {
-                                                               styles.addElement(styleRange);
+                                                               styles.add(styleRange);
                                                        }
                                                }
                                        }
-                                       StyleRange[] ranges = new StyleRange[styles.size()];
-                                       for (int k = 0; k < ranges.length; k++) {
-                                               ranges[k] = (StyleRange) styles.elementAt(k);
-                                       }
+                                       StyleRange[] ranges = 
+                                                       (StyleRange[]) styles.toArray(new StyleRange[styles.size()]);
                                        if (max >= 0 && ranges.length > 0) {
                                                setStyles(ranges, min, max - min);
                                        }