updating SQL plugin with latest Quantum code
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / BookmarkView.java
index 15a2d03..3363741 100644 (file)
@@ -1,9 +1,15 @@
 package net.sourceforge.phpdt.sql.view;
 
-import java.util.Vector;
 
-import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
 
+import org.apache.crimson.tree.XmlDocument;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IMenuListener;
 import org.eclipse.jface.action.IMenuManager;
@@ -15,6 +21,9 @@ import org.eclipse.jface.viewers.IDoubleClickListener;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
@@ -24,344 +33,458 @@ import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.part.ViewPart;
 
+import net.sourceforge.phpdt.sql.Messages;
+import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
 import net.sourceforge.phpdt.sql.actions.ConnectAction;
+import net.sourceforge.phpdt.sql.actions.DeleteAllRowsAction;
 import net.sourceforge.phpdt.sql.actions.DeleteBookmarkAction;
 import net.sourceforge.phpdt.sql.actions.DisconnectAction;
 import net.sourceforge.phpdt.sql.actions.EditBookmarkAction;
+import net.sourceforge.phpdt.sql.actions.ExportXMLAction;
 import net.sourceforge.phpdt.sql.actions.NewBookmarkAction;
 import net.sourceforge.phpdt.sql.actions.NextSequenceAction;
 import net.sourceforge.phpdt.sql.actions.RefreshBookmarkAction;
 import net.sourceforge.phpdt.sql.actions.ViewTableAction;
 import net.sourceforge.phpdt.sql.actions.ViewTableDetailsAction;
-import net.sourceforge.phpdt.sql.adapters.FeatureNotSupported;
-import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException;
+import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
+import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
+import net.sourceforge.phpdt.sql.model.Entity;
+import net.sourceforge.phpdt.sql.model.Sequence;
+import net.sourceforge.phpdt.sql.model.Table;
+import net.sourceforge.phpdt.sql.model.View;
+import net.sourceforge.phpdt.sql.sql.DbElementsConstants;
 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
-import net.sourceforge.phpdt.sql.sql.SQLHelper;
 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider;
 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
+import net.sourceforge.phpdt.sql.view.bookmark.GroupNode;
 import net.sourceforge.phpdt.sql.view.bookmark.Root;
 import net.sourceforge.phpdt.sql.view.bookmark.SequenceNode;
 import net.sourceforge.phpdt.sql.view.bookmark.TableNode;
