KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > EditorContextMenuProvider


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.eclipse.gef.ContextMenuProvider;
34 import org.eclipse.gef.EditPart;
35 import org.eclipse.gef.EditPartViewer;
36 import org.eclipse.gef.ui.actions.ActionRegistry;
37 import org.eclipse.gef.ui.actions.GEFActionConstants;
38 import org.eclipse.jface.action.IAction;
39 import org.eclipse.jface.action.IMenuManager;
40 import org.eclipse.jface.action.MenuManager;
41 import org.eclipse.jface.action.Separator;
42 import org.eclipse.ui.actions.ActionFactory;
43
44 import org.nightlabs.editor2d.actions.CloneAction;
45 import org.nightlabs.editor2d.actions.CopyAction;
46 import org.nightlabs.editor2d.actions.CutAction;
47 import org.nightlabs.editor2d.actions.EditShapeAction;
48 import org.nightlabs.editor2d.actions.NormalSelectionAction;
49 import org.nightlabs.editor2d.actions.PasteAction;
50 import org.nightlabs.editor2d.actions.ResetRotationCenterAction;
51 import org.nightlabs.editor2d.actions.RotateAction;
52 import org.nightlabs.editor2d.actions.SelectAllWithSameName;
53 import org.nightlabs.editor2d.actions.ShowDefaultRenderAction;
54 import org.nightlabs.editor2d.actions.order.ChangeOrderOneDown;
55 import org.nightlabs.editor2d.actions.order.ChangeOrderOneUp;
56 import org.nightlabs.editor2d.actions.order.ChangeOrderToLocalBack;
57 import org.nightlabs.editor2d.actions.order.ChangeOrderToLocalFront;
58 import org.nightlabs.editor2d.actions.order.SendToLayerAction;
59 import org.nightlabs.editor2d.edit.AbstractDrawComponentEditPart;
60
61
62 public class EditorContextMenuProvider
63 extends ContextMenuProvider
64 {
65   public static final String JavaDoc CONTEXT_MENU_ID = "org.nightlabs.editor2d.outline.contextmenu";
66   
67   /** The editor's action registry. */
68   private ActionRegistry actionRegistry;
69     
70   /**
71    * Instantiate a new menu context provider for the specified EditPartViewer
72    * and ActionRegistry.
73    * @param viewer the editor's graphical viewer
74    * @param registry the editor's action registry
75    * @throws IllegalArgumentException if registry is <tt>null</tt>.
76    */

77   public EditorContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
78     super(viewer);
79     if (registry == null) {
80         throw new IllegalArgumentException JavaDoc();
81     }
82     actionRegistry = registry;
83   }
84
85   /**
86    * Called when the context menu is about to show. Actions,
87    * whose state is enabled, will appear in the context menu.
88    * @see org.eclipse.gef.ContextMenuProvider#buildContextMenu(org.eclipse.jface.action.IMenuManager)
89    */

