Search in sources :

Example 1 with Resource

use of eu.esdihumboldt.hale.common.core.io.project.model.Resource in project hale by halestudio.

the class TemplateProject method onSuccess.

@Override
protected void onSuccess(Void context, String projectId, File projectFile, Project project, ReportFile reportFile) {
    super.onSuccess(context, projectId, projectFile, project, reportFile);
    // update locations in project file
    LocationUpdater updater = new LocationUpdater(project, projectFile.toURI());
    updater.updateProject(false);
    resources.clear();
    List<Path> invalidSources = new ArrayList<>();
    Path projectFolder = getProjectFolder().toPath();
    // validate resources
    for (IOConfiguration config : project.getResources()) {
        Resource resource = new IOConfigurationResource(config, getProjectFile().toURI());
        // check if file URIs are valid and inside project folder
        URI source = resource.getSource();
        if (source != null) {
            Path path = null;
            if (source.getScheme() == null) {
                // is a relative URI
                path = projectFile.toPath().resolve(source.toString()).normalize();
            } else if ("file".equals(source.getScheme())) {
                // is a file URI
                path = Paths.get(source).normalize();
            }
            if (path != null) {
                if (!path.startsWith(projectFolder) || !Files.exists(path)) {
                    // invalid source
                    invalidSources.add(path);
                }
            }
        }
        resources.put(resource.getActionId(), resource);
    }
    valid = invalidSources.isEmpty();
    if (!valid) {
        StringBuilder builder = new StringBuilder("Files referenced by the project could not be found: ");
        for (int i = 0; i < invalidSources.size(); i++) {
            if (i > 0)
                builder.append(", ");
            Path path = invalidSources.get(i);
            builder.append(path.getFileName().toString());
        }
        notValidMessage = builder.toString();
    } else {
        notValidMessage = "";
    }
    // additionally, try to find out cell count
    definedRelations = 0;
    // check if default alignment file exists
    try {
        File defAlignmentFile = new File(URI.create(projectFile.toURI().toASCIIString() + "." + AlignmentIO.PROJECT_FILE_ALIGNMENT));
        if (defAlignmentFile.exists()) {
            try (InputStream in = new BufferedInputStream(new FileInputStream(defAlignmentFile))) {
                /*
					 * Try loading the file with JAXB - only supports 2.6+
					 * projects.
					 */
                AlignmentType alignment = JaxbToAlignment.load(in, null);
                // XXX ignoring base alignments
                int count = 0;
                for (Object element : alignment.getCellOrModifier()) {
                    if (element instanceof CellType) {
                        count++;
                    }
                }
                definedRelations = count;
            } catch (Exception e) {
            // ignore
            }
        }
    } catch (Exception e) {
    // ignore
    }
}
Also used : Path(java.nio.file.Path) LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) URI(java.net.URI) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) BufferedInputStream(java.io.BufferedInputStream) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) ReportFile(eu.esdihumboldt.hale.common.headless.report.ReportFile) File(java.io.File)

Example 2 with Resource

use of eu.esdihumboldt.hale.common.core.io.project.model.Resource in project hale by halestudio.

the class ProjectServiceImpl method removeResources.

@Override
public List<? extends Resource> removeResources(String actionId) {
    Builder<Resource> removedBuilder = ImmutableList.builder();
    synchronized (this) {
        Iterator<IOConfiguration> iter = main.getResources().iterator();
        while (iter.hasNext()) {
            IOConfiguration conf = iter.next();
            if (conf.getActionId().equals(actionId)) {
                iter.remove();
                removedBuilder.add(new IOConfigurationResource(conf, projectLocation));
            }
        }
    }
    setChanged();
    List<Resource> removedResources = removedBuilder.build();
    notifyResourcesRemoved(actionId, removedResources);
    return removedResources;
}
Also used : IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource)

Example 3 with Resource

use of eu.esdihumboldt.hale.common.core.io.project.model.Resource in project hale by halestudio.

the class TransformDataWizardSourcePage method createControl.