+import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
 import net.sourceforge.phpdt.sql.view.bookmark.ViewNode;
 
 public class BookmarkView extends ViewPart {
-  private ConnectAction connectAction;
-  private DisconnectAction disconnectAction;
-  private NewBookmarkAction newBookmarkAction;
-  private EditBookmarkAction editBookmarkAction;
-  private DeleteBookmarkAction deleteBookmarkAction;
-  private ViewTableAction viewTableAction;
-  private RefreshBookmarkAction refreshBookmarkAction;
-  private ViewTableDetailsAction viewTableDetailsAction;
-  private NextSequenceAction nextSequenceAction;
-  private Action enableTableSizes;
-  private Action enableTableColumns;
-  private static BookmarkView instance = null;
-  private TreeViewer treeViewer;
-  private BookmarkContentProvider provider =
-    BookmarkContentProvider.getInstance();
-  private Label status;
-  private SQLHelper helper = new SQLHelper();
-  private boolean showTableSizes = false;
-  private boolean showTableColumns = false;
-  private BookmarkNode clipboard;
-  public synchronized static BookmarkView getInstance() {
-    return instance;
-  }
-  public Object getCurrent() {
-    return ((StructuredSelection) treeViewer.getSelection()).getFirstElement();
-  }
-  public BookmarkNode getCurrentBookmark() {
-    return (BookmarkNode) getCurrent();
-  }
-  public TableNode getCurrentTable() {
-    return (TableNode) getCurrent();
-  }
-  public void deleteCurrent() {
-    provider.removeBookmark(getCurrentBookmark());
-    treeViewer.refresh();
-  }
-  public void refreshBookmarkData() {
-    BookmarkNode current =
-      (BookmarkNode) MultiSQLServer.getInstance().getConnected();
-    BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
-    LogProxy log = LogProxy.getInstance();
-    if (current != null) {
-      try {
-        Vector children = new Vector();
-        setStatus(Messages.getString("bookmarkview.retrieveTableData")); //$NON-NLS-1$
-        Vector tables = new Vector();
-        try {
-          tables = helper.getTableList(current);
-          if (showTableSizes || showTableColumns) {
-            for (int i = 0; i < tables.size(); i++) {
-              TableNode table = (TableNode) tables.elementAt(i);
-              String name =
-                SQLHelper.getFullTableName(current, table.getName());
-              if (showTableSizes) {
-                int size = helper.getSize(name);
-                table.setSize(size);
-              }
-              if (showTableColumns) {
-                Vector metaData = helper.getMetaData(table, name);
-                table.setMetadata(metaData);
-              }
-            }
-          }
-        } catch (FeatureNotSupported e) {
-          setStatus(Messages.getString("bookmarkview.noTables")); //$NON-NLS-1$
-        }
-        setStatus(Messages.getString("bookmarkview.retrieveViewData")); //$NON-NLS-1$
-        Vector views = new Vector();
+       private ConnectAction connectAction;
+       private DisconnectAction disconnectAction;
+       private NewBookmarkAction newBookmarkAction;
+       private EditBookmarkAction editBookmarkAction;
+       private DeleteBookmarkAction deleteBookmarkAction;
+       private ViewTableAction viewTableAction;
+       private ExportXMLAction exportXMLAction;
+       private RefreshBookmarkAction refreshBookmarkAction;
+       private ViewTableDetailsAction viewTableDetailsAction;
+       private NextSequenceAction nextSequenceAction;
+
+    private DeleteAllRowsAction deleteAllRowsAction;
+
+       private Action enableTableSizes;
+       private static BookmarkView instance = null;
+       private TreeViewer treeViewer;
+       private BookmarkContentProvider provider =
+               BookmarkContentProvider.getInstance();
+       private Label status;
+       private boolean showTableSizes = false;
+       private BookmarkNode clipboard;
+       private Clipboard sysClip;
+       public synchronized static BookmarkView getInstance() {
+               return instance;
+       }
+       /**
+        * Returns the current selected object in the tree. If it's a multiple selection, return the first.
+        * @return
+        */
+       public Object getCurrent() {
+               if (treeViewer == null) return null;
+               return ((StructuredSelection) treeViewer.getSelection())
+                                       .getFirstElement();
+       }
+       /**
+        * Returns the current selected objects in the tree, in the form of a StructuredSelection.
+        * @return
+        */
+       public StructuredSelection getSelection() {
+               if (treeViewer == null) return null;
+               return ((StructuredSelection) treeViewer.getSelection());
+       }
+       
+       /** 
+        * Navigates the tree to get the current bookmark (root) of the selected element.
+        * If it's a multiple selection, it takes the first one.
+        * @return
+        */
+       public BookmarkNode getCurrentBookmark() {
+               TreeNode current = (TreeNode) getCurrent();
+               
+               return getRoot(current);
+       }
+
+       public static BookmarkNode getRoot(TreeNode node){
+               while (!( node instanceof BookmarkNode))
+               {
+                       node = (TreeNode) node.getParent();
+               }
+               return (BookmarkNode) node;
+
+       }
+       public void deleteCurrent() {
+               provider.removeBookmark(getCurrentBookmark());
+               treeViewer.refresh();
+       }
+       
+       /**
+        * Requeries the database to get the Data from all the bookmarks
+        */
+       public void refreshBookmarkData(){
+               if (treeViewer == null) return;
+               BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
+               Object[] objects = provider.getElements(Root.ROOT);
+               // We make a loop for all the bookmarks, and refresh each one
+               for (int i = 0; i < objects.length; i++) {
+                       BookmarkNode current = (BookmarkNode) objects[i];
+                       if (current.isConnected()) refreshOneBookmarkData(current);
+               }
+       }
+       
+       /**
+        * Requeries the database to get the Tables, Views, etc. from a Bookmark
+        * @param current       The BookmarkNode to be requeried and refreshed
+        */
+       public void refreshOneBookmarkData(BookmarkNode current) {
         try {
-          views = helper.getViewList(current);
-          if (showTableSizes || showTableColumns) {
-            for (int i = 0; i < views.size(); i++) {
-              ViewNode view = (ViewNode) views.elementAt(i);
-              String name = SQLHelper.getFullTableName(current, view.getName());
-              if (showTableSizes) {
-                int size = helper.getSize(name);
-                view.setSize(size);
-              }
-              if (showTableColumns) {
-                Vector metaData = helper.getMetaData(view, name);
-                view.setMetadata(metaData);
-              }
+               BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
+               if (current != null) {
+                
+                Map groups = new HashMap();
+                DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(current.getType());
+                
+                setStatus(Messages.getString("bookmarkview.retrieveEntities")); //$NON-NLS-1$
+                Entity[] entities = adapter.getEntities(current);
+                for (int i = 0, length = (entities == null) ? 0 : entities.length; i < length; i++) {
+                    
+                    Entity entity = entities[i];
+                    String type = entity.getType();
+                    
+                    if (!groups.containsKey(type)) {
+                        groups.put(type, new GroupNode(current, type));
+                    }
+                    GroupNode group = (GroupNode) groups.get(type);
+                    TreeNode treeNode = null;
+                    if (type.equals(DbElementsConstants.Table)) {
+                        treeNode = new TableNode(group, this.showTableSizes, (Table) entity);
+                    } else if (type.equals(DbElementsConstants.View)) { 
+                        treeNode = new ViewNode(group, this.showTableSizes, (View) entity); 
+                    } else if (type.equals(DbElementsConstants.Sequence)) {
+                        treeNode = new SequenceNode(group, (Sequence) entity);
+                    }
+                    
+                    if (treeNode != null) {
+                        group.addChild(treeNode);
+                    }
+                }
+                
+                Vector children = new Vector(groups.values());
+                Collections.sort(children);
+                
+                       provider.setChildren(current, children);
+                       BookmarkView.getInstance().refresh();
+                       setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
             }
-          }
-        } catch (FeatureNotSupported e) {
-          setStatus(Messages.getString("bookmarkview.noViews")); //$NON-NLS-1$
-        }
-        children.addAll(tables);
-        children.addAll(views);
-        setStatus(Messages.getString("bookmarkview.retrieveSeqData")); //$NON-NLS-1$
-        try {
-          children.addAll(helper.getSequenceList(current));
-        } catch (FeatureNotSupported e) {
-          setStatus(Messages.getString("bookmarkview.noSequences")); //$NON-NLS-1$
+        } catch (SQLException e) {
+            LogProxy.getInstance().addText(LogProxy.ERROR, e);
         }
-        provider.setChildren(current, children);
-        BookmarkView.getInstance().refresh();
-        setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
-      } catch (NoSuchAdapterException e) {
-        setStatus(Messages.getString("bookmarkview.metaDataError")); //$NON-NLS-1$
-        log.addText(SQLLogView.ERROR, "Error occurred: " + e.toString()); //$NON-NLS-1$
-        e.printStackTrace();
-      }
-    }
-  }
-  public void expandCurrent(BookmarkNode node) {
-    treeViewer.setExpandedState(node, true);
-    treeViewer.refresh(node, false);
-  }
-  public void refresh() {
-    treeViewer.refresh();
-  }
-  public void disconnect() {
-    MultiSQLServer server = MultiSQLServer.getInstance();
-    BookmarkNode current = (BookmarkNode) server.getConnected();
-    if (current != null) {
-      setStatus(Messages.getString("bookmarkview.disconnect") + current.getName()); //$NON-NLS-1$
-      MultiSQLServer.getInstance().disconnect(current);
-      BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
-      provider.setChildren(current, new Vector());
-      BookmarkView.getInstance().refresh();
-      setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
-    }
-  }
-  public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
-    instance = this;
-    initActions();
-    Composite main = new Composite(parent, SWT.NONE);
-    GridLayout layout = new GridLayout(1, false);
-    layout.horizontalSpacing = 0;
-    layout.verticalSpacing = 0;
-    main.setLayout(layout);
+       }
 
-    enableTableSizes = new Action() {
-      public void run() {
-        showTableSizes = enableTableSizes.isChecked();
-        refreshBookmarkData();
-      }
-    };
-    enableTableSizes.setText("Show Table Sizes");
-    enableTableSizes.setChecked(false);
+       public void expandCurrent(BookmarkNode node) {
+               treeViewer.setExpandedState(node, true);
+               treeViewer.refresh(node, false);
+       }
+       public void refresh() {
+               treeViewer.refresh();
+       }
+       public void disconnect() {
+               //Changed for multiple connections
+               BookmarkNode current = getCurrentBookmark();
+               if (current != null) {
+                       setStatus(Messages.getString("bookmarkview.disconnect") + current.getName()); //$NON-NLS-1$
+                       MultiSQLServer.getInstance().disconnect(current, current.getConnection());
+                       BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
+                       provider.setChildren(current, new Vector());
+                       BookmarkView.getInstance().refresh();
+                       setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
+               }
+       }
+       public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
+               instance = this;
+               initActions();
+               sysClip = new Clipboard(getSite().getShell().getDisplay());
+               Composite main = new Composite(parent, SWT.NONE);
+               GridLayout layout = new GridLayout(1, false);
+               layout.horizontalSpacing = 0;
+               layout.verticalSpacing = 0;
+               main.setLayout(layout);
 
-    enableTableColumns = new Action() {
-      public void run() {
-        showTableColumns = enableTableColumns.isChecked();
-        refreshBookmarkData();
-      }
-    };
-    enableTableColumns.setText("Show Table Column Data");
-    enableTableColumns.setChecked(false);
+               enableTableSizes = new Action() {
+                       public void run() {
+                               showTableSizes = enableTableSizes.isChecked();
+                               refreshBookmarkData();
+                       }
+               };
+               enableTableSizes.setText(Messages.getString("BookmarkView.ShowTableSizes")); //$NON-NLS-1$
+               enableTableSizes.setChecked(false);
+               
+               IActionBars actionBars = getViewSite().getActionBars();
+               actionBars.getMenuManager().add(enableTableSizes);
+       
+               treeViewer = new TreeViewer(main);
+               treeViewer.setContentProvider(provider);
+               treeViewer.setLabelProvider(new BookmarkLabelProvider());
+               treeViewer.setInput(Root.ROOT);
+               MenuManager manager = new MenuManager();
+               manager.setRemoveAllWhenShown(true);
+               Menu fTextContextMenu =
+                       manager.createContextMenu(treeViewer.getControl());
+               treeViewer.getControl().setMenu(fTextContextMenu);
+               treeViewer.addDoubleClickListener(new IDoubleClickListener() {
+                       public void doubleClick(DoubleClickEvent event) {
+                               Object sel = getCurrent();
+                               if (sel instanceof BookmarkNode) {
+                                       BookmarkNode node = (BookmarkNode) sel;
+                                       if (!node.isConnected()) {
+                                               connectAction.run();
+                                       }
+                               } else if (sel instanceof TableNode || sel instanceof ViewNode) {
+                                       viewTableAction.run();
+                               }
+                       }
+               });
+               manager.addMenuListener(new IMenuListener() {
+                       public void menuAboutToShow(IMenuManager mgr) {
+                               Object sel = getCurrent();
+                               if (sel instanceof BookmarkNode) {
+                                       BookmarkNode node = (BookmarkNode) sel;
+                                       if (node.isConnected()) {
+                                               mgr.add(disconnectAction);
+                                       } else {
+                                               mgr.add(connectAction);
+                                       }
+                                       mgr.add(new Separator());
+                                       mgr.add(newBookmarkAction);
+                                       mgr.add(editBookmarkAction);
+                                       mgr.add(deleteBookmarkAction);
+                                       mgr.add(new Separator());
+                                       mgr.add(copyAction);
+                                       copyAction.setText(Messages.getString("BookmarkView.Copy")); //$NON-NLS-1$
+                                       copyAction.setImageDescriptor(
+                                               PHPEclipseSQLPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
+                                       mgr.add(pasteAction);
+                                       pasteAction.setText(Messages.getString("BookmarkView.Paste")); //$NON-NLS-1$
+                                       pasteAction.setImageDescriptor(
+                                               PHPEclipseSQLPlugin.getImageDescriptor("paste.gif")); //$NON-NLS-1$
+                                       if (node.isConnected()) {
+                                               mgr.add(new Separator());
+                                               mgr.add(refreshBookmarkAction);
+                                       }
+                               } else if (sel instanceof TableNode || sel instanceof ViewNode) {
+                                       mgr.add(copyAction);
+                                       copyAction.setText(Messages.getString("BookmarkView.Copy")); //$NON-NLS-1$
+                                       copyAction.setImageDescriptor(
+                                               PHPEclipseSQLPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
+                                       mgr.add(viewTableAction);
+                                       mgr.add(exportXMLAction);
+                                       mgr.add(viewTableDetailsAction);
+                                       mgr.add(new Separator());
+                                       mgr.add(newBookmarkAction);
+                    mgr.add(new Separator());
+                    mgr.add(BookmarkView.this.deleteAllRowsAction);
+                               } else if (sel instanceof SequenceNode) {
+                                       mgr.add(nextSequenceAction);
+                                       mgr.add(new Separator());
+                                       mgr.add(newBookmarkAction);
+                               } else {
+                                       mgr.add(newBookmarkAction);
+                               }
+                       }
+               });
+               GridData gridData = new GridData();
+               gridData.horizontalAlignment = GridData.FILL;
+               gridData.verticalAlignment = GridData.FILL;
+               gridData.grabExcessHorizontalSpace = true;
+               gridData.grabExcessVerticalSpace = true;
+               treeViewer.getControl().setLayoutData(gridData);
+               status = new Label(main, SWT.NONE);
+               gridData = new GridData();
+               gridData.horizontalAlignment = GridData.FILL;
+               gridData.grabExcessHorizontalSpace = true;
+               status.setLayoutData(gridData);
+               
+               IActionBars bars = getViewSite().getActionBars();
+               bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
+               bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
+               bars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, deleteBookmarkAction);
 
-    IActionBars actionBars = getViewSite().getActionBars();
-    actionBars.getMenuManager().add(enableTableSizes);
-    actionBars.getMenuManager().add(enableTableColumns);
+               IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
+               toolBar.add(newBookmarkAction);
+               
+               status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
+       }
+       public void initActions() {
+               connectAction = new ConnectAction();
+               connectAction.setText(Messages.getString("bookmarkview.connect")); //$NON-NLS-1$
+               connectAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
+               connectAction.init(this);
+               disconnectAction = new DisconnectAction();
+               disconnectAction.setText(Messages.getString("bookmarkview.disconnect")); //$NON-NLS-1$
+               disconnectAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("stop.gif")); //$NON-NLS-1$
+               disconnectAction.init(this);
+               newBookmarkAction = new NewBookmarkAction();
+               newBookmarkAction.setText(Messages.getString("bookmarkview.newBookmark")); //$NON-NLS-1$
+               newBookmarkAction.setToolTipText(Messages.getString("bookmarkview.newBookmark")); //$NON-NLS-1$
+               newBookmarkAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("bookmarks.gif")); //$NON-NLS-1$
+               newBookmarkAction.init(this);
+               editBookmarkAction = new EditBookmarkAction();
+               editBookmarkAction.setText(Messages.getString("bookmarkview.editBookmark")); //$NON-NLS-1$
+               editBookmarkAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("edit.gif")); //$NON-NLS-1$
+               editBookmarkAction.init(this);
+               deleteBookmarkAction = new DeleteBookmarkAction();
+               deleteBookmarkAction.setText(Messages.getString("bookmarkview.deleteBookmark")); //$NON-NLS-1$
+               deleteBookmarkAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
+               deleteBookmarkAction.init(this);
+               refreshBookmarkAction = new RefreshBookmarkAction();
+               refreshBookmarkAction.setText(Messages.getString("bookmarkview.refresh")); //$NON-NLS-1$
+               refreshBookmarkAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
+               refreshBookmarkAction.init(this);
+               viewTableAction = new ViewTableAction();
+               viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
+               viewTableAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
+               viewTableAction.init(this);
+               exportXMLAction = new ExportXMLAction();
+               exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
+               exportXMLAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("xml.gif")); //$NON-NLS-1$
+               exportXMLAction.init(this);
+               viewTableDetailsAction = new ViewTableDetailsAction();
+               viewTableDetailsAction.setText(Messages.getString("bookmarkview.viewTableDetails")); //$NON-NLS-1$
+               viewTableDetailsAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
+               viewTableDetailsAction.init(this);
+               nextSequenceAction = new NextSequenceAction();
+               nextSequenceAction.setText(Messages.getString("bookmarkview.nextSequence")); //$NON-NLS-1$
+               nextSequenceAction.setImageDescriptor(
+                       PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
+               nextSequenceAction.init(this);
 
-    treeViewer = new TreeViewer(main);
-    treeViewer.setContentProvider(provider);
-    treeViewer.setLabelProvider(new BookmarkLabelProvider());
-    treeViewer.setInput(Root.ROOT);
-    MenuManager manager = new MenuManager();
-    manager.setRemoveAllWhenShown(true);
-    Menu fTextContextMenu = manager.createContextMenu(treeViewer.getControl());
-    treeViewer.getControl().setMenu(fTextContextMenu);
-    treeViewer.addDoubleClickListener(new IDoubleClickListener() {
-      public void doubleClick(DoubleClickEvent event) {
-        Object sel = getCurrent();
-        if (sel instanceof BookmarkNode) {
-          BookmarkNode node = (BookmarkNode) sel;
-          MultiSQLServer server = MultiSQLServer.getInstance();
-          if (server.getConnected() == null
-            || !node.equals(server.getConnected())) {
-            connectAction.run();
-          }
-        } else if (sel instanceof TableNode) {
-          viewTableAction.run();
-        } else if (sel instanceof ViewNode) {
-          viewTableAction.run();
-        }
-      }
-    });
-    manager.addMenuListener(new IMenuListener() {
-      public void menuAboutToShow(IMenuManager mgr) {
-        Object sel = getCurrent();
-        if (sel instanceof BookmarkNode) {
-          BookmarkNode node = (BookmarkNode) sel;
-          MultiSQLServer server = MultiSQLServer.getInstance();
-          boolean connected = false;
-          if (server.getConnected() != null
-            && node.equals(server.getConnected())) {
-            connected = true;
-          } else {
-            connected = false;
-          }
-          if (connected) {
-            mgr.add(disconnectAction);
-          } else {
-            mgr.add(connectAction);
-          }
-          mgr.add(new Separator());
-          mgr.add(newBookmarkAction);
-          mgr.add(editBookmarkAction);
-          mgr.add(deleteBookmarkAction);
-          mgr.add(new Separator());
-          mgr.add(copyAction);
-          copyAction.setText("Copy");
-          copyAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
-          mgr.add(pasteAction);
-          pasteAction.setText("Paste");
-          pasteAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("paste.gif")); //$NON-NLS-1$
-          if (connected) {
-            mgr.add(new Separator());
-            mgr.add(refreshBookmarkAction);
-          }
-        } else if (sel instanceof TableNode || sel instanceof ViewNode) {
-          mgr.add(viewTableAction);
-          mgr.add(viewTableDetailsAction);
-          mgr.add(new Separator());
-          mgr.add(newBookmarkAction);
-        } else if (sel instanceof SequenceNode) {
-          mgr.add(nextSequenceAction);
-          mgr.add(new Separator());
-          mgr.add(newBookmarkAction);
-        } else {
-          mgr.add(newBookmarkAction);
-        }
-      }
-    });
-    GridData gridData = new GridData();
-    gridData.horizontalAlignment = GridData.FILL;
-    gridData.verticalAlignment = GridData.FILL;
-    gridData.grabExcessHorizontalSpace = true;
-    gridData.grabExcessVerticalSpace = true;
-    treeViewer.getControl().setLayoutData(gridData);
-    status = new Label(main, SWT.NONE);
-    gridData = new GridData();
-    gridData.horizontalAlignment = GridData.FILL;
-    gridData.grabExcessHorizontalSpace = true;
-    status.setLayoutData(gridData);
+        this.deleteAllRowsAction = new DeleteAllRowsAction();        
+        this.deleteAllRowsAction.setText(Messages.getString("bookmarkview.deleteRowsAction"));
+        this.deleteAllRowsAction.setImageDescriptor(
+            PHPEclipseSQLPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
+        this.deleteAllRowsAction.init(this);
+       }
+       
+       public void setFocus() {
+               if (treeViewer == null) return;
+               BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
+               Object[] objects = provider.getElements(Root.ROOT);
+               for (int i = 0; i < objects.length; i++) {
+                       BookmarkNode current = (BookmarkNode) objects[i];
+                       if (current.isConnected() && (!current.hasChildren())) refreshOneBookmarkData(current);
+               }
+       }
+       
+       private Action copyAction = new Action() {
+               public void run() {
+                       Object sel = getCurrent();
+                       
+                       if (sel instanceof BookmarkNode) {
+                               clipboard = (BookmarkNode) sel;
+                               sysClip.setContents(
+                                       new Object[] { clipboard.getName()},
+                                       new Transfer[] { TextTransfer.getInstance()});
+                               
+                       } else if (sel instanceof TableNode || sel instanceof ViewNode) {
+                               XmlDocument doc = new XmlDocument();
+                               ExportXMLAction.ExportXMLSelection(doc, (StructuredSelection) treeViewer.getSelection());
+                               StringWriter text = new StringWriter();
+                               try {
+                                       doc.write(text);
+                               } catch (IOException e) {
+                                       e.printStackTrace();
+                               }
+                               String textXml = new String(text.getBuffer());
+                               sysClip.setContents(
+                                       new Object[] { textXml },
+                                       new Transfer[] { TextTransfer.getInstance()});
+
+                       }
+                       
+               }
+       };
+       private Action pasteAction = new Action() {
+               public void run() {
+                       if (clipboard != null) {
+                               BookmarkNode node = new BookmarkNode(clipboard);
+                               node.setName(Messages.getString("BookmarkView.CopyOf") + node.getName()); //$NON-NLS-1$
+                               provider.addBookmark(node);
+                               refresh();
+                       }
+               }
+       };
+       public void addNewBookmark(BookmarkNode bookmark) {
+               provider.addBookmark(bookmark);
+               treeViewer.refresh();
+       }
+       public void setStatus(String text) {
+               status.setText(text);
+       }
 