90   public void buildContextMenu(IMenuManager manager)
91   {
92     // Add standard action groups to the menu
93
GEFActionConstants.addStandardActionGroups(manager);
94     
95     IAction action;
96
97     // Undo
98
action = getActionRegistry().getAction(ActionFactory.UNDO.getId());
99     manager.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
100
101     // Redo
102
action = getActionRegistry().getAction(ActionFactory.REDO.getId());
103     manager.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
104     
105     // Copy
106
action = getActionRegistry().getAction(CopyAction.ID);
107     if (action.isEnabled())
108         manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
109             
110     // Paste
111
action = getActionRegistry().getAction(PasteAction.ID);
112     if (action.isEnabled())
113         manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
114     
115     // Cut
116
action = getActionRegistry().getAction(CutAction.ID);
117     if (action.isEnabled())
118         manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
119         
120     // Delete
121
action = getActionRegistry().getAction(ActionFactory.DELETE.getId());
122     if (action.isEnabled())
123         manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
124
125     action = getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT);
126     if (action.isEnabled())
127         manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
128     
129     // View SubMenu
130
MenuManager viewsubmenu = new MenuManager(EditorPlugin.getResourceString("menu.view"));
131     buildViewSubMenu(viewsubmenu);
132     if (!viewsubmenu.isEmpty())
133         manager.appendToGroup(GEFActionConstants.GROUP_REST, viewsubmenu);
134     
135     // Edit SubMenu
136
MenuManager editsubmenu = new MenuManager(EditorPlugin.getResourceString("menu.edit"));
137     buildEditSubMenu(editsubmenu);
138     if (!editsubmenu.isEmpty())
139         manager.appendToGroup(GEFActionConstants.GROUP_REST, editsubmenu);
140     
141     // Alignment SubMenu
142
MenuManager alignSubMenu = new MenuManager(EditorPlugin.getResourceString("menu.align"));
143     buildAlignSubMenu(alignSubMenu);
144     if (!alignSubMenu.isEmpty())
145         manager.appendToGroup(GEFActionConstants.GROUP_REST, alignSubMenu);
146
147     // Mark SubMenu
148
MenuManager markSubMenu = new MenuManager(EditorPlugin.getResourceString("menu.mark"));
149     buildMarkSubMenu(markSubMenu);
150     if (!markSubMenu.isEmpty())
151         manager.appendToGroup(GEFActionConstants.GROUP_REST, markSubMenu);
152
153     // Order SubMenu
154
MenuManager orderSubMenu = new MenuManager(EditorPlugin.getResourceString("menu.order"));
155     buildOrderSubMenu(orderSubMenu);
156     if (!orderSubMenu.isEmpty())
157         manager.appendToGroup(GEFActionConstants.GROUP_REST, orderSubMenu);
158     
159 // action = getActionRegistry().getAction(ActionFactory.SAVE.getId());
160
// manager.appendToGroup(GEFActionConstants.GROUP_SAVE, action);
161
}
162
163   protected IAction getAction(String JavaDoc actionId) {
164     return actionRegistry.getAction(actionId);
165   }
166
167   protected ActionRegistry getActionRegistry() {
168     return actionRegistry;
169   }
170
171   protected void setActionRegistry(ActionRegistry registry) {
172     actionRegistry = registry;
173   }
174     
175   protected void buildViewSubMenu(MenuManager menuMan)
176   {
177     IAction action = getActionRegistry().getAction(ShowDefaultRenderAction.ID);
178     if (action.isEnabled())
179       menuMan.add(action);
180   }
181
182   protected void buildOrderSubMenu(MenuManager menuMan)
183   {
184     IAction action = getActionRegistry().getAction(ChangeOrderToLocalFront.ID);
185     if (action.isEnabled())
186       menuMan.add(action);
187
188     action = getActionRegistry().getAction(ChangeOrderOneUp.ID);
189     if (action.isEnabled())
190       menuMan.add(action);
191
192     action = getActionRegistry().getAction(ChangeOrderOneDown.ID);
193     if (action.isEnabled())
194       menuMan.add(action);
195     
196     action = getActionRegistry().getAction(ChangeOrderToLocalBack.ID);
197     if (action.isEnabled())
198       menuMan.add(action);
199     
200     menuMan.add(new Separator());
201     
202     MenuManager sendToLayerSubMenu = new MenuManager(EditorPlugin.getResourceString("menu.sendToLayer"));
203     buildSendToLayerSubMenu(sendToLayerSubMenu);
204     if (!sendToLayerSubMenu.isEmpty())
205         menuMan.appendToGroup(GEFActionConstants.GROUP_REST, sendToLayerSubMenu);
206   }
207   
208   protected void buildSendToLayerSubMenu(MenuManager menuMan)
209   {
210     if (getViewer() instanceof AbstractEditor)
211     {
212         AbstractEditor editor = (AbstractEditor) getViewer();
213         List JavaDoc layers = editor.getMultiLayerDrawComponent().getDrawComponents();
214         for (Iterator JavaDoc it = layers.iterator(); it.hasNext(); )
215         {
216             Layer l = (Layer) it.next();
217             IAction action = new SendToLayerAction(editor, l);
218         if (action.isEnabled())
219           menuMan.add(action);
220         }
221     }
222 // EditPart content = getViewer().getContents();
223
// if (content instanceof AbstractDrawComponentEditPart)
224
// {
225
// AbstractDrawComponentEditPart dcep = (AbstractDrawComponentEditPart) content;
226
// List layers = dcep.getModelRoot().getMultiLayerDrawComponent().getDrawComponents();
227
// for (Iterator it = layers.iterator(); it.hasNext(); ) {
228
// IAction action = new SendToLayerAction();
229
// if (action.isEnabled())
230
// menuMan.add(action);
231
// }
232
// }
233
}
234   
235   protected void buildEditSubMenu(MenuManager menuMan)
236   {
237     IAction action = getActionRegistry().getAction(CloneAction.ID);
238     if (action.isEnabled())
239       menuMan.add(action);
240         
241     // Edit Shape Action
242
action = getActionRegistry().getAction(EditShapeAction.ID);
243     if (action.isEnabled())
244       menuMan.add(action);
245
246     // Rotate Action
247
action = getActionRegistry().getAction(RotateAction.ID);
248     if (action.isEnabled())
249       menuMan.add(action);
250         
251     // Normal Selection Action
252
action = getActionRegistry().getAction(NormalSelectionAction.ID);
253     if (action.isEnabled())
254       menuMan.add(action);
255     
256     // Reset Rotation Center Action
257
action = getActionRegistry().getAction(ResetRotationCenterAction.ID);
258 // if (action.isEnabled())
259
// manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
260
menuMan.add(action);
261   }
262   
263   protected void buildAlignSubMenu(MenuManager menuMan)
264   {
265     IAction action = getActionRegistry().getAction(GEFActionConstants.ALIGN_LEFT);
266     if (action.isEnabled())
267       menuMan.add(action);
268
269     action = getActionRegistry().getAction(GEFActionConstants.ALIGN_CENTER);
270     if (action.isEnabled())
271       menuMan.add(action);
272
273     action = getActionRegistry().getAction(GEFActionConstants.ALIGN_RIGHT);
274     if (action.isEnabled())
275       menuMan.add(action);
276         
277     menuMan.add(new Separator());
278     
279     action = getActionRegistry().getAction(GEFActionConstants.ALIGN_TOP);
280     if (action.isEnabled())
281       menuMan.add(action);
282
283     action = getActionRegistry().getAction(GEFActionConstants.ALIGN_MIDDLE);
284     if (action.isEnabled())
285       menuMan.add(action);
286
287     action = getActionRegistry().getAction(GEFActionConstants.ALIGN_BOTTOM);
288     if (action.isEnabled())
289       menuMan.add(action);
290   }
291   
292   protected void buildMarkSubMenu(MenuManager menuMan)
293   {
294     IAction action;
295     action = getActionRegistry().getAction(SelectAllWithSameName.ID);
296     menuMan.add(action);
297   }
298 }
299
Popular Tags