1 /*******************************************************************************
\r
2 * Copyright (c) 2006 Sybase, Inc. and others.
\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
10 * Sybase, Inc. - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.eclipse.jst.pagedesigner.actions.table;
\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
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
48 * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
\r
50 public void fillContextMenu(IMenuManager menu) {
\r
51 ISelection selection = this.getContext().getSelection();
\r
53 EditPart part = findCommonAncesterPart(selection);
\r
58 TableOperationContext context = TableOperationContext
\r
59 .getTableOperationContext(part);
\r
60 if (context == null) {
\r
64 final ElementEditPart tablePart = context.getTablePart();
\r
66 if (tablePart == null || !supportTableActions(tablePart)) {
\r
70 final int cellRow = context.getRowIndex();
\r
71 final int cellColumn = context.getColumnIndex();
\r
73 String tag = ((IDOMElement) tablePart.getIDOMNode()).getLocalName();
\r
74 final IMenuManager tableMenu = new MenuManager(tag);
\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
80 public void menuAboutToShow(IMenuManager manager) {
\r
81 tableMenu.removeAll();
\r
82 fillTableMenu(tablePart, cellRow, cellColumn, tableMenu);
\r
85 menu.appendToGroup(PageDesignerActionConstants.GROUP_CONTAINER,
\r
95 private void fillTableMenu(ElementEditPart tablePart, int cellRow,
\r
96 int cellColumn, IMenuManager tableMenu) {
\r
97 SelectEditPartAction action1 = SelectEditPartAction.create(
\r
99 .getResourceString("TableActionGroup.Submenu.SelectTable"), tablePart);//$NON-NLS-1$
\r
100 tableMenu.add(action1);
\r
102 tableMenu.add(new Separator());
\r
105 InsertRowColumnAction insertRowBeforeAction = new InsertRowColumnAction(
\r
107 .getResourceString("TableActionGroup.Submenu.InsertRowBefore"),//$NON-NLS-1$
\r
108 tablePart, cellRow, true, true);
\r
109 tableMenu.add(insertRowBeforeAction);
\r
111 InsertRowColumnAction insertRowAfterAction = new InsertRowColumnAction(
\r
113 .getResourceString("TableActionGroup.Submenu.InsertRowAfter"),//$NON-NLS-1$
\r
114 tablePart, cellRow, true, false);
\r
115 tableMenu.add(insertRowAfterAction);
\r
117 tableMenu.add(new Separator());
\r
121 InsertRowColumnAction insertColumnBeforeAction = new InsertRowColumnAction(
\r
123 .getResourceString("TableActionGroup.Submenu.InsertColumnBefore"),//$NON-NLS-1$
\r
124 tablePart, cellColumn, true, true);
\r
125 tableMenu.add(insertColumnBeforeAction);
\r
127 InsertRowColumnAction insertColumnAfterAction = new InsertRowColumnAction(
\r
129 .getResourceString("TableActionGroup.Submenu.InsertColumnAfter"),//$NON-NLS-1$
\r
130 tablePart, cellColumn, true, false);
\r
131 tableMenu.add(insertColumnAfterAction);
\r
133 tableMenu.add(new Separator());
\r
137 DeleteRowColumnAction deleteRowAction = new DeleteRowColumnAction(
\r
139 .getResourceString("TableActionGroup.Submenu.DeleteRow"),//$NON-NLS-1$
\r
140 tablePart, cellRow, true);
\r
141 tableMenu.add(deleteRowAction);
\r
143 DeleteRowColumnAction deleteColumnAction = new DeleteRowColumnAction(
\r
145 .getResourceString("TableActionGroup.Submenu.DeleteColumn"),//$NON-NLS-1$
\r
146 tablePart, cellColumn, false);
\r
147 tableMenu.add(deleteColumnAction);
\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
162 * Give a selection, find a single common container node as start for table
\r
163 * related operations.
\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
173 } else if (structsel.getFirstElement() instanceof EditPart) {
\r
174 return (EditPart) structsel.getFirstElement();
\r
178 } else if (selection instanceof DesignRange) {
\r
179 DesignRange range = (DesignRange) selection;
\r
180 if (!range.isValid()) {
\r
183 if (range.isEmpty()) {
\r
184 DesignPosition position = range.getStartPosition();
\r
185 if (position.getOffset() == 0) {
\r
186 return position.getContainerPart();
\r
188 return position.getSiblingEditPart(true);
\r
190 return RangeUtil.findCommonAncestor(range);
\r