misc changes
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / BookmarkView.java
1 package net.sourceforge.phpdt.sql.view;
2
3 import java.util.Vector;
4
5 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
6
7 import org.eclipse.jface.action.Action;
8 import org.eclipse.jface.action.IMenuListener;
9 import org.eclipse.jface.action.IMenuManager;
10 import org.eclipse.jface.action.IToolBarManager;
11 import org.eclipse.jface.action.MenuManager;
12 import org.eclipse.jface.action.Separator;
13 import org.eclipse.jface.viewers.DoubleClickEvent;
14 import org.eclipse.jface.viewers.IDoubleClickListener;
15 import org.eclipse.jface.viewers.StructuredSelection;
16 import org.eclipse.jface.viewers.TreeViewer;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Menu;
23 import org.eclipse.ui.IActionBars;
24 import org.eclipse.ui.IWorkbenchActionConstants;
25 import org.eclipse.ui.part.ViewPart;
26
27 import net.sourceforge.phpdt.sql.actions.ConnectAction;
28 import net.sourceforge.phpdt.sql.actions.DeleteBookmarkAction;
29 import net.sourceforge.phpdt.sql.actions.DisconnectAction;
30 import net.sourceforge.phpdt.sql.actions.EditBookmarkAction;
31 import net.sourceforge.phpdt.sql.actions.NewBookmarkAction;
32 import net.sourceforge.phpdt.sql.actions.NextSequenceAction;
33 import net.sourceforge.phpdt.sql.actions.RefreshBookmarkAction;
34 import net.sourceforge.phpdt.sql.actions.ViewTableAction;
35 import net.sourceforge.phpdt.sql.actions.ViewTableDetailsAction;
36 import net.sourceforge.phpdt.sql.adapters.FeatureNotSupported;
37 import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException;
38 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
39 import net.sourceforge.phpdt.sql.sql.SQLHelper;
40 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider;
41 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
42 import net.sourceforge.phpdt.sql.view.bookmark.Root;
43 import net.sourceforge.phpdt.sql.view.bookmark.SequenceNode;
44 import net.sourceforge.phpdt.sql.view.bookmark.TableNode;
45 import net.sourceforge.phpdt.sql.view.bookmark.ViewNode;
46
47 public class BookmarkView extends ViewPart {
48   private ConnectAction connectAction;
49   private DisconnectAction disconnectAction;
50   private NewBookmarkAction newBookmarkAction;
51   private EditBookmarkAction editBookmarkAction;
52   private DeleteBookmarkAction deleteBookmarkAction;
53   private ViewTableAction viewTableAction;
54   private RefreshBookmarkAction refreshBookmarkAction;
55   private ViewTableDetailsAction viewTableDetailsAction;
56   private NextSequenceAction nextSequenceAction;
57   private Action enableTableSizes;
58   private Action enableTableColumns;
59   private static BookmarkView instance = null;
60   private TreeViewer treeViewer;
61   private BookmarkContentProvider provider =
62     BookmarkContentProvider.getInstance();
63   private Label status;
64   private SQLHelper helper = new SQLHelper();
65   private boolean showTableSizes = false;
66   private boolean showTableColumns = false;
67   private BookmarkNode clipboard;
68   public synchronized static BookmarkView getInstance() {
69     return instance;
70   }
71   public Object getCurrent() {
72     return ((StructuredSelection) treeViewer.getSelection()).getFirstElement();
73   }
74   public BookmarkNode getCurrentBookmark() {
75     return (BookmarkNode) getCurrent();
76   }
77   public TableNode getCurrentTable() {
78     return (TableNode) getCurrent();
79   }
80   public void deleteCurrent() {
81     provider.removeBookmark(getCurrentBookmark());
82     treeViewer.refresh();
83   }
84   public void refreshBookmarkData() {
85     BookmarkNode current =
86       (BookmarkNode) MultiSQLServer.getInstance().getConnected();
87     BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
88     LogProxy log = LogProxy.getInstance();
89     if (current != null) {
90       try {
91         Vector children = new Vector();
92         setStatus(Messages.getString("bookmarkview.retrieveTableData")); //$NON-NLS-1$
93         Vector tables = new Vector();
94         try {
95           tables = helper.getTableList(current);
96           if (showTableSizes || showTableColumns) {
97             for (int i = 0; i < tables.size(); i++) {
98               TableNode table = (TableNode) tables.elementAt(i);
99               String name =
100                 SQLHelper.getFullTableName(current, table.getName());
101               if (showTableSizes) {
102                 int size = helper.getSize(name);
103                 table.setSize(size);
104               }
105               if (showTableColumns) {
106                 Vector metaData = helper.getMetaData(table, name);
107                 table.setMetadata(metaData);
108               }
109             }
110           }
111         } catch (FeatureNotSupported e) {
112           setStatus(Messages.getString("bookmarkview.noTables")); //$NON-NLS-1$
113         }
114         setStatus(Messages.getString("bookmarkview.retrieveViewData")); //$NON-NLS-1$
115         Vector views = new Vector();
116         try {
117           views = helper.getViewList(current);
118           if (showTableSizes || showTableColumns) {
119             for (int i = 0; i < views.size(); i++) {
120               ViewNode view = (ViewNode) views.elementAt(i);
121               String name = SQLHelper.getFullTableName(current, view.getName());
122               if (showTableSizes) {
123                 int size = helper.getSize(name);
124                 view.setSize(size);
125               }
126               if (showTableColumns) {
127                 Vector metaData = helper.getMetaData(view, name);
128                 view.setMetadata(metaData);
129               }
130             }
131           }
132         } catch (FeatureNotSupported e) {
133           setStatus(Messages.getString("bookmarkview.noViews")); //$NON-NLS-1$
134         }
135         children.addAll(tables);
136         children.addAll(views);
137         setStatus(Messages.getString("bookmarkview.retrieveSeqData")); //$NON-NLS-1$
138         try {
139           children.addAll(helper.getSequenceList(current));
140         } catch (FeatureNotSupported e) {
141           setStatus(Messages.getString("bookmarkview.noSequences")); //$NON-NLS-1$
142         }
143         provider.setChildren(current, children);
144         BookmarkView.getInstance().refresh();
145         setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
146       } catch (NoSuchAdapterException e) {
147         setStatus(Messages.getString("bookmarkview.metaDataError")); //$NON-NLS-1$
148         log.addText(SQLLogView.ERROR, "Error occurred: " + e.toString()); //$NON-NLS-1$
149         e.printStackTrace();
150       }
151     }
152   }
153   public void expandCurrent(BookmarkNode node) {
154     treeViewer.setExpandedState(node, true);
155     treeViewer.refresh(node, false);
156   }
157   public void refresh() {
158     treeViewer.refresh();
159   }
160   public void disconnect() {
161     MultiSQLServer server = MultiSQLServer.getInstance();
162     BookmarkNode current = (BookmarkNode) server.getConnected();
163     if (current != null) {
164       setStatus(Messages.getString("bookmarkview.disconnect") + current.getName()); //$NON-NLS-1$
165       MultiSQLServer.getInstance().disconnect(current);
166       BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
167       provider.setChildren(current, new Vector());
168       BookmarkView.getInstance().refresh();
169       setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
170     }
171   }
172   public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
173     instance = this;
174     initActions();
175     Composite main = new Composite(parent, SWT.NONE);
176     GridLayout layout = new GridLayout(1, false);
177     layout.horizontalSpacing = 0;
178     layout.verticalSpacing = 0;
179     main.setLayout(layout);
180
181     enableTableSizes = new Action() {
182       public void run() {
183         showTableSizes = enableTableSizes.isChecked();
184         refreshBookmarkData();
185       }
186     };
187     enableTableSizes.setText("Show Table Sizes");
188     enableTableSizes.setChecked(false);
189
190     enableTableColumns = new Action() {
191       public void run() {
192         showTableColumns = enableTableColumns.isChecked();
193         refreshBookmarkData();
194       }
195     };
196     enableTableColumns.setText("Show Table Column Data");
197     enableTableColumns.setChecked(false);
198
199     IActionBars actionBars = getViewSite().getActionBars();
200     actionBars.getMenuManager().add(enableTableSizes);
201     actionBars.getMenuManager().add(enableTableColumns);
202
203     treeViewer = new TreeViewer(main);
204     treeViewer.setContentProvider(provider);
205     treeViewer.setLabelProvider(new BookmarkLabelProvider());
206     treeViewer.setInput(Root.ROOT);
207     MenuManager manager = new MenuManager();
208     manager.setRemoveAllWhenShown(true);
209     Menu fTextContextMenu = manager.createContextMenu(treeViewer.getControl());
210     treeViewer.getControl().setMenu(fTextContextMenu);
211     treeViewer.addDoubleClickListener(new IDoubleClickListener() {
212       public void doubleClick(DoubleClickEvent event) {
213         Object sel = getCurrent();
214         if (sel instanceof BookmarkNode) {
215           BookmarkNode node = (BookmarkNode) sel;
216           MultiSQLServer server = MultiSQLServer.getInstance();
217           if (server.getConnected() == null
218             || !node.equals(server.getConnected())) {
219             connectAction.run();
220           }
221         } else if (sel instanceof TableNode) {
222           viewTableAction.run();
223         } else if (sel instanceof ViewNode) {
224           viewTableAction.run();
225         }
226       }
227     });
228     manager.addMenuListener(new IMenuListener() {
229       public void menuAboutToShow(IMenuManager mgr) {
230         Object sel = getCurrent();
231         if (sel instanceof BookmarkNode) {
232           BookmarkNode node = (BookmarkNode) sel;
233           MultiSQLServer server = MultiSQLServer.getInstance();
234           boolean connected = false;
235           if (server.getConnected() != null
236             && node.equals(server.getConnected())) {
237             connected = true;
238           } else {
239             connected = false;
240           }
241           if (connected) {
242             mgr.add(disconnectAction);
243           } else {
244             mgr.add(connectAction);
245           }
246           mgr.add(new Separator());
247           mgr.add(newBookmarkAction);
248           mgr.add(editBookmarkAction);
249           mgr.add(deleteBookmarkAction);
250           mgr.add(new Separator());
251           mgr.add(copyAction);
252           copyAction.setText("Copy");
253           copyAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
254           mgr.add(pasteAction);
255           pasteAction.setText("Paste");
256           pasteAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("paste.gif")); //$NON-NLS-1$
257           if (connected) {
258             mgr.add(new Separator());
259             mgr.add(refreshBookmarkAction);
260           }
261         } else if (sel instanceof TableNode || sel instanceof ViewNode) {
262           mgr.add(viewTableAction);
263           mgr.add(viewTableDetailsAction);
264           mgr.add(new Separator());
265           mgr.add(newBookmarkAction);
266         } else if (sel instanceof SequenceNode) {
267           mgr.add(nextSequenceAction);
268           mgr.add(new Separator());
269           mgr.add(newBookmarkAction);
270         } else {
271           mgr.add(newBookmarkAction);
272         }
273       }
274     });
275     GridData gridData = new GridData();
276     gridData.horizontalAlignment = GridData.FILL;
277     gridData.verticalAlignment = GridData.FILL;
278     gridData.grabExcessHorizontalSpace = true;
279     gridData.grabExcessVerticalSpace = true;
280     treeViewer.getControl().setLayoutData(gridData);
281     status = new Label(main, SWT.NONE);
282     gridData = new GridData();
283     gridData.horizontalAlignment = GridData.FILL;
284     gridData.grabExcessHorizontalSpace = true;
285     status.setLayoutData(gridData);
286
287     IActionBars bars = getViewSite().getActionBars();
288     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
289     bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
290     bars.setGlobalActionHandler(
291       IWorkbenchActionConstants.DELETE,
292       deleteBookmarkAction);
293
294     IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
295     toolBar.add(newBookmarkAction);
296
297     status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
298   }
299   public void initActions() {
300     connectAction = new ConnectAction();
301     connectAction.setText(Messages.getString("bookmarkview.connect")); //$NON-NLS-1$
302     connectAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
303     connectAction.init(this);
304     disconnectAction = new DisconnectAction();
305     disconnectAction.setText(Messages.getString("bookmarkview.disconnect")); //$NON-NLS-1$
306     disconnectAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("stop.gif")); //$NON-NLS-1$
307     disconnectAction.init(this);
308     newBookmarkAction = new NewBookmarkAction();
309     newBookmarkAction.setText(Messages.getString("bookmarkview.newBookmark")); //$NON-NLS-1$
310     newBookmarkAction.setToolTipText(
311       Messages.getString("bookmarkview.newBookmark"));
312     newBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("bookmarks.gif")); //$NON-NLS-1$
313     newBookmarkAction.init(this);
314     editBookmarkAction = new EditBookmarkAction();
315     editBookmarkAction.setText(Messages.getString("bookmarkview.editBookmark")); //$NON-NLS-1$
316     editBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("edit.gif")); //$NON-NLS-1$
317     editBookmarkAction.init(this);
318     deleteBookmarkAction = new DeleteBookmarkAction();
319     deleteBookmarkAction.setText(Messages.getString("bookmarkview.deleteBookmark")); //$NON-NLS-1$
320     deleteBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
321     deleteBookmarkAction.init(this);
322     refreshBookmarkAction = new RefreshBookmarkAction();
323     refreshBookmarkAction.setText(Messages.getString("bookmarkview.refresh")); //$NON-NLS-1$
324     refreshBookmarkAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
325     refreshBookmarkAction.init(this);
326     viewTableAction = new ViewTableAction();
327     viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
328     viewTableAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
329     viewTableAction.init(this);
330     viewTableDetailsAction = new ViewTableDetailsAction();
331     viewTableDetailsAction.setText(Messages.getString("bookmarkview.viewTableDetails")); //$NON-NLS-1$
332     viewTableDetailsAction.setImageDescriptor(PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
333     viewTableDetailsAction.init(this);
334     nextSequenceAction = new NextSequenceAction();
335     nextSequenceAction.setText(Messages.getString("bookmarkview.nextSequence"));
336     nextSequenceAction.setImageDescriptor(
337       PHPEclipseSQLPlugin.getImageDescriptor("table.gif"));
338     nextSequenceAction.init(this);
339   }
340   public void setFocus() {
341   }
342   private Action copyAction = new Action() {
343     public void run() {
344       Object sel = getCurrent();
345       if (sel instanceof BookmarkNode) {
346         clipboard = (BookmarkNode) sel;
347       }
348     }
349   };
350   private Action pasteAction = new Action() {
351     public void run() {
352       if (clipboard != null) {
353         BookmarkNode node = new BookmarkNode(clipboard);
354         node.setName("Copy of " + node.getName());
355         provider.addBookmark(node);
356         refresh();
357       }
358     }
359   };
360   public void addNewBookmark(BookmarkNode bookmark) {
361     provider.addBookmark(bookmark);
362     treeViewer.refresh();
363   }
364   public void setStatus(String text) {
365     status.setText(text);
366   }
367 }