new sql.gif
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / BookmarkViewActionGroup.java
1 package com.quantum.view.bookmark;
2
3 import java.util.Iterator;
4
5 import com.quantum.ImageStore;
6 import com.quantum.actions.AddToQuickListAction;
7 import com.quantum.actions.ConnectAction;
8 import com.quantum.actions.DeleteAllRowsAction;
9 import com.quantum.actions.DeleteBookmarkAction;
10 import com.quantum.actions.DisconnectAction;
11 import com.quantum.actions.NewBookmarkAction;
12 import com.quantum.actions.NextSequenceAction;
13 import com.quantum.actions.OpenQueryAction;
14 import com.quantum.actions.PrevSequenceAction;
15 import com.quantum.actions.RefreshBookmarkAction;
16 import com.quantum.actions.RemoveFromQuickListAction;
17 import com.quantum.actions.ViewTableAction;
18 import com.quantum.actions.ViewTableDetailsAction;
19 import com.quantum.model.Bookmark;
20 import com.quantum.util.versioning.VersioningHelper;
21
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.IActionBars;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IWorkbenchActionConstants;
32 import org.eclipse.ui.actions.ActionGroup;
33 import org.eclipse.ui.actions.ExportResourcesAction;
34 import org.eclipse.ui.actions.SelectionListenerAction;
35 import org.eclipse.ui.actions.SelectionProviderAction;
36 import org.eclipse.ui.dialogs.PropertyDialogAction;
37
38 /**
39  * @author BC
40  */
41 public class BookmarkViewActionGroup extends ActionGroup 
42     implements BookmarkClipboard {
43         
44     private Bookmark bookmarkClipboard;
45     
46     private Action newBookmarkAction;
47     
48     // bookmark node actions
49     private SelectionListenerAction connectAction;
50     private SelectionListenerAction disconnectAction;
51     private SelectionListenerAction deleteBookmarkAction;
52
53     // Query node actions
54     private SelectionListenerAction openQueryAction;
55
56     // Entity node actions
57     private SelectionListenerAction addToQuickListAction;
58     private SelectionListenerAction removeFromQuickListAction;
59     private SelectionListenerAction viewTableAction;
60     private SelectionListenerAction viewTableDetailsAction;
61
62     private SelectionListenerAction nextSequenceAction;
63     private SelectionListenerAction previousSequenceAction;
64     
65     
66     // other actions
67     private SelectionListenerAction refreshAction;
68     private SelectionListenerAction renameAction;
69
70     private ExportResourcesAction exportAction;
71
72     
73     private SelectionProviderAction propertiesAction;
74     
75     private SelectionListenerAction copyAction;
76     private SelectionListenerAction pasteAction;
77     private SelectionListenerAction deleteAllRowsAction;
78     
79     private IViewPart viewPart;
80     
81     public BookmarkViewActionGroup(
82         IViewPart viewPart, ISelectionProvider selectionProvider) {
83         this.viewPart = viewPart;
84         
85         this.newBookmarkAction = new NewBookmarkAction(this.viewPart);
86         
87         // bookmark actions
88         this.connectAction = new ConnectAction(this.viewPart);
89         this.disconnectAction = new DisconnectAction(this.viewPart);
90         this.deleteBookmarkAction = new DeleteBookmarkAction(this.viewPart);
91
92         // entity actions
93         this.previousSequenceAction = new PrevSequenceAction(this.viewPart);
94         this.nextSequenceAction = new NextSequenceAction(this.viewPart);
95         this.addToQuickListAction = new AddToQuickListAction(this.viewPart);
96         this.removeFromQuickListAction = new RemoveFromQuickListAction(this.viewPart);
97         this.deleteAllRowsAction = new DeleteAllRowsAction(this.viewPart);
98         this.viewTableDetailsAction = new ViewTableDetailsAction(this.viewPart);
99
100         this.openQueryAction = new OpenQueryAction(this.viewPart);
101         
102         this.viewTableAction = new ViewTableAction(this.viewPart);
103         this.refreshAction = new RefreshBookmarkAction(this.viewPart);
104         this.renameAction = new RenameAction(this.viewPart);
105         this.copyAction = new CopyAction(this.viewPart, this, selectionProvider);
106         this.pasteAction = new PasteAction(this.viewPart, this, selectionProvider);
107         this.exportAction = VersioningHelper.createExportResourcesAction(
108                         this.viewPart.getViewSite().getWorkbenchWindow());
109         this.exportAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.EXPORT));
110         
111         this.propertiesAction = new PropertyDialogAction(
112             this.viewPart.getSite().getShell(), selectionProvider);
113     }
114
115     
116     /**
117      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
118      */
119     public void fillContextMenu(IMenuManager menu) {
120         
121         menu.add(this.newBookmarkAction);
122         menu.add(new Separator());
123         
124         if (getStructuredSelection().size() > 0 && 
125             isEverySelectionInstanceof(BookmarkNode.class)) {
126                 
127             addToMenu(menu, this.connectAction);
128             addToMenu(menu, this.disconnectAction);
129         }
130
131         menu.add(new Separator());
132         menu.add(this.copyAction);
133         // TODO: paste needs to change enablement whenever something is added
134         //       to the clipboard
135         addToMenu(menu, this.pasteAction);
136         addToMenu(menu, this.deleteBookmarkAction);
137
138         if (getStructuredSelection().size() == 1 && 
139             isEverySelectionInstanceof(BookmarkNode.class)) {
140
141             addToMenu(menu, this.renameAction);
142         }
143
144         menu.add(new Separator());
145
146         // NOTE: In Eclipse 3.0.0 M6, Export is no longer a sub-class of 
147         //       SelectionListenerAction.
148         if (this.exportAction != null) {
149                 this.exportAction.selectionChanged(getStructuredSelection());
150                 menu.add(this.exportAction);
151         }
152
153         if (getStructuredSelection().size() == 1 && 
154             isEverySelectionInstanceof(BookmarkNode.class)) {
155         }
156
157         if (isEverySelectionInstanceof(QueryNode.class)) {
158             if (getStructuredSelection().size() == 1) {
159                 addToMenu(menu, this.openQueryAction);
160             }
161         }
162
163         if (getStructuredSelection().size() > 0 && 
164             isEverySelectionInstanceof(EntityNode.class)) {
165                 
166             menu.add(new Separator());
167                 
168             if (getStructuredSelection().size() == 1) {
169                 if (((EntityNode) getStructuredSelection().getFirstElement()).isSequence()) {
170                     addToMenu(menu, this.nextSequenceAction);
171                     addToMenu(menu, this.previousSequenceAction);
172                 } else {
173                     addToMenu(menu, this.viewTableAction);
174                     addToMenu(menu, this.deleteAllRowsAction);
175                     addToMenu(menu, this.viewTableDetailsAction);
176                 }
177             }
178             
179             addToMenu(menu, this.addToQuickListAction);
180             addToMenu(menu, this.removeFromQuickListAction);
181         }
182         
183         if (getStructuredSelection().size() == 1) {
184             menu.add(new Separator());
185             addToMenu(menu, this.refreshAction);
186         }
187         
188         createMarkerForActionsProvidedByOtherPlugins(menu);
189         
190         if (getStructuredSelection().size() == 1) {
191             addToMenu(menu, this.propertiesAction);
192         }
193         
194     }
195     
196     private void addToMenu(IMenuManager menu, SelectionListenerAction action) {
197         action.selectionChanged(getStructuredSelection());
198         menu.add(action);
199     }
200     
201     private void addToMenu(IMenuManager menu, SelectionProviderAction action) {
202         action.selectionChanged(getStructuredSelection());
203         menu.add(action);
204     }
205     
206     
207     private IStructuredSelection getStructuredSelection() {
208         return (IStructuredSelection) getContext().getSelection();
209     }
210     
211     private boolean isEverySelectionInstanceof(Class selectedClass) {
212         boolean result = true;
213         IStructuredSelection selection = getStructuredSelection();
214         for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
215             Object object = i.next();
216             result &= selectedClass.isAssignableFrom(object.getClass());
217         }
218         
219         return result;
220     }
221     
222     public IAction getOpenAction() {
223         if (isEverySelectionInstanceof(BookmarkNode.class)) {
224             this.connectAction.selectionChanged(getStructuredSelection());
225             return this.connectAction.isEnabled() ? this.connectAction : null; 
226         } else if (isEverySelectionInstanceof(EntityNode.class)) {
227             this.viewTableAction.selectionChanged(getStructuredSelection());
228             return this.viewTableAction.isEnabled() ? this.viewTableAction : null; 
229         } else if (isEverySelectionInstanceof(QueryNode.class)) {
230             this.openQueryAction.selectionChanged(getStructuredSelection());
231             return this.openQueryAction.isEnabled() ? this.openQueryAction : null; 
232         } else {
233             return null;
234         }
235     }
236
237     private void createMarkerForActionsProvidedByOtherPlugins(IMenuManager mgr) {
238         mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
239         mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
240         mgr.add(new Separator());
241     }
242     public void fillActionBars(IActionBars actionBars) {
243         IToolBarManager toolBar = actionBars.getToolBarManager();
244         toolBar.add(this.newBookmarkAction);
245         
246         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, this.copyAction);
247     }
248
249
250     /* (non-Javadoc)
251      * @see com.quantum.view.bookmark.BookmarkClipboard#setBookmark(com.quantum.model.Bookmark)
252      */
253     public void setBookmark(Bookmark bookmark) {
254         this.bookmarkClipboard = bookmark;
255     }
256
257
258     /* (non-Javadoc)
259      * @see com.quantum.view.bookmark.BookmarkClipboard#getBookmark()
260      */
261     public Bookmark getBookmark() {
262         return this.bookmarkClipboard;
263     }
264 }