Search in sources :

Example 1 with Enclosure

use of org.pentaho.platform.dataaccess.datasource.Enclosure in project data-access by pentaho.

the class CsvPhysicalStep method setBindings.

@Override
public void setBindings() {
    // $NON-NLS-1$
    uploadedFileTextBox = (XulTextbox) document.getElementById("uploadedFile");
    // $NON-NLS-1$
    csvTextPreview = (XulLabel) document.getElementById("csvTextPreview");
    encodingTypeMenuList = (XulMenuList<String>) document.getElementById("encodingTypeMenuList");
    encodingTypeMenuList.setElements(ENCODINGS);
    BindingConvertor<Integer, Boolean> isFirstRowHeaderConverter = BindingConvertor.integer2Boolean();
    bf.createBinding(datasourceModel.getModelInfo().getFileInfo(), CsvFileInfo.HEADER_ROWS_ATTRIBUTE, // $NON-NLS-1$
    "isHeaderCheckBox", // $NON-NLS-1$
    "checked", isFirstRowHeaderConverter);
    // Binding convertor to between Delimiter and radio group selected value
    BindingConvertor<String, String> delimiterBindingConvertor = new BindingConvertor<String, String>() {

        public String sourceToTarget(String source) {
            Delimiter delimiter = Delimiter.lookupValue(source);
            if (delimiter != null) {
                return Delimiter.lookupValue(source).getName();
            } else {
                return source;
            }
        }

        public String targetToSource(String target) {
            Delimiter delimiter = Delimiter.lookupName(target);
            if (delimiter != null) {
                return delimiter.getValue();
            } else {
                return target;
            }
        }
    };
    // add binding for the Delimiter to it's corresponding radio group
    bf.createBinding(datasourceModel.getModelInfo().getFileInfo(), CsvFileInfo.DELIMITER_ATTRIBUTE, // $NON-NLS-1$
    "delimiterRadioGroup", // $NON-NLS-1$
    "value", delimiterBindingConvertor);
    // Binding convertor to between Enclosure and radio group selected value
    BindingConvertor<String, String> enclosureBindingConvertor = new BindingConvertor<String, String>() {

        public String sourceToTarget(String source) {
            Enclosure e = Enclosure.lookupValue(source);
            if (e == null) {
                e = Enclosure.NONE;
            }
            return e.getName();
        }

        public String targetToSource(String target) {
            Enclosure e = Enclosure.lookupName(target);
            if (e == Enclosure.NONE) {
                return null;
            } else {
                return e.getValue();
            }
        }
    };
    // add binding for the Enclosure to it's corresponding radio group
    bf.createBinding(datasourceModel.getModelInfo().getFileInfo(), CsvFileInfo.ENCLOSURE_ATTRIBUTE, // $NON-NLS-1$
    "enclosureRadioGroup", // $NON-NLS-1$
    "value", enclosureBindingConvertor);
    // when the delimiter changes, we need to refresh the preview
    datasourceModel.getModelInfo().getFileInfo().addPropertyChangeListener(CsvFileInfo.DELIMITER_ATTRIBUTE, new RefreshPreviewPropertyChangeListener());
    // when the enclosure changes, we need to refresh the preview
    datasourceModel.getModelInfo().getFileInfo().addPropertyChangeListener(CsvFileInfo.ENCLOSURE_ATTRIBUTE, new RefreshPreviewPropertyChangeListener());
    // when the first-row-is-header flag changes, we need to refresh the preview
    datasourceModel.getModelInfo().getFileInfo().addPropertyChangeListener(CsvFileInfo.HEADER_ROWS_ATTRIBUTE, new RefreshPreviewPropertyChangeListener());
    uploadedFileTextBox.addPropertyChangeListener(new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("value")) {
                // set the defaults if none already selected
                String delimiter = datasourceModel.getModelInfo().getFileInfo().getDelimiter();
                if (delimiter == null || delimiter.equals("")) {
                    datasourceModel.getModelInfo().getFileInfo().setDelimiter(",");
                    datasourceModel.getModelInfo().getFileInfo().setHeaderRows(1);
                }
                String enclosure = datasourceModel.getModelInfo().getFileInfo().getEnclosure();
                if (enclosure == null || enclosure.equals("")) {
                    datasourceModel.getModelInfo().getFileInfo().setEnclosure("\"");
                }
                syncModelInfo();
                datasourceModel.getGuiStateModel().setDirty(true);
                datasourceModel.getModelInfo().validate();
            }
        }
    });
    bf.setBindingType(Binding.Type.ONE_WAY);
    // binding to set the first-row-is-header checkbox's enabled property based on the selectedItem in the filesList
    bf.createBinding(uploadedFileTextBox, "value", "isHeaderCheckBox", "!disabled", BindingConvertor.object2Boolean());
    // binding to set the delimiters enabled property based on the selectedItem in the filesList
    bf.createBinding(uploadedFileTextBox, "value", "delimiterRadioGroup", "!disabled", BindingConvertor.object2Boolean());
    // binding to set the enclosures enabled property based on the selectedItem in the filesList
    bf.createBinding(uploadedFileTextBox, "value", "enclosureRadioGroup", "!disabled", BindingConvertor.object2Boolean());
    bf.createBinding(datasourceModel.getModelInfo().getFileInfo(), "friendlyFilename", uploadedFileTextBox, // $NON-NLS-1$ //$NON-NLS-2$
    "value");
    bf.createBinding(uploadedFileTextBox, "value", "encodingTypeMenuList", "!disabled", BindingConvertor.object2Boolean());
    BindingConvertor<String, String> encodingBindingConvertor = new BindingConvertor<String, String>() {

        public String sourceToTarget(String source) {
            return source;
        }

        public String targetToSource(String target) {
            Collection<String> encodings = encodingTypeMenuList.getElements();
            if (target != null && !encodings.contains(target)) {
                encodings.add(target);
                encodingTypeMenuList.setElements(encodings);
            }
            return target;
        }
    };
    bf.setBindingType(Binding.Type.BI_DIRECTIONAL);
    bf.createBinding(encodingTypeMenuList, "value", datasourceModel.getModelInfo().getFileInfo(), CsvFileInfo.ENCODING, // $NON-NLS-1$ //$NON-NLS-2$
    encodingBindingConvertor);
    RefreshPreviewPropertyChangeListener previewChangeListener = new RefreshPreviewPropertyChangeListener();
    datasourceModel.getModelInfo().getFileInfo().addPropertyChangeListener(CsvFileInfo.ENCODING, previewChangeListener);
    datasourceModel.getModelInfo().getFileInfo().addPropertyChangeListener(CsvFileInfo.TMP_FILENAME_ATTRIBUTE, previewChangeListener);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) Delimiter(org.pentaho.platform.dataaccess.datasource.Delimiter) PropertyChangeListener(java.beans.PropertyChangeListener) Enclosure(org.pentaho.platform.dataaccess.datasource.Enclosure) BindingConvertor(org.pentaho.ui.xul.binding.BindingConvertor)

Aggregations

PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 Delimiter (org.pentaho.platform.dataaccess.datasource.Delimiter)1 Enclosure (org.pentaho.platform.dataaccess.datasource.Enclosure)1 BindingConvertor (org.pentaho.ui.xul.binding.BindingConvertor)1