Skip to main content
summaryrefslogtreecommitdiffstats
blob: a4dfb6ce45d53e87854aa54f77301c2611420a38 (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
/*******************************************************************************
 * 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.properties.attrgroup;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogField;
import org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogFieldGroupPage;
import org.eclipse.jst.jsf.common.ui.internal.dialogfield.ISupportTextValue;
import org.eclipse.jst.jsf.common.ui.internal.dialogs.CommonWizardDialog;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.commands.single.AddSubNodeCommand;
import org.eclipse.jst.pagedesigner.meta.IAttributeDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;

/**
 * @author mengbo
 * @version 1.5
 */
public class DialogUtil {
	/**
	 * Utility method, this method will popup a dialog for user to input
	 * attributes for initialize a sub element. In this method will create a
	 * command and execute it.
	 * 
	 * @param shell
	 *            parent shell for dialog
	 * @param parent
	 *            the parent element
	 * @param group
	 *            the AttributeGroup
	 * @return true if success, false if user canceled.
	 */
	public static boolean createSubElement(Shell shell,
			final IDOMElement parent, final AttributeGroup group) {
		group.setElementContext(parent, null);
		final DialogFieldGroupPage page = new DialogFieldGroupPage("", group); //$NON-NLS-1$
		page.setTitle(AttributeGroupMessages.getString(
				"DialogUtil.createTitle", group.getTagName())); //$NON-NLS-1$
		page
				.setDescription(AttributeGroupMessages
						.getString(
								"DialogUtil.createDescription", group.getTagName(), parent.getTagName())); //$NON-NLS-1$

		Wizard wizard = new Wizard() {
			public void addPages() {
				super.addPage(page);
			}

			public boolean performFinish() {
				DialogField[] fields = group.getDialogFields();
				Map map = new HashMap();
				for (int i = 0; i < fields.length; i++) {
					IAttributeDescriptor desc = group
							.getAttributeDescriptor(fields[i]);
					if (desc != null && fields[i] instanceof ISupportTextValue) {
						String value = ((ISupportTextValue) fields[i])
								.getText();
						if (value != null && value.length() > 0) {
							map.put(desc.getAttributeName(), value);
						}
					}
				}
				AddSubNodeCommand addSubCommand = new AddSubNodeCommand(
						AttributeGroupMessages
								.getString(
										"DialogUtil.createCommandLabel", group.getTagName()), parent, group.getTagName(), group.getURI(), map); //$NON-NLS-1$
				addSubCommand.execute();
				return true;
			}
		};
		wizard.setWindowTitle(AttributeGroupMessages.getString(
				"DialogUtil.createTitle", group.getTagName())); //$NON-NLS-1$
		wizard.setDefaultPageImageDescriptor(PDPlugin.getDefault()
				.getImageDescriptor("newsuade_wiz.gif")); //$NON-NLS-1$
		CommonWizardDialog dialog = new CommonWizardDialog(shell, wizard);

		return dialog.open() == Window.OK;
	}

	/**
	 * Utility method, this method will popup a dialog for user to input
	 * attributes for initialize a sub element. In this method will create a
	 * command and execute it.
	 * 
	 * @param shell
	 *            parent shell for dialog
	 * @param parent
	 *            parent element
	 * @param uri
	 *            new ele's uri
	 * @param tagName
	 *            new ele's tag name
	 * @param attributes
	 *            an array of attribute names. The dialog will create a
	 *            dialogfield for each of them to allow user to input initial
	 *            value. If null, the system will try to use all attribute of
	 *            the element.
	 * @return true if success, false if user canceled.
	 */
	public static boolean createSubElement(Shell shell,
			final IDOMElement parent, final String uri, final String tagName,
			final String[] attributes) {
		final AttributeGroup group = new AttributeGroup(uri, tagName,
				attributes);
		return createSubElement(shell, parent, group);
	}
}

Back to the top