Search in sources :

Example 1 with OccurringValues

use of eu.esdihumboldt.hale.ui.service.values.OccurringValues in project hale by halestudio.

the class OccurringValuesSection method createControls.

@Override
public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
    super.createControls(parent, aTabbedPropertySheetPage);
    Composite page = getWidgetFactory().createComposite(parent);
    GridLayoutFactory.swtDefaults().numColumns(2).applyTo(page);
    // refresh button
    refresh = getWidgetFactory().createButton(page, null, SWT.PUSH);
    refresh.setImage(CommonSharedImages.getImageRegistry().get(CommonSharedImages.IMG_REFRESH));
    refresh.setToolTipText("Update the occurring values");
    refresh.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            service.updateOccurringValues((PropertyEntityDefinition) getEntity());
        }
    });
    GridDataFactory.swtDefaults().align(SWT.END, SWT.BEGINNING).grab(false, false).applyTo(refresh);
    // values table
    values = new TableViewer(getWidgetFactory().createTable(page, SWT.MULTI | SWT.BORDER));
    GridDataFactory.fillDefaults().grab(true, true).span(1, 2).applyTo(values.getControl());
    values.setContentProvider(new ArrayContentProvider() {

        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof OccurringValues) {
                return ((OccurringValues) inputElement).getValues().entrySet().toArray();
            }
            return new Object[] {};
        }
    });
    values.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            if (element instanceof Entry) {
                // XXX use styled label provider instead?
                Entry<?> entry = (Entry<?>) element;
                if (entry.getCount() > 1) {
                    return super.getText(entry.getElement()) + "\t(\u00d7" + entry.getCount() + ")";
                } else
                    return super.getText(entry.getElement());
            }
            return super.getText(element);
        }
    });
    values.setInput(null);
    // values context menu
    MenuManager manager = new MenuManager();
    manager.setRemoveAllWhenShown(true);
    manager.addMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(IMenuManager manager) {
            // populate context menu
            // get selection
            ISelection selection = values.getSelection();
            if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
                Object[] sels = ((IStructuredSelection) selection).toArray();
                List<String> values = new ArrayList<String>();
                for (Object sel : sels) {
                    if (sel instanceof Entry<?>) {
                        values.add(((Entry<?>) sel).getElement().toString());
                    }
                }
                if (!values.isEmpty()) {
                    manager.add(new AddConditionAction(getEntity(), values, false));
                    manager.add(new AddParentConditionAction(getEntity(), values, false));
                    if (values.size() > 1) {
                        manager.add(new Separator());
                        manager.add(new AddConditionAction(getEntity(), values, true));
                        manager.add(new AddParentConditionAction(getEntity(), values, true));
                    }
                }
            }
        }
    });
    manager.setRemoveAllWhenShown(true);
    final Menu valuesMenu = manager.createContextMenu(values.getControl());
    values.getControl().setMenu(valuesMenu);
    // copy button
    copy = getWidgetFactory().createButton(page, null, SWT.PUSH);
    copy.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_COPY));
    copy.setToolTipText("Copy values to the clipboard");
    copy.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            copyToClipboard();
        }
    });
    GridDataFactory.swtDefaults().align(SWT.END, SWT.BEGINNING).grab(false, false).applyTo(copy);
    // add listener to service
    service.addListener(ovlistener = new OccurringValuesListener() {

        @Override
        public void occurringValuesUpdated(PropertyEntityDefinition property) {
            if (property.equals(OccurringValuesSection.this.getEntity())) {
                update();
            }
        }

        @Override
        public void occurringValuesInvalidated(SchemaSpaceID schemaSpace) {
            if (schemaSpace.equals(OccurringValuesSection.this.getEntity().getSchemaSpace())) {
                update();
            }
        }
    });
    update();
}
Also used : IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Entry(com.google.common.collect.Multiset.Entry) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ISelection(org.eclipse.jface.viewers.ISelection) ArrayList(java.util.ArrayList) List(java.util.List) OccurringValuesListener(eu.esdihumboldt.hale.ui.service.values.OccurringValuesListener) Menu(org.eclipse.swt.widgets.Menu) OccurringValues(eu.esdihumboldt.hale.ui.service.values.OccurringValues) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IMenuListener(org.eclipse.jface.action.IMenuListener) SchemaSpaceID(eu.esdihumboldt.hale.common.schema.SchemaSpaceID) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) TableViewer(org.eclipse.jface.viewers.TableViewer) LabelProvider(org.eclipse.jface.viewers.LabelProvider) Separator(org.eclipse.jface.action.Separator)

