/**
 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.talend.sdk.component.studio.ui.composite.controller;

import org.eclipse.gef.commands.Command;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.talend.commons.ui.runtime.image.ImageProvider;
import org.talend.core.model.process.IElementParameter;
import org.talend.core.ui.CoreUIPlugin;
import org.talend.core.ui.properties.tab.IDynamicProperty;
import org.talend.sdk.component.studio.model.parameter.ButtonParameter;

/**
 * Creates SWT Button control
 */
public class TaCoKitButtonController extends AbstractTaCoKitController {

    public TaCoKitButtonController(final IDynamicProperty dp) {
        super(dp);
    }

    @Override
    public Control createControl(final Composite subComposite, final IElementParameter param, final int numInRow,
            final int nbInRow, final int top, final Control lastControl) {
        Button button = new Button(subComposite, SWT.NONE);
        button.setBackground(subComposite.getBackground());
        if (param.getDisplayName().equals("")) {
            button.setImage(ImageProvider.getImage(CoreUIPlugin.getImageDescriptor(DOTS_BUTTON)));
        } else {
            button.setText(param.getDisplayName());
        }

        FormData data = new FormData();
        if (isInWizard()) {
            if (lastControl != null) {
                data.right = new FormAttachment(lastControl, 0);
            } else {
                data.right = new FormAttachment(100, -ITabbedPropertyConstants.HSPACE);
            }
        } else {
            if (lastControl != null) {
                data.left = new FormAttachment(lastControl, 0);
            } else {
                data.left = new FormAttachment((((numInRow - 1) * MAX_PERCENT) / nbInRow), 0);
            }
        }
        data.top = new FormAttachment(0, top);
        data.height = STANDARD_HEIGHT + 2;
        button.setLayoutData(data);
        button.setEnabled(!param.isReadOnly());
        button.setData(param);
        hashCurControls.put(param.getName(), button);
        button.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(final SelectionEvent e) {
                Command cmd = createCommand((Button) e.getSource());
                executeCommand(cmd);
            }
        });
        return button;
    }

    public Command createCommand(final Button button) {
        final ButtonParameter parameter = (ButtonParameter) button.getData();
        return parameter.getCommand();
    }

}

@@ -836,24 +836,63 @@
 ctionEvent;%0A
+import org.eclipse.swt.graphics.Point;%0A
 import org.e
@@ -921,24 +921,24 @@
 Attachment;%0A
-
 import org.e
@@ -1642,24 +1642,738 @@
 dp);%0A    %7D%0A%0A
+    /**%0A     * Creates Button control%0A     * Sets current row size in the end of the method. It is required to have proper position for subsequent controls%0A     * %0A     * @param subComposite composite which will contain created control%0A     * @param param ElementParameter instance%0A     * @param numInRow number of created control in the current row%0A     * @param nbInRow total number of all controls in the current row%0A     * @param top offset from top of the composite. It is computed as row size of all previously added controls + spaces between controls %0A     * @param lastControl previously added control in the current row. It is null, if it is first control in the row%0A     * @return created button%0A     */%0A
     @Overrid
@@ -3986,32 +3986,32 @@
 ;%0A            %7D%0A
-
         %7D);%0A    
@@ -4006,16 +4006,178 @@
     %7D);%0A
+        Point initialSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);%0A        dynamicProperty.setCurRowSize(initialSize.y + ITabbedPropertyConstants.VSPACE);%0A
         

fix tacokit button layout