Search in sources :

Example 21 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class ModelReader method readModel.

/**
 * Read model from XML.
 *  @return Model
 *  @throws Exception on error
 */
public DisplayModel readModel() throws Exception {
    final DisplayModel model = new DisplayModel();
    model.setUserData(DisplayModel.USER_DATA_INPUT_VERSION, version);
    // Read display's own properties
    model.getConfigurator(version).configureFromXML(this, model, root);
    // Read widgets of model
    readWidgets(model.runtimeChildren(), root);
    return model;
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel)

Example 22 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class DisplayEditorPart method createContextMenu.

private Menu createContextMenu(final Control parent) {
    final MenuManager mm = new MenuManager();
    final Action execute = new ExecuteDisplayAction(this);
    final MenuManager morph = new MorphWidgetMenuSupport(editor).getMenuManager();
    final ImageDescriptor icon = AbstractUIPlugin.imageDescriptorFromPlugin(ModelPlugin.ID, "icons/display.png");
    final Action perspective = new OpenPerspectiveAction(icon, Messages.OpenEditorPerspective, EditorPerspective.ID);
    final Action reload = new ReloadDisplayAction(this);
    mm.setRemoveAllWhenShown(true);
    mm.addMenuListener(manager -> {
        manager.add(execute);
        final List<Widget> selection = editor.getWidgetSelectionHandler().getSelection();
        if (!selection.isEmpty()) {
            if (selection.size() >= 1)
                manager.add(new CreateGroupAction(editor, selection));
            if (selection.size() == 1 && selection.get(0) instanceof GroupWidget)
                manager.add(new RemoveGroupAction(editor, (GroupWidget) selection.get(0)));
            if (selection.size() == 1 && selection.get(0) instanceof EmbeddedDisplayWidget)
                manager.add(new EditEmbeddedDisplayAction((EmbeddedDisplayWidget) selection.get(0)));
            manager.add(morph);
        }
        manager.add(reload);
        final DisplayModel model = editor.getModel();
        if (model != null && !model.isClassModel()) {
            manager.add(new ReloadClassesAction(this));
            if (selection.isEmpty())
                manager.add(new SetDisplaySize(editor));
        }
        manager.add(perspective);
    });
    return mm.createContextMenu(parent);
}
Also used : OpenPerspectiveAction(org.csstudio.ui.util.perspective.OpenPerspectiveAction) IAction(org.eclipse.jface.action.IAction) RedoAction(org.csstudio.display.builder.editor.rcp.actions.RedoAction) CutDeleteAction(org.csstudio.display.builder.editor.rcp.actions.CutDeleteAction) PasteAction(org.csstudio.display.builder.editor.rcp.actions.PasteAction) OpenPerspectiveAction(org.csstudio.ui.util.perspective.OpenPerspectiveAction) ReloadDisplayAction(org.csstudio.display.builder.editor.rcp.actions.ReloadDisplayAction) RemoveGroupAction(org.csstudio.display.builder.editor.rcp.actions.RemoveGroupAction) ExecuteDisplayAction(org.csstudio.display.builder.editor.rcp.actions.ExecuteDisplayAction) CreateGroupAction(org.csstudio.display.builder.editor.rcp.actions.CreateGroupAction) SelectAllAction(org.csstudio.display.builder.editor.rcp.actions.SelectAllAction) CopyAction(org.csstudio.display.builder.editor.rcp.actions.CopyAction) Action(org.eclipse.jface.action.Action) ReloadClassesAction(org.csstudio.display.builder.editor.rcp.actions.ReloadClassesAction) UndoAction(org.csstudio.display.builder.editor.rcp.actions.UndoAction) CreateGroupAction(org.csstudio.display.builder.editor.rcp.actions.CreateGroupAction) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) Widget(org.csstudio.display.builder.model.Widget) ReloadClassesAction(org.csstudio.display.builder.editor.rcp.actions.ReloadClassesAction) ReloadDisplayAction(org.csstudio.display.builder.editor.rcp.actions.ReloadDisplayAction) ExecuteDisplayAction(org.csstudio.display.builder.editor.rcp.actions.ExecuteDisplayAction) DisplayModel(org.csstudio.display.builder.model.DisplayModel) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) MenuManager(org.eclipse.jface.action.MenuManager) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) RemoveGroupAction(org.csstudio.display.builder.editor.rcp.actions.RemoveGroupAction) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget)

Example 23 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel 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 24 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class TrackerGridConstraint method constrain.

@Override
public Point2D constrain(final double x, final double y) {
    final DisplayModel copy = model;
    if (copy == null)
        return new Point2D(x, y);
    final int grid_x = copy.propGridStepX().getValue(), grid_y = copy.propGridStepY().getValue();
    return new Point2D(Math.floor((x + grid_x / 2) / grid_x) * grid_x, Math.floor((y + grid_y / 2) / grid_y) * grid_y);
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) Point2D(javafx.geometry.Point2D)

Example 25 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class DisplayEditor method setModel.

/**
 * Set Model
 *  @param model Model to show and edit
 */
public void setModel(final DisplayModel model) {
    // to allow resolving images etc. relative to that file
    if (model.getUserData(DisplayModel.USER_DATA_INPUT_FILE) == null)
        logger.log(Level.SEVERE, "Model lacks input file information");
    undo.clear();
    widget_naming.clear();
    selection.clear();
    group_handler.setModel(model);
    selection_tracker.setModel(model);
    final DisplayModel old_model = this.model;
    if (old_model != null)
        toolkit.disposeRepresentation(old_model);
    this.model = Objects.requireNonNull(model);
    // Create representation for model items
    try {
        toolkit.representModel(widget_parent, model);
    } catch (final Exception ex) {
        logger.log(Level.SEVERE, "Error representing model", ex);
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel)

Aggregations

DisplayModel (org.csstudio.display.builder.model.DisplayModel)73 Widget (org.csstudio.display.builder.model.Widget)15 Test (org.junit.Test)10 LabelWidget (org.csstudio.display.builder.model.widgets.LabelWidget)9 GroupWidget (org.csstudio.display.builder.model.widgets.GroupWidget)7 EmbeddedDisplayRepresentationUtil.loadDisplayModel (org.csstudio.display.builder.representation.EmbeddedDisplayRepresentationUtil.loadDisplayModel)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 EmbeddedDisplayWidget (org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget)5 Image (javafx.scene.image.Image)4 File (java.io.File)3 InputStream (java.io.InputStream)3 Parent (javafx.scene.Parent)3 Macros (org.csstudio.display.builder.model.macros.Macros)3 PictureWidget (org.csstudio.display.builder.model.widgets.PictureWidget)3 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 Rectangle2D (javafx.geometry.Rectangle2D)2 WidgetFactoryUnitTest (org.csstudio.display.builder.model.WidgetFactoryUnitTest)2 WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)2 ModelWriter (org.csstudio.display.builder.model.persist.ModelWriter)2