禁用单元格中的ButtonCell

时间:2011-07-21 04:31:00

标签: gwt

在我的CellTable中,One列有ButtonCells。因此,我需要通过单击位于CellTable外部的按钮来禁用这些ButtonCell。

3 个答案:

答案 0 :(得分:3)

您可以创建自己的Button Cell类。例如:

import com.google.gwt.cell.client.AbstractSafeHtmlCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;

/**
 * A {@link Cell} used to render a button.
 */
public class StyledButtonCell extends AbstractSafeHtmlCell<String> {

private String disabledString = "";

private boolean disabled = false;

/**
 * Construct a new ButtonCell that will use a {@link SimpleSafeHtmlRenderer}.
 */
public StyledButtonCell() {
    this(SimpleSafeHtmlRenderer.getInstance());
}

/**
 * Construct a new ButtonCell that will use a given {@link SafeHtmlRenderer}.
 * 
 * @param renderer a {@link SafeHtmlRenderer SafeHtmlRenderer<String>} instance
 */
public StyledButtonCell(SafeHtmlRenderer<String> renderer) {
    super(renderer, "click", "keydown");
}

@Override
public void onBrowserEvent(Context context, Element parent, String value,
        NativeEvent event, ValueUpdater<String> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    if ("click".equals(event.getType())) {
        EventTarget eventTarget = event.getEventTarget();
        if (!Element.is(eventTarget)) {
            return;
        }
        if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
            // Ignore clicks that occur outside of the main element.
            onEnterKeyDown(context, parent, value, event, valueUpdater);
        }
    }
}

@Override
public void render(Context context, SafeHtml data, SafeHtmlBuilder sb) {
    sb.appendHtmlConstant("<button type=\"button\" tabindex=\"-1\"" + disabledString + ">");
    if (data != null) {
        sb.append(data);
    }
    sb.appendHtmlConstant("</button>");
}

@Override
protected void onEnterKeyDown(Context context, Element parent, String value,
        NativeEvent event, ValueUpdater<String> valueUpdater) {
    if (valueUpdater != null) {
        valueUpdater.update(value);
    }
}

public boolean isDisabled() {
    return disabled;
}

public void setDisabled(boolean disabled) {
    this.disabled = disabled;
    if (disabled) {

        disabledString = "disabled=\"disabled\"";
    } else {
        disabledString = "";
    }
}
}

然后将其与您的手表一起使用:

final StyledButtonCell buttonCell = new StyledButtonCell();

        buttonColumn = new Column<SomeItem, String>(buttonCell) {

          public String getValue(SomeItem object) {
            // The value to display in the button.
            return "Go";
          }
        };

要禁用该按钮,只需致电:

buttonCell.setDisabled(true);
table.redraw();

答案 1 :(得分:1)

如果您不需要“重命名”按钮,您也可以根据布尔抽象单元格创建自己的按钮:

public class CustomButtonCell extends AbstractEditableCell<Boolean, Boolean> {

    private static final SafeHtml ENABLED = SafeHtmlUtils.fromSafeConstant("<button type=\"button\" tabindex=\"-1\">");
    private static final SafeHtml DISABLED = SafeHtmlUtils.fromSafeConstant("<button type=\"button\" tabindex=\"-1\" disabled=\"disabled\">");
    private static final SafeHtml CLOSE_BRACKET = SafeHtmlUtils.fromSafeConstant("</button>");

    private final boolean dependsOnSelection;
    private final boolean handlesSelection;
    private final String text;

    public CustomButtonCell(final String text) {
        this(false, false, text);
    }

    public CustomButtonCell(boolean dependsOnSelection, boolean handlesSelection, String text) {
        super(BrowserEvents.CHANGE, BrowserEvents.CLICK, BrowserEvents.KEYDOWN);
        this.dependsOnSelection = dependsOnSelection;
        this.handlesSelection = handlesSelection;
        this.text = text;
    }

    @Override
    public boolean dependsOnSelection() {
        return dependsOnSelection;
    }

    @Override
    public boolean handlesSelection() {
        return handlesSelection;
    }

    @Override
    public boolean isEditing(Context context, Element parent, Boolean value) {
        return false;
    }

    /**
     * Based on CheckboxCell
     */
    @Override
    public void onBrowserEvent(Context context, Element parent, Boolean value,
            NativeEvent event, ValueUpdater<Boolean> valueUpdater) {
        String type = event.getType();

        boolean enterPressed = BrowserEvents.KEYDOWN.equals(type)
                && event.getKeyCode() == KeyCodes.KEY_ENTER;
        if (BrowserEvents.CHANGE.equals(type) || BrowserEvents.CLICK.equals(type) || enterPressed) {
            InputElement input = parent.getFirstChild().cast();
            Boolean isChecked = input.isChecked();

            if (enterPressed && (handlesSelection() || !dependsOnSelection())) {
                isChecked = !isChecked;
                input.setChecked(isChecked);
            }

            if (value != isChecked && !dependsOnSelection()) {
                setViewData(context.getKey(), isChecked);
            } else {
                clearViewData(context.getKey());
            }

            if (valueUpdater != null) {
                valueUpdater.update(isChecked);
            }
        }
    }

    @Override
    public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
        // Get the view data.
        Object key = context.getKey();
        Boolean viewData = getViewData(key);
        if (viewData != null && viewData.equals(value)) {
            clearViewData(key);
            viewData = null;
        }

        if (value != null && ((viewData != null) ? viewData : value)) {
            sb.append(ENABLED);
        } else {
            sb.append(DISABLED);
        }
        sb.append(SafeHtmlUtils.fromString(text));
        sb.append(CLOSE_BRACKET);
    }
}

并通过“get value”mechanisme

进行编辑
    Column<TestJSO, Boolean> revokeColumn = CellTableUtils.createColumn(new CustomButtonCell("Test"),
            new GetValue<TestJSO, Boolean>() {

                @Override
                public Boolean getValue(TestJSO value) {
                    return value.isEnabled();
                }

            });

答案 2 :(得分:0)

早上好,

这是一个很好的小例子。在这里你有我的解决方案,我不是说,它是最好的,但它的工作!

public class _57_DisableAllButtons implements EntryPoint {

boolean enable = false;

@Override
public void onModuleLoad() {
    Button b1 = new Button("Button1");
    Button b2 = new Button("Button2");
    Button b3 = new Button("Button3");


    RootPanel.get("nameFieldContainer").add(b1);
    RootPanel.get("nameFieldContainer").add(b2);
    RootPanel.get("nameFieldContainer").add(b3);

    Button disableAll = new Button("Disabale all");     

    RootPanel.get("sendButtonContainer").add(disableAll);

    disableAll.addClickHandler(new ClickHandler() {         
        @Override
        public void onClick(ClickEvent event) {
            final RootPanel rootpannel = RootPanel.get("nameFieldContainer");

            int widgetcount = rootpannel.getWidgetCount();
            for (int i = 0; i < widgetcount; i++) {
                Widget w = rootpannel.getWidget(i);
                if(w instanceof Button){
                    ((Button) w).setEnabled(enable);
                }
            }
            enable = !enable;

        }
    });     
}

}