Skip to content

Commit

Permalink
fixup! feat: enable selecting more than one file when loading schema …
Browse files Browse the repository at this point in the history
…or data (#940)

Change to enable autoDetect checkbox by default when the user selects
multiple Shapefiles for data import. And by default un-check it when
only one file is selected.

ING-2969
  • Loading branch information
kapil-agnihotri authored Nov 16, 2021
1 parent 8755f27 commit a95983e
Showing 1 changed file with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package eu.esdihumboldt.hale.io.shp.ui;

import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.List;

import javax.xml.namespace.QName;

Expand All @@ -36,6 +39,7 @@

import eu.esdihumboldt.hale.common.core.io.IOProvider;
import eu.esdihumboldt.hale.common.core.io.Value;
import eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier;
import eu.esdihumboldt.hale.common.core.io.supplier.LocatableInputSupplier;
import eu.esdihumboldt.hale.common.instance.io.InstanceReader;
import eu.esdihumboldt.hale.common.schema.model.TypeDefinition;
Expand Down Expand Up @@ -66,6 +70,8 @@ public class TypeSelectionPage extends InstanceReaderConfigurationPage

private Button matchShortPropertyNames;

private boolean defaultSelection = false;

/**
* Button to enable auto detection of the schemas. Useful when importing
* multiple source data for multiple schema files.
Expand Down Expand Up @@ -112,10 +118,33 @@ protected void onShowPage(boolean firstShow) {
getWizard().getProvider().getSourceSchema(), null);
selector.getControl().setLayoutData(
GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());

selector.addSelectionChangedListener(new ISelectionChangedListener() {

@Override
public void selectionChanged(SelectionChangedEvent event) {
TypeDefinition selectedObject = selector.getSelectedObject();
// user selected None in the dialog box. Reset to default.
if (selectedObject == null) {
autoDetect.setSelection(true);
autoDetect.setEnabled(true);
defaultSelection = false;
validateSelection();
return;
}
if (!defaultSelection) {
// if the selection is changed then disable the
// autoDetect as the user wants to use just a single
// type.
autoDetect.setSelection(false);
autoDetect.setEnabled(false);
}
else {
// it is the first call to this listener which was
// triggered by the code so do not disable the
// autodetect.
defaultSelection = false;
}
validateSelection();
}
});
Expand All @@ -137,25 +166,43 @@ public void selectionChanged(SelectionChangedEvent event) {
public void widgetSelected(SelectionEvent e) {
if (autoDetect.getSelection()) {
selector.getControl().setEnabled(false);
setMessage(
"All the types will be mapped automatically to their respective file structures.",
DialogPage.INFORMATION);
setPageComplete(true);
}
else {
selector.getControl().setEnabled(true);
// reset the message in the window asking user to select
// schema.
setMessage(null);
}
}

@Override
public void widgetDefaultSelected(SelectionEvent e) {
// default selection is false.
selector.getControl().setEnabled(true);

}
});

page.layout();
page.pack();
}

populateSelector();

setDefaultOptions();
}

/**
* Populate selector by default based on the compatibility of the schema and
* the selected data file.
*/
private void populateSelector() {
LocatableInputSupplier<? extends InputStream> currentSource = getWizard().getProvider()
.getSource();

// avoid NPE when relative path check box is selected when loading the
// schema.
if (currentSource != null && !currentSource.equals(lastSource)) {
Expand All @@ -171,6 +218,7 @@ public void widgetDefaultSelected(SelectionEvent e) {
.getMostCompatibleShapeType(getWizard().getProvider().getSourceSchema(),
lastType, lastType.getName().getLocalPart());
if (pt != null) {
defaultSelection = true;
selector.setSelection(new StructuredSelection(pt.getFirst()));
}
}
Expand All @@ -180,6 +228,33 @@ public void widgetDefaultSelected(SelectionEvent e) {
}
}

/**
* Set default options to the checkbox based on if the user navigated from
* single file import or multiple file import.
*/
private void setDefaultOptions() {
// check how many data files were imported by the user.
List<URI> locations = null;
if (lastSource instanceof FilesIOSupplier) {
locations = ((FilesIOSupplier) lastSource).getLocations();
}
else {
locations = Arrays.asList(lastSource.getLocation());
}

if (locations != null && locations.size() > 1) {
// set auto detect to true as the user navigated from multiple
// files selection
autoDetect.setSelection(true);
selector.setSelection(StructuredSelection.EMPTY);
}
else {
// user navigated from single file selection
autoDetect.setSelection(false);
setMessage(null);
}
}

/**
* Validate the current selection. {@link #onShowPage(boolean)} must have
* been called first to set {@link #lastType}.
Expand Down Expand Up @@ -214,7 +289,12 @@ protected void validateSelection() {
}
}
else {
setMessage(null);
setPageComplete(true);
setMessage(
"All the types will be mapped automatically to their respective file structures.",
DialogPage.INFORMATION);

return;
}

setPageComplete(false);
Expand Down Expand Up @@ -256,10 +336,15 @@ protected void createContent(Composite page) {
*/
@Override
public boolean updateConfiguration(InstanceReader provider) {
if (selector.getSelectedObject() != null) {
QName name = selector.getSelectedObject().getName();
provider.setParameter(PARAM_TYPENAME, Value.of(name.toString()));

// make sure if the selection box is empty and autoDetect is checked
// then the user should be able to finish the wizard.
if ((autoDetect.getSelection() && selector.getSelectedObject() == null)
|| selector.getSelectedObject() != null) {
if (selector.getSelectedObject() != null) {
QName name = selector.getSelectedObject().getName();
provider.setParameter(PARAM_TYPENAME, Value.of(name.toString()));
}
provider.setParameter(PARAM_MATCH_SHORT_PROPERTY_NAMES,
Value.of(matchShortPropertyNames.getSelection()));
provider.setParameter(PARAM_AUTO_DETECT_SCHEMA_TYPES,
Expand Down

0 comments on commit a95983e

Please sign in to comment.