1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / BookmarkView.java
1 package net.sourceforge.phpdt.sql.view;
2
3
4 import java.io.IOException;
5 import java.io.StringWriter;
6 import java.sql.SQLException;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Vector;
11
12 import org.apache.crimson.tree.XmlDocument;
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IMenuListener;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.action.Separator;
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.IDoubleClickListener;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.TreeViewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.dnd.Clipboard;
25 import org.eclipse.swt.dnd.TextTransfer;
26 import org.eclipse.swt.dnd.Transfer;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Menu;
32 import org.eclipse.ui.IActionBars;
33 import org.eclipse.ui.IWorkbenchActionConstants;
34 import org.eclipse.ui.part.ViewPart;
35
36 import net.sourceforge.phpdt.sql.Messages;
37 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
38 import net.sourceforge.phpdt.sql.actions.ConnectAction;
39 import net.sourceforge.phpdt.sql.actions.DeleteAllRowsAction;
40 import net.sourceforge.phpdt.sql.actions.DeleteBookmarkAction;
41 import net.sourceforge.phpdt.sql.actions.DisconnectAction;
42 import net.sourceforge.phpdt.sql.actions.EditBookmarkAction;
43 import net.sourceforge.phpdt.sql.actions.ExportXMLAction;
44 import net.sourceforge.phpdt.sql.actions.NewBookmarkAction;
45 import net.sourceforge.phpdt.sql.actions.NextSequenceAction;
46 import net.sourceforge.phpdt.sql.actions.RefreshBookmarkAction;
47 import net.sourceforge.phpdt.sql.actions.ViewTableAction;
48 import net.sourceforge.phpdt.sql.actions.ViewTableDetailsAction;
49 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
50 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
51 import net.sourceforge.phpdt.sql.model.Entity;
52 import net.sourceforge.phpdt.sql.model.Sequence;
53 import net.sourceforge.phpdt.sql.model.Table;
54 import net.sourceforge.phpdt.sql.model.View;
55 import net.sourceforge.phpdt.sql.sql.DbElementsConstants;
56 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
57 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider;
58 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
59 import net.sourceforge.phpdt.sql.view.bookmark.GroupNode;
60 import net.sourceforge.phpdt.sql.view.bookmark.Root;
61 import net.sourceforge.phpdt.sql.view.bookmark.SequenceNode;
62 import net.sourceforge.phpdt.sql.view.bookmark.TableNode;
63 import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
64 import net.sourceforge.phpdt.sql.view.bookmark.ViewNode;
65
66 public class BookmarkView extends ViewPart {
67         private ConnectAction connectAction;
68         private DisconnectAction disconnectAction;
69         private NewBookmarkAction newBookmarkAction;
70         private EditBookmarkAction editBookmarkAction;
71         private DeleteBookmarkAction deleteBookmarkAction;
72         private ViewTableAction viewTableAction;
73         private ExportXMLAction exportXMLAction;
74         private RefreshBookmarkAction refreshBookmarkAction;
75         private ViewTableDetailsAction viewTableDetailsAction;
76         private NextSequenceAction nextSequenceAction;
77
78     private DeleteAllRowsAction deleteAllRowsAction;
79
80         private Action enableTableSizes;
81         private static BookmarkView instance = null;
82         private TreeViewer treeViewer;
83         private BookmarkContentProvider provider =
84                 BookmarkContentProvider.getInstance();
85         private Label status;
86         private boolean showTableSizes = false;
87         private BookmarkNode clipboard;
88         private Clipboard sysClip;
89         public synchronized static BookmarkView getInstance() {
90                 return instance;
91         }
92         /**
93          * Returns the current selected object in the tree. If it's a multiple selection, return the first.
94          * @return
95          */
96         public Object getCurrent() {
97                 if (treeViewer == null) return null;
98                 return ((StructuredSelection) treeViewer.getSelection())
99                                         .getFirstElement();
100         }
101         /**
102          * Returns the current selected objects in the tree, in the form of a StructuredSelection.
103          * @return
104          */
105         public StructuredSelection getSelection() {
106                 if (treeViewer == null) return null;
107                 return ((StructuredSelection) treeViewer.getSelection());
108         }
109         
110         /** 
111          * Navigates the tree to get the current bookmark (root) of the selected element.
112          * If it's a multiple selection, it takes the first one.
113          * @return
114          */
115         public BookmarkNode getCurrentBookmark() {
116                 TreeNode current = (TreeNode) getCurrent();
117                 
118                 return getRoot(current);
119         }
120
121         public static BookmarkNode getRoot(TreeNode node){
122                 while (!( node instanceof BookmarkNode))
123                 {
124                         node = (TreeNode) node.getParent();
125                 }
126                 return (BookmarkNode) node;
127
128         }
129         public void deleteCurrent() {
130                 provider.removeBookmark(getCurrentBookmark());
131                 treeViewer.refresh();
132         }
133         
134         /**
135          * Requeries the database to get the Data from all the bookmarks
136          */
137         public void refreshBookmarkData(){
138                 if (treeViewer == null) return;
139                 BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
140                 Object[] objects = provider.getElements(Root.ROOT);
141                 // We make a loop for all the bookmarks, and refresh each one
142                 for (int i = 0; i < objects.length; i++) {
143                         BookmarkNode current = (BookmarkNode) objects[i];
144                         if (current.isConnected()) refreshOneBookmarkData(current);
145                 }
146         }
147         
148         /**
149          * Requeries the database to get the Tables, Views, etc. from a Bookmark
150          * @param current       The BookmarkNode to be requeried and refreshed
151          */
152         public void refreshOneBookmarkData(BookmarkNode current) {
153         try {
154                 BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
155                 if (current != null) {
156                 
157                 Map groups = new HashMap();
158                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(current.getType());
159                 
160                 setStatus(Messages.getString("bookmarkview.retrieveEntities")); //$NON-NLS-1$
161                 Entity[] entities = adapter.getEntities(current);
162                 for (int i = 0, length = (entities == null) ? 0 : entities.length; i < length; i++) {
163                     
164                     Entity entity = entities[i];
165                     String type = entity.getType();
166                     
167                     if (!groups.containsKey(type)) {
168                         groups.put(type, new GroupNode(current, type));
169                     }
170                     GroupNode group = (GroupNode) groups.get(type);
171                     TreeNode treeNode = null;
172                     if (type.equals(DbElementsConstants.Table)) {
173                         treeNode = new TableNode(group, this.showTableSizes, (Table) entity);
174                     } else if (type.equals(DbElementsConstants.View)) { 
175                         treeNode = new ViewNode(group, this.showTableSizes, (View) entity); 
176                     } else if (type.equals(DbElementsConstants.Sequence)) {
177                         treeNode = new SequenceNode(group, (Sequence) entity);
178                     }
179                     
180                     if (treeNode != null) {
181                         group.addChild(treeNode);
182                     }
183                 }
184                 
185                 Vector children = new Vector(groups.values());
186                 Collections.sort(children);
187                 
188                         provider.setChildren(current, children);
189                         BookmarkView.getInstance().refresh();
190                         setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
191             }
192         } catch (SQLException e) {
193             LogProxy.getInstance().addText(LogProxy.ERROR, e);
194         }
195         }
196
197         public void expandCurrent(BookmarkNode node) {
198                 treeViewer.setExpandedState(node, true);
199                 treeViewer.refresh(node, false);
200         }
201         public void refresh() {
202                 treeViewer.refresh();
203         }
204         public void disconnect() {
205                 //Changed for multiple connections
206                 BookmarkNode current = getCurrentBookmark();
207                 if (current != null) {
208                         setStatus(Messages.getString("bookmarkview.disconnect") + current.getName()); //$NON-NLS-1$
209                         MultiSQLServer.getInstance().disconnect(current, current.getConnection());
210                         BookmarkContentProvider provider = BookmarkContentProvider.getInstance();
211                         provider.setChildren(current, new Vector());
212                         BookmarkView.getInstance().refresh();
213                         setStatus(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
214                 }
215         }
216         public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
217                 instance = this;
218                 initActions();
219                 sysClip = new Clipboard(getSite().getShell().getDisplay());
220                 Composite main = new Composite(parent, SWT.NONE);
221                 GridLayout layout = new GridLayout(1, false);
222                 layout.horizontalSpacing = 0;
223                 layout.verticalSpacing = 0;
224                 main.setLayout(layout);
225
226                 enableTableSizes = new Action() {
227                         public void run() {
228                                 showTableSizes = enableTableSizes.isChecked();
229                                 refreshBookmarkData();
230                         }
231                 };
232                 enableTableSizes.setText(Messages.getString("BookmarkView.ShowTableSizes")); //$NON-NLS-1$
233                 enableTableSizes.setChecked(false);
234                 
235                 IActionBars actionBars = getViewSite().getActionBars();
236                 actionBars.getMenuManager().add(enableTableSizes);
237         
238                 treeViewer = new TreeViewer(main);
239                 treeViewer.setContentProvider(provider);
240                 treeViewer.setLabelProvider(new BookmarkLabelProvider());
241                 treeViewer.setInput(Root.ROOT);
242                 MenuManager manager = new MenuManager();
243                 manager.setRemoveAllWhenShown(true);
244                 Menu fTextContextMenu =
245                         manager.createContextMenu(treeViewer.getControl());
246                 treeViewer.getControl().setMenu(fTextContextMenu);
247                 treeViewer.addDoubleClickListener(new IDoubleClickListener() {
248                         public void doubleClick(DoubleClickEvent event) {
249                                 Object sel = getCurrent();
250                                 if (sel instanceof BookmarkNode) {
251                                         BookmarkNode node = (BookmarkNode) sel;
252                                         if (!node.isConnected()) {
253                                                 connectAction.run();
254                                         }
255                                 } else if (sel instanceof TableNode || sel instanceof ViewNode) {
256                                         viewTableAction.run();
257                                 }
258                         }
259                 });
260                 manager.addMenuListener(new IMenuListener() {
261                         public void menuAboutToShow(IMenuManager mgr) {
262                                 Object sel = getCurrent();
263                                 if (sel instanceof BookmarkNode) {
264                                         BookmarkNode node = (BookmarkNode) sel;
265                                         if (node.isConnected()) {
266                                                 mgr.add(disconnectAction);
267                                         } else {
268                                                 mgr.add(connectAction);
269                                         }
270                                         mgr.add(new Separator());
271                                         mgr.add(newBookmarkAction);
272                                         mgr.add(editBookmarkAction);
273                                         mgr.add(deleteBookmarkAction);
274                                         mgr.add(new Separator());
275                                         mgr.add(copyAction);
276                                         copyAction.setText(Messages.getString("BookmarkView.Copy")); //$NON-NLS-1$
277                                         copyAction.setImageDescriptor(
278                                                 PHPEclipseSQLPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
279                                         mgr.add(pasteAction);
280                                         pasteAction.setText(Messages.getString("BookmarkView.Paste")); //$NON-NLS-1$
281                                         pasteAction.setImageDescriptor(
282                                                 PHPEclipseSQLPlugin.getImageDescriptor("paste.gif")); //$NON-NLS-1$
283                                         if (node.isConnected()) {
284                                                 mgr.add(new Separator());
285                                                 mgr.add(refreshBookmarkAction);
286                                         }
287                                 } else if (sel instanceof TableNode || sel instanceof ViewNode) {
288                                         mgr.add(copyAction);
289                                         copyAction.setText(Messages.getString("BookmarkView.Copy")); //$NON-NLS-1$
290                                         copyAction.setImageDescriptor(
291                                                 PHPEclipseSQLPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
292                                         mgr.add(viewTableAction);
293                                         mgr.add(exportXMLAction);
294                                         mgr.add(viewTableDetailsAction);
295                                         mgr.add(new Separator());
296                                         mgr.add(newBookmarkAction);
297                     mgr.add(new Separator());
298                     mgr.add(BookmarkView.this.deleteAllRowsAction);
299                                 } else if (sel instanceof SequenceNode) {
300                                         mgr.add(nextSequenceAction);
301                                         mgr.add(new Separator());
302                                         mgr.add(newBookmarkAction);
303                                 } else {
304                                         mgr.add(newBookmarkAction);
305                                 }
306                         }
307                 });
308                 GridData gridData = new GridData();
309                 gridData.horizontalAlignment = GridData.FILL;
310                 gridData.verticalAlignment = GridData.FILL;
311                 gridData.grabExcessHorizontalSpace = true;
312                 gridData.grabExcessVerticalSpace = true;
313                 treeViewer.getControl().setLayoutData(gridData);
314                 status = new Label(main, SWT.NONE);
315                 gridData = new GridData();
316                 gridData.horizontalAlignment = GridData.FILL;
317                 gridData.grabExcessHorizontalSpace = true;
318                 status.setLayoutData(gridData);
319                 
320                 IActionBars bars = getViewSite().getActionBars();
321                 bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
322                 bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
323                 bars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, deleteBookmarkAction);
324
325                 IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
326                 toolBar.add(newBookmarkAction);
327                 
328                 status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
329         }
330         public void initActions() {
331                 connectAction = new ConnectAction();
332                 connectAction.setText(Messages.getString("bookmarkview.connect")); //$NON-NLS-1$
333                 connectAction.setImageDescriptor(
334                         PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
335                 connectAction.init(this);
336                 disconnectAction = new DisconnectAction();
337                 disconnectAction.setText(Messages.getString("bookmarkview.disconnect")); //$NON-NLS-1$
338                 disconnectAction.setImageDescriptor(
339                         PHPEclipseSQLPlugin.getImageDescriptor("stop.gif")); //$NON-NLS-1$
340                 disconnectAction.init(this);
341                 newBookmarkAction = new NewBookmarkAction();
342                 newBookmarkAction.setText(Messages.getString("bookmarkview.newBookmark")); //$NON-NLS-1$
343                 newBookmarkAction.setToolTipText(Messages.getString("bookmarkview.newBookmark")); //$NON-NLS-1$
344                 newBookmarkAction.setImageDescriptor(
345                         PHPEclipseSQLPlugin.getImageDescriptor("bookmarks.gif")); //$NON-NLS-1$
346                 newBookmarkAction.init(this);
347                 editBookmarkAction = new EditBookmarkAction();
348                 editBookmarkAction.setText(Messages.getString("bookmarkview.editBookmark")); //$NON-NLS-1$
349                 editBookmarkAction.setImageDescriptor(
350                         PHPEclipseSQLPlugin.getImageDescriptor("edit.gif")); //$NON-NLS-1$
351                 editBookmarkAction.init(this);
352                 deleteBookmarkAction = new DeleteBookmarkAction();
353                 deleteBookmarkAction.setText(Messages.getString("bookmarkview.deleteBookmark")); //$NON-NLS-1$
354                 deleteBookmarkAction.setImageDescriptor(
355                         PHPEclipseSQLPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
356                 deleteBookmarkAction.init(this);
357                 refreshBookmarkAction = new RefreshBookmarkAction();
358                 refreshBookmarkAction.setText(Messages.getString("bookmarkview.refresh")); //$NON-NLS-1$
359                 refreshBookmarkAction.setImageDescriptor(
360                         PHPEclipseSQLPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
361                 refreshBookmarkAction.init(this);
362                 viewTableAction = new ViewTableAction();
363                 viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
364                 viewTableAction.setImageDescriptor(
365                         PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
366                 viewTableAction.init(this);
367                 exportXMLAction = new ExportXMLAction();
368                 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
369                 exportXMLAction.setImageDescriptor(
370                         PHPEclipseSQLPlugin.getImageDescriptor("xml.gif")); //$NON-NLS-1$
371                 exportXMLAction.init(this);
372                 viewTableDetailsAction = new ViewTableDetailsAction();
373                 viewTableDetailsAction.setText(Messages.getString("bookmarkview.viewTableDetails")); //$NON-NLS-1$
374                 viewTableDetailsAction.setImageDescriptor(
375                         PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
376                 viewTableDetailsAction.init(this);
377                 nextSequenceAction = new NextSequenceAction();
378                 nextSequenceAction.setText(Messages.getString("bookmarkview.nextSequence")); //$NON-NLS-1$
379                 nextSequenceAction.setImageDescriptor(
380                         PHPEclipseSQLPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
381                 nextSequenceAction.init(this);
382
383         this.deleteAllRowsAction = new DeleteAllRowsAction();        
384         this.deleteAllRowsAction.setText(Messages.getString("bookmarkview.deleteRowsAction"));
385         this.deleteAllRowsAction.setImageDescriptor(
386             PHPEclipseSQLPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
387         this.deleteAllRowsAction.init(this);
388         }
389         
390         public void setFocus() {
391                 if (treeViewer == null) return;
392                 BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
393                 Object[] objects = provider.getElements(Root.ROOT);
394                 for (int i = 0; i < objects.length; i++) {
395                         BookmarkNode current = (BookmarkNode) objects[i];
396                         if (current.isConnected() && (!current.hasChildren())) refreshOneBookmarkData(current);
397                 }
398         }
399         
400         private Action copyAction = new Action() {
401                 public void run() {
402                         Object sel = getCurrent();
403                         
404                         if (sel instanceof BookmarkNode) {
405                                 clipboard = (BookmarkNode) sel;
406                                 sysClip.setContents(
407                                         new Object[] { clipboard.getName()},
408                                         new Transfer[] { TextTransfer.getInstance()});
409                                 
410                         } else if (sel instanceof TableNode || sel instanceof ViewNode) {
411                                 XmlDocument doc = new XmlDocument();
412                                 ExportXMLAction.ExportXMLSelection(doc, (StructuredSelection) treeViewer.getSelection());
413                                 StringWriter text = new StringWriter();
414                                 try {
415                                         doc.write(text);
416                                 } catch (IOException e) {
417                                         e.printStackTrace();
418                                 }
419                                 String textXml = new String(text.getBuffer());
420                                 sysClip.setContents(
421                                         new Object[] { textXml },
422                                         new Transfer[] { TextTransfer.getInstance()});
423
424                         }
425                         
426                 }
427         };
428         private Action pasteAction = new Action() {
429                 public void run() {
430                         if (clipboard != null) {
431                                 BookmarkNode node = new BookmarkNode(clipboard);
432                                 node.setName(Messages.getString("BookmarkView.CopyOf") + node.getName()); //$NON-NLS-1$
433                                 provider.addBookmark(node);
434                                 refresh();
435                         }
436                 }
437         };
438         public void addNewBookmark(BookmarkNode bookmark) {
439                 provider.addBookmark(bookmark);
440                 treeViewer.refresh();
441         }
442         public void setStatus(String text) {
443                 status.setText(text);
444         }
445
446         /**
447          * @return
448          */
449         public BookmarkNode getClipboard() {
450                 return clipboard;
451         }
452         
453         /**
454          * Finds a child of the BookmarkView with the said name 
455          * @param name
456          * @return the TreeNode found. null if none
457          */
458         public BookmarkNode find(String name){
459                 if (treeViewer == null) return null;
460                 BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
461                 Object[] objects = provider.getElements(Root.ROOT);
462                 for (int i = 0; i < objects.length; i++) {
463                         BookmarkNode current = (BookmarkNode) objects[i];
464                         if (name.equals(current.getName())) return current;
465                 }
466                 return null;
467         }
468         
469         /**
470          * Returs a Vector with all the elements of the treeViewer
471          * @return
472          */
473         public Vector getElements(){
474                 Vector result = new Vector();
475                 if (treeViewer == null) return result;
476                 BookmarkContentProvider provider = (BookmarkContentProvider) treeViewer.getContentProvider();
477                 Object[] objects = provider.getElements(Root.ROOT);
478                 for (int i = 0; i < objects.length; i++) {
479                         BookmarkNode current = (BookmarkNode) objects[i];
480                         result.add(current);
481                 }
482                 return result;
483         }
484
485         public void dispose(){
486                 sysClip.dispose();
487                 super.dispose();
488         }
489
490 }