use of org.csstudio.display.builder.editor.undo.UpdateWidgetLocationAction in project org.csstudio.display.builder by kasemir.
the class SelectedWidgetUITracker method updateWidgetsFromTracker.
/**
* Updates widgets to current tracker location and size
*/
private void updateWidgetsFromTracker(final Rectangle2D original, final Rectangle2D current) {
if (updating)
return;
updating = true;
try {
group_handler.hide();
final List<Rectangle2D> orig_position = widgets.stream().map(GeometryTools::getBounds).collect(Collectors.toList());
// If there was only one widget, the tracker bounds represent
// the desired widget location and size.
// But tracker bounds can apply to one or more widgets, so need to
// determine the change in tracker bounds, apply those to each widget.
final double dx = current.getMinX() - original.getMinX();
final double dy = current.getMinY() - original.getMinY();
final double dw = current.getWidth() - original.getWidth();
final double dh = current.getHeight() - original.getHeight();
final int N = orig_position.size();
// Use compound action if there's more than one widget
final CompoundUndoableAction compound = N > 1 ? new CompoundUndoableAction(Messages.UpdateWidgetLocation) : null;
for (int i = 0; i < N; ++i) {
final Widget widget = widgets.get(i);
final Rectangle2D orig = orig_position.get(i);
final ChildrenProperty orig_parent_children = ChildrenProperty.getParentsChildren(widget);
ChildrenProperty parent_children = group_handler.getActiveParentChildren();
if (parent_children == null)
parent_children = widget.getDisplayModel().runtimeChildren();
if (orig_parent_children == parent_children) {
// Slightly faster since parent stays the same
if (!widget.propX().isUsingWidgetClass())
widget.propX().setValue((int) (orig.getMinX() + dx));
if (!widget.propY().isUsingWidgetClass())
widget.propY().setValue((int) (orig.getMinY() + dy));
} else {
// Update to new parent
if (widget.getDisplayModel().isClassModel()) {
logger.log(Level.WARNING, "Widget hierarchy is not permitted for class model");
return;
}
final Point2D old_offset = GeometryTools.getDisplayOffset(widget);
orig_parent_children.removeChild(widget);
parent_children.addChild(widget);
final Point2D new_offset = GeometryTools.getDisplayOffset(widget);
logger.log(Level.FINE, "{0} moves from {1} ({2}) to {3} ({4})", new Object[] { widget, orig_parent_children.getWidget(), old_offset, parent_children.getWidget(), new_offset });
// Account for old and new display offset
if (!widget.propX().isUsingWidgetClass())
widget.propX().setValue((int) (orig.getMinX() + dx + old_offset.getX() - new_offset.getX()));
if (!widget.propY().isUsingWidgetClass())
widget.propY().setValue((int) (orig.getMinY() + dy + old_offset.getY() - new_offset.getY()));
}
if (!widget.propWidth().isUsingWidgetClass())
widget.propWidth().setValue((int) Math.max(1, orig.getWidth() + dw));
if (!widget.propHeight().isUsingWidgetClass())
widget.propHeight().setValue((int) Math.max(1, orig.getHeight() + dh));
final UndoableAction step = new UpdateWidgetLocationAction(widget, orig_parent_children, parent_children, (int) orig.getMinX(), (int) orig.getMinY(), (int) orig.getWidth(), (int) orig.getHeight());
if (compound == null)
undo.add(step);
else
compound.add(step);
}
if (compound != null)
undo.add(compound);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Failed to move/resize widgets", ex);
} finally {
updating = false;
updateTrackerFromWidgets();
}
}
Aggregations