-    IActionBars bars = getViewSite().getActionBars();
-    bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
-    bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
-    bars.setGlobalActionHandler(
-      IWorkbenchActionConstants.DELETE,
-      deleteBookmarkAction);
+       /**
+        * @return
+        */
+       public BookmarkNode getClipboard() {
+               return clipboard;
+       }
+       
+       /**
+        * Finds a child of the BookmarkView with the said name 
+        * @param name
+        * @return the TreeNode found. null if none
+        */
+       public BookmarkNode find(String name){
+               if (treeViewer == null) return null;
+               BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
+               Object[] objects = provider.getElements(Root.ROOT);
+               for (int i = 0; i < objects.length; i++) {
+                       BookmarkNode current = (BookmarkNode) objects[i];
+                       if (name.equals(current.getName())) return current;
+               }
+               return null;
+       }
+       
+       /**
+        * Returs a Vector with all the elements of the treeViewer
+        * @return
+        */
+       public Vector getElements(){
+               Vector result = new Vector();
+               if (treeViewer == null) return result;
+               BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
+               Object[] objects = provider.getElements(Root.ROOT);
+               for (int i = 0; i < objects.length; i++) {
+                       BookmarkNode current = (BookmarkNode) objects[i];
+                       result.add(current);
+               }
+               return result;
+       }
 
