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
|
/*******************************************************************************
* Copyright (c) 2008 Conselleria de Infraestructuras y Transporte, Generalitat
* de la Comunitat Valenciana, Obeo. 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:
* Francisco Javier Cano Muñoz (Prodevelop) – Initial API implementation.
* Obeo
*
******************************************************************************/
package org.eclipse.papyrus.views.modelexplorer.actionprovider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.MenuManager;
/**
* Specialization of <AbstractCommonActionProvider> to be used in menu and
* submenu contributions.
*
* @author fjcano
* @author <a href="mailto:jerome.benois@obeo.fr">Jerome Benois</a>
*/
public abstract class AbstractSubmenuActionProvider extends
AbstractCommonActionProvider {
/**
* Organizes the given <Collection> of <IActions>.
*
* @param createActions
* <Collection> of <IActions> to organize
* @param token
* <String> that the <StringTokenizer> will use to trim each
* <IAction>'s text.
*
* @return a <Map> associating <String>s to <Collection>s of <IAction>s.
*/
protected Map<String, Collection<IAction>> extractSubmenuActions(
Collection<IAction> createActions, String token) {
Map<String, Collection<IAction>> createSubmenuActions = new LinkedHashMap<String, Collection<IAction>>();
if (createActions != null) {
for (Iterator<IAction> actions = createActions.iterator(); actions
.hasNext();) {
IAction action = actions.next();
StringTokenizer st = new StringTokenizer(action.getText(),
token);
if (st.countTokens() == 2) {
String text = st.nextToken().trim();
Collection<IAction> submenuActions = createSubmenuActions
.get(text);
if (submenuActions == null) {
createSubmenuActions.put(text,
submenuActions = new ArrayList<IAction>());
}
action.setText(st.nextToken().trim());
submenuActions.add(action);
actions.remove();
}
}
}
return createSubmenuActions;
}
/**
* Fills a <IContributionManager> with the given <Collection> of <IActions>.
*
* @param manager
* the manager
* @param actions
* the actions
* @param contributionID
* the contribution id
*/
protected void populateManager(IContributionManager manager,
Collection<? extends IAction> actions, String contributionID) {
if (actions != null) {
for (IAction action : actions) {
if (contributionID != null) {
manager.insertBefore(contributionID, action);
} else {
manager.add(action);
}
}
}
}
/**
* Fills a <IContributionManager> with two levels of menus, as specified by
* the <Map> of <String>s to <Collection>s of <IAction>s.
*
* @param manager
* the manager
* @param submenuActions
* the submenu actions
* @param contributionID
* the contribution id
*/
protected void populateManager(IContributionManager manager,
Map<String, Collection<IAction>> submenuActions,
String contributionID) {
if (submenuActions != null) {
for (Map.Entry<String, Collection<IAction>> entry : submenuActions
.entrySet()) {
MenuManager submenuManager = new MenuManager(entry.getKey());
if (contributionID != null) {
manager.insertBefore(contributionID, submenuManager);
} else {
manager.add(submenuManager);
}
populateManager(submenuManager, entry.getValue(), null);
}
}
}
}
|