//
// 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: NewExceptionCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
// ==============================================================================
//
package ch.ethz.prose.eclipse.internal.wizards;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IField;
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.dialogs.Dialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.dialogs.SelectionDialog;
/**
* Wizard page to customize exception crosscuts.
*
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: NewExceptionCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
*/
public class NewExceptionCutWizardPage extends NewCrosscutWizardPage {
protected static final String PAGE_NAME = "NewExceptionCutWizardPage"; //$NON-NLS-1$
protected static final String TARGET_EXCEPTION = PAGE_NAME + ".target_class"; //$NON-NLS-1$
protected StringButtonDialogField fTargetExceptionDialogField;
protected IType fTargetException;
protected IStatus fTargetExceptionStatus = new StatusInfo();
/**
* Create a new method cut customization wizard page.
*/
public NewExceptionCutWizardPage() {
super(PAGE_NAME);
setTitle("Exception Cut Customization");
setDescription("Select an optional target exception that must match.");
fTargetExceptionDialogField = new StringButtonDialogField(new IStringButtonAdapter() {
public void changeControlPressed(DialogField field) {
IType type = chooseTargetException();
if (type != null) {
fTargetExceptionDialogField.setText(JavaModelUtil.getFullyQualifiedName(type));
}
}
});
fTargetExceptionDialogField.setDialogFieldListener(new IDialogFieldListener() {
public void dialogFieldChanged(DialogField field) {
fTargetExceptionStatus = updateTargetException();
updateStatus(new IStatus[] { fTargetExceptionStatus });
}
});
fTargetExceptionDialogField.setLabelText("Target Exception:");
fTargetExceptionDialogField.setButtonLabel("Browse...");
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
*/
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite = new Composite(parent, SWT.NONE);
int nColumns = 3;
GridLayout layout = new GridLayout();
layout.numColumns = nColumns;
composite.setLayout(layout);
createTargetExceptionControls(composite, nColumns);
setControl(composite);
Dialog.applyDialogFont(composite);
}
protected void createTargetExceptionControls(Composite parent, int nColumns) {
fTargetExceptionDialogField.doFillIntoGrid(parent, nColumns);
LayoutUtil.setWidthHint(fTargetExceptionDialogField.getTextControl(null), convertWidthInCharsToPixels(40));
}
/**
* @return Text of target class input field
*/
public String getTargetExceptionName() {
return fTargetExceptionDialogField.getText();
}
/**
* @return Target exception
*/
public IType getTargetException() {
return fTargetException;
}
/* (non-Javadoc)
* @see ch.ethz.prose.eclipse.internal.wizards.NewCrosscutWizardPage#writeCrosscut(org.eclipse.jdt.core.IType, int, ch.ethz.prose.eclipse.internal.wizards.ImportsManager, org.eclipse.core.runtime.IProgressMonitor, java.lang.String)
*/
public IField writeCrosscut(IType aspect, int crosscutType, ImportsManager imports, IProgressMonitor monitor, String nl)
throws CoreException {
imports.addImport("ch.ethz.prose.crosscut.Crosscut");
imports.addImport("ch.ethz.prose.filter.PointCutter");
StringBuffer source = new StringBuffer();
source.append("public Crosscut ");
source.append(getUniqueFieldName(aspect));
if (crosscutType == NewAspectWizardPage.THROW_CUT_TYPE) {
imports.addImport("ch.ethz.prose.crosscut.ThrowCut");
source.append(" = new ThrowCut() {");
source.append(nl);
source.append("public void THROW_ARGS(");
} else {
imports.addImport("ch.ethz.prose.crosscut.CatchCut");
source.append(" = new CatchCut() {");
source.append(nl);
source.append("public void CATCH_ARGS(");
}
writeAdviceSignature(imports, source);
source.append(") {");
source.append(nl);
source.append("// TODO Write your advice code here.");
source.append(nl);
source.append("}");
source.append(nl);
source.append("protected PointCutter pointCutter() {");
getMainPage().writePointCutterBody(source, imports, nl);
source.append("}");
source.append(nl);
source.append("};");
source.append(nl);
source.append(nl);
return aspect.createField(source.toString(), null, false, new SubProgressMonitor(monitor, 2));
}
/* (non-Javadoc)
* @see ch.ethz.prose.eclipse.internal.wizards.NewCrosscutWizardPage#writeAdviceSignature(ch.ethz.prose.eclipse.internal.wizards.ImportsManager, java.lang.StringBuffer)
*/
public void writeAdviceSignature(ImportsManager imports, StringBuffer source) {
IType targetException = getTargetException();
String targetClassName = getTargetExceptionName();
if (targetException != null) {
imports.addImport(targetException.getFullyQualifiedName());
source.append(targetException.getElementName());
} else if (targetClassName.length() != 0) {
source.append(targetClassName);
} else {
return;
}
source.append(" target");
}
protected IStatus updateTargetException() {
StatusInfo status = new StatusInfo();
IPackageFragmentRoot root = getMainPage().getPackageFragmentRoot();
fTargetExceptionDialogField.enableButton(root != null);
fTargetException = null;
String targetClassName = getTargetExceptionName();
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 exception name is not valid.");
return status;
}
if (root != null) {
try {
IType type = resolveClassName(root.getJavaProject(), targetClassName);
if (type == null) {
status.setWarning("Target exception does not exist in current project.");
return status;
}
// TODO Check if exception is subclass of Throwable
fTargetException = type;
} catch (JavaModelException e) {
status.setError("Target exception name is not valid.");
JavaPlugin.log(e);
}
} else {
status.setError(""); //$NON-NLS-1$
}
return status;
}
protected IType chooseTargetException() {
IPackageFragmentRoot root = getMainPage().getPackageFragmentRoot();
if (root == null) {
return null;
}
IJavaElement[] elements = new IJavaElement[] { root.getJavaProject()};
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);
// TODO Filter for subclasses of Throwable
SelectionDialog dialog;
try {
dialog = JavaUI.createTypeDialog(
getShell(),
getWizard().getContainer(),
scope,
IJavaElementSearchConstants.CONSIDER_CLASSES,
false,
"**");
} catch (JavaModelException e) {
setErrorMessage(e.getMessage());
return null;
}
// TODO old //BEFORE - delete
// TypeSelectionDialog dialog = new TypeSelectionDialog(getShell(), getWizard().getContainer(), IJavaSearchConstants.CLASS,
// scope);
dialog.setTitle("Target Exception Selection");
dialog.setMessage("&Choose an exception:");
if (dialog.open() == Window.OK) {
return (IType) dialog.getResult()[0];
}
return null;
}
}