Example 2 with OccurringValues

use of eu.esdihumboldt.hale.ui.service.values.OccurringValues in project hale by halestudio.

the class OccurringValuesSection method update.

/**
 * Update the section state.
 */
private void update() {
    final Display display = PlatformUI.getWorkbench().getDisplay();
    display.syncExec(new Runnable() {

        @Override
        public void run() {
            if (values != null) {
                OccurringValues ov = null;
                if (getEntity() != null) {
                    ov = service.getOccurringValues((PropertyEntityDefinition) getEntity());
                }
                values.setInput(ov);
                values.getControl().setEnabled(ov != null);
                refresh.setEnabled(ov == null || !ov.isUpToDate());
                copy.setEnabled(ov != null);
                // TODO some icon to state that information is not
                // up-to-date?
                // relayout so scrolling is done inside table
                // XXX not really working
                values.getControl().getParent().layout();
                values.getControl().getParent().getParent().layout();
            }
        }
    });
}
Also used : OccurringValues(eu.esdihumboldt.hale.ui.service.values.OccurringValues) Display(org.eclipse.swt.widgets.Display)

Example 3 with OccurringValues

use of eu.esdihumboldt.hale.ui.service.values.OccurringValues in project hale by halestudio.

the class OccurringValuesServiceImpl method updateOccuringValues.

/**
 * Update the occurring values for the given property entity.
 *
 * @param property the property entity definition
 * @param values the map containing the current occurring values
 * @return <code>true</code> if the task to update the information has been
 *         started, <code>false</code> if the information was up-to-date
 */
private boolean updateOccuringValues(PropertyEntityDefinition property, Map<PropertyEntityDefinition, OccurringValuesImpl> values) {
    synchronized (values) {
        OccurringValues ov = values.get(property);
        if (ov != null && ov.isUpToDate()) {
            return false;
        }
    }
    // determine occurring values
    // determine data set
    DataSet dataSet;
    switch(property.getSchemaSpace()) {
        case TARGET:
            dataSet = DataSet.TRANSFORMED;
            break;
        default:
            dataSet = DataSet.SOURCE;
    }
    // determine if external data should be used
    boolean useExternalData = false;
    if (dataSet.equals(DataSet.SOURCE)) {
        ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
        useExternalData = InstanceViewPreferences.occurringValuesUseExternalData(ps.getConfigurationService());
    }
    InstanceCollection collection;
    if (!useExternalData) {
        collection = instances.getInstances(dataSet);
    } else {
        // use complete project data sources
        final AtomicReference<InstanceCollection> source = new AtomicReference<>();
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
                List<InstanceCollection> sources = new ArrayList<>();
                for (Resource resource : ps.getResources()) {
                    if (InstanceIO.ACTION_LOAD_SOURCE_DATA.equals(resource.getActionId())) {
                        // resource is source data
                        IOConfiguration conf = resource.copyConfiguration(true);
                        TransformDataImportAdvisor advisor = new TransformDataImportAdvisor();
                        ProjectResourcesUtil.executeConfiguration(conf, advisor, false, null);
                        if (advisor.getInstances() != null) {
                            sources.add(advisor.getInstances());
                        }
                    }
                }
                source.set(new MultiInstanceCollection(sources));
            }
        };
        try {
            ThreadProgressMonitor.runWithProgressDialog(op, false);
            collection = source.get();
        } catch (Exception e) {
            log.error("Error initializing data sources", e);
            return true;
        }
    }
    // go through instances to determine occurring values
    Job job = new OccurringValuesJob(property, values, collection);
    job.schedule();
    return true;
}
Also used : DataSet(eu.esdihumboldt.hale.common.instance.model.DataSet) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ArrayList(java.util.ArrayList) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TransformDataImportAdvisor(eu.esdihumboldt.hale.ui.transformation.TransformDataImportAdvisor) OccurringValues(eu.esdihumboldt.hale.ui.service.values.OccurringValues) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

OccurringValues (eu.esdihumboldt.hale.ui.service.values.OccurringValues)3 ArrayList (java.util.ArrayList)2 Entry (com.google.common.collect.Multiset.Entry)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)1 Resource (eu.esdihumboldt.hale.common.core.io.project.model.Resource)1 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 MultiInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection)1 SchemaSpaceID (eu.esdihumboldt.hale.common.schema.SchemaSpaceID)1 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)1 OccurringValuesListener (eu.esdihumboldt.hale.ui.service.values.OccurringValuesListener)1 TransformDataImportAdvisor (eu.esdihumboldt.hale.ui.transformation.TransformDataImportAdvisor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1