use of org.csstudio.display.builder.model.persist.ModelWriter in project org.csstudio.display.builder by kasemir.
the class ExampleModels method main.
public static void main(String[] args) throws Exception {
final DisplayModel model = createModel();
final ModelWriter writer = new ModelWriter(new FileOutputStream("example.opi"));
try {
writer.writeModel(model);
} finally {
writer.close();
}
}
use of org.csstudio.display.builder.model.persist.ModelWriter in project org.csstudio.display.builder by kasemir.
the class AllWidgetsAllProperties method main.
public static void main(String[] args) throws Exception {
final DisplayModel model = new DisplayModel();
for (final WidgetDescriptor widget_type : WidgetFactory.getInstance().getWidgetDescriptions()) {
Widget widget = widget_type.createWidget();
widget.setPropertyValue("name", widget_type.getName() + " 1");
// For some widgets, adjust default values
if (widget_type == ActionButtonWidget.WIDGET_DESCRIPTOR) {
ActionButtonWidget button = (ActionButtonWidget) widget;
final Macros macros = new Macros();
macros.add("S", "Test");
macros.add("N", "2");
button.propActions().setValue(new ActionInfos(Arrays.asList(new OpenDisplayActionInfo("Display", "other.opi", macros, Target.REPLACE))));
}
model.runtimeChildren().addChild(widget);
}
ModelWriter.skip_defaults = false;
try {
final ModelWriter writer = new ModelWriter(new FileOutputStream(EXAMPLE_FILE));
writer.writeModel(model);
writer.close();
} finally {
ModelWriter.skip_defaults = true;
}
}
use of org.csstudio.display.builder.model.persist.ModelWriter in project org.csstudio.display.builder by kasemir.
the class DisplayEditorPart method saveModelToFile.
/**
* Save model to file, using background thread
* @param save_monitor On success, <code>done</code> will be called <u>on UI thread</u>. Otherwise cancelled.
* @param file File to save
*/
private void saveModelToFile(final IProgressMonitor save_monitor, final IFile file) {
// Save on background thread
final Job job = new Job("Save") {
@Override
protected IStatus run(final IProgressMonitor monitor) {
final SubMonitor progress = SubMonitor.convert(monitor, 100);
// Refresh file to detect modification outside of workspace
try {
file.refreshLocal(IResource.DEPTH_ONE, progress);
} catch (CoreException ex) {
logger.log(Level.WARNING, "Cannot refresh " + file, ex);
}
// Check if the file has changed
final long mod = file.getModificationStamp();
if (mod != IResource.NULL_STAMP && modification_marker != IResource.NULL_STAMP && mod != modification_marker) {
// Prompt on UI thread
final Future<Boolean> prompt = toolkit.submit(() -> {
return MessageDialog.openConfirm(getSite().getShell(), "File has changed", "The file\n " + file.getFullPath().toString() + "\n" + "has been changed while you were editing it.\n\n" + "'OK' to save and thus overwrite what somebody else has written,\n" + "or\n" + "'Cancel' and then re-load the file or save it under a different name.");
});
// Wait for the UI task to complete
try {
// On cancel, return without calling save_monitor.done()
if (!prompt.get())
return Status.OK_STATUS;
} catch (Exception ex) {
logger.log(Level.WARNING, "Cannot prompt about changed file", ex);
return Status.OK_STATUS;
}
}
final DisplayModel model = editor.getModel();
logger.log(Level.FINE, "Save as {0}", file);
// but that requires a stream. So first persist into memory buffer..
try {
final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
final ModelWriter writer = new ModelWriter(tmp);
writer.writeModel(model);
writer.close();
progress.worked(40);
// .. then write file from buffer
final ByteArrayInputStream stream = new ByteArrayInputStream(tmp.toByteArray());
if (file.exists())
file.setContents(stream, true, false, monitor);
else
file.create(stream, true, monitor);
progress.worked(40);
modification_marker = file.getModificationStamp();
} catch (Exception ex) {
logger.log(Level.SEVERE, "Cannot save as " + file, ex);
save_monitor.setCanceled(true);
save_monitor.done();
return Status.OK_STATUS;
}
// Back on UI thread..
final IEditorInput input = new FileEditorInput(file);
final Future<Object> update_input = toolkit.submit(() -> {
// Update editor input to current file name
setInput(input);
setPartName(model.getDisplayName());
setTitleToolTip(input.getToolTipText());
// Clear 'undo'
editor.getUndoableActionManager().clear();
// Signal success
save_monitor.done();
return null;
});
// Wait for the UI task to complete
try {
update_input.get();
} catch (Exception ex) {
logger.log(Level.WARNING, "Cannot update editor input", ex);
save_monitor.setCanceled(true);
save_monitor.done();
}
progress.done();
// displays will use it.
if (editor.getModel().isClassModel())
org.csstudio.display.builder.rcp.Plugin.reloadConfigurationFiles();
return Status.OK_STATUS;
}
};
job.schedule();
}
use of org.csstudio.display.builder.model.persist.ModelWriter in project org.csstudio.display.builder by kasemir.
the class EditorDemoGUI method saveModelAs.
/**
* Save model to file
* @param file File into which to save the model
*/
public void saveModelAs(final File file) {
EditorUtil.getExecutor().execute(() -> {
logger.log(Level.FINE, "Save as {0}", file);
try (final ModelWriter writer = new ModelWriter(new FileOutputStream(file))) {
writer.writeModel(editor.getModel());
this.file = file;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Cannot save as " + file, ex);
}
});
}
Aggregations