0
votes

I have the (working) code below that initializes a GXT 3 Grid to show 2 date columns (completed and due) and a boolean column (applicable). It is configured for inline editing AND handles a 'Completed Edit' event by checking for certain conditions and acting on other columns based on those conditions. All good stuff.

I just have not been able to figure out how to do the following: Disable the date fields if the boolean field is false (and vice versa). I need to do this both on initialization of the grid with data and on user interaction with the boolean field.

Some of the missing code is place holder'ed by the comment below "HOW DO YOU DO THIS?" .

private void initializeColumnModel() {

    // Create the configurations for each column in the grid
    List<ColumnConfig<Reminder, ?>> ccs = new LinkedList<ColumnConfig<Reminder, ?>>();
    typeColumnConfig        = new ColumnConfig<Reminder,String>(    properties.name(),          120,    ColumnDescriptor.TYPE.getName()         );
    completedColumnConfig   = getDateCellColumn(                    properties.completed_(),    200,    ColumnDescriptor.COMPLETED.getName()    ); 
    dueColumnConfig         = getDateCellColumn(                    properties.due_(),          200,    ColumnDescriptor.DUE.getName()          ); 
    applicableColumnConfig  = new ColumnConfig<Reminder,Boolean>(   properties.applicable(),    140,    ColumnDescriptor.APPLICABLE.getName()   );

    // Add column configurations to ColumnModel in specified order.
    ccs.add(ColumnDescriptor.TYPE.ordinal(),        typeColumnConfig);
    ccs.add(ColumnDescriptor.COMPLETED.ordinal(),   completedColumnConfig);
    ccs.add(ColumnDescriptor.DUE.ordinal(),         dueColumnConfig);
    ccs.add(ColumnDescriptor.APPLICABLE.ordinal(),  applicableColumnConfig);

    applicableColumnConfig.setAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    typeColumnConfig.setAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    reminderColumnModel = new ColumnModel<Reminder>(ccs);

}   

private ColumnConfig<Reminder,Date> getDateCellColumn( 
        ValueProvider<Reminder,Date> colDateProvider, 
        int width, 
        String name) {
    SafeStyles fieldPaddingStyle = SafeStylesUtils.fromTrustedString("padding: 2px 3px;");
    ColumnConfig<Reminder, Date> dateColumn = new ColumnConfig<Reminder, Date>(colDateProvider, width, name);
    dateColumn.setColumnTextStyle(fieldPaddingStyle);
    return dateColumn;
}

private DateField getDateField(){
    DateField dateField = new DateField(new DateTimePropertyEditor(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));
    dateField.setClearValueOnParseError(false);
    return dateField;
}

@Override
public Widget asWidget() {
    if(!gridInitialized){

        editing = new GridInlineEditing<Reminder>(grid);

        DateField dueDateField          = getDateField();
        DateField completedDateField    = getDateField();
        CheckBox applicBox              =  new CheckBox();

        editing.addEditor(dueColumnConfig,          dueDateField);
        editing.addEditor(completedColumnConfig,    completedDateField);
        editing.addEditor(applicableColumnConfig,   applicBox);

        editing.addCompleteEditHandler(new CompleteEditHandler<Reminder>(){
            @Override
            public void onCompleteEdit(CompleteEditEvent<Reminder> event) {
                GridCell cell = event.getEditCell();

                int row = cell.getRow();
                int col = cell.getCol();
                Reminder rem = reminderStore.get(row);
                Store<Reminder>.Record rec = reminderStore.getRecord(rem);
                Change<Reminder, Boolean> applicChange  = rec.getChange(properties.applicable());
                Change<Reminder, Date>      dueChange   = rec.getChange(properties.due_());
                Change<Reminder, Date>      comChange   = rec.getChange(properties.completed_());

                System.err.println("Row "+(row+1)+" changed: ");
                if(col==ColumnDescriptor.APPLICABLE.ordinal()){
                    if(applicChange!=null){ 
                        boolean applicValue = applicChange.getValue();
                        enableDateFieldsForRow(applicValue, row);
                        System.out.println(" applicable changed to "+applicValue);
                    }
                    else{
                        enableDateFieldsForRow(rem.getApplicable(), row);
                        System.out.println(" applicable reverted to "+rem.getApplicable());
                    }
                }
                if(col==ColumnDescriptor.DUE.ordinal()){
                    if(dueChange!=null ){
                        Date dueValue = dueChange.getValue();
                        System.out.println(" due changed to: "+SimpleDate.convertFromDate(dueValue));
                    }
                    else
                        System.out.println(" due reverted to "+rem.getDue());
                }
                if(col==ColumnDescriptor.COMPLETED.ordinal()){
                    if(comChange!=null ){
                        Date comValue = comChange.getValue();
                        System.out.println(" com changed to: "+SimpleDate.convertFromDate(comValue));
                        try{
                            fixDueDate(row,comValue, rem, rec);
                        }
                        catch(Exception e){
                            //boo.
                            System.err.println("Could not update the due date for this completion date change.");
                        }
                    }
                    else
                        System.out.println(" due reverted to "+rem.getCompleted());

                }
            }
        });
        gridInitialized=true;
    }
    // Initialize the Revert Changes button.
    revert.addSelectHandler(new SelectHandler(){
        @Override
        public void onSelect(SelectEvent event) {
            reminderStore.rejectChanges();
        }
    });

    // ok, now, return the widget!
    return widget;
}

