*** empty log message ***
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableView.java
index 8e23888..9351717 100644 (file)
@@ -1,66 +1,60 @@
 package com.quantum.view.tableview;
 
-import java.util.Vector;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import com.quantum.QuantumPlugin;
+import com.quantum.sql.SQLResultSetCollection;
+import com.quantum.sql.SQLResultSetResults;
 
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IMenuListener;
-import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
-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.Menu;
 import org.eclipse.swt.widgets.TabFolder;
 import org.eclipse.swt.widgets.TabItem;
-import org.eclipse.swt.widgets.Table;
-import org.eclipse.swt.widgets.ToolBar;
-import org.eclipse.ui.ISelectionListener;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.WorkbenchException;
+import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.part.ViewPart;
 
-import com.quantum.Messages;
-import com.quantum.QuantumPlugin;
-import com.quantum.extensions.ProcessServiceMembers;
-import com.quantum.model.Bookmark;
-import com.quantum.model.Entity;
-import com.quantum.model.NotConnectedException;
-import com.quantum.sql.SQLResults;
-import com.quantum.ui.dialog.ExceptionDisplayDialog;
-import com.quantum.view.LogProxy;
-
 /**
  * The Table View. Displays tables and Queries.
  */
-public class TableView extends ViewPart implements ISelectionListener {
+public class TableView extends ViewPart implements ISelectionProvider, PropertyChangeListener {
 
-       private class DefaultEncodingAction extends Action {
-        private final TableAdapter ta;
-        private DefaultEncodingAction(TableAdapter ta) {
-            super();
-            this.ta = ta;
-        }
-        public void run() {
-               ta.setEncoding(TableAdapter.DEFAULT);
-        }
-    }
-       private TabFolder tabs = null;
-       private Composite parent;
-       private Vector extensionVector;
+       protected TabFolder tabs = null;
+       
+       private Set listeners = Collections.synchronizedSet(new HashSet());
+       
+       protected List resultSetViewers = Collections.synchronizedList(new ArrayList());
+       protected TableViewActionGroup actionGroup;
  
        /**
         * Generic contructor
         */
        public TableView() {
+               SQLResultSetCollection.getInstance().addPropertyChangeListener(this);
        }
 
 
        public void setFocus() {
-               setQualifiedTitle();
+       }
+       
+       public void dispose() {
+               SQLResultSetCollection.getInstance().removePropertyChangeListener(this);
+               super.dispose();
        }
        
     /**
@@ -74,209 +68,157 @@ public class TableView extends ViewPart implements ISelectionListener {
                return (TableView) QuantumPlugin.getDefault().getView("com.quantum.view.tableview.TableView");
        }
        
-       /**
-        * Close the current tab, disposing of it
-        */
-       public void closeCurrent() {
-               if (tabs == null) return;
-               
-               if (tabs.getSelectionIndex() >= 0) {
-                       try {
-                               TabItem item = tabs.getItem(tabs.getSelectionIndex());
-                               item.dispose();
-                       } catch (Throwable e) {
-                               LogProxy.getInstance().addText(LogProxy.ERROR, "Error Closing Current: " + e.toString()); //$NON-NLS-1$
-                               e.printStackTrace();
+       public void createPartControl(Composite parent) {
+               this.tabs = new TabFolder(parent, SWT.NONE);
+               this.tabs.addSelectionListener(new SelectionAdapter() {
+                       public void widgetSelected(SelectionEvent event) {
+                               fireSelectionChangedEvent();
                        }
-               } 
-               if (tabs.getItemCount() == 0) {
-                       setTitle(Messages.getString("tableview.QuantumTableViewName")); //$NON-NLS-1$
+               });
+
+               SQLResultSetResults[] resultSets = SQLResultSetCollection.getInstance().getResultSets();
+               for (int i = 0, length = resultSets == null ? 0 : resultSets.length; i < length; i++) {
+                       this.resultSetViewers.add(new ResultSetViewer(this, resultSets[i]));
                }
+               
+               initActions();
        }
        
-       /**
-        * Reload table or query data into the selected tab
-        */
-       public void refreshCurrent() {
-               TabItem item = tabs.getItem(tabs.getSelectionIndex());
-               TableAdapter adapter = (TableAdapter) item.getData();
-               Bookmark bookmark = adapter.getBookmark();
-               String table = adapter.getTable();
-               if (table == null) {
-                       loadTable(bookmark, item, null, null, true, true);
-               } else {
-                       loadTable(bookmark, item, null, null, true, true);
-               }
-               String title = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$
-               if (bookmark != null)
-                       title = bookmark.getName() + Messages.getString("tableview.ViewNameInitialDecoration") + title + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$ //$NON-NLS-2$
-               setTitle(title);
+       public void initActions() {
+               
+        this.actionGroup = new TableViewActionGroup(this);
+
+        IActionBars actionBars = getViewSite().getActionBars();
+        this.actionGroup.fillActionBars(actionBars);
+       }
+
+       public void addSelectionChangedListener(ISelectionChangedListener listener) {
+               this.listeners.add(listener);
        }
        
-    public void loadQuery(Bookmark bookmark, SQLResults results) {
-        loadTable(bookmark, null, null, results, true, false);
-    }
-       public void loadTable(Entity entity) {
-               loadTable(entity.getBookmark(), null, entity, null, false, true);
+       protected void fireSelectionChangedEvent() {
+               ISelection selection = getSelection();
+               for (Iterator i = this.listeners.iterator(); i.hasNext();) {
+                       ISelectionChangedListener listener = (ISelectionChangedListener) i.next();
+                       listener.selectionChanged(new SelectionChangedEvent(this, selection));
+               }
+               
+               ResultSetViewer results = getSelectedResultSetViewer();
+               if (results != null) {
+                       results.updateStatusLine();
+               } else {
+                       getViewSite().getActionBars().getStatusLineManager().setMessage("");
+               }
        }
-       public void loadTable(Bookmark bookmark, TabItem tabItem, Entity entity, SQLResults results, boolean query, boolean reload) {
-        try {
-               TableAdapter adapter;
-               // If no TabItem is given we have to create a new one, with the info of the table or view.
-               if (tabItem == null) {
-                       tabItem = new TabItem(tabs, SWT.NONE);
-                       // Data is stored in a TableAdapter object
-                       if (query) {
-                               adapter = TableAdapter.createFromQuery(bookmark, results);
-                       } else {
-                               adapter = TableAdapter.createFromTable(entity);
-                       }
-                       // That is stored in the tabItem, so it won't get lost
-                       tabItem.setData(adapter);
-                       // This does not really belong here, but it'll fail if done before the creation of the
-                       // first TabItem, so it remains here till a better place found.
-                       // We set a function to change the Title of the window depending on the selected tab.
-                       tabs.addSelectionListener(new SelectionListener() {
-                               public void widgetDefaultSelected(SelectionEvent e) {
-                               }
-                               public void widgetSelected(SelectionEvent e) {
-                                       setQualifiedTitle();
-                               }
-                       });
-               } else {
-                       // If there is already a TabItem, we take its TableAdapter object
-                       adapter = (TableAdapter) tabItem.getData();
-               }
-               
-               // We create a Composite widget (main) to display our data, with a GridLayout
-               Composite main = new Composite(tabs, SWT.NONE);
-               GridLayout layout = new GridLayout(1, false);
-               layout.horizontalSpacing = 0;
-               layout.verticalSpacing = 0;
-               main.setLayout(layout);
-    
-               
-               // load widgets, the order of loading them determines the appearance in screen
-               ToolBar widgetToolBar = new ToolBar(main, SWT.HORIZONTAL);
-               // We fill up our Composite widget, the main table display, etc.
-               final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
-               final Label label = new Label(main, SWT.NULL);
-               TableViewToolBar toolBar = new TableViewToolBar(this, widgetToolBar, table, adapter, label);
-               
-       
-               // load table
-               if (reload) {
-                       adapter.resetOffset();
-                       adapter.loadData();
-               }
-               // Load the table data from the adapter into the widget
-               adapter.loadTable(table);
-               // Experimental, won't make it into 2.2
-               // final TableViewer viewer = adapter.addTableViewer(table);
-               
-               String tableName = adapter.getTable();
-               if (tableName != null) {
-                       tabItem.setText(bookmark.getName() + ":" + tableName);
-               } else {
-                       tabItem.setText(bookmark.getName() + ": SQL");
-                       tabItem.setToolTipText(bookmark.getName() + ":\n" + adapter.getQuery());
-               }
-    
-               toolBar.getPrevious().setEnabled(adapter.hasPreviousPage());
-               toolBar.getNext().setEnabled(adapter.hasNextPage());
-               label.setText(adapter.getStatusString());
-               
-               GridData gridData = new GridData();
-               gridData.horizontalAlignment = GridData.FILL;
-               gridData.verticalAlignment = GridData.FILL;
-               gridData.grabExcessHorizontalSpace = true;
-               gridData.grabExcessVerticalSpace = true;
-               table.setLayoutData(gridData);
-    
-               gridData = new GridData();
-               gridData.horizontalAlignment = GridData.FILL;
-               label.setLayoutData(gridData);
-    
-               
-               toolBar.setColumns(this, adapter, table);
-               final TableAdapter ta = adapter;
-               final Action defaultEncodingAction = new DefaultEncodingAction(ta);
-               defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
-               final Action UTF8EncodingAction = new Action() {
-                       public void run() {
-                               ta.setEncoding(TableAdapter.UTF_8);
-                       }
-               };
-               UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
-               final Action UTF16EncodingAction = new Action() {
-                       public void run() {
-                               ta.setEncoding(TableAdapter.UTF_16);
-                       }
-               };
-               UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
-    
-               IMenuListener menuListener = new TableViewMenuListener(this, table, UTF16EncodingAction, ta, defaultEncodingAction, UTF8EncodingAction, extensionVector);
-    
-               // final setup
-               MenuManager manager = new MenuManager();
-               manager.setRemoveAllWhenShown(true);
-               Menu fTextContextMenu = manager.createContextMenu(table);
-               table.setMenu(fTextContextMenu);
-               table.setLinesVisible(true);
-               manager.addMenuListener(menuListener);
-               
-               tabItem.setControl(main);
-               tabs.setSelection(tabs.indexOf(tabItem));
-    
-               setQualifiedTitle();            
-        } catch (NotConnectedException e) {
-            e.printStackTrace();
-            handleException(e);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+
+       public ISelection getSelection() {
+               SQLResultSetResults selection = getSelectedResultSet();
+               return selection == null 
+                       ? new StructuredSelection() 
+                       : new StructuredSelection(selection);
        }
-    
-    protected void handleException(Exception e) {
-        ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
-    }
 
        /**
         * @return
         */
+       protected SQLResultSetResults getSelectedResultSet() {
+               ResultSetViewer viewer = getSelectedResultSetViewer();
+               return viewer == null ? null : viewer.getResultSet();
+       }
+
+       protected ResultSetViewer getSelectedResultSetViewer() {
+               ResultSetViewer selection = null;
+               int index = this.tabs.getSelectionIndex();
+               if (index >= 0) {
+                       TabItem item = this.tabs.getItem(index);
+                       for (Iterator i = this.resultSetViewers.iterator(); 
+                                       selection == null && i.hasNext();) {
+                               ResultSetViewer viewer = (ResultSetViewer) i.next();
+                               if (item == viewer.getTabItem()) {
+                                       selection = viewer;
+                               }
+                       }
+               }
+               return selection;
+       }
+
+
+       public void removeSelectionChangedListener(ISelectionChangedListener listener) {
+               this.listeners.remove(listener);
+       }
+
+       public void setSelection(ISelection selection) {
+       }
+
+       public void propertyChange(PropertyChangeEvent event) {
+               if ("resultSets".equals(event.getPropertyName())) {
+                       SQLResultSetResults selection = getSelectedResultSet();
+                       
+                       Collection additions = getAddedResultSets();
+                       for (Iterator i = additions.iterator(); i.hasNext();) {
+                               SQLResultSetResults results = (SQLResultSetResults) i.next();
+                               this.resultSetViewers.add(new ResultSetViewer(this, results));
+                       }
+                       
+                       Collection deletions = getRemovedResultSets(); 
+                       for (Iterator i = deletions.iterator(); i.hasNext();) {
+                               SQLResultSetResults results = (SQLResultSetResults) i.next();
+                               
+                               ResultSetViewer viewer = findViewerFor(results);
+                               this.resultSetViewers.remove(viewer);
+                               viewer.dispose();
+                       }
+                       
+                       SQLResultSetResults newSelection = getSelectedResultSet();
+                       if (selection != null && newSelection == null) {
+                               fireSelectionChangedEvent();
+                       } else if (selection == null && newSelection != null) {
+                               fireSelectionChangedEvent();
+                       } else if (selection != null && !selection.equals(newSelection)) {
+                               fireSelectionChangedEvent();
+                       }
+               }
+       }
+       
        /**
-        * Sets the title of the window to the text of the selected tab
+        * @return
         */
-       private void setQualifiedTitle() {
-               if (tabs.getSelectionIndex() < 0) return;
-               TabItem item = tabs.getItem(tabs.getSelectionIndex());
-               String defTitle = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$
-               String title = item.getText();
-               int ind = title.indexOf(Messages.getString("tableview.BookmarkSeparator")); //$NON-NLS-1$
-               if (ind > 0) defTitle = title.substring(0,ind) 
-                                                               + Messages.getString("tableview.ViewNameInitialDecoration")   //$NON-NLS-1$
-                                                               + defTitle 
-                                                               + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$
-               setTitle(defTitle);
+       private Collection getRemovedResultSets() {
+               SQLResultSetResults[] results = SQLResultSetCollection.getInstance().getResultSets();
+               Collection collection = (results == null) 
+                               ? new ArrayList() 
+                               : new ArrayList(Arrays.asList(results));
+               Collection visible = getResultSets();
+               visible.removeAll(collection);
+               return visible;
        }
 
-       public void createPartControl(Composite parent) {
-               this.parent = parent;
-               initActions();
-               tabs = new TabFolder(parent, SWT.NONE);
+       private Collection getAddedResultSets() {
+               SQLResultSetResults[] results = SQLResultSetCollection.getInstance().getResultSets();
+               Collection collection = (results == null) 
+                               ? new ArrayList() 
+                               : new ArrayList(Arrays.asList(results));
+               collection.removeAll(getResultSets());
+               return collection;
        }
        
-       public void initActions() {
-               extensionVector = new Vector();
-               try {
-                       ProcessServiceMembers.process(this, extensionVector);
-               } catch (WorkbenchException e) {
-                       e.printStackTrace();
+       private Collection getResultSets() {
+               List list = new ArrayList();
+               for (Iterator i = this.resultSetViewers.iterator(); i.hasNext();) {
+                       ResultSetViewer viewer = (ResultSetViewer) i.next();
+                       list.add(viewer.getResultSet());
                }
-
+               return list;
        }
-
-       public void selectionChanged(IWorkbenchPart part, ISelection selection) {
-
+       
+       private ResultSetViewer findViewerFor(SQLResultSetResults results) {
+               ResultSetViewer viewer = null;
+               for (Iterator i = this.resultSetViewers.iterator(); viewer == null && i.hasNext();) {
+                       ResultSetViewer temp = (ResultSetViewer) i.next();
+                       if (results != null && results.equals(temp.getResultSet())) {
+                               viewer = temp;
+                       }
+               }
+               return viewer;
        }
 }
\ No newline at end of file