Skip to main content
summaryrefslogtreecommitdiffstats
blob: 807cad294d4845f086d7f966f7a979c129026452 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.actions.container;

import org.eclipse.gef.EditPart;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.pagedesigner.elementedit.IElementEdit;
import org.eclipse.jst.pagedesigner.parts.ElementEditPart;
import org.eclipse.jst.pagedesigner.parts.NodeEditPart;
import org.eclipse.jst.pagedesigner.range.RangeUtil;
import org.eclipse.jst.pagedesigner.viewer.DesignPosition;
import org.eclipse.jst.pagedesigner.viewer.DesignRange;
import org.eclipse.ui.actions.ActionGroup;
import org.w3c.dom.Text;

/**
 * @author mengbo
 * @version 1.5
 */
public class ContainerActionGroup extends ActionGroup {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
	 */
	public void fillContextMenu(IMenuManager menu) {
		ISelection selection = this.getContext().getSelection();

		EditPart part = findCommonAncesterPart(selection);
		if (!(part instanceof NodeEditPart)) {
			return;
		}

		NodeEditPart original = (NodeEditPart) part;
		// start from the parent of part
		while (part.getParent() instanceof ElementEditPart) {
			ElementEditPart elementEditPart = (ElementEditPart) part
					.getParent();

			IElementEdit elementEdit = elementEditPart.getElementEdit();
			if (elementEdit != null) {
				boolean filled = elementEdit.fillContainerContextMenu(menu,
						elementEditPart, original, selection);
				if (filled) {
					break;
				}
			}

			part = part.getParent();
		}
	}

	/**
	 * Give a selection, find a single common container node as start for table
	 * related operations.
	 * 
	 * @param selection
	 * @return
	 */
	private EditPart findCommonAncesterPart(ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structsel = (IStructuredSelection) selection;
			if (structsel.size() != 1) {
				return null;
			} else if (structsel.getFirstElement() instanceof EditPart) {
				return (EditPart) structsel.getFirstElement();
			} else {
				return null;
			}
		} else if (selection instanceof DesignRange) {
			DesignRange range = (DesignRange) selection;
			if (!range.isValid()) {
				return null;
			}
			if (range.isEmpty()) {
				DesignPosition position = range.getStartPosition();
				if (position.getOffset() == 0
						|| position.getContainerNode() instanceof Text) {
					return position.getContainerPart();
				}
                return position.getSiblingEditPart(false);
			}
            return RangeUtil.findCommonAncestor(range);
		} else {
			return null;
		}
	}
}

Back to the top