Search in sources :

Example 21 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class DefinitionImages method getImage.

/**
 * Get the image for the given definition
 *
 * @param entityDef the entity definition, may be <code>null</code>
 * @param def the definition
 * @return the image, may be <code>null</code>
 */
protected Image getImage(EntityDefinition entityDef, Definition<?> def) {
    Classification clazz = Classification.getClassification(def);
    String imageName = getImageForClassification(clazz);
    // retrieve image for key
    Image image;
    if (imageName == null) {
        // default
        imageName = ISharedImages.IMG_OBJ_ELEMENT;
        image = PlatformUI.getWorkbench().getSharedImages().getImage(imageName);
    } else {
        image = CommonSharedImages.getImageRegistry().get(imageName);
    }
    // XXX not supported yet
    if (def instanceof TypeDefinition && !((TypeDefinition) def).getConstraint(AbstractFlag.class).isEnabled() && hasGeometry((TypeDefinition) def)) {
        TypeDefinition type = (TypeDefinition) def;
        DataSet dataSet = DataSet.SOURCE;
        // defined styles
        if (entityDef != null) {
            dataSet = DataSet.forSchemaSpace(entityDef.getSchemaSpace());
        }
        String typeKey = dataSet.name() + "::" + type.getIdentifier();
        // XXX check if style image is already there?
        // XXX how to handle style changes?
        BufferedImage img = getLegendImage(type, dataSet, true);
        if (img != null) {
            // replace image with style image
            ImageData imgData = SwingRcpUtilities.convertToSWT(img);
            image = new Image(Display.getCurrent(), imgData);
            final Image old;
            if (styleImages.containsKey(typeKey)) {
                old = styleImages.get(typeKey);
            } else {
                old = null;
            }
            styleImages.put(typeKey, image);
            if (old != null) {
                // schedule image to be disposed when there are no
                // references to it
                handles.addReference(image);
            }
        }
    } else // check for inline attributes
    {
        boolean attribute = (def instanceof PropertyDefinition) && ((PropertyDefinition) def).getConstraint(XmlAttributeFlag.class).isEnabled();
        boolean mandatory = false;
        if (!suppressMandatory) {
            if (def instanceof PropertyDefinition) {
                Cardinality cardinality = ((PropertyDefinition) def).getConstraint(Cardinality.class);
                mandatory = cardinality.getMinOccurs() > 0 && !((PropertyDefinition) def).getConstraint(NillableFlag.class).isEnabled();
            } else if (def instanceof GroupPropertyDefinition) {
                Cardinality cardinality = ((GroupPropertyDefinition) def).getConstraint(Cardinality.class);
                mandatory = cardinality.getMinOccurs() > 0;
            }
        }
        boolean deflt = false;
        boolean faded = false;
        if (entityDef != null) {
            // entity definition needed to determine if item is a default
            // geometry
            deflt = DefaultGeometryUtil.isDefaultGeometry(entityDef);
            // and to determine population
            PopulationService ps = PlatformUI.getWorkbench().getService(PopulationService.class);
            if (ps != null && ps.hasPopulation(entityDef.getSchemaSpace())) {
                Population pop = ps.getPopulation(entityDef);
                faded = (pop != null && pop.getOverallCount() == 0);
            }
        }
        if (deflt || mandatory || attribute || faded) {
            // overlayed image
            ImageConf conf = new ImageConf(imageName, attribute, deflt, mandatory, faded);
            Image overlayedImage = overlayedImages.get(conf);
            if (overlayedImage == null) {
                // apply overlays to image
                Image copy = new Image(image.getDevice(), image.getBounds());
                // draw on image
                GC gc = new GC(copy);
                try {
                    gc.drawImage(image, 0, 0);
                    if (attribute) {
                        gc.drawImage(attribOverlay, 0, 0);
                    }
                    if (deflt) {
                        gc.drawImage(defOverlay, 0, 0);
                    }
                    if (mandatory) {
                        gc.drawImage(mandatoryOverlay, 0, 0);
                    }
                } finally {
                    gc.dispose();
                }
                if (faded) {
                    ImageData imgData = copy.getImageData();
                    imgData.alpha = 150;
                    Image copy2 = new Image(image.getDevice(), imgData);
                    copy.dispose();
                    copy = copy2;
                }
                image = copy;
                overlayedImages.put(conf, copy);
            } else {
                image = overlayedImage;
            }
        }
    }
    return image;
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) DataSet(eu.esdihumboldt.hale.common.instance.model.DataSet) Image(org.eclipse.swt.graphics.Image) BufferedImage(java.awt.image.BufferedImage) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) BufferedImage(java.awt.image.BufferedImage) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ImageData(org.eclipse.swt.graphics.ImageData) Classification(eu.esdihumboldt.hale.common.schema.Classification) PopulationService(eu.esdihumboldt.hale.ui.common.service.population.PopulationService) Population(eu.esdihumboldt.hale.ui.common.service.population.Population) GC(org.eclipse.swt.graphics.GC)

