Search in sources :

Example 11 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class FileSource method createControls.

/**
 * @see ImportSource#createControls(Composite)
 */
@Override
public void createControls(Composite parent) {
    parent.setLayout(new GridLayout(4, false));
    ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
    projectLocation = ps.getLoadLocation() == null ? null : ps.getLoadLocation();
    boolean projectLocAvailable = projectLocation != null && "file".equals(projectLocation.getScheme());
    // source file
    sourceFile = new FileSourceFileFieldEditor("sourceFile", "Source file:", FileFieldEditor.VALIDATE_ON_KEY_STROKE, parent, projectLocation);
    sourceFile.setEmptyStringAllowed(false);
    sourceFile.setPage(getPage());
    // set content types for file field
    Collection<IOProviderDescriptor> factories = getConfiguration().getFactories();
    supportedTypes = new HashSet<IContentType>();
    for (IOProviderDescriptor factory : factories) {
        supportedTypes.addAll(factory.getSupportedTypes());
    }
    sourceFile.setContentTypes(supportedTypes);
    sourceFile.setPropertyChangeListener(new IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(FieldEditor.IS_VALID)) {
                updateState(true);
            } else if (event.getProperty().equals(FieldEditor.VALUE)) {
                updateState(true);
            }
        }
    });
    // provider selection
    // label
    Label providerLabel = new Label(parent, SWT.NONE);
    providerLabel.setText("Import as");
    // create provider combo
    ComboViewer providers = createProviders(parent);
    providers.getControl().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
    final Button relativeCheck = new Button(parent, SWT.CHECK);
    String text = "Use relative paths if possible.";
    relativeCheck.setText("Use relative paths if possible.");
    relativeCheck.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            sourceFile.setUseRelativeIfPossible(relativeCheck.getSelection());
        }
    });
    if (!projectLocAvailable) {
        relativeCheck.setEnabled(false);
        text += " Only available once the project is saved to a file.";
    }
    relativeCheck.setText(text);
    relativeCheck.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 4, 1));
    // initial state update
    updateState(true);
}
Also used : IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) IContentType(org.eclipse.core.runtime.content.IContentType) GridLayout(org.eclipse.swt.layout.GridLayout) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 12 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class URLSource method createControls.

/**
 * @see ImportSource#createControls(Composite)
 */
@Override
public void createControls(Composite parent) {
    parent.setLayout(new GridLayout(3, false));
    detectImage = HALEUIPlugin.getImageDescriptor("icons/find_obj.gif").createImage();
    // source file
    sourceURL = new URLSourceURIFieldEditor("sourceURL", "Source URL:", parent) {

        @Override
        protected void onHistorySelected(URI location, IContentType contentType) {
            // select the content type associated with the recent URL
            types.setSelection(new StructuredSelection(contentType));
            updateState(false);
        }
    };
    sourceURL.setPage(getPage());
    // set content types for file field
    Collection<IOProviderDescriptor> factories = getConfiguration().getFactories();
    supportedTypes = new HashSet<IContentType>();
    for (IOProviderDescriptor factory : factories) {
        supportedTypes.addAll(factory.getSupportedTypes());
    }
    sourceURL.setContentTypes(supportedTypes);
    sourceURL.setPropertyChangeListener(new IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(FieldEditor.IS_VALID)) {
                getPage().setMessage(null);
                updateState(false);
            } else if (event.getProperty().equals(FieldEditor.VALUE)) {
                getPage().setMessage(null);
                updateState(false);
            }
        }
    });
    // content type selection
    // label
    Label typesLabel = new Label(parent, SWT.NONE);
    typesLabel.setText("Content type");
    // types combo
    Composite group = new Composite(parent, SWT.NONE);
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
    group.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
    types = new ComboViewer(group, SWT.DROP_DOWN | SWT.READ_ONLY);
    types.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    types.setContentProvider(ArrayContentProvider.getInstance());
    types.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            if (element instanceof IContentType) {
                return ((IContentType) element).getName();
            }
            return super.getText(element);
        }
    });
    types.setInput(supportedTypes);
    // process selection changes
    types.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            updateState(true);
        }
    });
    // detect button
    detect = new Button(group, SWT.PUSH);
    detect.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    detect.setText("Detect");
    detect.setImage(detectImage);
    detect.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            runDetectContentType();
        }
    });
    // provider selection
    // label
    Label providerLabel = new Label(parent, SWT.NONE);
    providerLabel.setText("Import as");
    // create provider combo
    ComboViewer providers = createProviders(parent);
    providers.getControl().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
    // initial state update
    updateState(true);
}
Also used : IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Label(org.eclipse.swt.widgets.Label) IContentType(org.eclipse.core.runtime.content.IContentType) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) URI(java.net.URI) GridLayout(org.eclipse.swt.layout.GridLayout) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Example 13 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class IOReferenceContent method getIOProviderContent.

/**
 * @param providerId The id of the provider
 * @return The in input stream or null, if the given provider does not
 *         exist.
 * @throws Exception - if an error occurs
 */
