Search in sources :

Example 1 with ModelWriter

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();
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) FileOutputStream(java.io.FileOutputStream) ModelWriter(org.csstudio.display.builder.model.persist.ModelWriter)

Example 2 with ModelWriter

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;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ActionButtonWidget(org.csstudio.display.builder.model.widgets.ActionButtonWidget) ActionInfos(org.csstudio.display.builder.model.properties.ActionInfos) ActionButtonWidget(org.csstudio.display.builder.model.widgets.ActionButtonWidget) Macros(org.csstudio.display.builder.model.macros.Macros) OpenDisplayActionInfo(org.csstudio.display.builder.model.properties.OpenDisplayActionInfo) ModelWriter(org.csstudio.display.builder.model.persist.ModelWriter)

Example 3 with ModelWriter

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();
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) DisplayModel(org.csstudio.display.builder.model.DisplayModel) ByteArrayInputStream(java.io.ByteArrayInputStream) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Job(org.eclipse.core.runtime.jobs.Job) ModelWriter(org.csstudio.display.builder.model.persist.ModelWriter) IEditorInput(org.eclipse.ui.IEditorInput)

Example 4 with ModelWriter

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);
        }
    });
}
Also used : FileOutputStream(java.io.FileOutputStream) ModelWriter(org.csstudio.display.builder.model.persist.ModelWriter)

Aggregations

ModelWriter (org.csstudio.display.builder.model.persist.ModelWriter)4 FileOutputStream (java.io.FileOutputStream)3 DisplayModel (org.csstudio.display.builder.model.DisplayModel)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Macros (org.csstudio.display.builder.model.macros.Macros)1 ActionInfos (org.csstudio.display.builder.model.properties.ActionInfos)1 OpenDisplayActionInfo (org.csstudio.display.builder.model.properties.OpenDisplayActionInfo)1 ActionButtonWidget (org.csstudio.display.builder.model.widgets.ActionButtonWidget)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 IEditorInput (org.eclipse.ui.IEditorInput)1 PartInitException (org.eclipse.ui.PartInitException)1 FileEditorInput (org.eclipse.ui.part.FileEditorInput)1