Example 22 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class DefaultDefinitionLabelFactory method createLabel.

/**
 * @see DefinitionLabelFactory#createLabel(Composite, Definition, boolean)
 */
@Override
public Control createLabel(Composite parent, Definition<?> definition, boolean verbose) {
    String name = definition.getDisplayName();
    String description = definition.getDescription();
    if (description != null && !description.isEmpty()) {
        // link for displaying documentation
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        String linkText = "<a href=\"" + definition.getIdentifier() + "\">" + name + "</a>";
        final Map<String, String> tips = new HashMap<String, String>();
        tips.put(definition.getIdentifier(), description);
        if (verbose && definition instanceof PropertyDefinition) {
            TypeDefinition parentType = ((PropertyDefinition) definition).getParentType();
            String typeDescription = parentType.getDescription();
            if (typeDescription != null) {
                tips.put(parentType.getIdentifier(), typeDescription);
                linkText = // $NON-NLS-1$ //$NON-NLS-2$
                "<a href=\"" + parentType.getIdentifier() + "\">" + parentType.getDisplayName() + "</a>." + // $NON-NLS-1$
                linkText;
            } else {
                // $NON-NLS-1$
                linkText = parentType.getDisplayName() + "." + linkText;
            }
        }
        final Link link = new Link(parent, SWT.NONE);
        link.setText(linkText);
        link.addSelectionListener(new SelectionAdapter() {

            private Shell lastShell = null;

            @Override
            public void widgetSelected(SelectionEvent e) {
                // link target is in e.text - but not needed here
                String href = e.text;
                // show tip
                String tip = tips.get(href);
                if (tip != null) {
                    BrowserTip.hideToolTip(lastShell);
                    lastShell = browserTip.showToolTip(link, 0, link.getSize().y, tip);
                }
            }
        });
        return link;
    } else {
        Label label = new Label(parent, SWT.NONE);
        if (verbose && definition instanceof PropertyDefinition) {
            label.setText(((PropertyDefinition) definition).getParentType().getDisplayName() + "." + // $NON-NLS-1$
            name);
        } else {
            label.setText(name);
        }
        return label;
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) HashMap(java.util.HashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Label(org.eclipse.swt.widgets.Label) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Link(org.eclipse.swt.widgets.Link) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 23 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class Pattern method match.

/**
 * Matches the type against the encoding pattern.
 *
 * @param type the type definition
 * @param path the definition path
 * @param gmlNs the GML namespace
 * @param checkedTypes the type definitions that have already been checked
 *            (to prevent cycles)
 * @param remainingElements the remaining elements to match
 *
 * @return the new path if there is a match, <code>null</code> otherwise
 */
private static DefinitionPath match(TypeDefinition type, DefinitionPath path, String gmlNs, HashSet<TypeDefinition> checkedTypes, List<PatternElement> remainingElements) {
    if (remainingElements == null || remainingElements.isEmpty()) {
        return null;
    }
    if (checkedTypes.contains(type)) {
        return null;
    } else {
        checkedTypes.add(type);
    }
    PatternElement first = remainingElements.get(0);
    PatternElement checkAgainst;
    boolean allowAttributeDescent;
    boolean removeFirstForAttributeDescent = false;
    boolean allowSubtypeDescent = true;
    switch(first.getType()) {
        case ONE_ELEMENT:
            // only descend
            checkAgainst = null;
            allowAttributeDescent = true;
            // first element may not be
            removeFirstForAttributeDescent = true;
            // special case: was last element
            if (remainingElements.size() == 1) {
                return path;
            }
            break;
        case ANY_ELEMENTS:
            // check against the next named element
            PatternElement named = null;
            for (int i = 1; i < remainingElements.size() && named == null; i++) {
                PatternElement element = remainingElements.get(i);
                if (element.getType().equals(ElementType.NAMED_ELEMENT)) {
                    named = element;
                }
            }
            if (named == null) {
                // no named element
                return null;
            } else {
                checkAgainst = named;
            }
            allowAttributeDescent = true;
            break;
        case NAMED_ELEMENT:
            // check the current
            checkAgainst = first;
            // only allow sub-type descent
            allowAttributeDescent = false;
            break;
        default:
            // $NON-NLS-1$
            throw new IllegalStateException("Unknown pattern element type");
    }
    if (checkAgainst != null) {
        // get the last path element
        QName elementName = path.getLastName();
        QName name = checkAgainst.getName();
        // inject namespace if needed
        if (name.getNamespaceURI() == GML_NAMESPACE_PLACEHOLDER) {
            name = new QName(gmlNs, name.getLocalPart());
        }
        // check direct match
        if (name.equals(elementName)) {
            // match for the element name -> we are on the right track
            int index = remainingElements.indexOf(checkAgainst);
            if (index == remainingElements.size() - 1) {
                // is last - we have a full match
                return path;
            }
            // remove the element (and any leading wildcards) from the queue
            remainingElements = remainingElements.subList(index + 1, remainingElements.size());
            // for a name match, no sub-type descent is allowed
            allowSubtypeDescent = false;
            // but an attribute descent is ok
            allowAttributeDescent = true;
        } else {
        // no name match
        // sub-type descent is still allowed, don't remove element
        }
    }
    if (allowSubtypeDescent) {
    // step down sub-types
    // XXX now represented in choices
    // XXX sub-type must work through parent choice
    // for (SchemaElement element : type.getSubstitutions(path.getLastName())) {
    // DefinitionPath candidate = match(
    // element.getType(),
    // new DefinitionPath(path).addSubstitution(element),
    // gmlNs,
    // new HashSet<TypeDefinition>(checkedTypes),
    // new ArrayList<PatternElement>(remainingElements));
    // 
    // if (candidate != null) {
    // return candidate;
    // }
    // }
    }
    if (allowAttributeDescent) {
        if (removeFirstForAttributeDescent) {
            remainingElements.remove(0);
        }
        // step down properties
        @java.lang.SuppressWarnings("unchecked") Iterable<ChildDefinition<?>> children = (Iterable<ChildDefinition<?>>) ((path.isEmpty()) ? (type.getChildren()) : (type.getDeclaredChildren()));
        Iterable<DefinitionPath> childPaths = GmlWriterUtil.collectPropertyPaths(children, path, true);
        for (DefinitionPath childPath : childPaths) {
            DefinitionPath candidate = match(childPath.getLastType(), childPath, gmlNs, new HashSet<TypeDefinition>(checkedTypes), new ArrayList<PatternElement>(remainingElements));
            if (candidate != null) {
                return candidate;
            }
        }
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefinitionPath(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.DefinitionPath)

Example 24 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class MsAccessDataReaderTestSuit method schemaReaderTest.

/**
 * Test - reads a sample MsAccess Database schema. UCanAccess lib should not
 * throw any error.
 *
 * @throws Exception if an error occurs
 */
public void schemaReaderTest() throws Exception {
    MsAccessSchemaReader schemaReader = new MsAccessSchemaReader();
    schemaReader.setSource(new FileIOSupplier(getSourceTempFilePath()));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_USER, Value.of(USER_NAME));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_PASSWORD, Value.of(PASSWORD));
    IOReport report = schemaReader.execute(new LogProgressIndicator());
    assertTrue(report.isSuccess());
    TEMP_SOURCE_FILE_NAME = null;
    Schema schema = schemaReader.getSchema();
    assertTrue(schema != null);
    Collection<? extends TypeDefinition> k = schema.getMappingRelevantTypes();
    for (TypeDefinition def : k) System.out.println(def.getDisplayName());
    checkTables(k);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) MsAccessSchemaReader(eu.esdihumboldt.hale.io.jdbc.msaccess.MsAccessSchemaReader) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 25 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class SpatiaLiteTestSuite method schemaReaderTest.

/**
 * Test - reads a sample SpatiaLite schema
 *
 * @throws Exception if an error occurs
 */
public void schemaReaderTest() throws Exception {
    if (!isSpatiaLiteExtensionAvailable()) {
        log.info("Skipping test because SpatiaLite extension is not available");
        return;
    }
    Set<String> propertyNames = new HashSet<String>(Arrays.asList(SOUURCE_TYPE_PROPERTY_NAMES));
    SpatiaLiteSchemaReader schemaReader = new SpatiaLiteSchemaReader();
    schemaReader.setSource(new FileIOSupplier(new File(getSourceTempFilePath())));
    IOReport report = schemaReader.execute(new LogProgressIndicator());
    assertTrue(report.isSuccess());
    Schema schema = schemaReader.getSchema();
    assertEquals(1, schema.getMappingRelevantTypes().size());
    TypeDefinition type = schema.getMappingRelevantTypes().iterator().next();
    checkType(type, SOUURCE_TYPE_LOCAL_NAME, propertyNames);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) SpatiaLiteSchemaReader(eu.esdihumboldt.hale.io.jdbc.spatialite.reader.internal.SpatiaLiteSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) File(java.io.File) HashSet(java.util.HashSet) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)192 QName (javax.xml.namespace.QName)58 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)38 ArrayList (java.util.ArrayList)32 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)26 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)26 Test (org.junit.Test)24 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)22 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)21 HashSet (java.util.HashSet)21 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)20 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)20 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)16 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)15 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)15 Cell (eu.esdihumboldt.hale.common.align.model.Cell)14 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)14 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)12 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)12 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)12