Search in sources :

Example 11 with ChildrenProperty

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

the class WidgetNaming method setDefaultName.

/**
 * Set widget's name
 *
 *  <p>Widget that already has unique name remains unchanged.
 *  Otherwise a "_123" instance index is added, using the next unused index.
 *  Name defaults to the widget type.
 *
 *  @param model  Model that will contain the widget (but doesn't, yet)
 *  @param widget Widget to name
 */
public void setDefaultName(final DisplayModel model, final Widget widget) {
    // Check that widget is not already in the model, because otherwise
    // a name lookup would find the widget itself and consider the name
    // as already "in use".
    DisplayModel widget_model;
    try {
        widget_model = widget.getDisplayModel();
    } catch (Exception ex) {
        // OK to not be in any model
        widget_model = null;
    }
    if (widget_model == model)
        throw new IllegalStateException(widget + " already in model " + model);
    String name = widget.getName();
    // Default to human-readable widget type
    if (name.isEmpty())
        name = WidgetFactory.getInstance().getWidgetDescriptor(widget.getType()).getName();
    // Does the name match "SomeName_14"?
    final Matcher matcher = pattern.matcher(name);
    final String base;
    int number;
    if (matcher.matches()) {
        base = matcher.group(1);
        number = Integer.parseInt(matcher.group(2));
    } else {
        base = name;
        number = 0;
    }
    // Check for the next unused instance of this widget name
    synchronized (max_used_instance) {
        final Integer used = max_used_instance.get(base);
        if (used != null) {
            number = Math.max(number, used + 1);
            name = base + "_" + number;
        }
        // Locate next available "SomeName_14"
        while (model.runtimeChildren().getChildByName(name) != null) {
            ++number;
            name = base + "_" + number;
        }
        max_used_instance.put(base, number);
    }
    widget.setPropertyValue(CommonWidgetProperties.propName, name);
    final ChildrenProperty children = ChildrenProperty.getChildren(widget);
    if (children != null) {
        for (Widget child : children.getValue()) setDefaultName(model, child);
    }
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty) DisplayModel(org.csstudio.display.builder.model.DisplayModel) Matcher(java.util.regex.Matcher) Widget(org.csstudio.display.builder.model.Widget)

Example 12 with ChildrenProperty

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

the class WidgetTree method addWidget.

/**
 * Add widget to existing model & tree
 *  @param added_widget Widget to add
 *  @param index Index of widget within parent. -1 to add at end
 */
private void addWidget(final Widget added_widget, final int index) {
    // Have widget and its parent in model
    final Widget widget_parent = added_widget.getParent().get();
    // Determine parent tree item
    TreeItem<WidgetOrTab> item_parent = null;
    if (widget_parent instanceof TabsWidget) {
        for (TabItemProperty tab : ((TabsWidget) widget_parent).propTabs().getValue()) if (tab.children().getValue().contains(added_widget)) {
            item_parent = tab_name2tree.get(tab.name());
            break;
        }
    } else
        item_parent = widget2tree.get(widget_parent);
    Objects.requireNonNull(item_parent, "Cannot obtain parent item for " + added_widget);
    // Create Tree item
    final TreeItem<WidgetOrTab> item = new TreeItem<>(WidgetOrTab.of(added_widget));
    widget2tree.put(added_widget, item);
    item.setExpanded(true);
    if (index >= 0)
        // Add at same index into Tree
        item_parent.getChildren().add(index, item);
    else
        // Append to end
        item_parent.getChildren().add(item);
    added_widget.propName().addPropertyListener(name_listener);
    if (added_widget instanceof TabsWidget) {
        final ArrayWidgetProperty<TabItemProperty> tabs = ((TabsWidget) added_widget).propTabs();
        addTabs(tabs.getValue());
        tabs.addPropertyListener(tabs_property_listener);
    } else {
        final ChildrenProperty children = ChildrenProperty.getChildren(added_widget);
        if (children != null) {
            children.addPropertyListener(children_listener);
            for (Widget child : children.getValue()) addWidget(child, -1);
        }
    }
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty) TabsWidget(org.csstudio.display.builder.model.widgets.TabsWidget) TabItemProperty(org.csstudio.display.builder.model.widgets.TabsWidget.TabItemProperty) TreeItem(javafx.scene.control.TreeItem) TabsWidget(org.csstudio.display.builder.model.widgets.TabsWidget) Widget(org.csstudio.display.builder.model.Widget)

Example 13 with ChildrenProperty

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

the class WidgetTree method removeWidgetListeners.

/**
 * Recursively remove model widget listeners
 *  @param container Widgets to unlink
 */
