<commit_before>/*******************************************************************************
 * Copyright (c) 2015-2016 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.display.builder.editor.rcp.actions;

import java.util.List;

import org.csstudio.display.builder.editor.DisplayEditor;
import org.csstudio.display.builder.editor.Messages;
import org.csstudio.display.builder.editor.undo.GroupWidgetsAction;
import org.csstudio.display.builder.editor.util.GeometryTools;
import org.csstudio.display.builder.model.ChildrenProperty;
import org.csstudio.display.builder.model.ModelPlugin;
import org.csstudio.display.builder.model.Widget;
import org.csstudio.display.builder.model.widgets.GroupWidget;
import org.csstudio.display.builder.util.undo.UndoableActionManager;
import org.eclipse.jface.action.Action;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import javafx.geometry.Rectangle2D;

/** SWT Action to create group
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class CreateGroupAction extends Action
{
    private final DisplayEditor editor;
    private final List<Widget> widgets;

    public CreateGroupAction(final DisplayEditor editor, final List<Widget> widgets)
    {
        super(Messages.CreateGroup,
              AbstractUIPlugin.imageDescriptorFromPlugin(ModelPlugin.ID, "icons/group.png"));
        this.editor = editor;
        this.widgets = widgets;
    }

    @Override
    public void run()
    {
        editor.getWidgetSelectionHandler().clear();

        final GroupWidget group = new GroupWidget();

        // Create group that surrounds the original widget boundaries
        final Rectangle2D rect = GeometryTools.getDisplayBounds(widgets);

        // Inset depends on representation and changes with group style and font.
        // Can be obtained via group.runtimePropInsets() _after_ the group has
        // been represented, but right now this guess is based on the
        // JFX GroupRepresentation with group box and default font
        final int inset = 16;
        group.propX().setValue((int) rect.getMinX() - inset);
        group.propY().setValue((int) rect.getMinY() - inset);
        group.propWidth().setValue((int) rect.getWidth() + 2*inset);
        group.propHeight().setValue((int) rect.getHeight() + 2*inset);
        group.propName().setValue(org.csstudio.display.builder.model.Messages.GroupWidget_Name);
<commit_msg>Editor: Fix for creating 'group' of widgets within 'group'<commit_after>/*******************************************************************************
 * Copyright (c) 2015-2016 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.display.builder.editor.rcp.actions;

import java.util.List;

import org.csstudio.display.builder.editor.DisplayEditor;
import org.csstudio.display.builder.editor.Messages;
import org.csstudio.display.builder.editor.undo.GroupWidgetsAction;
import org.csstudio.display.builder.editor.util.GeometryTools;
import org.csstudio.display.builder.model.ChildrenProperty;
import org.csstudio.display.builder.model.ModelPlugin;
import org.csstudio.display.builder.model.Widget;
import org.csstudio.display.builder.model.widgets.GroupWidget;
import org.csstudio.display.builder.util.undo.UndoableActionManager;
import org.eclipse.jface.action.Action;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import javafx.geometry.Rectangle2D;

/** SWT Action to create group
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class CreateGroupAction extends Action
{
    private final DisplayEditor editor;
    private final List<Widget> widgets;

    public CreateGroupAction(final DisplayEditor editor, final List<Widget> widgets)
    {
        super(Messages.CreateGroup,
              AbstractUIPlugin.imageDescriptorFromPlugin(ModelPlugin.ID, "icons/group.png"));
        this.editor = editor;
        this.widgets = widgets;
    }

    @Override
    public void run()
    {
        editor.getWidgetSelectionHandler().clear();

        // Create group that surrounds the original widget boundaries
        final GroupWidget group = new GroupWidget();

        // Get bounds of widgets relative to their container,
        // which might be a group within the display
        // or the display itself
        final Rectangle2D rect = widgets.stream()
                                        .map(GeometryTools::getBounds)
                                        .reduce(null, GeometryTools::join);

        // Inset depends on representation and changes with group style and font.
        // Can be obtained via group.runtimePropInsets() _after_ the group has
        // been represented, but right now this guess is based on the
        // JFX GroupRepresentation with group box and default font
        final int inset = 16;
        group.propX().setValue((int) rect.getMinX() - inset);
        group.propY().setValue((int) rect.getMinY() - inset);
        group.propWidth().setValue((int) rect.getWidth() + 2*inset);
        group.propHeight().setValue((int) rect.getHeight() + 2*inset);
        group.propName().setValue(org.csstudio.display.builder.model.Messages.GroupWidget_Name);

{'fraction_non_alphanumeric': 0.1481417158735672, 'fraction_numerical': 0.006252170892671066, 'mean_word_length': 3.747732893652102, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '23920', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1484, 'n_words': 389}