Search in sources :

Example 31 with Model

use of org.csstudio.trends.databrowser3.model.Model in project org.csstudio.display.builder by kasemir.

the class XMLPersistence method write.

/**
 * Write XML formatted Model content.
 *  @param model Model to write
 *  @param out OutputStream, will NOT be closed when done.
 */
public void write(final Model model, final OutputStream out) {
    final PrintWriter writer = new PrintWriter(out);
    XMLWriter.header(writer);
    XMLWriter.start(writer, 0, TAG_DATABROWSER);
    writer.println();
    XMLWriter.XML(writer, 1, TAG_TITLE, model.getTitle().orElse(""));
    XMLWriter.XML(writer, 1, TAG_SAVE_CHANGES, model.shouldSaveChanges());
    // Visibility of toolbar and legend
    XMLWriter.XML(writer, 1, TAG_SHOW_LEGEND, model.isLegendVisible());
    XMLWriter.XML(writer, 1, TAG_SHOW_TOOLBAR, model.isToolbarVisible());
    // Time axis
    XMLWriter.XML(writer, 1, TAG_GRID, model.isGridVisible());
    XMLWriter.XML(writer, 1, TAG_SCROLL, model.isScrollEnabled());
    XMLWriter.XML(writer, 1, TAG_UPDATE_PERIOD, model.getUpdatePeriod());
    XMLWriter.XML(writer, 1, TAG_SCROLL_STEP, model.getScrollStep().getSeconds());
    XMLWriter.XML(writer, 1, TAG_START, model.getStartSpec());
    XMLWriter.XML(writer, 1, TAG_END, model.getEndSpec());
    XMLWriter.XML(writer, 1, TAG_ARCHIVE_RESCALE, model.getArchiveRescale().name());
    writeColor(writer, 1, TAG_BACKGROUND, model.getPlotBackground());
    XMLWriter.XML(writer, 1, TAG_TITLE_FONT, SWTMediaPool.getFontDescription(model.getTitleFont()));
    XMLWriter.XML(writer, 1, TAG_LABEL_FONT, SWTMediaPool.getFontDescription(model.getLabelFont()));
    XMLWriter.XML(writer, 1, TAG_SCALE_FONT, SWTMediaPool.getFontDescription(model.getScaleFont()));
    XMLWriter.XML(writer, 1, TAG_LEGEND_FONT, SWTMediaPool.getFontDescription(model.getLegendFont()));
    // Value axes
    XMLWriter.start(writer, 1, TAG_AXES);
    writer.println();
    for (AxisConfig axis : model.getAxes()) axis.write(writer);
    XMLWriter.end(writer, 1, TAG_AXES);
    writer.println();
    // Annotations
    XMLWriter.start(writer, 1, TAG_ANNOTATIONS);
    writer.println();
    for (AnnotationInfo annotation : model.getAnnotations()) annotation.write(writer);
    XMLWriter.end(writer, 1, TAG_ANNOTATIONS);
    writer.println();
    // PVs (Formulas)
    XMLWriter.start(writer, 1, TAG_PVLIST);
    writer.println();
    for (ModelItem item : model.getItems()) item.write(writer);
    XMLWriter.end(writer, 1, TAG_PVLIST);
    writer.println();
    XMLWriter.end(writer, 0, TAG_DATABROWSER);
    writer.flush();
}
Also used : AxisConfig(org.csstudio.trends.databrowser3.model.AxisConfig) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) PrintWriter(java.io.PrintWriter) AnnotationInfo(org.csstudio.trends.databrowser3.model.AnnotationInfo)

Example 32 with Model

use of org.csstudio.trends.databrowser3.model.Model in project org.csstudio.display.builder by kasemir.

the class XMLPersistence method load.

