Search in sources :

Example 1 with Pair

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);
}
Also used : TransformationTreeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TransformationTreeImpl) Entity(eu.esdihumboldt.hale.common.align.model.Entity) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) ArrayList(java.util.ArrayList) TransformationTree(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree) LinkedList(java.util.LinkedList) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) Collection(java.util.Collection) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Pair(eu.esdihumboldt.util.Pair)

Example 2 with Pair

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;
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) LinkedList(java.util.LinkedList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(eu.esdihumboldt.util.Pair)

Example 3 with Pair

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;
}
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 4 with Pair

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();
        }
    });
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) TreeEditor(org.eclipse.swt.custom.TreeEditor) MetadataActionExtension(eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionExtension) TreeItem(org.eclipse.swt.widgets.TreeItem) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) ViewerCell(org.eclipse.jface.viewers.ViewerCell) URL(java.net.URL) MouseMoveListener(org.eclipse.swt.events.MouseMoveListener) MetadataActionFactory(eu.esdihumboldt.hale.common.instance.extension.metadata.MetadataActionFactory) Control(org.eclipse.swt.widgets.Control) ToolBar(org.eclipse.swt.widgets.ToolBar) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) ToolItem(org.eclipse.swt.widgets.ToolItem) Pair(eu.esdihumboldt.util.Pair)

Example 5 with Pair

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);
}
Also used : TreeColumnViewerLabelProvider(org.eclipse.jface.viewers.TreeColumnViewerLabelProvider) ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) Set(java.util.Set) HashSet(java.util.HashSet) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TreeColumnLayout(org.eclipse.jface.layout.TreeColumnLayout) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) Layout(org.eclipse.swt.widgets.Layout) TreeColumnLayout(org.eclipse.jface.layout.TreeColumnLayout) TreeColumn(org.eclipse.swt.widgets.TreeColumn) HashSet(java.util.HashSet) Pair(eu.esdihumboldt.util.Pair)

Aggregations

Pair (eu.esdihumboldt.util.Pair)33 ArrayList (java.util.ArrayList)11 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 List (java.util.List)7 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 Cell (eu.esdihumboldt.hale.common.align.model.Cell)4 Entity (eu.esdihumboldt.hale.common.align.model.Entity)4 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 Group (eu.esdihumboldt.hale.common.instance.model.Group)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 Point (org.eclipse.swt.graphics.Point)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)3 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Collection (java.util.Collection)3 QName (javax.xml.namespace.QName)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3