use of net.parostroj.timetable.model.ls.LSFile 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.LSFile in project grafikon by jub77.
the class LoadDiagramModelAction method backgroundAction.
@Override
protected void backgroundAction() {
selectedFile = (File) getActionContext().getAttribute("diagramFile");
if (selectedFile == null) {
// skip
return;
}
setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
setWaitDialogVisible(true);
long time = System.currentTimeMillis();
try {
try {
LSFile ls = LSFileFactory.getInstance().createForLoad(selectedFile);
context.setAttribute("diagram", ls.load(selectedFile));
} 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.LSFile in project grafikon by jub77.
the class ModelUtils method saveModelData.
public static void saveModelData(final ApplicationModel model, File file) throws LSException {
// update author and date before save
TrainDiagram diagram = model.getDiagram();
boolean originalSkip = diagram.getAttributes().isSkipListeners();
diagram.getAttributes().setSkipListeners(true);
String user = model.getProgramSettings().getUserNameOrSystemUser();
diagram.setSaveUser(user);
final ChangesTracker tracker = diagram.getChangesTracker();
final DiagramChangeSet set = tracker.getCurrentChangeSet();
if (set != null && tracker.isTrackingEnabled()) {
try {
// do the update in event dispatch thread (because of events)
SwingUtilities.invokeAndWait(() -> {
tracker.updateCurrentChangeSet(set.getVersion(), user, Calendar.getInstance());
});
} catch (Exception e) {
log.warn("Error updating values for current diagram change set.", e);
}
}
log.info("Saving: {}", file);
LSFile ls = LSFileFactory.getInstance().createForSave();
ls.save(diagram, file);
diagram.getAttributes().setSkipListeners(originalSkip);
}
use of net.parostroj.timetable.model.ls.LSFile in project grafikon by jub77.
the class MainFrame method aboutMenuItemActionPerformed.
private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// show about dialog
ResourceBundle aboutBundle = ResourceBundle.getBundle("about");
LSFileFactory f = LSFileFactory.getInstance();
LSFile fls = null;
try {
fls = f.createForSave();
} catch (LSException e) {
log.warn("Cannot create FileLoadSave", e);
}
AboutDialog dialog = new AboutDialog(this, true, String.format(aboutBundle.getString("text"), model.getVersionInfo().getVersion(), fls == null ? "-" : fls.getSaveVersion()), getClass().getResource(aboutBundle.getString("image")), true, model.getVersionInfo());
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);
dialog.dispose();
}
use of net.parostroj.timetable.model.ls.LSFile in project grafikon by jub77.
the class NewOpenAction method open.
private void open(final Component parent, final File preselectedFile) {
// check changes
final int result = ModelUtils.checkModelChangedContinue(model, parent);
if (result == JOptionPane.CANCEL_OPTION) {
return;
}
// save old diagram
ActionContext context = new ActionContext(parent);
if (result == JOptionPane.YES_OPTION) {
ModelAction saveAction = SaveAction.getSaveModelAction(context, model.getOpenedFile(), parent, model);
ActionHandler.getInstance().execute(saveAction);
}
ModelAction openAction = new CombinedModelAction(context) {
private int retVal;
private TrainDiagram diagram;
private String errorMessage;
private Exception errorException;
private File selectedFile;
@Override
protected void eventDispatchActionBefore() {
if (preselectedFile == null) {
try (CloseableFileChooser modelFileChooser = FileChooserFactory.getInstance().getFileChooser(FileChooserFactory.Type.GTM)) {
retVal = modelFileChooser.showOpenDialog(parent);
if (retVal == JFileChooser.APPROVE_OPTION) {
selectedFile = modelFileChooser.getSelectedFile();
}
}
} else {
selectedFile = preselectedFile;
retVal = JFileChooser.APPROVE_OPTION;
}
}
@Override
protected void backgroundAction() {
if (retVal != JFileChooser.APPROVE_OPTION) {
return;
}
setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
setWaitDialogVisible(true);
long time = System.currentTimeMillis();
try {
try {
model.setOpenedFile(selectedFile);
log.info("Loading: {}", selectedFile);
LSFile ls = LSFileFactory.getInstance().createForLoad(selectedFile);
diagram = ls.load(selectedFile);
} catch (LSException e) {
log.warn("Error loading model.", e);
if (e.getCause() instanceof FileNotFoundException) {
// remove from last opened
model.removeLastOpenedFile(selectedFile);
// create error message
errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
} else if (e.getCause() instanceof IOException) {
errorMessage = ResourceLoader.getString("dialog.error.loading");
} else {
errorMessage = ResourceLoader.getString("dialog.error.loading");
errorException = e;
}
} catch (Exception e) {
log.warn("Error loading model.", e);
errorMessage = ResourceLoader.getString("dialog.error.loading");
}
} finally {
log.debug("Loaded in {}ms", System.currentTimeMillis() - time);
}
}
@Override
protected void eventDispatchActionAfter() {
try {
if (retVal != JFileChooser.APPROVE_OPTION) {
return;
}
if (diagram != null) {
model.setDiagram(diagram);
} else {
String text = errorMessage + " " + selectedFile.getName();
if (errorException != null) {
text = text + "\n(" + errorException.getMessage() + ")";
}
context.setAttribute("error", text);
model.setDiagram(null);
}
} finally {
setWaitDialogVisible(false);
}
}
};
ActionHandler.getInstance().execute(openAction);
ActionHandler.getInstance().execute(ModelAction.newEdtAction(context, () -> {
// show error
if (context.getAttribute("error") != null) {
GuiComponentUtils.showError((String) context.getAttribute("error"), parent);
}
}));
}
Aggregations