// Enable/disable date fields based on whether they are applicable (applicValue).
private void enableDateFieldsForRow(boolean applicValue, int row){
    // ** HOW DO YOU DO THIS? ***
}
private void fixDueDate(int row, Date completedDate, Reminder rem, Store<Reminder>.Record rec ) 
        throws InvalidDateFormatException, InvalidDateException{
    SimpleDate newCompDate = new SimpleDate(completedDate);
    SimpleDate dueDate = newCompDate.addYears(rem.getRenewalYears());

    rec.addChange(dueColumnConfig.getValueProvider(), dueDate.toDate());
}

public List<Reminder> getModifications(){
    System.out.println("grid modifications:");
    List<Reminder> modified = new ArrayList<Reminder>();
    for(Store<Reminder>.Record record : reminderStore.getModifiedRecords()){
        Reminder model = record.getModel();
        Reminder clone = model.clone();
        for(Change<Reminder,?> change : record.getChanges()){
            change.modify(clone);
        }
        modified.add(clone);
    }
    return modified;
}

public List<Reminder> commitChanges(){
    reminderStore.commitChanges();
    return reminderStore.getAll();
}

public void updateList(List<Reminder> list){
    reminderStore.clear();

    for(Reminder r : list){
        reminderStore.add(r);
    }

    // disable selection of rows.
    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    //grid.disableEvents();
}

Thanks.

1
completeEditHanlder do the work for meVivek Vardhan

1 Answers

0
votes

I am also faced the same issue to disable and enable the cell in Grid.But I am using EditorGrid of GXT 2.2.3.After trying so much,I got one solution,instead thinking of disabling and enabling the cell, I just hide and show the cell using CSS. Below is my code which saves me to reach this requirement.

GridCellRenderer<AttendanceCaseCreationModel> checkinRenderer=new GridCellRenderer<AttendanceCaseCreationModel>() {

        @Override
        public Object render(AttendanceCaseCreationModel model, String property,
                ColumnData config, int rowIndex, int colIndex,
                ListStore<AttendanceCaseCreationModel> store,
                Grid<AttendanceCaseCreationModel> grid) {

            String color="pink";
            if(eventcombo.getValue()!=null){


                if(eventcombo.getRawValue().equalsIgnoreCase("Forgot To Checkin") || 
                        eventcombo.getRawValue().equalsIgnoreCase("Mark/Modify Attendance")){
                    color="pink";
                }
                else{

                    config.style=config.style+ ";visibility: hidden;";
                }

            }

            config.style=config.style+ ";background-color:" + color  + ";";
            config.style=config.style+ ";display: block;";
            Object value = model.get(property);
            return value;

        }
    };