//
// 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: NewFieldCutWizardPage.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.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.LayoutUtil;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
/**
* Wizard page to customize field crosscuts.
*
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: NewFieldCutWizardPage.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
*/
public class NewFieldCutWizardPage extends NewMemberCutWizardPage {
protected static final String PAGE_NAME = "NewFieldCutWizardPage"; //$NON-NLS-1$
protected static final String FIELD_TYPE = PAGE_NAME + ".field_type"; //$NON-NLS-1$
protected StringDialogField fFieldTypeDialogField;
protected IStatus fFieldTypeStatus = new StatusInfo();
/**
* Create a new method cut customization wizard page.
*/
public NewFieldCutWizardPage() {
super(PAGE_NAME);
setTitle("Field Cut Customization");
setDescription("Select an optional target class and a field type that must match.");
fFieldTypeDialogField = new StringDialogField();
fFieldTypeDialogField.setDialogFieldListener(new IDialogFieldListener() {
public void dialogFieldChanged(DialogField field) {
fFieldTypeStatus = updateFieldType();
handleFieldChanged(FIELD_TYPE);
}
});
fFieldTypeDialogField.setLabelText("Field Type:");
}
/*
* (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);
createFieldTypeControls(composite, nColumns - 1);
setControl(composite);
Dialog.applyDialogFont(composite);
}
protected void createFieldTypeControls(Composite parent, int nColumns) {
fFieldTypeDialogField.doFillIntoGrid(parent, nColumns);
LayoutUtil.setWidthHint(fFieldTypeDialogField.getTextControl(null), convertWidthInCharsToPixels(40));
}
/**
* @return Text of field type input field
*/
public String getFieldTypeName() {
return fFieldTypeDialogField.getText();
}
protected IStatus updateFieldType() {
StatusInfo status = new StatusInfo();
if (getTargetClassName().length() == 0 && getFieldTypeName().length() > 0) {
status.setError("Target class must not be empty.");
}
return status;
}
protected void handleFieldChanged(String fieldName) {
if (fieldName == TARGET_CLASS) {
fFieldTypeStatus = updateFieldType();
}
IStatus status[] = new IStatus[] { fTargetClassStatus, fFieldTypeStatus };
updateStatus(status);
}
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.GET_CUT_TYPE) {
imports.addImport("ch.ethz.prose.crosscut.GetCut");
source.append(" = new GetCut() {");
source.append(nl);
source.append("public void GET_ARGS(");
} else {
imports.addImport("ch.ethz.prose.crosscut.SetCut");
source.append(" = new SetCut() {");
source.append(nl);
source.append("public void SET_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, ");
source.append(getFieldTypeName());
source.append(" field");
}
}