package tool.wizards;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;
import tool.ToolProjectSupport;
import tool.model.ToolClass;
import tool.model.ToolPlan;
/**
* The "New" wizard page allows setting the container for the new file as well
* as the file name. The page will only accept file name without the extension
* OR with the extension that matches the expected one (cdf).
*/
public class NewClassWizardPage extends WizardPage {
private DataBindingContext m_bindingContext;
private Text projectText;
private Text classNameText;
private ISelection selection;
private ToolClass cls;
private ToolPlan plan;
private Label lblclassName;
private GridData gd_classNameText;
private Label lblproject;
private GridData gd_projectText;
private Text superClassText;
private Button btnWindowClass;
/**
* Constructor for SampleNewWizardPage.
*
* @param pageName
*/
public NewClassWizardPage(ISelection selection) {
super("wizardPage");
setImageDescriptor(ResourceManager.getPluginImageDescriptor("Tool", "icons/new_class.gif"));
setMessage("Create an new Tool class");
setTitle("New Tool Class");
setDescription("This wizard creates a new Tool class ");
this.selection = selection;
}
/**
* @see IDialogPage#createControl(Composite)
*/
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 3;
layout.verticalSpacing = 9;
Label label;
lblproject = new Label(container, SWT.NULL);
lblproject.setText("&Project:");
projectText = new Text(container, SWT.BORDER | SWT.SINGLE);
projectText.setEditable(false);
GridData gd;
gd_projectText = new GridData(GridData.FILL_HORIZONTAL);
projectText.setLayoutData(gd_projectText);
projectText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
new Label(container, SWT.NONE);
lblclassName = new Label(container, SWT.NULL);
lblclassName.setText("&Class name:");
classNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
gd_classNameText = new GridData(GridData.FILL_HORIZONTAL);
classNameText.setLayoutData(gd_classNameText);
classNameText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
initialize();
dialogChanged();
setControl(container);
btnWindowClass = new Button(container, SWT.CHECK);
btnWindowClass.setText("Window Class");
Label lblSuperClass = new Label(container, SWT.NONE);
lblSuperClass.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblSuperClass.setText("Super Class:");
superClassText = new Text(container, SWT.BORDER);
superClassText.setText("Framework.Object");
superClassText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Button btnTypeLookup = new Button(container, SWT.NONE);
btnTypeLookup.setImage(ResourceManager.getPluginImage("Tool", "icons/look_up.gif"));
m_bindingContext = initDataBindings();
}
/**
* Tests if the current workbench selection is a suitable container to use.
*/
private void initialize() {
if (selection != null && selection.isEmpty() == false
&& selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) selection;
if (ssel.size() > 1)
return;
Object obj = ssel.getFirstElement();
if (obj instanceof IResource) {
IContainer container;
if ((obj instanceof IFolder)
&& (ToolProjectSupport.isPlanFolder((IFolder)obj))){
this.plan = ToolProjectSupport.getPlanFromFolder((IFolder)obj);
this.cls = new ToolClass(this.plan, false);
container = (IFolder) obj;
projectText.setText(container.getFullPath().toString());
}
}
}
classNameText.setText("new_class");
}
/**
* Ensures that both text fields are set.
*/
private void dialogChanged() {
IResource container = ResourcesPlugin.getWorkspace().getRoot()
.findMember(new Path(getContainerName()));
String fileName = getFileName();
if (getContainerName().length() == 0) {
updateStatus("File container must be specified");
return;
}
if (container == null
|| (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
updateStatus("File container must exist");
return;
}
if (!container.isAccessible()) {
updateStatus("Project must be writable");
return;
}
if (fileName.length() == 0) {
updateStatus("File name must be specified");
return;
}
if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
updateStatus("File name must be valid");
return;
}
int dotLoc = fileName.lastIndexOf('.');
if (dotLoc != -1) {
String ext = fileName.substring(dotLoc + 1);
if (ext.equalsIgnoreCase("cdf") == false) {
updateStatus("File extension must be \"cdf\"");
return;
}
}
updateStatus(null);
}
private void updateStatus(String message) {
setErrorMessage(message);
setPageComplete(message == null);
}
public String getContainerName() {
return projectText.getText();
}
public String getFileName() {
return classNameText.getText();
}
public ToolClass getCls() {
return cls;
}
public void setCls(ToolClass cls) {
this.cls = cls;
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue projectTextObserveTextObserveWidget = SWTObservables.observeText(projectText, SWT.FocusOut);
IObservableValue planNameObserveValue = BeansObservables.observeValue(plan, "toolName");
bindingContext.bindValue(projectTextObserveTextObserveWidget, planNameObserveValue, null, null);
//
IObservableValue classNameTextObserveTextObserveWidget = SWTObservables.observeText(classNameText, SWT.FocusOut);
IObservableValue clsNameObserveValue = BeansObservables.observeValue(cls, "toolName");
bindingContext.bindValue(classNameTextObserveTextObserveWidget, clsNameObserveValue, null, null);
//
IObservableValue btnWindowClassObserveSelectionObserveWidget = SWTObservables.observeSelection(btnWindowClass);
IObservableValue clsMappedObserveValue = BeansObservables.observeValue(cls, "mapped");
bindingContext.bindValue(btnWindowClassObserveSelectionObserveWidget, clsMappedObserveValue, null, null);
//
IObservableValue superClassTextObserveTextObserveWidget = SWTObservables.observeText(superClassText, SWT.FocusOut);
IObservableValue clsSuperClassObserveValue = BeansObservables.observeValue(cls, "superClass");
bindingContext.bindValue(superClassTextObserveTextObserveWidget, clsSuperClassObserveValue, null, null);
//
return bindingContext;
}
}