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