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;
}
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);
}
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();
}
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);
}
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);
}
}
Aggregations