2013-10-29 74 views
2

在我的項目中,我使用gwt 2.5.1gxt 3.0.1。 我創建樹。更改節點樣式

protected void createTree(TreeStore<ProjectExtended> treeStore) { 

    ValueProvider<ProjectExtended, SafeHtml> provider = new ValueProvider<ProjectExtended, SafeHtml>() { 

     @Override 
     public void setValue(ProjectExtended object, SafeHtml value) { 

     } 

     @Override 
     public SafeHtml getValue(ProjectExtended object) { 

      if(object == null) return SafeHtmlUtils.EMPTY_SAFE_HTML; 

      if (!object.getActive()) { 
       return SafeHtmlUtils.fromTrustedString(
         "<span class=\"projectNotActive\">" + object.getName() + 
         "</span> " + "<span class=\"dictIdText\"> (" + 
         object.getId() + "/" + object.getParentId() + ") </span>"); 
      } else { 
       return SafeHtmlUtils.fromTrustedString(
         object.getName() + "<span class=\"dictIdText\"> (" + 
         object.getId() + "/" + object.getParentId() + ") </span>"); 
      } 
     } 

     @Override 
     public String getPath() { 
      return "name"; 
     } 
    }; 

    Tree<ProjectExtended, SafeHtml> tree = new Tree<ProjectExtended, SafeHtml>(treeStore, provider); 
    tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); 

    add(tree); 
} 

我嘗試改變未激活的節點的樣式。但我無法解決這個問題,因爲瀏覽器顯示它們錯了!

http://i.stack.imgur.com/0Jmdq.png

是有沒有辦法解決此問題的方法?

回答

2

我解決了這個問題。幫助的文章是here

有一個簡單的代碼示例。

  1. 您應該創建一個繼承的類AbstractCell和重寫Render方法:

    public class SimpleAbstractCell extends AbstractCell<String> { 
    
        @Override 
        public void render(Context context, String value, SafeHtmlBuilder sb) { 
         /* 
         * Always do a null check on the value. Cell widgets can pass null to 
         * cells if the underlying data contains a null, or if the data arrives 
         * out of order. 
         */ 
         if (value == null) { 
          return; 
         } 
    
         SafeHtml safeValue = SafeHtmlUtils.fromTrustedString(value); 
         sb.append(safeValue); 
        } 
    } 
    
  2. 在創建過程中一棵樹,你應該申報contentCell:

    protected void createTree(TreeStore<ProjectExtended> treeStore) { 
    
        ValueProvider<ProjectExtended, String> provider = new ValueProvider<ProjectExtended, String>() { 
    
         @Override 
         public void setValue(ProjectExtended object, String value) { 
    
         } 
    
         @Override 
         public String getValue(ProjectExtended object) { 
    
          if(object == null) return ""; 
    
          if (!object.getActive()) { 
           return 
            "<span class=\"projectNotActive\">" + object.getName() + 
            "</span> " + "<span class=\"dictIdText\"> (" + 
            object.getId() + "/" + object.getParentId() + ") </span>"; 
          } else { 
           return 
            object.getName() + "<span class=\"dictIdText\"> (" + 
            object.getId() + "/" + object.getParentId() + ") </span>"; 
          } 
         } 
    
         @Override 
         public String getPath() { 
          return "name"; 
         } 
        }; 
    
        Tree<ProjectExtended, String> tree = new Tree<ProjectExtended, String>(treeStore, provider); 
        tree.setCell(new SimpleAbstractCell()); 
        tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); 
    
        add(tree); 
    } 
    

這就是全部。我希望它對smb有用。