Search in sources :

Example 1 with ImportComponent

use of net.parostroj.timetable.model.imports.ImportComponent in project grafikon by jub77.

the class ImportModelAction method backgroundAction.

@Override
protected void backgroundAction() {
    setWaitMessage(ResourceLoader.getString("wait.message.import"));
    setWaitDialogVisible(true);
    long time = System.currentTimeMillis();
    try {
        ExportImportSelection selection = context.getAttribute("selection", ExportImportSelection.class);
        TrainDiagram diagram = context.getAttribute("diagramImport", TrainDiagram.class);
        TrainImportConfig trainImportConfig = context.getAttribute("trainImport", TrainImportConfig.class);
        Map<ImportComponent, Collection<ObjectWithId>> map = selection.getObjectMap();
        imports = new TrainDiagramPartImport(diagram, selection.getImportMatch(), selection.isImportOverwrite());
        List<ObjectWithId> list = map.values().stream().sequential().flatMap(item -> item.stream().sequential()).collect(Collectors.toList());
        if (list.isEmpty()) {
            return;
        }
        if (trainImportConfig != null && trainImportConfig.isRemoveExisting()) {
            // remove existing trains in group
            Consumer<ObjectWithId> deleteProcess = item -> diagram.getTrains().remove(item);
            Iterable<Train> filteredTrains = Iterables.filter(diagram.getTrains(), ModelPredicates.inGroup(trainImportConfig.getToGroup()));
            processItems(filteredTrains, deleteProcess);
        }
        // import new objects
        Consumer<ObjectWithId> importProcess = item -> {
            ImportComponent i = ImportComponent.getByComponentClass(item.getClass());
            if (i != null) {
                ObjectWithId imported = imports.importPart(item);
                processImportedObject(imported, trainImportConfig);
            } else {
                log.warn("No import for class {}", item.getClass().getName());
            }
        };
        processItems(list, importProcess);
    } finally {
        log.debug("Import finished in {}ms", System.currentTimeMillis() - time);
        setWaitDialogVisible(false);
    }
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) Iterables(com.google.common.collect.Iterables) Logger(org.slf4j.Logger) Collection(java.util.Collection) GuiComponentUtils(net.parostroj.timetable.gui.utils.GuiComponentUtils) LoggerFactory(org.slf4j.LoggerFactory) ImportError(net.parostroj.timetable.model.imports.Import.ImportError) ModelPredicates(net.parostroj.timetable.filters.ModelPredicates) ImportComponent(net.parostroj.timetable.model.imports.ImportComponent) JOptionPane(javax.swing.JOptionPane) Collectors(java.util.stream.Collectors) Node(net.parostroj.timetable.model.Node) Consumer(java.util.function.Consumer) ExportImportSelection(net.parostroj.timetable.gui.components.ExportImportSelection) List(java.util.List) ObjectWithId(net.parostroj.timetable.model.ObjectWithId) ResourceLoader(net.parostroj.timetable.gui.utils.ResourceLoader) TrainDiagramPartImport(net.parostroj.timetable.model.imports.TrainDiagramPartImport) Map(java.util.Map) LinkedList(java.util.LinkedList) TrainType(net.parostroj.timetable.model.TrainType) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) Group(net.parostroj.timetable.model.Group) Train(net.parostroj.timetable.model.Train) ImportComponent(net.parostroj.timetable.model.imports.ImportComponent) ObjectWithId(net.parostroj.timetable.model.ObjectWithId) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) ExportImportSelection(net.parostroj.timetable.gui.components.ExportImportSelection) Collection(java.util.Collection) Train(net.parostroj.timetable.model.Train) TrainDiagramPartImport(net.parostroj.timetable.model.imports.TrainDiagramPartImport)

Example 2 with ImportComponent

use of net.parostroj.timetable.model.imports.ImportComponent in project grafikon by jub77.

the class ImportModelAction method eventDispatchActionAfter.

@Override
protected void eventDispatchActionAfter() {
    if (context.isCancelled()) {
        return;
    }
    List<ImportError> errors = new LinkedList<>();
    for (ImportComponent comp : imports.getImportComponents()) {
        errors.addAll(imports.getErrors(comp));
    }
    // create string ...
    if (!errors.isEmpty()) {
        StringBuilder message = new StringBuilder();
        int lineLength = 70;
        int nextLimit = lineLength;
        for (ImportError error : errors) {
            if (message.length() != 0) {
                message.append(", ");
            }
            if (nextLimit < message.length()) {
                message.append('\n');
                nextLimit += lineLength;
            }
            message.append(getText(error));
        }
        JOptionPane.showConfirmDialog(getActionContext().getLocationComponent(), message, ResourceLoader.getString("import.warning.title"), JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
    }
}
Also used : ImportComponent(net.parostroj.timetable.model.imports.ImportComponent) ImportError(net.parostroj.timetable.model.imports.Import.ImportError) LinkedList(java.util.LinkedList)

Example 3 with ImportComponent

use of net.parostroj.timetable.model.imports.ImportComponent in project grafikon by jub77.

the class ExportImportSelectionPanel method setSelectionSource.

public void setSelectionSource(ExportImportSelectionSource source) {
    typeComboBox.removeAllItems();
    currentSelection = null;
    selectionMap.clear();
    for (ImportComponent type : source.getTypes()) {
        Collection<ObjectWithId> elements = source.getElementsForType(type);
        if (!elements.isEmpty()) {
            Selection selection = new Selection(type, Wrapper.getWrapperList(elements), new ArrayList<>());
            selectionMap.put(type, selection);
            typeComboBox.addItem(Wrapper.getWrapper(type));
        }
    }
    typeComboBox.setMaximumRowCount(source.getTypes().size());
}
Also used : ImportComponent(net.parostroj.timetable.model.imports.ImportComponent) ObjectWithId(net.parostroj.timetable.model.ObjectWithId)

Aggregations

ImportComponent (net.parostroj.timetable.model.imports.ImportComponent)3 LinkedList (java.util.LinkedList)2 ObjectWithId (net.parostroj.timetable.model.ObjectWithId)2 ImportError (net.parostroj.timetable.model.imports.Import.ImportError)2 Iterables (com.google.common.collect.Iterables)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 JOptionPane (javax.swing.JOptionPane)1 ModelPredicates (net.parostroj.timetable.filters.ModelPredicates)1 ExportImportSelection (net.parostroj.timetable.gui.components.ExportImportSelection)1 GuiComponentUtils (net.parostroj.timetable.gui.utils.GuiComponentUtils)1 ResourceLoader (net.parostroj.timetable.gui.utils.ResourceLoader)1 Group (net.parostroj.timetable.model.Group)1 Node (net.parostroj.timetable.model.Node)1 Train (net.parostroj.timetable.model.Train)1 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)1