//
// This file is part of the Prose Development Tools for Eclipse package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is Prose Development Tools for Eclipse.
//
// The Initial Developer of the Original Code is Angela Nicoara. Portions
// created by Angela Nicoara are Copyright (C) 2006 Angela Nicoara.
// All Rights Reserved.
//
// Contributor(s):
// $Id: NewMemberCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
// ==============================================================================
//
package ch.ethz.prose.eclipse.internal.wizards;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SelectionDialog;
/**
* Common functionality for method and field crosscut pages.
*
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: NewMemberCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
*/
public abstract class NewMemberCutWizardPage extends NewCrosscutWizardPage {
/** Id of the container field */
protected static final String TARGET_CLASS = "NewMemberCutWizardPage.target_class"; //$NON-NLS-1$
protected StringButtonDialogField fTargetClassDialogField;
protected IType fTargetClass;
protected IStatus fTargetClassStatus = new StatusInfo();
/**
* Create a new crosscut page.
*
* @param name Page name
*/
public NewMemberCutWizardPage(String name) {
super(name);
fTargetClassDialogField = new StringButtonDialogField(new IStringButtonAdapter() {
public void changeControlPressed(DialogField field) {
IType type = chooseTargetClass();
if (type != null) {
fTargetClassDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
}
}
});
fTargetClassDialogField.setDialogFieldListener(new IDialogFieldListener() {
public void dialogFieldChanged(DialogField field) {
fTargetClassStatus = updateTargetClass();
handleFieldChanged(TARGET_CLASS);
}
});
fTargetClassDialogField.setLabelText("Target Class:");
fTargetClassDialogField.setButtonLabel("Browse...");
}
/**
* @return Text of target class input field
*/
public String getTargetClassName() {
return fTargetClassDialogField.getText();
}
/**
* @return Target class
*/
public IType getTargetClass() {
return fTargetClass;
}
/**
* Notify that a field changed.
*
* @param fieldName Name of the field that changed
*/
protected abstract void handleFieldChanged(String fieldName);
protected void createTargetClassControls(Composite composite, int nColumns) {
fTargetClassDialogField.doFillIntoGrid(composite, nColumns);
Text text = fTargetClassDialogField.getTextControl(null);
LayoutUtil.setWidthHint(text, convertWidthInCharsToPixels(40));
}
protected IStatus updateTargetClass() {
StatusInfo status = new StatusInfo();
IPackageFragmentRoot root = getMainPage().getPackageFragmentRoot();
fTargetClassDialogField.enableButton(root != null);
fTargetClass = null;
String targetClassName = getTargetClassName();
if (targetClassName.length() == 0) {
// accept the empty field (stands for java.lang.Object)
return status;
}
IStatus val = JavaConventions.validateJavaTypeName(targetClassName);
if (val.getSeverity() == IStatus.ERROR) {
status.setError("Target class name is not valid.");
return status;
}
if (root != null) {
try {
IType type = resolveClassName(root.getJavaProject(), targetClassName);
if (type == null) {
status.setWarning("Target class does not exist in current project.");
return status;
}
fTargetClass = type;
} catch (JavaModelException e) {
status.setError("Target class name is not valid.");
JavaPlugin.log(e);
}
} else {
status.setError(""); //$NON-NLS-1$
}
return status;
}
protected IType chooseTargetClass() {
IPackageFragmentRoot root = getMainPage().getPackageFragmentRoot();
if (root == null) { return null; }
IJavaElement[] elements = new IJavaElement[] { root.getJavaProject() };
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);
SelectionDialog dialog;
try {
dialog = JavaUI.createTypeDialog(
getShell(),
getWizard().getContainer(),
scope,
IJavaElementSearchConstants.CONSIDER_CLASSES,
false,
"**");
} catch (JavaModelException e) {
setErrorMessage(e.getMessage());
return null;
}
// TODO out //BEFORE - delete
// TypeSelectionDialog dialog = new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.TYPE,
// scope);
dialog.setTitle("Target Class Selection");
dialog.setMessage("&Choose a type:");
if (dialog.open() == Window.OK) { return (IType) dialog.getResult()[0]; }
return null;
}
}