private InputStream getIOProviderContent(String providerId) throws Exception {
    final String id = providerId;
    // try to get I/O provider
    IOProviderDescriptor providerDescriptor = HaleIO.findIOProviderFactory(IOProvider.class, null, providerId);
    if (providerDescriptor == null) {
        log.warn("I/O provider " + id + " does not exist.");
        return null;
    }
    final IOProviderDescriptor descriptor = providerDescriptor;
    return getContentFromTemplate(providerId, TEMPLATE_PROVIDER, new Callable<VelocityContext>() {

        @Override
        public VelocityContext call() throws Exception {
            VelocityContext context = new VelocityContext();
            // I/O provider descriptor
            context.put("descriptor", descriptor);
            // I/O provider type
            String type = (ImportProvider.class.isAssignableFrom(descriptor.getProviderType())) ? ("reader") : ("writer");
            context.put("providerType", type);
            // collect all optional parameter
            Collection<IOProviderParameter> parameter = new ArrayList<IOProviderParameter>();
            Map<String, String> example = new HashMap<String, String>();
            Map<String, String> defaults = new HashMap<String, String>();
            for (IOProviderParameter param : descriptor.getProviderParameter()) {
                parameter.add(param);
                // get example use of parameter
                if (param.getValueDescriptor() != null) {
                    // store sample value
                    Value sample = param.getValueDescriptor().getSampleData();
                    String displayValue = toDisplayValue(sample);
                    if (displayValue != null) {
                        example.put(param.getName(), displayValue);
                    }
                    // store default value
                    Value defaultValue = param.getValueDescriptor().getDefaultValue();
                    displayValue = toDisplayValue(defaultValue);
                    if (displayValue != null) {
                        defaults.put(param.getName(), displayValue);
                    }
                }
            }
            context.put("parameter", parameter);
            context.put("example", example);
            context.put("defaults", defaults);
            return context;
        }
    });
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) VelocityContext(org.apache.velocity.VelocityContext) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) TransformerException(javax.xml.transform.TransformerException) IOProviderParameter(eu.esdihumboldt.hale.common.core.parameter.IOProviderParameter) Value(eu.esdihumboldt.hale.common.core.io.Value) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class HaleIO method findIOProviderFactory.

/**
 * Find an I/O provider factory
 *
 * @param
 * 			<P>
 *            the provider interface type
 *
 * @param providerType the provider type, usually an interface
 * @param contentType the content type the provider must match, may be
 *            <code>null</code> if providerId is set
 * @param providerId the id of the provider to use, may be <code>null</code>
 *            if contentType is set
 * @return the I/O provider factory or <code>null</code> if no matching I/O
 *         provider factory is found
 */
public static <P extends IOProvider> IOProviderDescriptor findIOProviderFactory(Class<P> providerType, IContentType contentType, String providerId) {
    Preconditions.checkArgument(contentType != null || providerId != null);
    Collection<IOProviderDescriptor> factories = getProviderFactories(providerType);
    if (contentType != null) {
        factories = filterFactories(factories, contentType);
    }
    IOProviderDescriptor result = null;
    if (providerId != null) {
        for (IOProviderDescriptor factory : factories) {
            if (factory.getIdentifier().equals(providerId)) {
                result = factory;
                break;
            }
        }
    } else {
        // TODO choose priority based?
        if (!factories.isEmpty()) {
            result = factories.iterator().next();
        }
    }
    return result;
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)

Example 15 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class HaleIO method findContentType.

/**
 * Find the content type for the given input
 *
 * @param
 * 			<P>
 *            the provider interface type
 *
 * @param providerType the provider type, usually an interface
 * @param in the input supplier to use for testing, may be <code>null</code>
 *            if the file name is not <code>null</code>
 * @param filename the file name, may be <code>null</code> if the input
 *            supplier is not <code>null</code>
 * @return the content type or <code>null</code> if no matching content type
 *         is found
 */
public static <P extends IOProvider> IContentType findContentType(Class<P> providerType, InputSupplier<? extends InputStream> in, String filename) {
    Collection<IOProviderDescriptor> providers = getProviderFactories(providerType);
    // collect supported content types
    Set<IContentType> supportedTypes = new HashSet<IContentType>();
    for (IOProviderDescriptor factory : providers) {
        supportedTypes.addAll(factory.getSupportedTypes());
    }
    // find matching content type
    List<IContentType> types = findContentTypesFor(supportedTypes, in, filename);
    if (types == null || types.isEmpty()) {
        return null;
    }
    // TODO choose?
    return types.iterator().next();
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IContentType(org.eclipse.core.runtime.content.IContentType) HashSet(java.util.HashSet)

Aggregations

IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)40 IContentType (org.eclipse.core.runtime.content.IContentType)17 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)8 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)8 Label (org.eclipse.swt.widgets.Label)7 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 ComboViewer (org.eclipse.jface.viewers.ComboViewer)6 SelectionEvent (org.eclipse.swt.events.SelectionEvent)6 GridData (org.eclipse.swt.layout.GridData)6 Composite (org.eclipse.swt.widgets.Composite)6 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)5 ArrayList (java.util.ArrayList)5 IPropertyChangeListener (org.eclipse.jface.util.IPropertyChangeListener)5 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)5 GridLayout (org.eclipse.swt.layout.GridLayout)5 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)4 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)4 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)4 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)4