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);
}
}
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();
}
}
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;
}
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);
}
}
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;
}
Aggregations