Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9bf37dda0a7f12c894f01eebafe6c8ed3f982e19 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*******************************************************************************
 * 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.single;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.css2.property.ICSSPropertyID;
import org.eclipse.jst.pagedesigner.dom.DOMStyleUtil;
import org.eclipse.jst.pagedesigner.editors.PageDesignerActionConstants;
import org.eclipse.jst.pagedesigner.elementedit.IElementEdit;
import org.eclipse.jst.pagedesigner.parts.ElementEditPart;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.w3c.dom.Element;

/**
 * Group that constructs context menu items for a single selected element.
 * 
 * @author mengbo
 * @version 1.5
 */
public class SingleElementActionGroup extends ActionGroup {
    // TODO: why?
	private static final Action action = new Action() {
        // create a default instance that does nothing
        // Action is abstract but has no abstract methods
        // run() on this object is a NOOP
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
	 */
	public void fillContextMenu(IMenuManager menu) {
		ElementEditPart part = fixUpSelection(getContext().getSelection());
		if (part == null) {
			return;
		}
		IDOMElement ele = (IDOMElement) part.getIDOMNode();
        addPositionRelativeMenu(menu, part, ele);
		addStylelMenu(menu, part, ele);

		// next add element special actions
		IElementEdit elementEdit = part.getElementEdit();
		if (elementEdit != null) {
			elementEdit.fillContextMenu(menu, ele);
		}
	}

	/**
	 * @param selection
	 * @return
	 */
	private ElementEditPart fixUpSelection(ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection) selection;
			if (sel.size() != 1) {
				return null;
			}
			if (sel.getFirstElement() instanceof ElementEditPart) {
				return (ElementEditPart) sel.getFirstElement();
			}
		}
		return null;
	}

    /**
     * Add menu actions that select relative to the current position
     * 
     * @param menu
     * @param part
     * @param ele
     */
    private void addPositionRelativeMenu(IMenuManager menu, ElementEditPart part, IDOMElement ele)
    {
        IContributionItem subMenuItem = menu.find(PageDesignerActionConstants.SELECT_SUBMENU_ID);
        
        if (subMenuItem instanceof IMenuManager)
        {
            final IMenuManager subMenu = (IMenuManager) subMenuItem;
    //        final IMenuManager selectMenu = new MenuManager(PDPlugin
    //                .getResourceString("ActionGroup.Submenu.SelectRelative"));//$NON-NLS-1$
            SelectParentAction  selectParentAction = SelectParentAction.create(ele, part);
            // Eclipse UI guideline: 6.13
            // even if there is no parent, a disabled action will be returned by
            // create and this should be added to the menu
            subMenu.add(selectParentAction);
            
            List children = new ArrayList();
            for (Iterator it = part.getChildren().iterator(); it.hasNext();)
            {
                EditPart childPart = (EditPart) it.next();
                
                // only include selectable element edit part that are modelling
                // XML elements in the source doc
                if (childPart instanceof ElementEditPart
                        && ((ElementEditPart)childPart).isSelectable()
                        && ((ElementEditPart)childPart).getModel() instanceof Element)
                {
                    children.add(SelectNodeAction
                            .create(((Element)((ElementEditPart)childPart).getModel()).getNodeName(), childPart));
                }
            }

            
            // don't add the select Children menu unless there are actually children
            if (children.size() > 0)
            {
                MenuManager selectChildMenu = new MenuManager("Children"/*PDPlugin
                .getResourceString("ActionGroup.Submenu.StyleClasses")*/);
                subMenu.add(selectChildMenu);

                for (final Iterator it = children.iterator(); it.hasNext();)
                {
                    selectChildMenu.add((Action)it.next());
                }
            }
            else
            {   // Eclipse UI guideline 6.13
                // create the child actions even if no children but make it
                // a disabled option if no children
                Action childrenAction = new Action("Children"){/* do nothing*/};
                childrenAction.setEnabled(false);
                subMenu.add(childrenAction);
            }
        }
    }
    
	/**
	 * @param menu
	 * @param part
	 */
	private void addStylelMenu(IMenuManager menu, ElementEditPart part,
			IDOMElement ele) {
        IContributionItem subMenu = 
            menu.find(PageDesignerActionConstants.STYLE_SUBMENU_ID);

        if (subMenu instanceof IMenuManager)
        {
            final IMenuManager subMenuManager = 
                (IMenuManager) subMenu;
    		addStyle(subMenuManager, part, ele);
    		addStyleClassesMenu(subMenuManager, part, ele);
    
    		if (DOMStyleUtil.supportStyleAttribute(ele)) {
    			// addBorderStyleMenu(stylesub, part, ele);
    			addColorMenu(subMenuManager, part, ele);
    			addBackgroundMenu(subMenuManager, part, ele);
    		}
        }
	}

	/**
	 * 
	 * @param menu
	 * @param part
	 * @param ele
	 */
	private void addStyle(IMenuManager subMenu, ElementEditPart part,
			IDOMElement ele) {
        StyleSupport.createStyleAction(subMenu, part, ele);
	}

	/**
	 * @param stylesub
	 * @param part
	 */
	private void addStyleClassesMenu(IMenuManager subMenu,
			ElementEditPart part, final IDOMElement ele) {
		final IMenuManager classmenu = new MenuManager(PDPlugin
				.getResourceString("ActionGroup.Submenu.StyleClasses"));//$NON-NLS-1$
		StyleClassSupport.createStyleClassActions(classmenu, ele);

        subMenu.appendToGroup(PageDesignerActionConstants.GROUP_STYLE,
                classmenu);
	}

	/**
	 * @param stylesub
	 * @param part
	 */
	private void addColorMenu(IMenuManager subMenu, ElementEditPart part,
			final IDOMElement ele) {
		final IMenuManager colorSub = new MenuManager(PDPlugin
				.getResourceString("ActionGroup.Submenu.Color"));//$NON-NLS-1$
		colorSub.add(action);
		colorSub.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				colorSub.removeAll();
				ColorSupport.createColorActions(colorSub, ele,
						ICSSPropertyID.ATTR_COLOR);
			}
		});
        subMenu.appendToGroup(PageDesignerActionConstants.GROUP_STYLE,
				colorSub);

	}

	/**
	 * @param stylesub
	 * @param part
	 */
	private void addBackgroundMenu(IMenuManager subMenu, ElementEditPart part,
			final IDOMElement ele) {
		final IMenuManager colorSub = new MenuManager(PDPlugin
				.getResourceString("ActionGroup.Submenu.BackgroundColor"));//$NON-NLS-1$
		colorSub.add(action);
		colorSub.addMenuListener(new IMenuListener() {

			public void menuAboutToShow(IMenuManager manager) {
				colorSub.removeAll();
				ColorSupport.createColorActions(colorSub, ele,
						ICSSPropertyID.ATTR_BACKGROUND_COLOR);
			}
		});

        subMenu.appendToGroup(PageDesignerActionConstants.GROUP_STYLE,
				colorSub);
	}
}

Back to the top