//
// 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: NewMethodRedefineCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
// ==============================================================================
//
package ch.ethz.prose.eclipse.internal.wizards;
import java.util.Iterator;
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.IType;
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.IListAdapter;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
/**
* Wizard page to customize method redefine crosscuts.
*
* @author Angela Nicoara
* @version $Id: NewMethodRedefineCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
*/
public class NewMethodRedefineCutWizardPage extends NewMemberCutWizardPage {
protected static final String PAGE_NAME = "NewMethodRedefineCutWizardPage"; //$NON-NLS-1$
protected static final String ARGUMENT_TYPES = PAGE_NAME + ".argument_types"; //$NON-NLS-1$
protected ListDialogField fArgumentTypesDialogField;
protected IStatus fArgumentTypesStatus = new StatusInfo();
/**
* Create a new method redefine cut customization wizard page.
*/
public NewMethodRedefineCutWizardPage() {
super(PAGE_NAME);
setTitle("MethodRedefine Cut Customization");
setDescription("Select an optional target class and argument types that must match.");
String[] buttonNames = new String[] { "Add...", "Remove", null, "Up", "Down"};
fArgumentTypesDialogField = new ListDialogField(new ListAdapter(), buttonNames, new LabelProvider());
fArgumentTypesDialogField.setDialogFieldListener(new IDialogFieldListener() {
public void dialogFieldChanged(DialogField field) {
fArgumentTypesStatus = updateArgumentTypes();
handleFieldChanged(ARGUMENT_TYPES);
}
});
fArgumentTypesDialogField.setLabelText("Argument Types:");
fArgumentTypesDialogField.setRemoveButtonIndex(1);
fArgumentTypesDialogField.setUpButtonIndex(3);
fArgumentTypesDialogField.setDownButtonIndex(4);
}
/*
* (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);
createTargetClassControls(composite, nColumns);
createArgumentTypesControls(composite, nColumns);
setControl(composite);
Dialog.applyDialogFont(composite);
}
protected void createArgumentTypesControls(Composite parent, int nColumns) {
fArgumentTypesDialogField.doFillIntoGrid(parent, nColumns);
GridData gd = (GridData) fArgumentTypesDialogField.getListControl(null).getLayoutData();
gd.grabExcessVerticalSpace = false;
gd.widthHint = convertWidthInCharsToPixels(40);
}
protected IStatus updateArgumentTypes() {
StatusInfo status = new StatusInfo();
if (getTargetClassName().length() == 0 && fArgumentTypesDialogField.getElements().size() > 0) {
status.setError("Target class must not be empty.");
}
return status;
}
protected void handleFieldChanged(String fieldName) {
if (fieldName == TARGET_CLASS) {
fArgumentTypesStatus = updateArgumentTypes();
}
IStatus status[] = new IStatus[] { fTargetClassStatus, fArgumentTypesStatus };
updateStatus(status);
}
/* (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.crosscut.MethodRedefineCut");
imports.addImport("ch.ethz.prose.filter.PointCutter");
imports.addImport("ch.ethz.prose.crosscut.*"); //bugfix: added a new import in case of REST //angy
StringBuffer source = new StringBuffer();
source.append("public Crosscut ");
source.append(getUniqueFieldName(aspect));
source.append(" = new MethodRedefineCut() {");
source.append(nl);
source.append("public void METHOD_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 targetClass = getTargetClass();
String targetClassName = getTargetClassName();
if (targetClass != null) {
imports.addImport(targetClass.getFullyQualifiedName());
source.append(targetClass.getElementName());
} else if (targetClassName.length() != 0) {
source.append(targetClassName);
} else {
return;
}
source.append(" target");
int count = 1;
for (Iterator all = fArgumentTypesDialogField.getElements().iterator(); all.hasNext();) {
ArgumentType each = (ArgumentType) all.next();
source.append(", ");
source.append(each);
source.append(" arg");
source.append(count++);
}
}
/**
* List adapter used for argument type list.
*/
protected class ListAdapter implements IListAdapter {
public void customButtonPressed(ListDialogField field, int index) {
if (index == 0) { // "Add..." button
InputDialog dialog = new InputDialog(getShell(), "Add Argument Type", "Specify an argument type to add:", "", null);
if (dialog.open() == Window.OK && dialog.getValue().length() > 0) {
fArgumentTypesDialogField.addElement(new ArgumentType(dialog.getValue()));
}
}
}
public void selectionChanged(ListDialogField field) {
}
public void doubleClicked(ListDialogField field) {
}
}
/**
* Representation of an argument type.
*/
protected class ArgumentType {
String type;
protected ArgumentType(String type) {
this.type = type;
}
public String toString() {
return type;
}
}
}