Search in sources :

Example 11 with TrainDiagram

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

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

the class RemoveWeightsAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    final Component parent = GuiComponentUtils.getTopLevelComponent(e.getSource());
    int result = JOptionPane.showConfirmDialog(parent, ResourceLoader.getString("dialog.confirm.action.progress"), ResourceLoader.getString("menu.special.remove.weights"), JOptionPane.YES_NO_OPTION);
    if (result == JOptionPane.YES_OPTION) {
        // remove weights
        RxActionHandler.getInstance().newExecution("weight_removal", GuiComponentUtils.getTopLevelComponent(e.getSource()), model.get()).onBackground().logTime().setMessage(ResourceLoader.getString("wait.message.recalculate")).split(TrainDiagram::getTrains, 10).addEdtBatchConsumer((context, train) -> {
            train.removeAttribute(Train.ATTR_WEIGHT);
        }).execute();
    }
}
Also used : ApplicationModel(net.parostroj.timetable.gui.ApplicationModel) AbstractAction(javax.swing.AbstractAction) GuiComponentUtils(net.parostroj.timetable.gui.utils.GuiComponentUtils) RxActionHandler(net.parostroj.timetable.gui.actions.execution.RxActionHandler) JOptionPane(javax.swing.JOptionPane) Component(java.awt.Component) ActionEvent(java.awt.event.ActionEvent) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) Train(net.parostroj.timetable.model.Train) ResourceLoader(net.parostroj.timetable.utils.ResourceLoader) Component(java.awt.Component) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Example 13 with TrainDiagram

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

the class OutputPM method getUniqueKey.

private String getUniqueKey(OutputTemplate template) {
    TrainDiagram diagram = diagramRef.get();
    String keyStr = template.getKey();
    if (diagram != null) {
        String originalKey = keyStr;
        int counter = 0;
        while (!checkUnique(keyStr, diagram)) {
            keyStr = String.format("%s_%d", originalKey, ++counter);
        }
    }
    return keyStr;
}
Also used : LocalizedString(net.parostroj.timetable.model.LocalizedString) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Example 14 with TrainDiagram

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

the class LoadSave method load.

@Override
public TrainDiagram load(File file) throws LSException {
    try (ZipFile zip = new ZipFile(file)) {
        TrainDiagram diagram = null;
        // load metadata
        ZipEntry entry = zip.getEntry(METADATA);
        Properties metadata = new Properties();
        if (entry != null) {
            // load metadata
            metadata.load(zip.getInputStream(entry));
        }
        // set model version
        ModelVersion modelVersion = null;
        if (metadata.getProperty(METADATA_KEY_MODEL_VERSION) == null) {
            modelVersion = ModelVersion.parseModelVersion("1.0");
        } else {
            modelVersion = ModelVersion.parseModelVersion(metadata.getProperty(METADATA_KEY_MODEL_VERSION));
        }
        ModelVersion latest = LSSerializer.getLatestVersion();
        if (latest.getMajorVersion() < modelVersion.getMajorVersion() || (latest.getMajorVersion() == modelVersion.getMajorVersion() && latest.getMinorVersion() < modelVersion.getMinorVersion())) {
            throw new LSException("Cannot load newer model.");
        }
        // load train types
        entry = zip.getEntry(TRAIN_TYPES_NAME);
        InputStream isTypes = null;
        if (entry == null) {
            isTypes = DefaultTrainTypeListSource.getDefaultTypesInputStream();
        } else {
            isTypes = zip.getInputStream(entry);
        }
        LSTrainTypeSerializer tts = LSTrainTypeSerializer.getLSTrainTypeSerializer(modelVersion);
        LSTrainTypeList trainTypeList = tts.load(new InputStreamReader(isTypes, "utf-8"));
        // load model
        entry = zip.getEntry(TRAIN_DIAGRAM_NAME);
        if (entry == null) {
            throw new LSException("Model not found.");
        }
        diagram = this.loadTrainDiagram(modelVersion, metadata, new InputStreamReader(zip.getInputStream(entry), "utf-8"), trainTypeList);
        // load images
        LoadSaveImages lsImages = new LoadSaveImages();
        lsImages.loadTimetableImages(diagram, zip);
        return diagram;
    } catch (ZipException ex) {
        throw new LSException(ex);
    } catch (IOException ex) {
        throw new LSException(ex);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) Properties(java.util.Properties) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) ZipFile(java.util.zip.ZipFile) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) LSException(net.parostroj.timetable.model.ls.LSException)

Example 15 with TrainDiagram

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

the class ChildrenDelegateTypesImpl method addChildNode.

@Override
public int addChildNode(TrainTreeNode node, TrainTreeNode newChildNode) {
    TrainType newType = this.getTypeFromNode(newChildNode);
    if (newType != null) {
        TrainDiagram diagram = newType.getDiagram();
        for (int i = 0; i < node.getChildCount(); i++) {
            TrainTreeNode childNode = (TrainTreeNode) node.getChildAt(i);
            if (this.compareTypes(diagram, this.getTypeFromNode(newChildNode), this.getTypeFromNode(childNode)) < 0) {
                node.insert(newChildNode, i);
                return i;
            }
        }
    }
    node.add(newChildNode);
    return node.getChildCount() - 1;
}
Also used : TrainType(net.parostroj.timetable.model.TrainType) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Aggregations

TrainDiagram (net.parostroj.timetable.model.TrainDiagram)23 LSException (net.parostroj.timetable.model.ls.LSException)5 IOException (java.io.IOException)4 Train (net.parostroj.timetable.model.Train)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 TrainsCycle (net.parostroj.timetable.model.TrainsCycle)3 Component (java.awt.Component)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ZipEntry (java.util.zip.ZipEntry)2 JOptionPane (javax.swing.JOptionPane)2 AfterLoadCheck (net.parostroj.timetable.actions.AfterLoadCheck)2 ExportImportSelection (net.parostroj.timetable.gui.components.ExportImportSelection)2 GuiComponentUtils (net.parostroj.timetable.gui.utils.GuiComponentUtils)2 LocalizedString (net.parostroj.timetable.model.LocalizedString)2 Library (net.parostroj.timetable.model.library.Library)2 ModelVersion (net.parostroj.timetable.model.ls.ModelVersion)2 Predicate (com.google.common.base.Predicate)1