use of eu.esdihumboldt.hale.io.jdbc.extension.DriverConfiguration in project hale by halestudio.
the class JDBCComponents method createDriverSelector.
/**
* Create a component for selecting a JDBC driver.
*
* @param parent the parent composite
* @return the combo viewer for selecting the driver
*/
public static ComboViewer createDriverSelector(Composite parent) {
ComboViewer driver = new ComboViewer(parent, SWT.BORDER | SWT.READ_ONLY);
driver.setContentProvider(ArrayContentProvider.getInstance());
driver.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof Pair<?, ?>) {
@SuppressWarnings("unchecked") Pair<DriverConfiguration, Driver> driverInfo = (Pair<DriverConfiguration, Driver>) element;
return driverInfo.getFirst().getName();
}
return super.getText(element);
}
});
// driver input
List<Pair<DriverConfiguration, Driver>> drivers = new ArrayList<>();
for (DriverConfiguration dc : DriverConfigurationExtension.getInstance().getElements()) {
if (dc.isFileBased())
continue;
// determine associated driver
Driver jdbcDriver = null;
Enumeration<Driver> enDrivers = DriverManager.getDrivers();
while (enDrivers.hasMoreElements()) {
Driver candidate = enDrivers.nextElement();
if (dc.matchesDriver(candidate)) {
jdbcDriver = candidate;
break;
}
}
// if (driver != null) {
// XXX ignore if the driver is null, seems to work nonetheless
drivers.add(new Pair<>(dc, jdbcDriver));
// }
}
driver.setInput(drivers);
if (!drivers.isEmpty()) {
// by default select a driver if possible
driver.setSelection(new StructuredSelection(drivers.get(0)));
}
return driver;
}
use of eu.esdihumboldt.hale.io.jdbc.extension.DriverConfiguration in project hale by halestudio.
the class JDBCComponents method buildURI.
/**
* Build a JDBC URI from UI components
*
* @param page the dialog page
* @param driver the driver selector
* @param host the host field
* @param database the database field
* @return the JDBC URI or <code>null</code>
*/
@Nullable
public static URI buildURI(DialogPage page, ComboViewer driver, Text host, Text database) {
page.setErrorMessage(null);
if (driver != null) {
ISelection sel = driver.getSelection();
if (!sel.isEmpty() && sel instanceof IStructuredSelection) {
@SuppressWarnings({ "unchecked" }) Pair<DriverConfiguration, Driver> driverInfo = (Pair<DriverConfiguration, Driver>) ((IStructuredSelection) sel).getFirstElement();
try {
URI uri = driverInfo.getFirst().getURIBuilder().createJdbcUri(host.getText(), database.getText());
if (driverInfo.getSecond() == null || driverInfo.getSecond().acceptsURL(uri.toString())) {
return uri;
}
} catch (Exception e) {
page.setErrorMessage(e.getLocalizedMessage());
}
}
page.setMessage(null);
} else {
page.setMessage("Please select a JDBC database driver", DialogPage.INFORMATION);
}
return null;
}
Aggregations