Search in sources :

Example 1 with ObjectWithId

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

the class LSLibraryItem method createLibraryItem.

public LibraryItem createLibraryItem(LibraryBuilder libraryBuilder) throws LSException {
    LibraryItemType type = LibraryItemType.valueOf(getType());
    ObjectWithId object = null;
    switch(type) {
        case ENGINE_CLASS:
            object = ((LSEngineClass) getObject()).createEngineClass(id -> {
                ObjectWithId foundObject = libraryBuilder.getObjectById(id);
                return foundObject instanceof LineClass ? (LineClass) foundObject : null;
            });
            break;
        case LINE_CLASS:
            object = ((LSLineClass) getObject()).createLineClass();
            break;
        case NODE:
            object = ((LSNode) getObject()).createNode(libraryBuilder.getPartFactory(), libraryBuilder::getObjectById);
            break;
        case OUTPUT_TEMPLATE:
            object = ((LSOutputTemplate) getObject()).createOutputTemplate(libraryBuilder.getPartFactory(), libraryBuilder::getObjectById);
            break;
        case TRAIN_TYPE:
            object = ((LSTrainType) getObject()).createTrainType(libraryBuilder.getPartFactory(), libraryBuilder::getObjectById, id -> {
                ObjectWithId foundObject = libraryBuilder.getObjectById(id);
                return foundObject instanceof TrainTypeCategory ? (TrainTypeCategory) foundObject : null;
            });
            break;
        case TRAIN_TYPE_CATEGORY:
            object = ((LSTrainTypeCategory) getObject()).createTrainTypeCategory();
            break;
    }
    LibraryItem item = libraryBuilder.addObject(object);
    item.getAttributes().add(this.getAttributes().createAttributes(libraryBuilder::getObjectById));
    return item;
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) LibraryItem(net.parostroj.timetable.model.library.LibraryItem) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Node(net.parostroj.timetable.model.Node) ObjectWithId(net.parostroj.timetable.model.ObjectWithId) OutputTemplate(net.parostroj.timetable.model.OutputTemplate) XmlType(javax.xml.bind.annotation.XmlType) TrainTypeCategory(net.parostroj.timetable.model.TrainTypeCategory) XmlElements(javax.xml.bind.annotation.XmlElements) LibraryBuilder(net.parostroj.timetable.model.library.LibraryBuilder) LSException(net.parostroj.timetable.model.ls.LSException) EngineClass(net.parostroj.timetable.model.EngineClass) TrainType(net.parostroj.timetable.model.TrainType) LibraryItemType(net.parostroj.timetable.model.library.LibraryItemType) LineClass(net.parostroj.timetable.model.LineClass) XmlElement(javax.xml.bind.annotation.XmlElement) ObjectWithId(net.parostroj.timetable.model.ObjectWithId) LineClass(net.parostroj.timetable.model.LineClass) LibraryItem(net.parostroj.timetable.model.library.LibraryItem) LibraryItemType(net.parostroj.timetable.model.library.LibraryItemType) TrainTypeCategory(net.parostroj.timetable.model.TrainTypeCategory)

Example 2 with ObjectWithId

use of net.parostroj.timetable.model.ObjectWithId 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 3 with ObjectWithId

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

the class ImportModelAction method processItems.

private void processItems(Iterable<? extends ObjectWithId> list, Consumer<ObjectWithId> importProcess) {
    List<ObjectWithId> batch = new LinkedList<>();
    int cnt = 0;
    for (ObjectWithId o : list) {
        batch.add(o);
        if (++cnt == CHUNK_SIZE) {
            processChunk(batch, importProcess);
            cnt = 0;
            batch = new LinkedList<>();
        }
    }
    if (!batch.isEmpty()) {
        processChunk(batch, importProcess);
    }
}
Also used : ObjectWithId(net.parostroj.timetable.model.ObjectWithId) LinkedList(java.util.LinkedList)

Example 4 with ObjectWithId

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

the class EditOutputDialog method setPresentationModel.

@Override
public void setPresentationModel(final OutputPM pModel) {
    provider.setPresentationModel(pModel);
    pModel.setOperationEditSelection(() -> {
        Collection<? extends ObjectWithId> currentSelection = pModel.getSelection();
        ElementSelectionDialog<ObjectWithId> dialog = new ElementSelectionDialog<>(this, true);
        OutputTemplate template = pModel.getEditedOutput().getTemplate();
        Collection<ObjectWithId> collection = template.getSelectionType().extract(template.getDiagram(), ObjectWithId.class);
        dialog.setLocationRelativeTo(this);
        List<ObjectWithId> newSelection = dialog.selectElements(collection, currentSelection);
        if (newSelection != null) {
            if (newSelection.isEmpty()) {
                newSelection = null;
            }
            pModel.updateSelection(newSelection);
        }
        return true;
    });
}
Also used : ObjectWithId(net.parostroj.timetable.model.ObjectWithId) OutputTemplate(net.parostroj.timetable.model.OutputTemplate)

Example 5 with ObjectWithId

use of net.parostroj.timetable.model.ObjectWithId 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

ObjectWithId (net.parostroj.timetable.model.ObjectWithId)7 TrainType (net.parostroj.timetable.model.TrainType)3 LinkedList (java.util.LinkedList)2 EngineClass (net.parostroj.timetable.model.EngineClass)2 LineClass (net.parostroj.timetable.model.LineClass)2 Node (net.parostroj.timetable.model.Node)2 OutputTemplate (net.parostroj.timetable.model.OutputTemplate)2 TrainTypeCategory (net.parostroj.timetable.model.TrainTypeCategory)2 ImportComponent (net.parostroj.timetable.model.imports.ImportComponent)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 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 XmlElements (javax.xml.bind.annotation.XmlElements)1