Search in sources :

Example 1 with PropertyTypeFactory

use of eu.esdihumboldt.hale.io.csv.PropertyTypeFactory in project hale by halestudio.

the class DefaultSchemaTypePage method update.

/**
 * Update all fields (should be called if page is initialized or the
 * selection of input (file or table) has been changed
 */
protected void update() {
    int length = 0;
    // the header is not valid so clear all content
    if (header == null || header.length == 0) {
        // clear properties
        clearPage();
        return;
    }
    if (secondRow == null || secondRow.length == 0) {
        secondRow = header;
    }
    if (header.length != 0) {
        length = header.length;
    }
    // dispose all property names if the read configuration has been changed
    if (lastSecondRow != null && !Arrays.equals(header, lastSecondRow)) {
        for (TypeNameField properties : fields) {
            properties.dispose();
            properties.getTextControl(group).dispose();
            properties.getLabelControl(group).dispose();
        }
        for (ComboViewer combViewer : comboFields) {
            combViewer.getCombo().dispose();
        }
        fields.clear();
        comboFields.clear();
    }
    if (!Arrays.equals(header, lastSecondRow)) {
        for (int i = 0; i < length; i++) {
            final TypeNameField propField;
            final ComboViewer cv;
            validSel.add(true);
            propField = new TypeNameField("properties", Integer.toString(i + 1), group);
            fields.add(propField);
            propField.setEmptyStringAllowed(false);
            propField.setErrorMessage("Please enter a valid Property Name");
            propField.setPropertyChangeListener(new IPropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent event) {
                    HashSet<String> hs = new HashSet<String>();
                    if (event.getProperty().equals(StringFieldEditor.VALUE)) {
                        for (TypeNameField field : fields) {
                            if (!hs.add(field.getStringValue())) {
                                valid = false;
                                break;
                            } else {
                                valid = true;
                            }
                        }
                    }
                    if (event.getProperty().equals(StringFieldEditor.IS_VALID)) {
                        isValid = (Boolean) event.getNewValue();
                    }
                    setPageComplete(isValid());
                }
            });
            propField.setStringValue(header[i].replace(" ", ""));
            cv = new ComboViewer(group);
            comboFields.add(cv);
            cv.addSelectionChangedListener(new ISelectionChangedListener() {

                @Override
                public void selectionChanged(SelectionChangedEvent event) {
                    int i = comboFields.indexOf(event.getSource());
                    PropertyTypeFactory actualSelection = ((PropertyTypeFactory) ((IStructuredSelection) cv.getSelection()).getFirstElement());
                    try {
                        actualSelection.createExtensionObject().convertFromField(secondRow[i]);
                        validSel.set(i, true);
                    } catch (Exception e) {
                        validSel.set(i, false);
                    }
                    if (validSel.contains(false)) {
                        int j = validSel.indexOf(false) + 1;
                        setMessage("Your selection in field # " + j + " is not valid!", WARNING);
                    } else {
                        setMessage(null);
                    }
                }
            });
            cv.setContentProvider(ArrayContentProvider.getInstance());
            cv.setLabelProvider(new LabelProvider() {

                /**
                 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
                 */
                @Override
                public String getText(Object element) {
                    if (element instanceof PropertyTypeFactory) {
                        return ((PropertyTypeFactory) element).getDisplayName();
                    }
                    return super.getText(element);
                }
            });
            Collection<PropertyTypeFactory> elements = PropertyTypeExtension.getInstance().getFactories();
            cv.setInput(elements);
            PropertyTypeFactory defaultSelection = PropertyTypeExtension.getInstance().getFactory("java.lang.String");
            if (defaultSelection != null) {
                cv.setSelection(new StructuredSelection(defaultSelection));
            } else if (!elements.isEmpty()) {
                cv.setSelection(new StructuredSelection(elements.iterator().next()));
            }
        }
    }
    group.setLayout(new GridLayout(3, false));
    lastSecondRow = header;
    group.layout();
    sc.layout();
// setPageComplete(sfe.isValid() && isValid && valid);
}
Also used : IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ComboViewer(org.eclipse.jface.viewers.ComboViewer) LabelProvider(org.eclipse.jface.viewers.LabelProvider) HashSet(java.util.HashSet) PropertyTypeFactory(eu.esdihumboldt.hale.io.csv.PropertyTypeFactory)

Aggregations

PropertyTypeFactory (eu.esdihumboldt.hale.io.csv.PropertyTypeFactory)1 HashSet (java.util.HashSet)1 IPropertyChangeListener (org.eclipse.jface.util.IPropertyChangeListener)1 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)1 ComboViewer (org.eclipse.jface.viewers.ComboViewer)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 GridLayout (org.eclipse.swt.layout.GridLayout)1