use of net.parostroj.timetable.model.ls.LSException 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);
}
}));
}
use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.
the class NewModelPM method create.
private void create() {
final Template templateInstance = template.getValue();
final Scale scaleValue = scale.getValue();
final double timeScaleValue = timeScale.getBigDecimal().doubleValue();
this.createTask = new Callable<TrainDiagram>() {
@Override
public TrainDiagram call() throws LSException {
TrainDiagram diagram = templateLoader.loadTemplate(templateInstance);
diagram.setAttribute(TrainDiagram.ATTR_SCALE, scaleValue);
diagram.setAttribute(TrainDiagram.ATTR_TIME_SCALE, timeScaleValue);
return diagram;
}
};
}
use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.
the class LSOutputTemplate method createOutputTemplate.
public OutputTemplate createOutputTemplate(PartFactory partFactory, Function<String, ObjectWithId> mapping, FileLoadSaveAttachments flsAttachments) throws LSException {
OutputTemplate outputTemplate = partFactory.createOutputTemplate(id);
if (name != null) {
// name mapped to key
outputTemplate.setKey(name);
}
if (this.template != null) {
outputTemplate.setTemplate(this.template.createTextTemplate());
}
if (this.script != null) {
outputTemplate.setScript(this.script.createScript());
}
outputTemplate.getAttributes().add(attributes.createAttributes(mapping));
// process attachments
if (attachments != null) {
for (LSAttachment attachment : attachments) {
if (attachment.getRef() != null) {
if (flsAttachments == null) {
throw new LSException("Attachment loader cannot be null");
}
flsAttachments.addForLoad(attachment, outputTemplate);
} else {
// process inline data
if (attachment.getBinaryData() != null) {
outputTemplate.getAttachments().add(new Attachment(attachment.getName(), attachment.getBinaryData()));
} else if (attachment.getTextData() != null) {
outputTemplate.getAttachments().add(new Attachment(attachment.getName(), attachment.getTextData()));
}
}
}
}
return outputTemplate;
}
Aggregations