<commit_before>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.trends.databrowser2.model;

import java.io.PrintWriter;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;

import org.csstudio.apputil.xml.DOMHelper;
import org.csstudio.apputil.xml.XMLWriter;
import org.csstudio.swt.rtplot.PointType;
import org.csstudio.swt.rtplot.TraceType;
import org.csstudio.swt.rtplot.data.PlotDataItem;
import org.csstudio.trends.databrowser2.persistence.XMLPersistence;
import org.csstudio.trends.databrowser2.preferences.Preferences;
import org.eclipse.swt.graphics.RGB;
import org.w3c.dom.Element;

/** Base of {@link PVItem} and {@link FormulaItem},
 *  i.e. the items held by the {@link Model}.
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
abstract public class ModelItem
{
    /** Name by which the item is identified: PV name, formula */
    private volatile String name;

    /** Model that contains this item. Empty while not assigned to a model
     */
    protected Optional<Model> model = Optional.empty();

    /** Preferred display name, used in plot legend */
    private volatile String display_name;

    /** Units, may be <code>null</code> */
    private volatile String units;

    /** Show item's samples? */
    private volatile boolean visible = true;

    /** RGB for item's color
     *  <p>
     *  Technically, swt.graphics.RGB adds a UI dependency to the Model.
     *  As long as the Model can still run without a Display
     *  or Shell, this might be OK.
     */
    private volatile RGB rgb = null;

    /** How to display the trace */
    private volatile TraceType trace_type = Preferences.getTraceType();

    /** Line width [pixel] */
    private volatile int line_width = Preferences.getLineWidth();

    /** How to display the points of the trace */
    private volatile PointType point_type = PointType.NONE;

    /** Point size [pixel] */
    private volatile int point_size = Preferences.getLineWidth();

    /** Y-Axis */
    private volatile AxisConfig axis = null;

    /** Sample that is currently selected, for example via cursor */
    private volatile Optional<PlotDataItem<Instant>> selected_sample = Optional.empty();

    /** Initialize
     *  @param name Name of the PV or the formula
     */
    public ModelItem(final String name)
    {
        this.name = name;
        this.display_name = name;
    }

    /** @return Model that contains this item */
    public Optional<Model> getModel()
    {
        return model;
    }

    /** Called by Model to add item to Model or remove it from model.
     *  Should not be called by other code!
     *  @param model Model to which item was added or <code>null</code> when removed
     */
    void setModel(final Model model)
    {
        final Optional<Model> new_model = Optional.ofNullable(model);
        if (this.model.equals(new_model))
            throw new RuntimeException("Item re-assigned to same model: " + name);
        this.model = new_model;
    }

    /** @return Name of this item (PV, Formula, ...), may contain macros */
    public String getName()
    {
        return name;
    }

    /** @return Name of this item (PV, Formula, ...) with all macros resolved */
    public String getResolvedName()
    {
        if (model.isPresent())
            return model.get().resolveMacros(name);
        else
            return name;
    }

    /** @param new_name New item name
     *  @see #getName()
     *  @return <code>true</code> if name was actually changed
     *  @throws Exception on error (cannot create PV for new name, ...)
     */
    public boolean setName(String new_name) throws Exception
    {
        new_name = new_name.trim();
        if (new_name.equals(name))
            return false;
        name = new_name;
        fireItemLookChanged();
        return true;<commit_msg>Prevent changing DB pv name to an empty string.<commit_after>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.trends.databrowser2.model;

import java.io.PrintWriter;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;

import org.csstudio.apputil.xml.DOMHelper;
import org.csstudio.apputil.xml.XMLWriter;
import org.csstudio.swt.rtplot.PointType;
import org.csstudio.swt.rtplot.TraceType;
import org.csstudio.swt.rtplot.data.PlotDataItem;
import org.csstudio.trends.databrowser2.persistence.XMLPersistence;
import org.csstudio.trends.databrowser2.preferences.Preferences;
import org.eclipse.swt.graphics.RGB;
import org.w3c.dom.Element;

/** Base of {@link PVItem} and {@link FormulaItem},
 *  i.e. the items held by the {@link Model}.
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
abstract public class ModelItem
{
    /** Name by which the item is identified: PV name, formula */
    private volatile String name;

    /** Model that contains this item. Empty while not assigned to a model
     */
    protected Optional<Model> model = Optional.empty();

    /** Preferred display name, used in plot legend */
    private volatile String display_name;

    /** Units, may be <code>null</code> */
    private volatile String units;

    /** Show item's samples? */
    private volatile boolean visible = true;

    /** RGB for item's color
     *  <p>
     *  Technically, swt.graphics.RGB adds a UI dependency to the Model.
     *  As long as the Model can still run without a Display
     *  or Shell, this might be OK.
     */
    private volatile RGB rgb = null;

    /** How to display the trace */
    private volatile TraceType trace_type = Preferences.getTraceType();

    /** Line width [pixel] */
    private volatile int line_width = Preferences.getLineWidth();

    /** How to display the points of the trace */
    private volatile PointType point_type = PointType.NONE;

    /** Point size [pixel] */
    private volatile int point_size = Preferences.getLineWidth();

    /** Y-Axis */
    private volatile AxisConfig axis = null;

    /** Sample that is currently selected, for example via cursor */
    private volatile Optional<PlotDataItem<Instant>> selected_sample = Optional.empty();

    /** Initialize
     *  @param name Name of the PV or the formula
     */
    public ModelItem(final String name)
    {
        this.name = name;
        this.display_name = name;
    }

    /** @return Model that contains this item */
    public Optional<Model> getModel()
    {
        return model;
    }

    /** Called by Model to add item to Model or remove it from model.
     *  Should not be called by other code!
     *  @param model Model to which item was added or <code>null</code> when removed
     */
    void setModel(final Model model)
    {
        final Optional<Model> new_model = Optional.ofNullable(model);
        if (this.model.equals(new_model))
            throw new RuntimeException("Item re-assigned to same model: " + name);
        this.model = new_model;
    }

    /** @return Name of this item (PV, Formula, ...), may contain macros */
    public String getName()
    {
        return name;
    }

    /** @return Name of this item (PV, Formula, ...) with all macros resolved */
    public String getResolvedName()
    {
        if (model.isPresent())
            return model.get().resolveMacros(name);
        else
            return name;
    }

    /** @param new_name New item name
     *  @see #getName()
     *  @return <code>true</code> if name was actually changed
     *  @throws Exception on error (cannot create PV for new name, ...)
     */
    public boolean setName(String new_name) throws Exception
    {
        new_name = new_name.trim();
        if (new_name.isEmpty())
            throw new Exception("Empty PV name");
        if (new_name.equals(name))
            return false;
        name = new_name;
        fireItemLookChanged();
        return true;
{'fraction_non_alphanumeric': 0.13491329479768785, 'fraction_numerical': 0.0027745664739884392, 'mean_word_length': 2.8933393339333935, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2685245', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2329, 'n_words': 812}