fixed ID bug
[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 addSchemaAction;
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.addSchemaAction = new AddSchemaAction(this.viewPart);
104         this.refreshAction = new RefreshBookmarkAction(this.viewPart);
105         this.renameAction = new RenameAction(this.viewPart);
106         this.copyAction = new CopyAction(this.viewPart, this, selectionProvider);
107         this.pasteAction = new PasteAction(this.viewPart, this, selectionProvider);
108         this.exportAction = new ExportResourcesAction(
109             this.viewPart.getViewSite().getWorkbenchWindow());
110         this.exportAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("export.gif"));
111         
112         this.propertiesAction = new PropertyDialogAction(
113             this.viewPart.getSite().getShell(), selectionProvider);
114     }
115
116     
117     /**
118      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
119      */
120     public void fillContextMenu(IMenuManager menu) {
121         
122         menu.add(this.newBookmarkAction);
123         menu.add(new Separator());
124         
125         if (getStructuredSelection().size() > 0 && 
126             isEverySelectionInstanceof(BookmarkNode.class)) {
127                 
128             addToMenu(menu, this.connectAction);
129             addToMenu(menu, this.disconnectAction);
130         }
131
132         menu.add(new Separator());
133         menu.add(this.copyAction);
134         // TODO: paste needs to change enablement whenever something is added
135         //       to the clipboard
136         addToMenu(menu, this.pasteAction);
137         addToMenu(menu, this.deleteBookmarkAction);
138
139         if (getStructuredSelection().size() == 1 && 
140             isEverySelectionInstanceof(BookmarkNode.class)) {
141
142             addToMenu(menu, this.renameAction);
143         }
144
145         menu.add(new Separator());
146
147         // NOTE: In Eclipse 3.0.0 M6, Export is no longer a sub-class of 
148         //       SelectionListenerAction.
149         this.exportAction.selectionChanged(getStructuredSelection());
150         menu.add(this.exportAction);
151
152         if (getStructuredSelection().size() == 1 && 
153             isEverySelectionInstanceof(BookmarkNode.class)) {
154                 
155             menu.add(new Separator());
156             addToMenu(menu, this.addSchemaAction);
157         }
158
159         if (isEverySelectionInstanceof(QueryNode.class)) {
160             if (getStructuredSelection().size() == 1) {
161                 addToMenu(menu, this.openQueryAction);
162             }
163         }
164
165         if (getStructuredSelection().size() > 0 && 
166             isEverySelectionInstanceof(EntityNode.class)) {
167                 
168             menu.add(new Separator());
169                 
170             if (getStructuredSelection().size() == 1) {
171                 if (((EntityNode) getStructuredSelection().getFirstElement()).isSequence()) {
172                     addToMenu(menu, this.nextSequenceAction);
173                     addToMenu(menu, this.previousSequenceAction);
174                 } else {
175                     addToMenu(menu, this.viewTableAction);
176                     addToMenu(menu, this.deleteAllRowsAction);
177                     addToMenu(menu, this.viewTableDetailsAction);
178                 }
179             }
180             
181             addToMenu(menu, this.addToQuickListAction);
182             addToMenu(menu, this.removeFromQuickListAction);
183         }
184         
185         if (getStructuredSelection().size() == 1) {
186             menu.add(new Separator());
187             addToMenu(menu, this.refreshAction);
188         }
189         
190         createMarkerForActionsProvidedByOtherPlugins(menu);
191         
192         if (getStructuredSelection().size() == 1) {
193             addToMenu(menu, this.propertiesAction);
194         }
195         
196     }
197     
198     private void addToMenu(IMenuManager menu, SelectionListenerAction action) {
199         action.selectionChanged(getStructuredSelection());
200         menu.add(action);
201     }
202     
203     private void addToMenu(IMenuManager menu, SelectionProviderAction action) {
204         action.selectionChanged(getStructuredSelection());
205         menu.add(action);
206     }
207     
208     
209     private IStructuredSelection getStructuredSelection() {
210         return (IStructuredSelection) getContext().getSelection();
211     }
212     
213     private boolean isEverySelectionInstanceof(Class selectedClass) {
214         boolean result = true;
215         IStructuredSelection selection = getStructuredSelection();
216         for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
217             Object object = i.next();
218             result &= selectedClass.isAssignableFrom(object.getClass());
219         }
220         
221         return result;
222     }
223     
224     public IAction getOpenAction() {
225         if (isEverySelectionInstanceof(BookmarkNode.class)) {
226             this.connectAction.selectionChanged(getStructuredSelection());
227             return this.connectAction.isEnabled() ? this.connectAction : null; 
228         } else if (isEverySelectionInstanceof(EntityNode.class)) {
229             this.viewTableAction.selectionChanged(getStructuredSelection());
230             return this.viewTableAction.isEnabled() ? this.viewTableAction : null; 
231         } else if (isEverySelectionInstanceof(QueryNode.class)) {
232             this.openQueryAction.selectionChanged(getStructuredSelection());
233             return this.openQueryAction.isEnabled() ? this.openQueryAction : null; 
234         } else {
235             return null;
236         }
237     }
238
239     private void createMarkerForActionsProvidedByOtherPlugins(IMenuManager mgr) {
240         mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
241         mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
242         mgr.add(new Separator());
243     }
244     public void fillActionBars(IActionBars actionBars) {
245         IToolBarManager toolBar = actionBars.getToolBarManager();
246         toolBar.add(this.newBookmarkAction);
247         
248         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, this.copyAction);
249     }
250
251
252     /* (non-Javadoc)
253      * @see com.quantum.view.bookmark.BookmarkClipboard#setBookmark(com.quantum.model.Bookmark)
254      */
255     public void setBookmark(Bookmark bookmark) {
256         this.bookmarkClipboard = bookmark;
257     }
258
259
260     /* (non-Javadoc)
261      * @see com.quantum.view.bookmark.BookmarkClipboard#getBookmark()
262      */
263     public Bookmark getBookmark() {
264         return this.bookmarkClipboard;
265     }
266 }