2011-07-21 61 views

回答

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; 

     } 
    });  
} 

}

+0

我不知道這是去工作,因爲CellWidgets通常不會呈現經典的GWT部件(即TextField或Button) –

2

你可以創建自己的鈕釦電池類。例如:

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(); 
+0

謝謝,很棒的工作。很高興在一年後找到你回答的問題,看到答案不好,並採取更好的答案。 只做了一個小小的改變:juse「HasEnabled」接口並將您的方法命名爲「isEnabled」和「setEnabled」,而不是「isDisabled」和「setDisabled」。 – Stefan

+0

https://github.com/pressgang-ccms/PressGangCCMSUI/blob/master/src/main/java/com/google/gwt/user/client/ui/DisableableButtonCell.java對我來說是一個小小的變化在我自己的項目中。 – Phyxx

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); 
    } 
} 

,並通過其「獲取價值」 mechanisme編輯

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

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

      });