2012-12-05 18 views
5

SWT-Widget'larda otomatik olarak kimlik oluşturmanın bir yolu var mı? Böylece UI-Testleri bunlara referans verebilir? SeData'yi kullanarak bir kimliği manuel olarak ayarlayabildiğimi biliyorum ancak bu özelliği mevcut bir uygulama için biraz genel bir şekilde uygulamak istiyorum.SWT-Widgets'ta otomatik olarak kimlikler oluşturma

cevap

8

Display.getCurrent().getShells(); ve Widget.setData(); öğelerini kullanarak uygulamanızda tüm kabuklarınız için kimlikleri yinelemeli olarak atayabilirsiniz. Kimlikleri

Shell []shells = Display.getCurrent().getShells(); 

for(Shell obj : shells) { 
    setIds(obj); 
} 

Sen yöntemle Display.getCurrent().getShells(); ile uygulamadaki tüm aktif (atılmazsa) Kabuklar erişebilir Ayar

. Her bir Shell'un tüm alt öğeleri arasında geçiş yapabilir ve her Control yöntemine Widget.setData(); yöntemiyle bir kimlik atayabilirsiniz. Control o kompozit iç kontrollere sahip olabilen bir Composite ise

private Integer count = 0; 

private void setIds(Composite c) { 
    Control[] children = c.getChildren(); 
    for(int j = 0 ; j < children.length; j++) { 
     if(children[j] instanceof Composite) { 
      setIds((Composite) children[j]); 
     } else { 
      children[j].setData(count); 
      System.out.println(children[j].toString()); 
      System.out.println(" '-> ID: " + children[j].getData()); 
      ++count; 
     } 
    } 
} 

, bu benim örnekte bir özyinelemeli çözümü kullanmış sebebidir. Ile

public Control findControlById(Integer id) { 
    Shell[] shells = Display.getCurrent().getShells(); 

    for(Shell e : shells) { 
     Control foundControl = findControl(e, id); 
     if(foundControl != null) { 
      return foundControl; 
     } 
    } 
    return null; 
} 

private Control findControl(Composite c, Integer id) { 
    Control[] children = c.getChildren(); 
    for(Control e : children) { 
     if(e instanceof Composite) { 
      Control found = findControl((Composite) e, id); 
      if(found != null) { 
       return found; 
      } 
     } else { 
      int value = id.intValue(); 
      int objValue = ((Integer)e.getData()).intValue(); 

      if(value == objValue) 
       return e; 
     } 
    } 
    return null; 
} 

: Eğer ben benzer, özyinelemeli, yaklaşım öneririm senin kabukları birinde bir Kontrolü bulmak isterseniz, Şimdi kimliği

tarafından Kontroller bulma


yöntem findControlById() kolayca onun tarafından bir Control bulabilirsiniz.

Control foundControl = findControlById(12); 
    System.out.println(foundControl.toString()); 

Linkler

İlgili konular