private void load(final Model model, final Document doc) throws Exception {
    if (model.getItems().iterator().hasNext())
        throw new RuntimeException("Model was already in use");
    // Check if it's a <databrowser/>.
    doc.getDocumentElement().normalize();
    final Element root_node = doc.getDocumentElement();
    if (!root_node.getNodeName().equals(TAG_DATABROWSER))
        throw new Exception("Expected " + TAG_DATABROWSER + " but got " + root_node.getNodeName());
    // Global settings
    String title = DOMHelper.getSubelementString(root_node, TAG_TITLE);
    if (!title.isEmpty())
        model.setTitle(title);
    model.setSaveChanges(DOMHelper.getSubelementBoolean(root_node, TAG_SAVE_CHANGES, true));
    model.setGridVisible(DOMHelper.getSubelementBoolean(root_node, TAG_GRID, false));
    model.enableScrolling(DOMHelper.getSubelementBoolean(root_node, TAG_SCROLL, true));
    model.setUpdatePeriod(DOMHelper.getSubelementDouble(root_node, TAG_UPDATE_PERIOD, Preferences.getUpdatePeriod()));
    try {
        model.setScrollStep(Duration.ofSeconds(DOMHelper.getSubelementInt(root_node, TAG_SCROLL_STEP, (int) Preferences.getScrollStep().getSeconds())));
    } catch (Throwable ex) {
    // Ignore
    }
    final String start = DOMHelper.getSubelementString(root_node, TAG_START);
    final String end = DOMHelper.getSubelementString(root_node, TAG_END);
    if (start.length() > 0 && end.length() > 0)
        model.setTimerange(start, end);
    final String rescale = DOMHelper.getSubelementString(root_node, TAG_ARCHIVE_RESCALE, ArchiveRescale.STAGGER.name());
    try {
        model.setArchiveRescale(ArchiveRescale.valueOf(rescale));
    } catch (Throwable ex) {
    // Ignore
    }
    model.setToolbarVisible(DOMHelper.getSubelementBoolean(root_node, TAG_SHOW_TOOLBAR, true));
    model.setLegendVisible(DOMHelper.getSubelementBoolean(root_node, TAG_SHOW_LEGEND, true));
    // Value Axes
    Element list = DOMHelper.findFirstElementNode(root_node.getFirstChild(), TAG_AXES);
    if (list != null) {
        Element item = DOMHelper.findFirstElementNode(list.getFirstChild(), TAG_AXIS);
        while (item != null) {
            model.addAxis(AxisConfig.fromDocument(item));
            item = DOMHelper.findNextElementNode(item, TAG_AXIS);
        }
    } else {
        // Check for legacy <xyGraphSettings> <axisSettingsList>
        list = DOMHelper.findFirstElementNode(root_node.getFirstChild(), TAG_OLD_XYGRAPH_SETTINGS);
        if (list != null) {
            loadColorFromDocument(list, "plotAreaBackColor").ifPresent(model::setPlotBackground);
            Element item = DOMHelper.findFirstElementNode(list.getFirstChild(), "axisSettingsList");
            if (item != null) {
                // First axis is 'X'
                model.setGridVisible(DOMHelper.getSubelementBoolean(item, "showMajorGrid", false));
                // Read 'Y' axes
                item = DOMHelper.findNextElementNode(item, "axisSettingsList");
                while (item != null) {
                    final String name = DOMHelper.getSubelementString(item, "title", null);
                    final AxisConfig axis = new AxisConfig(name);
                    loadColorFromDocument(item, "foregroundColor").ifPresent(axis::setColor);
                    axis.setGridVisible(DOMHelper.getSubelementBoolean(item, "showMajorGrid", false));
                    axis.setLogScale(DOMHelper.getSubelementBoolean(item, "logScale", false));
                    axis.setAutoScale(DOMHelper.getSubelementBoolean(item, "autoScale", false));
                    final Element range = DOMHelper.findFirstElementNode(item.getFirstChild(), "range");
                    if (range != null) {
                        double min = DOMHelper.getSubelementDouble(range, "lower", axis.getMin());
                        double max = DOMHelper.getSubelementDouble(range, "upper", axis.getMax());
                        axis.setRange(min, max);
                    }
                    model.addAxis(axis);
                    // Using legacy settings from _last_ axis for fonts
                    loadFontFromDocument(item, "scaleFont").ifPresent(model::setScaleFont);
                    loadFontFromDocument(item, "titleFont").ifPresent(model::setLabelFont);
                    item = DOMHelper.findNextElementNode(item, "axisSettingsList");
                }
            }
        }
    }
    // New settings, possibly replacing settings from legacy <xyGraphSettings> <axisSettingsList>
    loadColorFromDocument(root_node, TAG_BACKGROUND).ifPresent(model::setPlotBackground);
    loadFontFromDocument(root_node, TAG_TITLE_FONT).ifPresent(model::setTitleFont);
    loadFontFromDocument(root_node, TAG_LABEL_FONT).ifPresent(model::setLabelFont);
    loadFontFromDocument(root_node, TAG_SCALE_FONT).ifPresent(model::setScaleFont);
    loadFontFromDocument(root_node, TAG_LEGEND_FONT).ifPresent(model::setLegendFont);
    // Load Annotations
    list = DOMHelper.findFirstElementNode(root_node.getFirstChild(), TAG_ANNOTATIONS);
    if (list != null) {
        // Load PV items
        final List<AnnotationInfo> annotations = new ArrayList<>();
        Element item = DOMHelper.findFirstElementNode(list.getFirstChild(), TAG_ANNOTATION);
        while (item != null) {
            try {
                annotations.add(AnnotationInfo.fromDocument(item));
            } catch (Throwable ex) {
                Activator.getLogger().log(Level.INFO, "XML error in Annotation", ex);
            }
            item = DOMHelper.findNextElementNode(item, TAG_ANNOTATION);
        }
        model.setAnnotations(annotations);
    }
    // Load PVs/Formulas
    list = DOMHelper.findFirstElementNode(root_node.getFirstChild(), TAG_PVLIST);
    if (list != null) {
        // Load PV items
        Element item = DOMHelper.findFirstElementNode(list.getFirstChild(), TAG_PV);
        while (item != null) {
            final PVItem model_item = PVItem.fromDocument(model, item);
            // Adding item creates the axis for it if not already there
            model.addItem(model_item);
            // Ancient data browser stored axis configuration with each item: Update axis from that.
            final AxisConfig axis = model_item.getAxis();
            String s = DOMHelper.getSubelementString(item, TAG_AUTO_SCALE);
            if (s.equalsIgnoreCase("true"))
                axis.setAutoScale(true);
            s = DOMHelper.getSubelementString(item, TAG_LOG_SCALE);
            if (s.equalsIgnoreCase("true"))
                axis.setLogScale(true);
            final double min = DOMHelper.getSubelementDouble(item, TAG_MIN, axis.getMin());
            final double max = DOMHelper.getSubelementDouble(item, TAG_MAX, axis.getMax());
            axis.setRange(min, max);
            item = DOMHelper.findNextElementNode(item, TAG_PV);
        }
        // Load Formulas
        item = DOMHelper.findFirstElementNode(list.getFirstChild(), TAG_FORMULA);
        while (item != null) {
            model.addItem(FormulaItem.fromDocument(model, item));
            item = DOMHelper.findNextElementNode(item, TAG_FORMULA);
        }
    }
    // Update items from legacy <xyGraphSettings>
    list = DOMHelper.findFirstElementNode(root_node.getFirstChild(), TAG_OLD_XYGRAPH_SETTINGS);
    if (list != null) {
        title = DOMHelper.getSubelementString(list, TAG_TITLE);
        if (!title.isEmpty())
            model.setTitle(title);
        final Iterator<ModelItem> model_items = model.getItems().iterator();
        Element item = DOMHelper.findFirstElementNode(list.getFirstChild(), "traceSettingsList");
        while (item != null) {
            if (!model_items.hasNext())
                break;
            final ModelItem pv = model_items.next();
            Optional<RGB> rgb = loadColorFromDocument(item, "traceColor");
            if (rgb.isPresent()) {
                pv.setColor(SWTMediaPool.getJFX(rgb.get()));
            }
            pv.setLineWidth(DOMHelper.getSubelementInt(item, "lineWidth", pv.getLineWidth()));
            pv.setDisplayName(DOMHelper.getSubelementString(item, "name", pv.getDisplayName()));
            item = DOMHelper.findNextElementNode(item, "traceSettingsList");
        }
    }
}
Also used : AxisConfig(org.csstudio.trends.databrowser3.model.AxisConfig) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) RGB(org.eclipse.swt.graphics.RGB) PVItem(org.csstudio.trends.databrowser3.model.PVItem) AnnotationInfo(org.csstudio.trends.databrowser3.model.AnnotationInfo)

