use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.
the class LoadDiagramUrlModelAction method backgroundAction.
@Override
protected void backgroundAction() {
url = (String) getActionContext().getAttribute("diagramUrl");
if (url == null) {
// skip
return;
}
setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
setWaitDialogVisible(true);
long time = System.currentTimeMillis();
try {
try (ZipInputStream is = new ZipInputStream(new URL(url).openStream())) {
LSFile ls = LSFileFactory.getInstance().createForLoad(is);
context.setAttribute("diagram", ls.load(is));
} catch (LSException e) {
log.warn("Error loading model.", e);
if (e.getCause() instanceof FileNotFoundException) {
errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
} else {
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} catch (Exception e) {
log.warn("Error loading model.", e);
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} finally {
log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
setWaitDialogVisible(false);
}
}
use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.
the class LoadLibraryUrlModelAction method backgroundAction.
@Override
protected void backgroundAction() {
url = (String) getActionContext().getAttribute("libraryUrl");
if (url == null) {
// skip
return;
}
setWaitMessage(ResourceLoader.getString("wait.message.loadlibrary"));
setWaitDialogVisible(true);
long time = System.currentTimeMillis();
try {
try (ZipInputStream is = new ZipInputStream(new URL(url).openStream())) {
LSLibrary ls = LSLibraryFactory.getInstance().createForLoad(is);
context.setAttribute("library", ls.load(is));
} catch (LSException e) {
log.warn("Error loading model.", e);
if (e.getCause() instanceof FileNotFoundException) {
errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
} else {
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} catch (Exception e) {
log.warn("Error loading model.", e);
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} finally {
log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
setWaitDialogVisible(false);
}
}
use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.
the class LoadLibraryModelAction method backgroundAction.
@Override
protected void backgroundAction() {
selectedFile = (File) getActionContext().getAttribute("libraryFile");
if (selectedFile == null) {
// skip
return;
}
setWaitMessage(ResourceLoader.getString("wait.message.loadlibrary"));
setWaitDialogVisible(true);
long time = System.currentTimeMillis();
try {
try {
LSLibrary ls = LSLibraryFactory.getInstance().createForLoad(selectedFile);
try (ZipInputStream stream = new ZipInputStream(new FileInputStream(selectedFile))) {
context.setAttribute("library", ls.load(stream));
}
} catch (LSException e) {
log.warn("Error loading model.", e);
if (e.getCause() instanceof FileNotFoundException) {
errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
} else {
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} catch (Exception e) {
log.warn("Error loading model.", e);
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} finally {
log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
setWaitDialogVisible(false);
}
}
use of net.parostroj.timetable.model.ls.LSException 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.ls.LSException in project grafikon by jub77.
the class LoadSave method save.
@Override
public void save(TrainDiagram diagram, File file) throws LSException {
try (ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(file))) {
// save metadata
zipOutput.putNextEntry(new ZipEntry(METADATA));
this.createMetadata().store(zipOutput, null);
// save train types
LSTrainTypeList trainTypeList = new LSTrainTypeList(diagram.getTrainTypes(), diagram.getTrainsData());
zipOutput.putNextEntry(new ZipEntry(TRAIN_TYPES_NAME));
LSTrainTypeSerializer tts = LSTrainTypeSerializer.getLSTrainTypeSerializer(LSSerializer.getLatestVersion());
tts.save(new OutputStreamWriter(zipOutput, "utf-8"), trainTypeList);
// save diagram
zipOutput.putNextEntry(new ZipEntry(TRAIN_DIAGRAM_NAME));
this.saveTrainDiagram(new OutputStreamWriter(zipOutput, "utf-8"), diagram, trainTypeList);
// save images
LoadSaveImages lsImages = new LoadSaveImages();
lsImages.saveTimetableImages(diagram, zipOutput);
} catch (IOException ex) {
throw new LSException(ex);
}
}
Aggregations