<commit_before>/*
 * Copyright 2011 JBoss Inc
 * 
 * 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.drools.guvnor.client.decisiontable.widget;

import java.util.ArrayList;

import org.drools.guvnor.client.widgets.decoratedgrid.DecoratedGridSidebarWidget;
import org.drools.guvnor.client.widgets.decoratedgrid.DecoratedGridWidget;
import org.drools.guvnor.client.widgets.decoratedgrid.DynamicDataRow;
import org.drools.guvnor.client.widgets.decoratedgrid.HasRows;
import org.drools.ide.common.client.modeldriven.dt.DTColumnConfig;

import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.TableElement;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.dom.client.TableSectionElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.CellPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A sidebar for a VericalDecisionTable. This provides a vertical list of
 * controls to add and remove the associated row from the DecisionTable.
 * 
 * @author manstis
 * 
 */
public class VerticalDecisionTableSidebarWidget extends
        DecoratedGridSidebarWidget<DTColumnConfig> {

    /**
     * Widget to render selectors beside rows. Two selectors are provided per
     * row: (1) A "add new row (above selected)" and (2) "delete row".
     */
    private class VerticalSelectorWidget extends CellPanel {

        // Widgets (selectors) created (so they can be removed later)
        private ArrayList<Widget> widgets = new ArrayList<Widget>();

        private VerticalSelectorWidget() {
            getBody().getParentElement().<TableElement> cast()
                    .setCellSpacing( 0 );
            getBody().getParentElement().<TableElement> cast()
                    .setCellPadding( 0 );
            sinkEvents( Event.getTypeInt( "click" ) );
        }

        // Add a new row
        private void appendSelector(DynamicDataRow row) {
            insertSelectorBefore( row,
                                  widgets.size() );
        }

        // Delete a row at the given index
        private void deleteSelector(int index) {
            if ( index < 0 ) {
                throw new IllegalArgumentException( "index cannot be less than zero" );
            }
            if ( index > widgets.size()) {
                throw new IllegalArgumentException( "index cannot be greate than the number of rows" );
            }

            Widget widget = widgets.get( index );
            remove( widget );
            getBody().<TableSectionElement> cast().deleteRow( index );
            widgets.remove( index );
            fixStyles( index );
        }

        // Row styles need to be re-applied after inserting and deleting rows
        private void fixStyles(int iRow) {
            while ( iRow < getBody().getChildCount() ) {
                TableRowElement tre = getBody().getChild( iRow )
                        .<TableRowElement> cast();
                tre.setClassName( getRowStyle( iRow ) );
                iRow++;
            }
        }

        // Get style applicable to row
        private String getRowStyle(int iRow) {
            boolean isEven = iRow % 2 == 0;
            String trClasses = isEven ? style.cellTableEvenRow() : style
                    .cellTableOddRow();
            return trClasses;
        }

        // Initialise for a complete redraw
        private void initialise() {
            int totalRows = widgets.size();
            for ( int iRow = 0; iRow < totalRows; iRow++ ) {
                deleteSelector( 0 );
            }
        }

        // Insert a new row before the given index
        private void insertSelectorBefore(DynamicDataRow row,
                                          int index) {
            if ( row == null ) {
                throw new IllegalArgumentException( "row cannot be null" );
            }
            if ( index < 0 ) {
                throw new IllegalArgumentException( "index cannot be less than zero" );
            }
            if ( index > widgets.size() ) {
                throw new IllegalArgumentException( "index cannot be greate than the number of rows" );
            }

            Element tre = DOM.createTR();
            Element tce = DOM.createTD();
            tre.setClassName( getRowStyle( widgets.size() ) );
            tre.getStyle().setHeight( style.rowHeight(),
                                      Unit.PX );
            tce.addClassName( style.selectorCell() );
            DOM.insertChild( getBody(),
                             tre,
                             index );
            tre.appendChild( tce );

            Widget widget = makeRowWidget( row );
            add( widget,<commit_msg>dtable: Fix x-browser rendering issue.<commit_after>/*
 * Copyright 2011 JBoss Inc
 * 
 * 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.drools.guvnor.client.decisiontable.widget;

import java.util.ArrayList;

import org.drools.guvnor.client.widgets.decoratedgrid.DecoratedGridSidebarWidget;
import org.drools.guvnor.client.widgets.decoratedgrid.DecoratedGridWidget;
import org.drools.guvnor.client.widgets.decoratedgrid.DynamicDataRow;
import org.drools.guvnor.client.widgets.decoratedgrid.HasRows;
import org.drools.ide.common.client.modeldriven.dt.DTColumnConfig;

import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.TableElement;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.dom.client.TableSectionElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.CellPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A sidebar for a VericalDecisionTable. This provides a vertical list of
 * controls to add and remove the associated row from the DecisionTable.
 * 
 * @author manstis
 * 
 */
public class VerticalDecisionTableSidebarWidget extends
        DecoratedGridSidebarWidget<DTColumnConfig> {

    /**
     * Widget to render selectors beside rows. Two selectors are provided per
     * row: (1) A "add new row (above selected)" and (2) "delete row".
     */
    private class VerticalSelectorWidget extends CellPanel {

        // Widgets (selectors) created (so they can be removed later)
        private ArrayList<Widget> widgets = new ArrayList<Widget>();

        private VerticalSelectorWidget() {
            getBody().getParentElement().<TableElement> cast()
                    .setCellSpacing( 0 );
            getBody().getParentElement().<TableElement> cast()
                    .setCellPadding( 0 );
            sinkEvents( Event.getTypeInt( "click" ) );
        }

        // Add a new row
        private void appendSelector(DynamicDataRow row) {
            insertSelectorBefore( row,
                                  widgets.size() );
        }

        // Delete a row at the given index
        private void deleteSelector(int index) {
            if ( index < 0 ) {
                throw new IllegalArgumentException( "index cannot be less than zero" );
            }
            if ( index > widgets.size()) {
                throw new IllegalArgumentException( "index cannot be greate than the number of rows" );
            }

            Widget widget = widgets.get( index );
            remove( widget );
            getBody().<TableSectionElement> cast().deleteRow( index );
            widgets.remove( index );
            fixStyles( index );
        }

        // Row styles need to be re-applied after inserting and deleting rows
        private void fixStyles(int iRow) {
            while ( iRow < getBody().getChildCount() ) {
                TableRowElement tre = getBody().getChild( iRow )
                        .<TableRowElement> cast();
                tre.setClassName( getRowStyle( iRow ) );
                iRow++;
            }
        }

        // Get style applicable to row
        private String getRowStyle(int iRow) {
            boolean isEven = iRow % 2 == 0;
            String trClasses = isEven ? style.cellTableEvenRow() : style
                    .cellTableOddRow();
            return trClasses;
        }

        // Initialise for a complete redraw
        private void initialise() {
            int totalRows = widgets.size();
            for ( int iRow = 0; iRow < totalRows; iRow++ ) {
                deleteSelector( 0 );
            }
        }

        // Insert a new row before the given index
        private void insertSelectorBefore(DynamicDataRow row,
                                          int index) {
            if ( row == null ) {
                throw new IllegalArgumentException( "row cannot be null" );
            }
            if ( index < 0 ) {
                throw new IllegalArgumentException( "index cannot be less than zero" );
            }
            if ( index > widgets.size() ) {
                throw new IllegalArgumentException( "index cannot be greate than the number of rows" );
            }

            Element tre = DOM.createTR();
            Element tce = DOM.createTD();
            tre.setClassName( getRowStyle( widgets.size() ) );
            tce.getStyle().setHeight( style.rowHeight(),
                                      Unit.PX );
            tce.addClassName( style.selectorCell() );
            DOM.insertChild( getBody(),
                             tre,
                             index );
            tre.appendChild( tce );

            Widget widget = makeRowWidget( row );
            add( widget,
{'fraction_non_alphanumeric': 0.09486753519279531, 'fraction_numerical': 0.0031476785870420566, 'mean_word_length': 2.1362763915547025, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2899217', 'n_tokens_mistral': 3080, 'n_tokens_neox': 2967, 'n_words': 861}