Example 33 with Model

use of org.csstudio.trends.databrowser3.model.Model in project org.csstudio.display.builder by kasemir.

the class OpenDisplayFile method openDisplay.

/**
 * {@inheritDoc}
 */
@Override
public void openDisplay(final String path, final String data) throws Exception {
    final Model model = new Model();
    // Read file
    final ResourceHelper resources = SingleSourcePlugin.getResourceHelper();
    final IPath ipath = resources.newPath(path);
    try (final InputStream stream = resources.getInputStream(ipath)) {
        new XMLPersistence().load(model, stream);
    }
    final IEditorInput input = new DataBrowserModelEditorInput(new PathEditorInput(ipath), model);
    // Create new editor
    final DataBrowserEditor editor = DataBrowserEditor.createInstance(input);
    if (editor == null)
        throw new Exception("Cannot create Data Browser");
}
Also used : IPath(org.eclipse.core.runtime.IPath) DataBrowserModelEditorInput(org.csstudio.trends.databrowser3.editor.DataBrowserModelEditorInput) InputStream(java.io.InputStream) Model(org.csstudio.trends.databrowser3.model.Model) ResourceHelper(org.csstudio.utility.singlesource.ResourceHelper) XMLPersistence(org.csstudio.trends.databrowser3.persistence.XMLPersistence) IEditorInput(org.eclipse.ui.IEditorInput) DataBrowserEditor(org.csstudio.trends.databrowser3.editor.DataBrowserEditor) PathEditorInput(org.csstudio.utility.singlesource.PathEditorInput)