private void removeWidgetListeners(final Widget widget) {
    if (widget instanceof TabsWidget) {
        final ArrayWidgetProperty<TabItemProperty> tabs = ((TabsWidget) widget).propTabs();
        tabs.removePropertyListener(tabs_property_listener);
        for (TabItemProperty tab : tabs.getValue()) {
            tab.children().removePropertyListener(children_listener);
            tab.name().removePropertyListener(tab_name_listener);
        }
    }
    widget.propName().removePropertyListener(name_listener);
    final ChildrenProperty children = ChildrenProperty.getChildren(widget);
    if (children != null) {
        children.removePropertyListener(children_listener);
        for (Widget child : children.getValue()) removeWidgetListeners(child);
    }
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty) TabsWidget(org.csstudio.display.builder.model.widgets.TabsWidget) TabItemProperty(org.csstudio.display.builder.model.widgets.TabsWidget.TabItemProperty) TabsWidget(org.csstudio.display.builder.model.widgets.TabsWidget) Widget(org.csstudio.display.builder.model.Widget)

Example 14 with ChildrenProperty

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

the class ToolkitRepresentation method representWidget.

/**
 * Create a toolkit widget for a model widget.
 *
 *  <p>Will log errors, but not raise exception.
 *
 *  @param parent Toolkit parent (Group, Container, ..)
 *  @param widget Model widget to represent
 *  @return Toolkit item that represents the widget
 *  @see #disposeWidget(Object, Widget)
 */
public void representWidget(final TWP parent, final Widget widget) {
    @SuppressWarnings("unchecked") final WidgetRepresentationFactory<TWP, TW> factory = (WidgetRepresentationFactory<TWP, TW>) factories.get(widget.getType());
    if (factory == null) {
        logger.log(Level.SEVERE, "Lacking representation for " + widget.getType());
        return;
    }
    final TWP re_parent;
    try {
        final WidgetRepresentation<TWP, TW, Widget> representation = factory.create();
        representation.initialize(this, widget);
        re_parent = representation.createComponents(parent);
        widget.setUserData(Widget.USER_DATA_REPRESENTATION, representation);
        logger.log(Level.FINE, "Representing {0} as {1}", new Object[] { widget, representation });
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Cannot represent " + widget, ex);
        return;
    }
    // Recurse into child widgets
    final ChildrenProperty children = ChildrenProperty.getChildren(widget);
    if (children != null) {
        representChildren(re_parent, widget, children);
        logger.log(Level.FINE, "Tracking changes to children of {0}", widget);
        children.addPropertyListener(container_children_listener);
    }
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty) Widget(org.csstudio.display.builder.model.Widget)

Example 15 with ChildrenProperty

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

the class ToolkitRepresentation method disposeWidget.

/**
 * Remove toolkit widget for model widget
 *  @param widget Model widget that should no longer be represented
 */
public void disposeWidget(final Widget widget) {
    final ChildrenProperty children = ChildrenProperty.getChildren(widget);
    if (children != null) {
        logger.log(Level.FINE, "No longer tracking changes to children of {0}", widget);
        children.removePropertyListener(container_children_listener);
    }
    final WidgetRepresentation<TWP, TW, ? extends Widget> representation = widget.clearUserData(Widget.USER_DATA_REPRESENTATION);
    if (representation != null) {
        logger.log(Level.FINE, "Disposing {0} for {1}", new Object[] { representation, widget });
        representation.dispose();
    }
// else: Widget has no representation because not implemented for this toolkit
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty)

Aggregations

ChildrenProperty (org.csstudio.display.builder.model.ChildrenProperty)17 Widget (org.csstudio.display.builder.model.Widget)12 TabsWidget (org.csstudio.display.builder.model.widgets.TabsWidget)4 Point2D (javafx.geometry.Point2D)3 Rectangle2D (javafx.geometry.Rectangle2D)3 ArrayWidget (org.csstudio.display.builder.model.widgets.ArrayWidget)3 GroupWidget (org.csstudio.display.builder.model.widgets.GroupWidget)3 TabItemProperty (org.csstudio.display.builder.model.widgets.TabsWidget.TabItemProperty)3 AddWidgetAction (org.csstudio.display.builder.editor.undo.AddWidgetAction)2 Matcher (java.util.regex.Matcher)1 TreeItem (javafx.scene.control.TreeItem)1 GroupWidgetsAction (org.csstudio.display.builder.editor.undo.GroupWidgetsAction)1 UpdateWidgetLocationAction (org.csstudio.display.builder.editor.undo.UpdateWidgetLocationAction)1 DisplayModel (org.csstudio.display.builder.model.DisplayModel)1 ParseAgainException (org.csstudio.display.builder.model.WidgetConfigurator.ParseAgainException)1 WidgetTypeException (org.csstudio.display.builder.model.WidgetFactory.WidgetTypeException)1 WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)1 Macros (org.csstudio.display.builder.model.macros.Macros)1 CommonWidgetProperties.propMacros (org.csstudio.display.builder.model.properties.CommonWidgetProperties.propMacros)1 ActionButtonWidget (org.csstudio.display.builder.model.widgets.ActionButtonWidget)1