Search in sources :

Example 1 with CategoryElement

use of org.openlca.app.navigation.elements.CategoryElement in project olca-app by GreenDelta.

the class CopyPaste method copy.

private static void copy(CategoryElement element, INavigationElement<?> category) {
    Category parent = getCategory(category);
    Queue<CategoryElement> elements = new LinkedList<>();
    elements.add(element);
    while (!elements.isEmpty()) {
        CategoryElement current = elements.poll();
        Category copy = current.getContent().copy();
        copy.childCategories.clear();
        copy.category = parent;
        if (parent == null)
            copy = new CategoryDao(Database.get()).insert(copy);
        else {
            parent.childCategories.add(copy);
            copy = new CategoryDao(Database.get()).update(parent);
        }
        for (INavigationElement<?> child : current.getChildren()) if (child instanceof CategoryElement)
            elements.add((CategoryElement) child);
        else {
            RootEntity modelCopy = copy((ModelElement) child);
            modelCopy.category = copy;
            insert(modelCopy);
        }
        parent = copy;
    }
}
Also used : ModelElement(org.openlca.app.navigation.elements.ModelElement) Category(org.openlca.core.model.Category) CategoryElement(org.openlca.app.navigation.elements.CategoryElement) CategoryDao(org.openlca.core.database.CategoryDao) LinkedList(java.util.LinkedList) RootEntity(org.openlca.core.model.RootEntity)

Example 2 with CategoryElement

use of org.openlca.app.navigation.elements.CategoryElement in project olca-app by GreenDelta.

the class DeleteModelAction method run.

@Override
public void run() {
    boolean canceled = false;
    boolean dontAsk = false;
    showInUseMessage = true;
    for (ModelElement element : models) {
        var descriptor = element.getContent();
        if (descriptor == null)
            continue;
        String name = Labels.name(descriptor);
        int answer = dontAsk ? IDialogConstants.YES_ID : askDelete(name);
        if (answer == IDialogConstants.CANCEL_ID) {
            canceled = true;
            break;
        }
        if (answer == IDialogConstants.NO_TO_ALL_ID)
            break;
        if (answer == IDialogConstants.NO_ID)
            continue;
        if (answer == IDialogConstants.YES_TO_ALL_ID)
            dontAsk = true;
        if (isUsed(descriptor))
            continue;
        App.close(descriptor);
        delete(descriptor);
        Navigator.refresh(element.getParent());
    }
    models.clear();
    if (canceled) {
        categories.clear();
        return;
    }
    boolean dontAskEmpty = false;
    for (CategoryElement element : categories) {
        Category category = element.getContent();
        if (category == null)
            continue;
        boolean empty = element.getChildren().isEmpty();
        int answer;
        if (!empty) {
            answer = dontAskEmpty ? IDialogConstants.YES_ID : askNotEmptyDelete(category.name);
        } else {
            answer = dontAsk ? IDialogConstants.YES_ID : askDelete(category.name);
        }
        if (answer == IDialogConstants.NO_TO_ALL_ID)
            break;
        if (answer == IDialogConstants.CANCEL_ID)
            break;
        if (answer == IDialogConstants.NO_ID)
            continue;
        if (answer == IDialogConstants.YES_TO_ALL_ID) {
            if (empty) {
                dontAskEmpty = true;
            } else {
                dontAsk = true;
            }
        }
        if (delete(element)) {
            INavigationElement<?> typeElement = Navigator.findElement(category.modelType);
            Navigator.refresh(typeElement);
        }
    }
    categories.clear();
}
Also used : ModelElement(org.openlca.app.navigation.elements.ModelElement) Category(org.openlca.core.model.Category) CategoryElement(org.openlca.app.navigation.elements.CategoryElement)

Example 3 with CategoryElement

use of org.openlca.app.navigation.elements.CategoryElement in project olca-app by GreenDelta.

the class DeleteModelAction method delete.

private boolean delete(CategoryElement element) {
    boolean canBeDeleted = true;
    for (INavigationElement<?> child : element.getChildren()) {
        if (child instanceof CategoryElement) {
            boolean deleted = delete((CategoryElement) child);
            if (!deleted) {
                canBeDeleted = false;
            }
        } else if (child instanceof ModelElement) {
            var descriptor = ((ModelElement) child).getContent();
            if (isUsed(descriptor)) {
                canBeDeleted = false;
                continue;
            }
            App.close(descriptor);
            delete(descriptor);
        }
    }
    if (!canBeDeleted) {
        Navigator.refresh(element);
        return false;
    }
    Category category = element.getContent();
    try {
        CategoryDao dao = new CategoryDao(Database.get());
        Category parent = category.category;
        if (parent != null) {
            parent.childCategories.remove(category);
            category.category = null;
            dao.update(parent);
        }
        dao.delete(category);
        Cache.evict(Descriptor.of(category));
        return true;
    } catch (Exception e) {
        ErrorReporter.on("failed to delete category " + category, e);
        return false;
    }
}
Also used : ModelElement(org.openlca.app.navigation.elements.ModelElement) Category(org.openlca.core.model.Category) CategoryElement(org.openlca.app.navigation.elements.CategoryElement) CategoryDao(org.openlca.core.database.CategoryDao)

Example 4 with CategoryElement

use of org.openlca.app.navigation.elements.CategoryElement in project olca-app by GreenDelta.

the class CategoryDialog method createTreeViewer.

private void createTreeViewer(Composite composite) {
    TreeViewer viewer = NavigationTree.forSingleSelection(composite, modelType);
    viewer.setFilters(new ViewerFilter[] { new Filter() });
    UI.gridData(viewer.getTree(), true, true);
    viewer.addSelectionChangedListener(e -> {
        INavigationElement<?> element = Selections.firstOf(e);
        if (element instanceof CategoryElement) {
            category = (Category) element.getContent();
        } else {
            category = null;
        }
    });
}
Also used : CategoryElement(org.openlca.app.navigation.elements.CategoryElement) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TreeViewer(org.eclipse.jface.viewers.TreeViewer)

Example 5 with CategoryElement

use of org.openlca.app.navigation.elements.CategoryElement in project olca-app by GreenDelta.

the class FlowTypeFilter method select.

@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
    boolean select = true;
    TreeViewer treeViewer = (TreeViewer) viewer;
    if (element instanceof ModelElement) {
        select = !matchType((ModelElement) element);
    } else if (element instanceof CategoryElement) {
        if (filterEmptyCategories(treeViewer)) {
            CategoryElement catElem = (CategoryElement) element;
            Category category = catElem.getContent();
            if (category.modelType == ModelType.FLOW)
                select = containsOtherTypes(catElem);
        }
    }
    return select;
}
Also used : ModelElement(org.openlca.app.navigation.elements.ModelElement) Category(org.openlca.core.model.Category) CategoryElement(org.openlca.app.navigation.elements.CategoryElement) TreeViewer(org.eclipse.jface.viewers.TreeViewer)

Aggregations

CategoryElement (org.openlca.app.navigation.elements.CategoryElement)7 ModelElement (org.openlca.app.navigation.elements.ModelElement)6 Category (org.openlca.core.model.Category)4 TreeViewer (org.eclipse.jface.viewers.TreeViewer)2 CategoryDao (org.openlca.core.database.CategoryDao)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 InputDialog (org.eclipse.jface.dialogs.InputDialog)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 INavigationElement (org.openlca.app.navigation.elements.INavigationElement)1 ParameterDao (org.openlca.core.database.ParameterDao)1 RootEntity (org.openlca.core.model.RootEntity)1