/**
 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createControl(Composite parent) {
    Composite content = new Composite(parent, SWT.NONE);
    content.setLayout(GridLayoutFactory.swtDefaults().create());
    final ListViewer listViewer = new ListViewer(content);
    listViewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    if (!useProjectData) {
        Button addButton = new Button(content, SWT.PUSH);
        addButton.setText("Add source file");
        addButton.setLayoutData(GridDataFactory.swtDefaults().align(SWT.END, SWT.CENTER).create());
        addButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                InstanceImportWizard importWizard = new InstanceImportWizard();
                TransformDataImportAdvisor advisor = new TransformDataImportAdvisor();
                // specifying null as actionId results in no call to
                // ProjectService.rememberIO
                importWizard.setAdvisor(advisor, null);
                if (new HaleWizardDialog(getShell(), importWizard).open() == Dialog.OK) {
                    if (advisor.getInstances() != null) {
                        sourceCollections.add(advisor.getInstances());
                        listViewer.add(advisor.getLocation());
                        getContainer().updateButtons();
                    }
                }
            }
        });
    } else {
        // initialize project source data
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Prepare data sources", IProgressMonitor.UNKNOWN);
                ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
                final List<URI> locations = 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) {
                            sourceCollections.add(advisor.getInstances());
                            locations.add(advisor.getLocation());
                        }
                    }
                }
                PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        for (URI location : locations) {
                            listViewer.add(location);
                        }
                    }
                });
                monitor.done();
            }
        };
        try {
            ThreadProgressMonitor.runWithProgressDialog(op, false);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    setControl(content);
}
Also used : InstanceImportWizard(eu.esdihumboldt.hale.ui.io.instance.InstanceImportWizard) ListViewer(org.eclipse.jface.viewers.ListViewer) Composite(org.eclipse.swt.widgets.Composite) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ArrayList(java.util.ArrayList) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) HaleWizardDialog(eu.esdihumboldt.hale.ui.util.wizard.HaleWizardDialog)

Example 4 with Resource

use of eu.esdihumboldt.hale.common.core.io.project.model.Resource in project hale by halestudio.

the class ResourcesLabelProvider method getText.

@Override
public String getText(Object element) {
    if (element instanceof ProjectToken) {
        ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
        String name = ps.getProjectInfo().getName();
        if (name == null) {
            return "<Unnamed project>";
        }
        return name;
    }
    if (element instanceof IOAction) {
        IOAction action = (IOAction) element;
        // resource category name
        if (action.getResourceCategoryName() != null) {
            return action.getResourceCategoryName();
        }
        // action name
        if (action.getName() != null) {
            return action.getName();
        }
        // action ID
        return action.getId();
    }
    if (element instanceof Resource) {
        Resource resource = (Resource) element;
        ActionUI actionUI = null;
        if (resource.getActionId() != null) {
            actionUI = ActionUIExtension.getInstance().findActionUI(resource.getActionId());
        }
        if (actionUI != null && actionUI.getUIAdvisor() != null && actionUI.getUIAdvisor().supportsCustomName()) {
            String name = actionUI.getUIAdvisor().getCustomName(resource.getResourceId());
            if (name != null) {
                return name;
            }
        }
        if (resource.getSource() != null) {
            String location = resource.getSource().toString();
            int index = location.lastIndexOf('/');
            if (index > 0 && index < location.length()) {
                return location.substring(index + 1);
            } else {
                return location;
            }
        }
        return resource.getResourceId();
    }
    if (element instanceof IContentType) {
        IContentType ct = (IContentType) element;
        return ct.getName();
    }
    return element.toString();
}
Also used : Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) IContentType(org.eclipse.core.runtime.content.IContentType) StyledString(org.eclipse.jface.viewers.StyledString)

Example 5 with Resource

use of eu.esdihumboldt.hale.common.core.io.project.model.Resource in project hale by halestudio.

the class ResourcesLabelProvider method getImage.

@Override
public Image getImage(Object element) {
    if (element instanceof IOAction) {
        IOAction action = (IOAction) element;
        Image actionImage = actionImages.get(action.getId());
        if (actionImage == null) {
            ActionUI actionUI = ActionUIExtension.getInstance().findActionUI(action.getId());
            URL iconUrl = actionUI.getIconURL();
            if (iconUrl != null) {
                actionImage = ImageDescriptor.createFromURL(iconUrl).createImage();
                actionImages.put(action.getId(), actionImage);
            }
        }
        if (actionImage != null) {
            return actionImage;
        }
        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
    }
    if (element instanceof Resource) {
        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
    }
    if (element instanceof ProjectToken) {
        return projectImage;
    }
    return null;
}
Also used : Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) Image(org.eclipse.swt.graphics.Image) URL(java.net.URL)

Aggregations

Resource (eu.esdihumboldt.hale.common.core.io.project.model.Resource)12 IOAction (eu.esdihumboldt.hale.common.core.io.IOAction)5 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)5 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)5 ArrayList (java.util.ArrayList)4 IOConfigurationResource (eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource)3 ActionUI (eu.esdihumboldt.hale.ui.io.action.ActionUI)3 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URI (java.net.URI)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 StyledString (org.eclipse.jface.viewers.StyledString)2 AlignmentType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType)1 CellType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 ProjectInfo (eu.esdihumboldt.hale.common.core.io.project.ProjectInfo)1 LocationUpdater (eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater)1 ReportFile (eu.esdihumboldt.hale.common.headless.report.ReportFile)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1