misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / SQLQueryView.java
index a8469e7..efdac83 100644 (file)
@@ -7,6 +7,8 @@ import java.util.NoSuchElementException;
 import java.util.Vector;
 
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.Separator;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.ExtendedModifyEvent;
 import org.eclipse.swt.custom.ExtendedModifyListener;
@@ -15,12 +17,9 @@ import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.graphics.Color;
-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.Label;
-import org.eclipse.swt.widgets.ProgressBar;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
 import org.eclipse.ui.IActionBars;
@@ -38,16 +37,51 @@ import com.quantum.model.NotConnectedException;
 import com.quantum.sql.MultiSQLServer;
 import com.quantum.sql.parser.SQLLexx;
 import com.quantum.sql.parser.Token;
+import com.quantum.util.versioning.VersioningHelper;
 import com.quantum.view.bookmark.BookmarkNode;
 import com.quantum.view.bookmark.BookmarkView;
 
 public class SQLQueryView extends ViewPart {
+       private class ClearAction extends Action {
+               
+               public ClearAction() {
+                       setImageDescriptor(QuantumPlugin.getImageDescriptor("clear.gif"));
+                       setToolTipText(Messages.getString("sqlqueryview.clear"));
+               }
+
+               public void run() {
+                       setQuery("");
+               }
+       }
+
+       private class AutoCommitPreferenceAction extends Action {
+               
+               public AutoCommitPreferenceAction() {
+                       super(Messages.getString("SQLQueryView.AutoCommit"), SWT.CHECK);
+                       setToolTipText(Messages.getString("SQLQueryView.AutoCommit"));
+                       setImageDescriptor(QuantumPlugin.getImageDescriptor("autocommit.gif"));
+               }
+               
+               public void run() {
+                       Connection connection = null; 
+                       try { 
+                               // Get the connection
+                               connection = getBookmark().getConnection();
+                               // If connected (else will throw exception and jump out) switchs the state of the
+                               // autoCommit option of the JDBC driver
+                               MultiSQLServer.getInstance().setAutoCommit(     connection, isChecked());
+            } catch (NotConnectedException e) {
+               //Doesn't matter
+            }
+            // Update the bookmark and the buttons
+                       updateAutoCommitState(getBookmark(), connection);
+               }
+       }
+       
+       
        private ExecuteAction executeAction;
        private ImportQueryAction importQueryAction;
        private ExportQueryAction exportQueryAction;
-       private Label statusImage;
-       private Label status;
-       private ProgressBar progress;
        private StyledText widget;
        private ToolItem autoCommitItem;
        private ToolItem commitItem;
@@ -57,25 +91,23 @@ public class SQLQueryView extends ViewPart {
        private Color COMMENT;
        private Color NUMERIC;
        private Color DEFAULT;
-       private long parseTime = 0;
-       private long fullTime = 0;
+       private AutoCommitPreferenceAction autoCommitPreferenceAction;
+       
        public SQLQueryView() {
                super();
        }
        public void setFocus() {
                
                String title = "Quantum SQL Query Editor";
-               BookmarkNode bookmarkNode = null;
                Bookmark bookmark = null;
                Connection con = null;
                if (BookmarkView.getInstance() != null ) {
-                       bookmarkNode = BookmarkView.getInstance().getCurrentBookmark();
+                       bookmark = getBookmark();
                }
-               if (bookmarkNode != null)
-                       bookmark = bookmarkNode.getBookmark();
                if (bookmark != null) {         
                        title = bookmark.getName() + " (" + title + ")";
-                       setTitle( title );
+                       VersioningHelper.setPartName(this, title);
+//                     setPartName("fred");
                        try {
                                con = bookmark.getConnection();
                        } catch (NotConnectedException e) {
@@ -88,6 +120,17 @@ public class SQLQueryView extends ViewPart {
                widget.setFocus();
 
        }
+       /**
+        * @return
+        */
+       private Bookmark getBookmark() {
+               if (BookmarkView.getInstance() != null ) {
+                       BookmarkNode node = BookmarkView.getInstance().getCurrentBookmark();
+                       return node == null ? null : node.getBookmark();
+               } else {
+                       return null;
+               }
+       }
        public static SQLQueryView getInstance() {
                return (SQLQueryView) QuantumPlugin.getDefault().getView("com.quantum.view.sqlqueryview");
 
@@ -95,6 +138,7 @@ public class SQLQueryView extends ViewPart {
 
        public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
                initActions();
+               
                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);
@@ -106,49 +150,6 @@ public class SQLQueryView extends ViewPart {
                layout.verticalSpacing = 0;
                main.setLayout(layout);
                ToolBar toolbar = new ToolBar(main, SWT.HORIZONTAL);
-               ToolItem item = new ToolItem(toolbar, SWT.PUSH);
-               item.setImage(QuantumPlugin.getImage("play.gif")); //$NON-NLS-1$
-               item.setToolTipText(Messages.getString("sqlqueryview.executeQuery")); //$NON-NLS-1$
-               item.addSelectionListener(new SelectionListener() {
-                       public void widgetDefaultSelected(SelectionEvent e) {
-                       }
-                       public void widgetSelected(SelectionEvent e) {
-                               executeAction.run();
-                       }
-               });
-               item = new ToolItem(toolbar, SWT.SEPARATOR);
-               item = new ToolItem(toolbar, SWT.PUSH);
-               item.setImage(QuantumPlugin.getImage("import.gif")); //$NON-NLS-1$
-               item.setToolTipText(Messages.getString("sqlqueryview.importQuery")); //$NON-NLS-1$
-               item.addSelectionListener(new SelectionListener() {
-                       public void widgetDefaultSelected(SelectionEvent e) {
-                       }
-                       public void widgetSelected(SelectionEvent e) {
-                               importQueryAction.run();
-                       }
-               });
-               item = new ToolItem(toolbar, SWT.PUSH);
-               item.setImage(QuantumPlugin.getImage("export.gif")); //$NON-NLS-1$
-               item.setToolTipText(Messages.getString("sqlqueryview.exportQuery")); //$NON-NLS-1$
-               item.addSelectionListener(new SelectionListener() {
-                       public void widgetDefaultSelected(SelectionEvent e) {
-                       }
-                       public void widgetSelected(SelectionEvent e) {
-                               exportQueryAction.run();
-                       }
-               });
-               item = new ToolItem(toolbar, SWT.PUSH);
-               item.setImage(QuantumPlugin.getImage("clear.gif")); //$NON-NLS-1$
-               item.setToolTipText(Messages.getString("sqlqueryview.clear")); //$NON-NLS-1$
-               item.addSelectionListener(new SelectionListener() {
-                       public void widgetDefaultSelected(SelectionEvent e) {
-                       }
-                       public void widgetSelected(SelectionEvent e) {
-                               setQuery(""); //$NON-NLS-1$
-                       }
-               });
-
-               item = new ToolItem(toolbar, SWT.SEPARATOR);
 
                commitItem = new ToolItem(toolbar, SWT.PUSH);
                commitItem.setImage(QuantumPlugin.getImage("commit.gif")); //$NON-NLS-1$
@@ -208,9 +209,19 @@ public class SQLQueryView extends ViewPart {
                                
                        }
                });
-               BookmarkNode node = BookmarkView.getInstance().getCurrentBookmark();
-               if (node == null) autoCommitItem.setSelection(true); 
-               else autoCommitItem.setSelection(node.getBookmark().isAutoCommit());
+
+               // TODO: BCH -- this is causing some problems during start-up
+               Bookmark bookmark = null;
+               try {
+                       bookmark = getBookmark();
+               } catch (NullPointerException e) {
+               }
+               
+               if (bookmark == null) {
+                       autoCommitItem.setSelection(true); 
+               } else {
+                       autoCommitItem.setSelection(bookmark.isAutoCommit());
+               }
                if (autoCommitItem.getSelection()) {
                        commitItem.setEnabled(false);
                        rollbackItem.setEnabled(false);
@@ -236,32 +247,6 @@ public class SQLQueryView extends ViewPart {
                gridData.grabExcessVerticalSpace = true;
                widget.setLayoutData(gridData);
 
-               Composite bottomStatus = new Composite(main, SWT.NONE);
-               gridData = new GridData();
-               gridData.horizontalAlignment = GridData.FILL;
-               gridData.grabExcessHorizontalSpace = true;
-               bottomStatus.setLayoutData(gridData);
-               
-               GridLayout horizontal = new GridLayout(3, false);
-               layout.horizontalSpacing = 0;
-               layout.verticalSpacing = 0;
-               layout.marginHeight = 0;
-               layout.marginWidth = 0;
-               bottomStatus.setLayout(horizontal);
-
-               statusImage = new Label(bottomStatus, SWT.NONE);
-               status = new Label(bottomStatus, SWT.NONE);
-               gridData = new GridData();
-               gridData.horizontalAlignment = GridData.FILL;
-               gridData.grabExcessHorizontalSpace = true;
-               status.setLayoutData(gridData);
-
-               progress = new ProgressBar(bottomStatus, SWT.HORIZONTAL);
-
-               status.setText(Messages.getString("sqlqueryview.done")); //$NON-NLS-1$
-               statusImage.setImage(QuantumPlugin.getImage("success.gif")); //$NON-NLS-1$
-               progress.setMinimum(0);
-        
         IKeyBindingService keyBindingService = getSite().getKeyBindingService();
         // TODO: check the version numbers for this method
         keyBindingService.setScopes(new String[] {
@@ -275,14 +260,14 @@ public class SQLQueryView extends ViewPart {
         * Sets the state of the "Commit", "Rollback" and "autoCommit" buttons
         * to reflect the situation in the connection
         */
-       protected void updateAutoCommitState(Bookmark bookmark, Connection con) {
+       protected void updateAutoCommitState(Bookmark bookmark, Connection connection) {
                boolean autoCommit = true;
                // Calculate the state of the autoCommit option
-               if (con != null)
+               if (connection != null)
                {
                        // If we have a connection, the autoCommit state is that of the connection
                        try {
-                               autoCommit = con.getAutoCommit();
+                               autoCommit = connection.getAutoCommit();
                        } catch (SQLException e) {
                                // Doesn't matter, we take default
                        }
@@ -294,6 +279,7 @@ public class SQLQueryView extends ViewPart {
                if (bookmark != null) bookmark.setAutoCommit(autoCommit);
                // Set the state of the buttons to the correct autoCommit state
                autoCommitItem.setSelection(autoCommit);
+               this.autoCommitPreferenceAction.setChecked(autoCommit);
                if (autoCommitItem.getSelection()) {
                        commitItem.setEnabled(false);
                        rollbackItem.setEnabled(false);
@@ -302,30 +288,30 @@ public class SQLQueryView extends ViewPart {
                        rollbackItem.setEnabled(true);
                }
        }
-       public void setProgress(int increment, int max) {
-               progress.setMaximum(max);
-               progress.setSelection(increment);
-       }
-       
        private void initActions() {
+
                executeAction = new ExecuteAction();
                executeAction.init(this);
-               importQueryAction = new ImportQueryAction();
-               importQueryAction.init(this);
-               exportQueryAction = new ExportQueryAction();
-               exportQueryAction.init(this);
-       }
 
-       public void setStatus(String text) {
-               statusImage.setImage(null);
-               status.setText(text);
+               IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
+               toolBar.add(this.executeAction);
+//             toolBar.add(this.importQueryAction);
+//             toolBar.add(this.exportQueryAction);
+               
+               toolBar.add(new ClearAction());
+               
+        IActionBars actionBars = getViewSite().getActionBars();
+               this.importQueryAction = new ImportQueryAction();
+               this.importQueryAction.init(this);
+        actionBars.getMenuManager().add(this.importQueryAction);
+        this.exportQueryAction = new ExportQueryAction();
+        this.exportQueryAction.init(this);
+        actionBars.getMenuManager().add(this.exportQueryAction);
+        actionBars.getMenuManager().add(new Separator());
+        this.autoCommitPreferenceAction = new AutoCommitPreferenceAction();
+        actionBars.getMenuManager().add(this.autoCommitPreferenceAction);
        }
 
-       public void setStatus(Image img, String text) {
-               statusImage.setImage(img);
-               status.setText(text);
-       }
-       
        public String getQuery() {
                return widget.getText();
        }
@@ -404,9 +390,7 @@ public class SQLQueryView extends ViewPart {
                                        //int dirtyStart = request.start;
                                        //int dirtyEnd = request.start + request.length;
                                        StyleRange styleRange;
-                                       long startTime = System.currentTimeMillis();
                                        Vector tokens = SQLLexx.parse(text);
-                                       long subTime = System.currentTimeMillis();
                                        Vector styles = new Vector();
                                        int min = Integer.MAX_VALUE;
                                        int max = 0;
@@ -462,9 +446,6 @@ public class SQLQueryView extends ViewPart {
                                        if (max >= 0 && ranges.length > 0) {
                                                setStyles(ranges, min, max - min);
                                        }
-                                       long endTime = System.currentTimeMillis();
-                                       parseTime = subTime - startTime;
-                                       fullTime = endTime - startTime;
                                } catch (NoSuchElementException e) {
                                        // ignore a missing request
                                } catch (InterruptedException e) {