Skip to main content
summaryrefslogtreecommitdiffstats
blob: b718752aff31cb5622efd4822b03ac95ff663c70 (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
/*******************************************************************************
 * 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.jsf.ui.actions;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
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.action.Separator;
import org.eclipse.jst.jsf.core.jsfappconfig.JSFAppConfigManager;
import org.eclipse.jst.jsf.facesconfig.emf.ConverterIdType;
import org.eclipse.jst.jsf.facesconfig.emf.ConverterType;
import org.eclipse.jst.jsf.facesconfig.emf.ValidatorIdType;
import org.eclipse.jst.jsf.facesconfig.emf.ValidatorType;
import org.eclipse.jst.pagedesigner.editors.PageDesignerActionConstants;
import org.eclipse.jst.pagedesigner.utils.StructuredModelUtil;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;

/**
 * @author mengbo
 * @version 1.5
 */
public class JSFAddActionGroup {

	private final static Action EMPTY_ACTION = new Action() {
        // TODO: why?
	};


	/**
	 * @param menu
	 * @param element
	 * @param support
	 */
	public void fillContextMenu(IMenuManager menu, final IDOMElement element,
			final IJSFCoreSupport support) {
        IContributionItem item = menu.find(PageDesignerActionConstants.INSERT_SUBMENU_ID);
        
        if (item instanceof IMenuManager)
        {
    		final IMenuManager submenu = (IMenuManager) item;
    		submenu.add(EMPTY_ACTION);
    		submenu.addMenuListener(new IMenuListener() {
    			public void menuAboutToShow(IMenuManager manager) {
    				submenu.removeAll();
    				addJSFAddItems(submenu, element, support);
    			}
    		});
        }
		//menu.appendToGroup(PageDesignerActionConstants.GROUP_SPECIAL, submenu);
	}

	/**
	 * @param submenu
	 * @param element
	 */
	private void addJSFAddItems(IMenuManager submenu, IDOMElement element,
			IJSFCoreSupport support) {
		AddActionListenerAction actionListenerAction = new AddActionListenerAction(
				element);
		actionListenerAction.setEnabled(support.isActionSource());
		submenu.add(actionListenerAction);

		AddAttributeAction attrAction = new AddAttributeAction(element);
		attrAction.setEnabled(support.isUIComponent());
		submenu.add(attrAction);

		IProject prj = null;
		IFile file = StructuredModelUtil.getFileFor(element.getModel());
		if (file != null) {
			prj = file.getProject();
		}

		IMenuManager converterMenu = new MenuManager(ActionsResources
				.getString("Submenu.JSFAdd.Converter"));//$NON-NLS-1$
		boolean supportConverter = support.isValueHolder();
		String[] converterIds = getRegisteredConverterIds(prj);
		if (converterIds != null && converterIds.length > 0) {
			for (int i = 0; i < converterIds.length; i++) {
				AddConverterAction action = new AddConverterAction(
						converterIds[i], element);
				action.setEnabled(supportConverter);
				converterMenu.add(action);
			}
			converterMenu.add(new Separator());
		}
		AddConvertDateTimeAction dateTimeAction = new AddConvertDateTimeAction(
				element);
		dateTimeAction.setEnabled(supportConverter);
		converterMenu.add(dateTimeAction);
		AddConvertNumberAction numberAction = new AddConvertNumberAction(
				element);
		numberAction.setEnabled(supportConverter);
		converterMenu.add(numberAction);
		submenu.add(converterMenu);

		AddParamAction addParamAction = new AddParamAction(element);
		addParamAction.setEnabled(support.isUIComponent());
		submenu.add(addParamAction);

		AddSelectItemAction selectItemAction = new AddSelectItemAction(element);
		selectItemAction.setEnabled(support.supportSelectItems());
		submenu.add(selectItemAction);

		AddSelectItemsAction selectItemsAction = new AddSelectItemsAction(
				element);
		selectItemsAction.setEnabled(support.supportSelectItems());
		submenu.add(selectItemsAction);

		IMenuManager validatorMenu = new MenuManager(ActionsResources
				.getString("Submenu.JSFAdd.Validators"));//$NON-NLS-1$
		boolean supportValidator = support.isEditableValueHolder();
		String[] validatorIds = getRegisteredValidatorIds(prj);
		if (validatorIds != null && validatorIds.length > 0) {
			for (int i = 0; i < validatorIds.length; i++) {
				AddValidatorAction action = new AddValidatorAction(
						validatorIds[i], element);
				action.setEnabled(supportValidator);
				validatorMenu.add(action);
			}
			validatorMenu.add(new Separator());
		}
		AddValidateDoubleRangeAction doubleRangeAction = new AddValidateDoubleRangeAction(
				element);
		doubleRangeAction.setEnabled(supportValidator);
		validatorMenu.add(doubleRangeAction);
		AddValidateLengthAction lengthAction = new AddValidateLengthAction(
				element);
		lengthAction.setEnabled(supportValidator);
		validatorMenu.add(lengthAction);
		AddValidateLongRangeAction longRangeAction = new AddValidateLongRangeAction(
				element);
		longRangeAction.setEnabled(supportValidator);
		validatorMenu.add(longRangeAction);
		submenu.add(validatorMenu);

		AddValueChangeListenerAction valueChangeAction = new AddValueChangeListenerAction(
				element);
		valueChangeAction.setEnabled(support.isEditableValueHolder());
		submenu.add(valueChangeAction);
	}

	/**
	 * @return
	 */
	private String[] getRegisteredValidatorIds(IProject project) 
    {
        String[] result = null;
        
        JSFAppConfigManager appConfigMgr = 
            JSFAppConfigManager.getInstance(project);

        // getInstance may return null if there is a problem
        if (appConfigMgr != null)
        {
            final List list = appConfigMgr.getValidators();
            result = new String[list.size()];
            int i = 0;
            for (final Iterator it = list.iterator(); it.hasNext();) 
            {
                ValidatorType validator = (ValidatorType) it.next();
                ValidatorIdType validatorId = validator.getValidatorId();
                if (validatorId != null)
                {
                    result[i++] = validatorId.getTextContent() != null ?
                            validatorId.getTextContent().trim() : ""; //$NON-NLS-1$
                }
            }
        }
        return result;
	}

	/**
	 * @return
	 */
	private String[] getRegisteredConverterIds(IProject project) 
    {
        String[] result = null;
        
        JSFAppConfigManager appConfigMgr = 
            JSFAppConfigManager.getInstance(project);

        // getInstance may return null if there is a problem
        if (appConfigMgr != null)
        {
            final List list = appConfigMgr.getConverters();
            result = new String[list.size()];
            int i = 0;
            for (final Iterator it = list.iterator(); it.hasNext();)
            {
                ConverterType converter = (ConverterType) it.next();
                ConverterIdType converterId = converter.getConverterId();
                if (converterId != null)
                {
                    result[i++] = converterId.getTextContent() != null ? 
                            converterId.getTextContent().trim() : ""; //$NON-NLS-1$
                }
            }
        }
		return result;
	}
}

Back to the top