-    IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
-    toolBar.add(newBookmarkAction);
+       public void dispose(){
+               sysClip.dispose();
+               super.dispose();
+       }
 
-    status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
-  }
-  public void initActions() {
-    connectAction = new ConnectAction();
-    connectAction.setText(Messages.getString("bookmarkview.connect")); //$NON-NLS-1$
-    connectAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
-    connectAction.init(this);
-    disconnectAction = new DisconnectAction();
-    disconnectAction.setText(Messages.getString("bookmarkview.disconnect")); //$NON-NLS-1$
-    disconnectAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("stop.gif")); //$NON-NLS-1$
-    disconnectAction.init(this);
-    newBookmarkAction = new NewBookmarkAction();
-    newBookmarkAction.setText(Messages.getString("bookmarkview.newBookmark")); //$NON-NLS-1$
-    newBookmarkAction.setToolTipText(
-      Messages.getString("bookmarkview.newBookmark"));
-    newBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("bookmarks.gif")); //$NON-NLS-1$
-    newBookmarkAction.init(this);
-    editBookmarkAction = new EditBookmarkAction();
-    editBookmarkAction.setText(Messages.getString("bookmarkview.editBookmark")); //$NON-NLS-1$
-    editBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("edit.gif")); //$NON-NLS-1$
-    editBookmarkAction.init(this);
-    deleteBookmarkAction = new DeleteBookmarkAction();
-    deleteBookmarkAction.setText(Messages.getString("bookmarkview.deleteBookmark")); //$NON-NLS-1$
-    deleteBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
-    deleteBookmarkAction.init(this);
-    refreshBookmarkAction = new RefreshBookmarkAction();
-    refreshBookmarkAction.setText(Messages.getString("bookmarkview.refresh")); //$NON-NLS-1$
-    refreshBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
-    refreshBookmarkAction.init(this);
-    viewTableAction = new ViewTableAction();
-    viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
-    viewTableAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
-    viewTableAction.init(this);
-    viewTableDetailsAction = new ViewTableDetailsAction();
-    viewTableDetailsAction.setText(Messages.getString("bookmarkview.viewTableDetails")); //$NON-NLS-1$
-    viewTableDetailsAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
-    viewTableDetailsAction.init(this);
-    nextSequenceAction = new NextSequenceAction();
-    nextSequenceAction.setText(Messages.getString("bookmarkview.nextSequence"));
-    nextSequenceAction.setImageDescriptor(
-      PHPEclipseSQLPlugin.getImageDescriptor("table.gif"));
-    nextSequenceAction.init(this);
-  }
-  public void setFocus() {
-  }
-  private Action copyAction = new Action() {
-    public void run() {
-      Object sel = getCurrent();
-      if (sel instanceof BookmarkNode) {
-        clipboard = (BookmarkNode) sel;
-      }
-    }
-  };
-  private Action pasteAction = new Action() {
-    public void run() {
-      if (clipboard != null) {
-        BookmarkNode node = new BookmarkNode(clipboard);
-        node.setName("Copy of " + node.getName());
-        provider.addBookmark(node);
-        refresh();
-      }
-    }
-  };
-  public void addNewBookmark(BookmarkNode bookmark) {
-    provider.addBookmark(bookmark);
-    treeViewer.refresh();
-  }
-  public void setStatus(String text) {
-    status.setText(text);
-  }
 }