Permalink
Browse files

[#115]: New Spec Wizard acts weird with preselection

Adopted changes to new mapping spec wizard

Fixes #115

Task-Url: http://github.com/roberthilbrich/assist-public/issues/115
  • Loading branch information...
RobertHilbrich committed Jul 18, 2018
1 parent b434f67 commit 80db42ac28d4d4f97f36ff65b595b42c349c2370
@@ -4,6 +4,8 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
@@ -30,178 +32,194 @@
*/
public class NewMappingSpecificationWizardPage extends WizardPage {
private Text containerText;
private Text fileText;
private ISelection selection;
/**
* Constructor
*
* @param pageName
*/
public NewMappingSpecificationWizardPage(ISelection selection) {
super("Create Specification");
setTitle("Create a new mapping specification");
setDescription("This wizard creates a new mapping specification file with *.mdsl extension.");
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 fileNameLabel = new Label(container, SWT.NULL);
fileNameLabel.setText("&File name:");
fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
fileText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
fileText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
Label fileNameExtensionLabel = new Label(container, SWT.NULL);
fileNameExtensionLabel.setText(".mdsl");
Label projectLabel = new Label(container, SWT.NULL);
projectLabel.setText("&Project:");
containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
containerText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
containerText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
Button button = new Button(container, SWT.PUSH);
button.setText("Browse...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
handleBrowse();
}
});
initialize();
dialogChanged();
setControl(container);
}
/**
* Tests if the current workbench selection is a suitable container to use.
*/
private void initialize() {
if (selection == null) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
selection = window.getSelectionService().getSelection("org.eclipse.ui.navigator.ProjectExplorer");
}
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 IContainer)
container = (IContainer) obj;
else
container = ((IResource) obj).getParent();
containerText.setText(container.getFullPath().toString());
}
}
String name = "newMapper";
int i=1;
while(ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName() + "/" + name + ".mdsl")) != null) {
name = "newMapper" + String.valueOf(i);
i++;
}
fileText.setText("newSpecification");
}
/**
* Uses the standard container selection dialog to choose the new value for
* the container field.
*/
private void handleBrowse() {
ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, "Select project");
if (dialog.open() == ContainerSelectionDialog.OK) {
Object[] result = dialog.getResult();
if (result.length == 1) {
Path path = (Path) result[0];
if (path.segmentCount() > 1) path = (Path) path.removeLastSegments(path.segmentCount()-1);
containerText.setText(path.toString());
}
}
}
/**
* 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("Project must be specified");
return;
}
if (container == null
|| (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
updateStatus("Project 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;
}
IResource temp = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName() + "/Mapping/" + fileName + ".mdsl"));
if (temp !=null) {
if (temp.exists()) {
updateStatus("File already exists");
return;
}
}
updateStatus(null);
}
private void updateStatus(String message) {
setErrorMessage(message);
setPageComplete(message == null);
}
public String getContainerName() {
return containerText.getText();
}
public String getFileName() {
return fileText.getText();
}
private Text containerText;
private Text fileText;
private ISelection selection;
/**
* Constructor
*
* @param pageName
*/
public NewMappingSpecificationWizardPage(ISelection selection) {
super("Create Specification");
setTitle("Create a new mapping specification");
setDescription("This wizard creates a new mapping specification file with *.mdsl extension.");
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 fileNameLabel = new Label(container, SWT.NULL);
fileNameLabel.setText("&File name:");
fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
fileText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
fileText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
Label fileNameExtensionLabel = new Label(container, SWT.NULL);
fileNameExtensionLabel.setText(".mdsl");
Label projectLabel = new Label(container, SWT.NULL);
projectLabel.setText("&Project:");
containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
containerText.setEditable(false);
containerText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
containerText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
Button button = new Button(container, SWT.PUSH);
button.setText("Browse...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
handleBrowse();
}
});
initialize();
dialogChanged();
setControl(container);
}
/**
* Tests if the current workbench selection is a suitable container to use.
*/
private void initialize() {
if (selection.isEmpty()) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
selection = window.getSelectionService().getSelection("org.eclipse.ui.navigator.ProjectExplorer");
}
if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) selection;
if (ssel.size() > 1)
return;
Object obj = ssel.getFirstElement();
IContainer container;
// If a project or a file is preselected
if (obj instanceof IResource) {
if (obj instanceof IContainer)
container = (IContainer) obj;
else
container = ((IResource) obj).getProject();
// No leading "/"
containerText.setText(container.getFullPath().toString().substring(1));
}
// If a package (subfolder) is preselected)
else if (obj instanceof JavaElement) {
container = ((JavaElement) obj).getResource().getProject();
// No leading "/"
containerText.setText(container.getFullPath().toString().substring(1));
}
// If the classpath container got selected
else if (obj instanceof PackageFragmentRootContainer) {
container = ((PackageFragmentRootContainer) obj).getJavaProject().getProject();
// No leading "/"
containerText.setText(container.getFullPath().toString().substring(1));
}
}
fileText.setText("newSpecification");
}
/**
* Uses the standard container selection dialog to choose the new value for the
* container field.
*/
private void handleBrowse() {
ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
ResourcesPlugin.getWorkspace().getRoot(), false, "Select project");
if (dialog.open() == ContainerSelectionDialog.OK) {
Object[] result = dialog.getResult();
if (result.length == 1) {
Path path = (Path) result[0];
if (path.segmentCount() > 1)
path = (Path) path.removeLastSegments(path.segmentCount() - 1);
// No leading "/"
containerText.setText(path.toString().substring(1));
}
}
}
/**
* 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("Project must be specified");
return;
}
if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
updateStatus("Project 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;
}
IResource temp = ResourcesPlugin.getWorkspace().getRoot()
.findMember(new Path(getContainerName() + "/Mapping/" + fileName + ".mdsl"));
if (temp != null) {
if (temp.exists()) {
updateStatus("File already exists");
return;
}
}
updateStatus(null);
}
private void updateStatus(String message) {
setErrorMessage(message);
setPageComplete(message == null);
}
public String getContainerName() {
return containerText.getText();
}
public String getFileName() {
return fileText.getText();
}
}

0 comments on commit 80db42a

Please sign in to comment.