Example 34 with Model

use of org.csstudio.trends.databrowser3.model.Model in project org.csstudio.display.builder by kasemir.

the class DataBrowserEditor method save.

/**
 * Save current model content, mark editor as clean.
 *
 *  @param monitor <code>IProgressMonitor</code>, may be null.
 *  @param stream The stream to use.
 *  @return Returns <code>true</code> when successful.
 */
private void save(final IProgressMonitor monitor, final OutputStream stream) throws Exception {
    monitor.beginTask(Messages.Save, IProgressMonitor.UNKNOWN);
    try {
        new XMLPersistence().write(model, stream);
        setDirty(false);
        return;
    } catch (Exception ex) {
        ExceptionDetailsErrorDialog.openError(getSite().getShell(), Messages.Error, ex);
        // Writing failed, prompt for different name or 'cancel'
        doSaveAs();
    } finally {
        monitor.done();
    }
}
Also used : XMLPersistence(org.csstudio.trends.databrowser3.persistence.XMLPersistence) PartInitException(org.eclipse.ui.PartInitException)

Example 35 with Model

use of org.csstudio.trends.databrowser3.model.Model in project org.csstudio.display.builder by kasemir.

the class WaveformView method updateAnnotation.

private void updateAnnotation(final Instant time, final double value) {
    final List<AnnotationInfo> annotations = new ArrayList<AnnotationInfo>(model.getAnnotations());
    // Initial annotation offset
    Point2D offset = new Point2D(20, -20);
    // If already in model, note its offset and remove
    for (AnnotationInfo annotation : annotations) {
        if (annotation.getText().equals(ANNOTATION_TEXT)) {
            // Update offset to where user last placed it
            offset = annotation.getOffset();
            waveform_annotation = annotation;
            annotations.remove(waveform_annotation);
            break;
        }
    }
    int i = 0;
    int item_index = 0;
    for (ModelItem item : model.getItems()) {
        if (item == model_item) {
            item_index = i;
            break;
        }
        i++;
    }
    waveform_annotation = new AnnotationInfo(true, item_index, time, value, offset, ANNOTATION_TEXT);
    annotations.add(waveform_annotation);
    changing_annotations = true;
    model.setAnnotations(annotations);
    changing_annotations = false;
}
Also used : Point2D(javafx.geometry.Point2D) ArrayList(java.util.ArrayList) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) AnnotationInfo(org.csstudio.trends.databrowser3.model.AnnotationInfo)

Aggregations

ModelItem (org.csstudio.trends.databrowser3.model.ModelItem)16 Model (org.csstudio.trends.databrowser3.model.Model)15 ArrayList (java.util.ArrayList)10 PVItem (org.csstudio.trends.databrowser3.model.PVItem)10 AxisConfig (org.csstudio.trends.databrowser3.model.AxisConfig)9 XMLPersistence (org.csstudio.trends.databrowser3.persistence.XMLPersistence)7 Instant (java.time.Instant)6 InputStream (java.io.InputStream)5 FormulaInput (org.csstudio.trends.databrowser3.model.FormulaInput)5 FormulaItem (org.csstudio.trends.databrowser3.model.FormulaItem)4 Shell (org.eclipse.swt.widgets.Shell)4 AnnotationInfo (org.csstudio.trends.databrowser3.model.AnnotationInfo)3 PlotSample (org.csstudio.trends.databrowser3.model.PlotSample)3 PlotSamples (org.csstudio.trends.databrowser3.model.PlotSamples)3 MinSizeTableColumnLayout (org.csstudio.ui.util.MinSizeTableColumnLayout)3 IPath (org.eclipse.core.runtime.IPath)3 Separator (org.eclipse.jface.action.Separator)3 TableColumnLayout (org.eclipse.jface.layout.TableColumnLayout)3 Composite (org.eclipse.swt.widgets.Composite)3 PartInitException (org.eclipse.ui.PartInitException)3