Search in sources :

Example 1 with DriverConfiguration

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;
}
Also used : DriverConfiguration(eu.esdihumboldt.hale.io.jdbc.extension.DriverConfiguration) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Driver(java.sql.Driver) ComboViewer(org.eclipse.jface.viewers.ComboViewer) LabelProvider(org.eclipse.jface.viewers.LabelProvider) Pair(eu.esdihumboldt.util.Pair)

Example 2 with DriverConfiguration

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;
}
Also used : DriverConfiguration(eu.esdihumboldt.hale.io.jdbc.extension.DriverConfiguration) ISelection(org.eclipse.jface.viewers.ISelection) Driver(java.sql.Driver) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) URI(java.net.URI) Pair(eu.esdihumboldt.util.Pair) Nullable(javax.annotation.Nullable)

Aggregations

DriverConfiguration (eu.esdihumboldt.hale.io.jdbc.extension.DriverConfiguration)2 Pair (eu.esdihumboldt.util.Pair)2 Driver (java.sql.Driver)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 ComboViewer (org.eclipse.jface.viewers.ComboViewer)1 ISelection (org.eclipse.jface.viewers.ISelection)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1