1 package net.sourceforge.phpdt.sql.view;
3 import java.util.Vector;
5 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
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;
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;
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();
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() {
71 public Object getCurrent() {
72 return ((StructuredSelection) treeViewer.getSelection()).getFirstElement();
74 public BookmarkNode getCurrentBookmark() {
75 return (BookmarkNode) getCurrent();
77 public TableNode getCurrentTable() {
78 return (TableNode) getCurrent();
80 public void deleteCurrent() {
81 provider.removeBookmark(getCurrentBookmark());
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) {
91 Vector children = new Vector();
92 setStatus(Messages.getString("bookmarkview.retrieveTableData")); //$NON-NLS-1$
93 Vector tables = new Vector();
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);
100 SQLHelper.getFullTableName(current, table.getName());
101 if (showTableSizes) {
102 int size = helper.getSize(name);
105 if (showTableColumns) {
106 Vector metaData = helper.getMetaData(table, name);
107 table.setMetadata(metaData);
111 } catch (FeatureNotSupported e) {
112 setStatus(Messages.getString("bookmarkview.noTables")); //$NON-NLS-1$
114 setStatus(Messages.getString("bookmarkview.retrieveViewData")); //$NON-NLS-1$
115 Vector views = new Vector();
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);
126 if (showTableColumns) {
127 Vector metaData = helper.getMetaData(view, name);
128 view.setMetadata(metaData);
132 } catch (FeatureNotSupported e) {
133 setStatus(Messages.getString("bookmarkview.noViews")); //$NON-NLS-1$
135 children.addAll(tables);
136 children.addAll(views);
137 setStatus(Messages.getString("bookmarkview.retrieveSeqData")); //$NON-NLS-1$
139 children.addAll(helper.getSequenceList(current));
140 } catch (FeatureNotSupported e) {
141 setStatus(Messages.getString("bookmarkview.noSequences")); //$NON-NLS-1$
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$
153 public void expandCurrent(BookmarkNode node) {
154 treeViewer.setExpandedState(node, true);
155 treeViewer.refresh(node, false);
157 public void refresh() {
158 treeViewer.refresh();
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$
172 public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
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);
181 enableTableSizes = new Action() {
183 showTableSizes = enableTableSizes.isChecked();
184 refreshBookmarkData();
187 enableTableSizes.setText("Show Table Sizes");
188 enableTableSizes.setChecked(false);
190 enableTableColumns = new Action() {
192 showTableColumns = enableTableColumns.isChecked();
193 refreshBookmarkData();
196 enableTableColumns.setText("Show Table Column Data");
197 enableTableColumns.setChecked(false);
199 IActionBars actionBars = getViewSite().getActionBars();
200 actionBars.getMenuManager().add(enableTableSizes);
201 actionBars.getMenuManager().add(enableTableColumns);
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())) {
221 } else if (sel instanceof TableNode) {
222 viewTableAction.run();
223 } else if (sel instanceof ViewNode) {
224 viewTableAction.run();
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())) {
242 mgr.add(disconnectAction);
244 mgr.add(connectAction);
246 mgr.add(new Separator());
247 mgr.add(newBookmarkAction);
248 mgr.add(editBookmarkAction);
249 mgr.add(deleteBookmarkAction);
250 mgr.add(new Separator());
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$
258 mgr.add(new Separator());
259 mgr.add(refreshBookmarkAction);
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);
271 mgr.add(newBookmarkAction);
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);
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);
294 IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
295 toolBar.add(newBookmarkAction);
297 status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
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);
340 public void setFocus() {
342 private Action copyAction = new Action() {
344 Object sel = getCurrent();
345 if (sel instanceof BookmarkNode) {
346 clipboard = (BookmarkNode) sel;
350 private Action pasteAction = new Action() {
352 if (clipboard != null) {
353 BookmarkNode node = new BookmarkNode(clipboard);
354 node.setName("Copy of " + node.getName());
355 provider.addBookmark(node);
360 public void addNewBookmark(BookmarkNode bookmark) {
361 provider.addBookmark(bookmark);
362 treeViewer.refresh();
364 public void setStatus(String text) {
365 status.setText(text);