Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / actions / table / TableActionGroup.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.actions.table;\r
13 \r
14 import org.eclipse.gef.EditPart;\r
15 import org.eclipse.jface.action.Action;\r
16 import org.eclipse.jface.action.IMenuListener;\r
17 import org.eclipse.jface.action.IMenuManager;\r
18 import org.eclipse.jface.action.MenuManager;\r
19 import org.eclipse.jface.action.Separator;\r
20 import org.eclipse.jface.viewers.ISelection;\r
21 import org.eclipse.jface.viewers.IStructuredSelection;\r
22 import org.eclipse.jst.pagedesigner.PDPlugin;\r
23 import org.eclipse.jst.pagedesigner.actions.single.SelectEditPartAction;\r
24 import org.eclipse.jst.pagedesigner.editors.PageDesignerActionConstants;\r
25 import org.eclipse.jst.pagedesigner.parts.ElementEditPart;\r
26 import org.eclipse.jst.pagedesigner.range.RangeUtil;\r
27 import org.eclipse.jst.pagedesigner.tableedit.DeleteRowColumnAction;\r
28 import org.eclipse.jst.pagedesigner.tableedit.InsertRowColumnAction;\r
29 import org.eclipse.jst.pagedesigner.viewer.DesignPosition;\r
30 import org.eclipse.jst.pagedesigner.viewer.DesignRange;\r
31 import org.eclipse.ui.actions.ActionGroup;\r
32 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;\r
33 \r
34 /**\r
35  * \r
36  * @author mengbo\r
37  * @version 1.5\r
38  */\r
39 /*package*/ class TableActionGroup extends ActionGroup {\r
40         // TODO: I think there's a pattern emerging...\r
41     private final static Action action = new Action() {\r
42         // create an empty no-op\r
43         };\r
44 \r
45         /*\r
46          * (non-Javadoc)\r
47          * \r
48          * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)\r
49          */\r
50         public void fillContextMenu(IMenuManager menu) {\r
51                 ISelection selection = this.getContext().getSelection();\r
52 \r
53                 EditPart part = findCommonAncesterPart(selection);\r
54                 if (part == null) {\r
55                         return;\r
56                 }\r
57 \r
58                 TableOperationContext context = TableOperationContext\r
59                                 .getTableOperationContext(part);\r
60                 if (context == null) {\r
61                         return;\r
62                 }\r
63 \r
64                 final ElementEditPart tablePart = context.getTablePart();\r
65 \r
66                 if (tablePart == null || !supportTableActions(tablePart)) {\r
67                         return;\r
68                 }\r
69 \r
70                 final int cellRow = context.getRowIndex();\r
71                 final int cellColumn = context.getColumnIndex();\r
72 \r
73                 String tag = ((IDOMElement) tablePart.getIDOMNode()).getLocalName();\r
74                 final IMenuManager tableMenu = new MenuManager(tag);\r
75 \r
76                 // ok, we passed the checking, now let's create the actions.\r
77                 tableMenu.add(action);\r
78                 tableMenu.addMenuListener(new IMenuListener() {\r
79 \r
80                         public void menuAboutToShow(IMenuManager manager) {\r
81                                 tableMenu.removeAll();\r
82                                 fillTableMenu(tablePart, cellRow, cellColumn, tableMenu);\r
83                         }\r
84                 });\r
85                 menu.appendToGroup(PageDesignerActionConstants.GROUP_CONTAINER,\r
86                                 tableMenu);\r
87         }\r
88 \r
89         /**\r
90          * @param tablePart\r
91          * @param cellRow\r
92          * @param cellColumn\r
93          * @param tableMenu\r
94          */\r
95         private void fillTableMenu(ElementEditPart tablePart, int cellRow,\r
96                         int cellColumn, IMenuManager tableMenu) {\r
97                 SelectEditPartAction action1 = SelectEditPartAction.create(\r
98                                 PDPlugin\r
99                                                 .getResourceString("TableActionGroup.Submenu.SelectTable"), tablePart);//$NON-NLS-1$\r
100                 tableMenu.add(action1);\r
101 \r
102                 tableMenu.add(new Separator());\r
103 \r
104                 {\r
105                         InsertRowColumnAction insertRowBeforeAction = new InsertRowColumnAction(\r
106                                         PDPlugin\r
107                                                         .getResourceString("TableActionGroup.Submenu.InsertRowBefore"),//$NON-NLS-1$\r
108                                         tablePart, cellRow, true, true);\r
109                         tableMenu.add(insertRowBeforeAction);\r
110 \r
111                         InsertRowColumnAction insertRowAfterAction = new InsertRowColumnAction(\r
112                                         PDPlugin\r
113                                                         .getResourceString("TableActionGroup.Submenu.InsertRowAfter"),//$NON-NLS-1$\r
114                                         tablePart, cellRow, true, false);\r
115                         tableMenu.add(insertRowAfterAction);\r
116 \r
117                         tableMenu.add(new Separator());\r
118                 }\r
119 \r
120                 {\r
121                         InsertRowColumnAction insertColumnBeforeAction = new InsertRowColumnAction(\r
122                                         PDPlugin\r
123                                                         .getResourceString("TableActionGroup.Submenu.InsertColumnBefore"),//$NON-NLS-1$\r
124                                         tablePart, cellColumn, true, true);\r
125                         tableMenu.add(insertColumnBeforeAction);\r
126 \r
127                         InsertRowColumnAction insertColumnAfterAction = new InsertRowColumnAction(\r
128                                         PDPlugin\r
129                                                         .getResourceString("TableActionGroup.Submenu.InsertColumnAfter"),//$NON-NLS-1$\r
130                                         tablePart, cellColumn, true, false);\r
131                         tableMenu.add(insertColumnAfterAction);\r
132 \r
133                         tableMenu.add(new Separator());\r
134                 }\r
135 \r
136                 {\r
137                         DeleteRowColumnAction deleteRowAction = new DeleteRowColumnAction(\r
138                                         PDPlugin\r
139                                                         .getResourceString("TableActionGroup.Submenu.DeleteRow"),//$NON-NLS-1$\r
140                                         tablePart, cellRow, true);\r
141                         tableMenu.add(deleteRowAction);\r
142 \r
143                         DeleteRowColumnAction deleteColumnAction = new DeleteRowColumnAction(\r
144                                         PDPlugin\r
145                                                         .getResourceString("TableActionGroup.Submenu.DeleteColumn"),//$NON-NLS-1$\r
146                                         tablePart, cellColumn, false);\r
147                         tableMenu.add(deleteColumnAction);\r
148                 }\r
149         }\r
150 \r
151         /**\r
152          * @param tablePart\r
153          * @return\r
154          */\r
155         private boolean supportTableActions(ElementEditPart tablePart) {\r
156             // TODO: what's the point of this method?\r
157         // it's only used in one place and always resolves to the same value..\r
158                 return true;\r
159         }\r
160 \r
161         /**\r
162          * Give a selection, find a single common container node as start for table\r
163          * related operations.\r
164          * \r
165          * @param selection\r
166          * @return\r
167          */\r
168         private EditPart findCommonAncesterPart(ISelection selection) {\r
169                 if (selection instanceof IStructuredSelection) {\r
170                         IStructuredSelection structsel = (IStructuredSelection) selection;\r
171                         if (structsel.size() != 1) {\r
172                                 return null;\r
173                         } else if (structsel.getFirstElement() instanceof EditPart) {\r
174                                 return (EditPart) structsel.getFirstElement();\r
175                         } else {\r
176                                 return null;\r
177                         }\r
178                 } else if (selection instanceof DesignRange) {\r
179                         DesignRange range = (DesignRange) selection;\r
180                         if (!range.isValid()) {\r
181                                 return null;\r
182                         }\r
183                         if (range.isEmpty()) {\r
184                                 DesignPosition position = range.getStartPosition();\r
185                                 if (position.getOffset() == 0) {\r
186                                         return position.getContainerPart();\r
187                                 }\r
188                 return position.getSiblingEditPart(true);\r
189                         }\r
190             return RangeUtil.findCommonAncestor(range);\r
191                 } else {\r
192                         return null;\r
193                 }\r
194         }\r
195 \r
196 }\r