Skip to main content
summaryrefslogtreecommitdiffstats
blob: d7e3cb71c02e47d1a423cd60969e4c3f8335cac8 (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
/*******************************************************************************
 * 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.jsf.common.ui.internal.dialogfield;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.jst.jsf.common.ui.internal.guiutils.Alerts;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;

/**
 * @author mengbo
 * @version 1.5
 */
/*package*/ abstract class AbstractClassButtonDialogField extends
		StringButtonDialogField {
	private IProject _project;

	private String _superClass;

	private List _interfacesList;

	private IType _type;

	private int _typeFlag = IJavaElementSearchConstants.CONSIDER_ALL_TYPES;

	private boolean _autoOpenResource = true;

	/**
	 * @param autoOpenResource
	 */
	public void setAutoOpenResource(boolean autoOpenResource) {
		_autoOpenResource = autoOpenResource;
	}

	/**
	 * @param project
	 */
	public AbstractClassButtonDialogField(IProject project) {
		super(null);
		this._project = project;

		setHyperLink(new HyperlinkAdapter() {
			public void linkActivated(HyperlinkEvent e) {
				activeLink();
			}
		});

		setStringButtonAdapter(new IStringButtonAdapter() {
			public void changeControlPressed(DialogField field) {
				browseButtonPressed();
			}
		});
	}

	private void activeLink() {
		String className = getText();
		className = trimNonAlphaChars(className);
		if (className.length() > 0
				&& JavaUIHelper.doesClassExist(_project, className)) {
			JavaUIHelper.doOpenClass(_project, className);
		} else {
			try {
				if (_project == null || !_project.hasNature(JavaCore.NATURE_ID)) {
					ResourceBundle rb = ResourceBundle
							.getBundle("org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogFieldResources");
					Alerts alerts = new Alerts(JSFUICommonPlugin.getDefault(), rb);
					alerts.error("ClassButtonDialogField.Alert.Title",
							"ClassButtonDialogField.Alert.Msg");
					return;
				}
			} catch (CoreException e) {
				e.printStackTrace();
				return;
			}
			JavaClassWizard wizard = new JavaClassWizard(_project, className,
					_superClass, getImplementInterfaces());
			wizard.setAutoOpenResource(_autoOpenResource);
			WizardDialog dialog = new WizardDialog(getShell(), wizard);
			dialog.create();

			setDialogSize(dialog, 400, 500);
			if (dialog.open() == WizardDialog.OK) {
				String newValue = wizard.getClassNameWithArgs();
				if (!newValue.equals(className)) {
					setText(newValue);
				}
			}
		}
	}

	/**
	 * @return the interfaces 
	 * TODO: the contract seems inconsistent
	 * as whether to return null or empty list when none
	 */
	protected abstract List getImplementInterfaces();

	/**
	 * @return the java search scope to be used.  Must not be null
	 */
	protected abstract IJavaSearchScope getJavaSearchScope();

	private void browseButtonPressed() {
		Shell shell = getShell();
		SelectionDialog dialog = JavaUIHelper.openSelectionDialog(shell,
				getJavaSearchScope(), _typeFlag);
		dialog.setTitle(JSFUICommonPlugin
				.getResourceString("DialogField.ClassButton.SelectType"));//$NON-NLS-1$

		if (dialog.open() == SelectionDialog.OK) {
			String oldClassName = getText();
			if (dialog.getResult() != null) {
				_type = (IType) dialog.getResult()[0];
				String newClassName = _type.getFullyQualifiedName();
				if (!oldClassName.equals(newClassName)) {
					setText(newClassName);
				}
			}
		}
	}

	private void setDialogSize(Dialog dialog, int width, int height) {
		Point computedSize = dialog.getShell().computeSize(SWT.DEFAULT,
				SWT.DEFAULT);
		width = Math.max(computedSize.x, width);
		height = Math.max(computedSize.y, height);
		dialog.getShell().setSize(width, height);
	}

	private String trimNonAlphaChars(String className) {
		className = className.trim();
		while (className.length() > 0
				&& !Character.isLetter(className.charAt(0))) {
			className = className.substring(1, className.length());
		}
		int loc = className.indexOf(":"); //$NON-NLS-1$
		if (loc != -1 && loc > 0) {
			className = className.substring(0, loc);
		} else if (loc == 0) {
			className = ""; //$NON-NLS-1$
		}
		return className;
	}

	/**
	 * @return Returns the project.
	 */
	public IProject getProject() {
		return _project;
	}

	/**
	 * @param project
	 *            The project to set.
	 */
	public void setProject(IProject project) {
		this._project = project;
	}

	/**
	 * @return Returns the superClassName.
	 */
	public String getSuperClassName() {
		return _superClass;
	}

	/**
	 * @param superClassName
	 *            The superClassName to set.
	 */
	public void setSuperClassName(String superClassName) {
		this._superClass = superClassName;
	}

	/**
	 * @return Returns the interfacesList.
	 */
	protected List getInterfacesList() {
		return _interfacesList;
	}

	/**
	 * Sets (replaces) the interface list
	 * TODO: this list can have at most one element
	 * @param interfaceName
	 */
	public void setInterface(String interfaceName) {
		_interfacesList = new ArrayList();
		_interfacesList.add(interfaceName);
	}

	/**
	 * @return Returns the _type.
	 */
	public IType getType() {
		return _type;
	}

	/**
	 * @return Returns the typeFalg.
	 */
	public int getTypeFlag() {
		return _typeFlag;
	}

	/**
	 * @param typeFalg
	 *            The typeFalg to set.
	 */
	public void setTypeFlag(int typeFalg) {
		this._typeFlag = typeFalg;
	}
}

Back to the top