use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class TransformationTreeContentProvider method getElements.
/**
* @see ArrayContentProvider#getElements(Object)
*/
@SuppressWarnings("unchecked")
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof TransformationTree)
return collectNodes((TransformationTree) inputElement).toArray();
Collection<Instance> instances = null;
if (inputElement instanceof Pair<?, ?>) {
Pair<?, ?> pair = (Pair<?, ?>) inputElement;
inputElement = pair.getFirst();
if (pair.getSecond() instanceof Collection<?>) {
instances = (Collection<Instance>) pair.getSecond();
}
}
if (inputElement instanceof Alignment) {
Alignment alignment = (Alignment) inputElement;
// input contained specific instances
if (instances != null && !instances.isEmpty()) {
Collection<Object> result = new ArrayList<Object>();
// create transformation trees for each instance
for (Instance instance : instances) {
// find type cells matching the instance
Collection<? extends Cell> typeCells = alignment.getActiveTypeCells();
Collection<Cell> associatedTypeCells = new LinkedList<Cell>();
for (Cell typeCell : typeCells) for (Entity entity : typeCell.getSource().values()) {
TypeEntityDefinition type = (TypeEntityDefinition) entity.getDefinition();
if (type.getDefinition().equals(instance.getDefinition()) && (type.getFilter() == null || type.getFilter().match(instance))) {
associatedTypeCells.add(typeCell);
break;
}
}
// for each type cell one tree
for (Cell cell : associatedTypeCells) {
TransformationTree tree = createInstanceTree(instance, cell, alignment);
if (tree != null)
result.addAll(collectNodes(tree));
}
}
return result.toArray();
}
// input was alignment only, show trees for all type cells
Collection<? extends Cell> typeCells = alignment.getActiveTypeCells();
Collection<Object> result = new ArrayList<Object>(typeCells.size());
for (Cell typeCell : typeCells) {
// create tree and add nodes for each cell
result.addAll(collectNodes(new TransformationTreeImpl(alignment, typeCell)));
}
return result.toArray();
}
return super.getElements(inputElement);
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class AbstractGeometrySchemaService method determineDefaultGeometry.
/**
* Determine the path to a geometry property to be used as default geometry
* for the given type. By default the first geometry property found with a
* breadth first search is used.
*
* @param type the type definition
* @return the path to the default geometry property or <code>null</code> if
* unknown
*/
protected List<QName> determineDefaultGeometry(TypeDefinition type) {
// breadth first search for geometry properties
Queue<Pair<List<QName>, DefinitionGroup>> groups = new LinkedList<Pair<List<QName>, DefinitionGroup>>();
groups.add(new Pair<List<QName>, DefinitionGroup>(new ArrayList<QName>(), type));
List<QName> firstGeometryPath = null;
while (firstGeometryPath == null && !groups.isEmpty()) {
// for each parent group...
Pair<List<QName>, DefinitionGroup> group = groups.poll();
DefinitionGroup parent = group.getSecond();
List<QName> parentPath = group.getFirst();
// max depth for default geometries
if (parentPath.size() > 5)
continue;
// queue
for (ChildDefinition<?> child : DefinitionUtil.getAllChildren(parent)) {
// path for child
List<QName> path = new ArrayList<QName>(parentPath);
path.add(child.getName());
if (child.asProperty() != null) {
PropertyDefinition property = child.asProperty();
TypeDefinition propertyType = property.getPropertyType();
// check if we found a geometry property
if (propertyType.getConstraint(GeometryType.class).isGeometry()) {
// match
firstGeometryPath = path;
} else {
// test children later on
groups.add(new Pair<List<QName>, DefinitionGroup>(path, propertyType));
}
} else if (child.asGroup() != null) {
// test group later on
GroupPropertyDefinition childGroup = child.asGroup();
groups.add(new Pair<List<QName>, DefinitionGroup>(path, childGroup));
} else {
throw new IllegalStateException("Invalid child definition encountered");
}
}
}
if (firstGeometryPath != null) {
// a geometry property was found
return generalizeGeometryProperty(type, firstGeometryPath);
} else {
return null;
}
}
use of eu.esdihumboldt.util.Pair 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.util.Pair in project hale by halestudio.
the class MetadataActionProvider method setup.
/**
* Adds the action to the certain TreeViewer cell
*/
public void setup() {
final TreeEditor metaEditor = new TreeEditor(treeViewer.getTree());
metaEditor.horizontalAlignment = SWT.LEFT;
metaEditor.verticalAlignment = SWT.TOP;
treeViewer.getTree().addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(MouseEvent e) {
final ViewerCell cell = treeViewer.getCell(new Point(e.x, e.y));
// Selected cell changed?
if (cell == null || metaEditor.getItem() != cell.getItem() || metaEditor.getColumn() != cell.getColumnIndex()) {
// Clean up any previous editor control
Control oldmetaEditor = metaEditor.getEditor();
if (oldmetaEditor != null) {
oldmetaEditor.dispose();
metaEditor.setEditor(null, null, 0);
}
}
// No selected cell or selected cell didn't change.
if (cell == null || cell.getColumnIndex() == 0 || (metaEditor.getItem() == cell.getItem() && metaEditor.getColumn() == cell.getColumnIndex())) {
return;
}
// Initiate the extension-point
MetadataActionExtension mae = MetadataActionExtension.getInstance();
final Pair<Object, Object> data = retrieveMetadata(cell);
if (data == null) {
return;
}
// get all defined actions
final List<MetadataActionFactory> actionSources = mae.getMetadataActions(data.getFirst().toString());
if (actionSources == null || actionSources.isEmpty()) {
return;
}
// Tool-bar used to view and trigger the different possible
// actions
ToolBar tooli = new ToolBar(treeViewer.getTree(), SWT.NONE);
for (final MetadataActionFactory source : actionSources) {
ToolItem actionItem = new ToolItem(tooli, SWT.PUSH);
// get the Icon of the action
URL iconURL = source.getIconURL();
Image image = ImageDescriptor.createFromURL(iconURL).createImage();
actionItem.setImage(image);
actionItem.setToolTipText(source.getDisplayName());
actionItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
source.createExtensionObject().execute(data.getFirst(), data.getSecond());
} catch (Exception e1) {
_log.userError("error creating metadata action", e1);
}
}
});
}
metaEditor.setEditor(tooli, (TreeItem) cell.getItem(), cell.getColumnIndex());
tooli.pack();
}
});
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class DefinitionInstanceTreeViewer method setInput.
/**
* @see InstanceViewer#setInput(TypeDefinition, Iterable)
*/
@Override
public void setInput(TypeDefinition type, Iterable<Instance> instances) {
// remove old columns
TreeColumn[] columns = treeViewer.getTree().getColumns();
if (columns != null) {
for (TreeColumn column : columns) {
column.dispose();
}
labelProviders.clear();
}
// set input
if (type != null) {
// pass metadatas to the treeviewer, if instances contain metadatas
Set<String> completeMetaNames = new HashSet<String>();
for (Instance inst : instances) {
for (String name : inst.getMetaDataNames()) {
completeMetaNames.add(name);
}
}
Pair<TypeDefinition, Set<String>> pair = new Pair<TypeDefinition, Set<String>>(type, completeMetaNames);
treeViewer.setInput(pair);
} else
treeViewer.setInput(Collections.emptySet());
Layout layout = treeViewer.getTree().getParent().getLayout();
// add type column
if (type != null) {
TreeViewerColumn column = new TreeViewerColumn(treeViewer, SWT.LEFT);
column.getColumn().setText(type.getDisplayName());
column.setLabelProvider(new TreeColumnViewerLabelProvider(new DefinitionMetaCompareLabelProvider(treeViewer)));
if (layout instanceof TreeColumnLayout) {
((TreeColumnLayout) layout).setColumnData(column.getColumn(), new ColumnWeightData(1));
}
}
// add columns for features
int index = 1;
if (instances != null) {
// // sort features
// List<Feature> sortedFeatures = new ArrayList<Feature>();
// for (Feature f : features) {
// sortedFeatures.add(f);
// }
// Collections.sort(sortedFeatures, new Comparator<Feature>() {
//
// @Override
// public int compare(Feature o1, Feature o2) {
// FeatureId id1 = FeatureBuilder.getSourceID(o1);
// if (id1 == null) {
// id1 = o1.getIdentifier();
// }
//
// FeatureId id2 = FeatureBuilder.getSourceID(o2);
// if (id2 == null) {
// id2 = o2.getIdentifier();
// }
//
// return id1.getID().compareTo(id2.getID());
// }
//
// });
List<Instance> insts = new ArrayList<Instance>();
for (Instance instance : instances) {
// sortedFeatures) {
final TreeViewerColumn column = new TreeViewerColumn(treeViewer, SWT.LEFT);
// FeatureId id = FeatureBuilder.getSourceID(feature);
// if (id == null) {
// id = feature.getIdentifier();
// }
// column.getColumn().setText(id.toString());
// XXX
column.getColumn().setText(String.valueOf(index));
// identifier?
DefinitionInstanceLabelProvider labelProvider = new DefinitionInstanceLabelProvider(instance);
labelProviders.put(index, labelProvider);
column.setLabelProvider(labelProvider);
if (layout instanceof TreeColumnLayout) {
((TreeColumnLayout) layout).setColumnData(column.getColumn(), new ColumnWeightData(1));
}
// add tool tip
// new ColumnBrowserTip(treeViewer, 400, 300, true, index, null);
insts.add(instance);
index++;
}
((MetadataCompareActionProvider) maep).setInput(insts, labelProviders);
}
treeViewer.refresh();
treeViewer.getTree().getParent().layout(true, true);
selectionProvider.updateSelection(instances);
// auto-expand attributes/metadata
treeViewer.expandToLevel(2);
}
Aggregations