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);
}
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);
}
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;
}
});
}
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;
}
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();
}
Aggregations