|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.internal.menus; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 16 |
import org.eclipse.core.runtime.Path; |
| 17 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
| 18 |
import org.eclipse.e4.ui.model.application.MApplication; |
| 19 |
import org.eclipse.e4.ui.model.application.ui.MElementContainer; |
| 20 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenu; |
| 21 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution; |
| 22 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; |
| 23 |
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl; |
| 24 |
import org.eclipse.e4.ui.workbench.swt.modeling.MenuServiceFilter; |
| 25 |
import org.eclipse.ui.IWorkbenchActionConstants; |
| 26 |
import org.eclipse.ui.internal.e4.compatibility.E4Util; |
| 27 |
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since e4 |
| 31 |
* |
| 32 |
*/ |
| 33 |
public class ActionSet { |
| 34 |
|
| 35 |
private IConfigurationElement configElement; |
| 36 |
|
| 37 |
private MApplication application; |
| 38 |
|
| 39 |
public ActionSet(MApplication application, IEclipseContext appContext, |
| 40 |
IConfigurationElement element) { |
| 41 |
this.application = application; |
| 42 |
this.configElement = element; |
| 43 |
} |
| 44 |
|
| 45 |
public void addToModel(ArrayList<MMenuContribution> contributions) { |
| 46 |
|
| 47 |
String idContrib = MenuHelper.getId(configElement); |
| 48 |
IConfigurationElement[] menus = configElement |
| 49 |
.getChildren(IWorkbenchRegistryConstants.TAG_MENU); |
| 50 |
for (IConfigurationElement element : menus) { |
| 51 |
addContribution(idContrib, contributions, element, true); |
| 52 |
} |
| 53 |
|
| 54 |
IConfigurationElement[] actions = configElement |
| 55 |
.getChildren(IWorkbenchRegistryConstants.TAG_ACTION); |
| 56 |
for (IConfigurationElement element : actions) { |
| 57 |
addContribution(idContrib, contributions, element, false); |
| 58 |
} |
| 59 |
|
| 60 |
// for entertainment purposes only |
| 61 |
// printContributions(contributions); |
| 62 |
} |
| 63 |
|
| 64 |
void printContributions(ArrayList<MMenuContribution> contributions) { |
| 65 |
for (MMenuContribution c : contributions) { |
| 66 |
for (MMenuElement element : c.getChildren()) { |
| 67 |
printElement(1, element); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
private void printElement(int level, MMenuElement element) { |
| 73 |
for (int i = 0; i < level; i++) { |
| 74 |
System.out.print('\t'); |
| 75 |
} |
| 76 |
System.out.println(element); |
| 77 |
if (element instanceof MMenu) { |
| 78 |
for (MMenuElement item : ((MMenu) element).getChildren()) { |
| 79 |
printElement(level + 1, item); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private void addContribution(String idContrib, ArrayList<MMenuContribution> contributions, |
| 85 |
IConfigurationElement element, boolean isMenu) { |
| 86 |
MMenuContribution menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution(); |
| 87 |
menuContribution.getTags().add(MenuServiceFilter.MC_MENU); |
| 88 |
final String elementId = MenuHelper.getId(element); |
| 89 |
if (idContrib != null && idContrib.length() > 0) { |
| 90 |
menuContribution.setElementId(idContrib + "/" + elementId); //$NON-NLS-1$ |
| 91 |
} else { |
| 92 |
menuContribution.setElementId(elementId); |
| 93 |
} |
| 94 |
|
| 95 |
String path = isMenu ? MenuHelper.getPath(element) : MenuHelper.getMenuBarPath(element); |
| 96 |
if (path == null || path.length() == 0) { |
| 97 |
if (!isMenu) { |
| 98 |
return; |
| 99 |
} |
| 100 |
path = IWorkbenchActionConstants.MB_ADDITIONS; |
| 101 |
} |
| 102 |
Path menuPath = new Path(path); |
| 103 |
String parentId = "org.eclipse.ui.main.menu"; //$NON-NLS-1$ |
| 104 |
String positionInParent = "after=" + menuPath.segment(0); //$NON-NLS-1$ |
| 105 |
int segmentCount = menuPath.segmentCount(); |
| 106 |
if (segmentCount > 1) { |
| 107 |
parentId = menuPath.segment(segmentCount - 2); |
| 108 |
positionInParent = "after=" + menuPath.segment(segmentCount - 1); //$NON-NLS-1$ |
| 109 |
} |
| 110 |
menuContribution.setParentID(parentId); |
| 111 |
menuContribution.setPositionInParent(positionInParent); |
| 112 |
if (isMenu) { |
| 113 |
MMenu menu = MenuHelper.createMenuAddition(element); |
| 114 |
menuContribution.getChildren().add(menu); |
| 115 |
} else { |
| 116 |
if (parentId.equals("org.eclipse.ui.main.menu")) { //$NON-NLS-1$ |
| 117 |
E4Util.unsupported("****MC: bad pie: " + menuPath); //$NON-NLS-1$ |
| 118 |
parentId = IWorkbenchActionConstants.M_WINDOW; |
| 119 |
menuContribution.setParentID(parentId); |
| 120 |
} |
| 121 |
MMenuElement action = MenuHelper.createLegacyActionAdditions(application, element); |
| 122 |
if (action != null) { |
| 123 |
menuContribution.getChildren().add(action); |
| 124 |
} |
| 125 |
} |
| 126 |
if (menuContribution.getChildren().size() > 0) { |
| 127 |
contributions.add(menuContribution); |
| 128 |
} |
| 129 |
if (isMenu) { |
| 130 |
processGroups(idContrib, contributions, element); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
private void processGroups(String idContrib, ArrayList<MMenuContribution> contributions, |
| 135 |
IConfigurationElement element) { |
| 136 |
MMenuContribution menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution(); |
| 137 |
menuContribution.getTags().add(MenuServiceFilter.MC_MENU); |
| 138 |
final String elementId = MenuHelper.getId(element); |
| 139 |
if (idContrib != null && idContrib.length() > 0) { |
| 140 |
menuContribution.setElementId(idContrib + "/" + elementId + ".groups"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 141 |
} else { |
| 142 |
menuContribution.setElementId(elementId + ".groups"); //$NON-NLS-1$ |
| 143 |
} |
| 144 |
menuContribution.setParentID(elementId); |
| 145 |
IConfigurationElement[] children = element.getChildren(); |
| 146 |
for (IConfigurationElement sepAddition : children) { |
| 147 |
String name = sepAddition.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); |
| 148 |
MMenuElement sep = MenuFactoryImpl.eINSTANCE.createMenuSeparator(); |
| 149 |
sep.setElementId(name); |
| 150 |
if (!MenuHelper.isSeparatorVisible(sepAddition)) { |
| 151 |
sep.setVisible(false); |
| 152 |
} |
| 153 |
menuContribution.getChildren().add(sep); |
| 154 |
} |
| 155 |
if (menuContribution.getChildren().size() > 0) { |
| 156 |
contributions.add(menuContribution); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
MElementContainer<MMenuElement> findMenuFromPath(MElementContainer<MMenuElement> menu, |
| 161 |
Path menuPath, int segment) { |
| 162 |
int idx = MenuHelper.indexForId(menu, menuPath.segment(segment)); |
| 163 |
if (idx == -1) { |
| 164 |
if (segment + 1 < menuPath.segmentCount() || !menuPath.hasTrailingSeparator()) { |
| 165 |
return null; |
| 166 |
} |
| 167 |
return menu; |
| 168 |
} |
| 169 |
MElementContainer<MMenuElement> item = (MElementContainer<MMenuElement>) menu.getChildren() |
| 170 |
.get(idx); |
| 171 |
if (item.getChildren().size() == 0) { |
| 172 |
if (segment + 1 == menuPath.segmentCount()) { |
| 173 |
return menu; |
| 174 |
} else { |
| 175 |
return null; |
| 176 |
} |
| 177 |
} |
| 178 |
return findMenuFromPath(item, menuPath, segment + 1); |
| 179 |
} |
| 180 |
} |