Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Cannot enable/disable toolbar button in a dialog

    SmartClient Version: v10.1p_2016-01-07/PowerEdition Deployment (built 2016-01-07)

    Not browser specific; tried with FireFox 24, IE 11, and Chrome Version 47.0.2526.106 m (64-bit)

    Description:
    When using toolbar buttons on a com.smartgwt.client.widgets.Dialog, the buttons cannot be enabled/disabled programatically after they are drawn.
    For example, during creation, a button can be disabled, but cannot be re-enabled from a selection handler on a list grid.

    Sample Code:

    Code:
    package com.smartgwt.sample.showcase.client.dataintegration.java.file;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.smartgwt.client.widgets.Button;
    import com.smartgwt.client.widgets.Dialog;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.grid.ListGridRecord;
    import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
    import com.smartgwt.client.widgets.grid.events.SelectionEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    
    public class MySampleDlg extends Dialog {
    
        private static final int HEIGHT = 320;
    
        private static final int WIDTH = 566;
    
        private static final int LISTGRID_HEIGHT = 220;
    
    
        private final ListGrid myListGrid = new ListGrid();
    
        final Button deleteButton = new Button( "Delete" );
    
        public MySampleDlg() {
    
            this.initialize();
        }
    
        private void initialize() {
    
            this.setWidth( WIDTH );
            this.setHeight( HEIGHT );
    
            final Button okButton = new Button( "OK" );
    
            this.setTitle( "My Sample Dialog" );
            this.setToolbarButtons( okButton, this.deleteButton );
    
            this.setCanDragReposition( Boolean.TRUE );
            this.setShowMinimizeButton( Boolean.FALSE );
            this.setIsModal( Boolean.TRUE );
            this.setShowModalMask( Boolean.TRUE );
            this.centerInPage();
    
            okButton.addClickHandler( new ClickHandler() {
    
                @Override
                public void onClick( com.smartgwt.client.widgets.events.ClickEvent event ) {
    
                    MySampleDlg.this.close();
                }
    
            } );
    
            // disabled until an item is selected.
            this.deleteButton.setDisabled( true );
    
            this.initializeListGrid();
    
            this.addItem( this.myListGrid );
        }
    
        private void initializeListGrid() {
            
            final List<ListGridField> fields = new ArrayList<ListGridField>();
            fields.add(new ListGridField ("Jan", "January"));
            fields.add(new ListGridField ("Feb", "February"));
            fields.add(new ListGridField ("Mar", "March"));
            fields.add(new ListGridField ("Apr", "April"));
            fields.add(new ListGridField ("May", "May"));
            fields.add(new ListGridField ("Jun", "June"));
            fields.add(new ListGridField ("Jul", "July"));
            fields.add(new ListGridField ("Aug", "August"));
            fields.add(new ListGridField ("Sep", "September"));
            fields.add(new ListGridField ("Oct", "October"));
            fields.add(new ListGridField ("Nov", "November"));
            fields.add(new ListGridField ("Dec", "December"));
    
            char maxProduct = 'E';
            List<ListGridRecord> salesData = new ArrayList<ListGridRecord>();
            for (char prod = 'A'; prod <= maxProduct; prod++) {
                ListGridRecord rec = new ListGridRecord();
                rec.setAttribute("product", "Product " + prod);
                long minSales = Math.round(Math.random() * 8000) + 2000;
                long maxVariance = minSales / 3;
                for (ListGridField field : fields) {
                    rec.setAttribute(field.getName(), Math.round(Math.random() * maxVariance) + minSales);
                }
                salesData.add(rec);
            }
    
            final ListGridField field = new ListGridField("product", "Products");
            field.setCanEdit(Boolean.FALSE);
            fields.add(0, field);
    
    
            this.myListGrid.setHeight( MySampleDlg.LISTGRID_HEIGHT );
            this.myListGrid.setFields(fields.toArray(new ListGridField[0]));
            this.myListGrid.setData(salesData.toArray(new ListGridRecord[0]));
    
            this.myListGrid.addSelectionChangedHandler( new MyListGridSelectionChangedHandler() );
    
        }
    
    
        private final class MyListGridSelectionChangedHandler implements SelectionChangedHandler {
    
            @Override
            public void onSelectionChanged( SelectionEvent event ) {
    
                ListGridRecord[] records = MySampleDlg.this.myListGrid.getSelectedRecords();
                if ( records == null || records.length == 0 ) {
                    MySampleDlg.this.deleteButton.setDisabled( true );
                } else {
                    MySampleDlg.this.deleteButton.setDisabled( false );
                }
    
            }
    
        }
    }
    In the sample, the "Delete" button is displayed to the user as a disabled button, as expected. However, when selecting an item in the ListGrid, the button is not enabled.
    When stepping through the code, I can see that the SelectionChangedHandler is invoked as expected, and the setDisabled(false) call made.


    NOTE: The enabling/disabling of this button worked with SmartGWT 5 and SmartGWT 4.1.
    For example, it works as expected with SmartClient Version: v10.0p_2015-08-28/PowerEdition Deployment (built 2015-08-28)

    #2
    When you call setToolbarButtons, toolbar buttons are created from the Button instance you pass, but the instance you pass is not valid to subsequently call APIs on: it is just used as a bunch of settings.

    To understand this in more depth, read the AutoChildren overview - the same pattern is used here. You can then access your buttons by accessing the Toolbar AutoChild, then finding them by index, title, or any other attribute you choose to assign.

    Note that this same code should not work in earlier versions either - perhaps you made other changes at the same time, or this test case isn't quite the same as the code that used to work in older versions.

    Comment


      #3
      Originally posted by Isomorphic View Post
      Note that this same code should not work in earlier versions either - perhaps you made other changes at the same time, or this test case isn't quite the same as the code that used to work in older versions.
      This same code does work in earlier versions. I just verified by running this exact sample using SmartClient Version v9.1p_2015-06-05/PowerEdition Deployment (built 2015-06-05)

      I understand from your response that it was not the intention for the earlier versions to work in this way.
      Thank you for the detailed explanation, I will take a look at the AutoChildren documentation.

      Comment


        #4
        Hmm. This may be a skin-specific thing then, as different Button classes are used in different skins by default. Either way, we'll clarify the docs to explicitly mention that the Buttons passed do not become the live buttons, and show code to retrieve the live Buttons.

        Comment

        Working...
        X