2012-12-05 45 views
5

क्या एसडब्ल्यूटी-विजेट्स पर स्वचालित रूप से आईडी उत्पन्न करने का कोई तरीका है ताकि UI-Test उन्हें संदर्भित कर सकें? मुझे पता है कि मैं seData का उपयोग कर मैन्युअल रूप से एक आईडी सेट कर सकता हूं लेकिन मैं इस सुविधा को किसी मौजूदा एप्लिकेशन के लिए कुछ हद तक सामान्य रूप से कार्यान्वित करना चाहता हूं।स्वचालित रूप से एसडब्ल्यूटी-विजेट्स पर आईडी जेनरेट करें

उत्तर

8

आप Display.getCurrent().getShells(); और Widget.setData(); का उपयोग करके अपने आवेदन में अपने सभी गोले के लिए आईडी आवंटित कर सकते हैं।

आईडी

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

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

आप विधि Display.getCurrent().getShells(); के साथ अपने आवेदन में सभी सक्रिय (निपटारा नहीं) शेल की पहुंच है की स्थापना। आप प्रत्येक Shell के सभी बच्चों के माध्यम से लूप कर सकते हैं और विधि Widget.setData(); विधि के साथ प्रत्येक Control पर एक आईडी असाइन कर सकते हैं।

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

तो Control एक Composite यह समग्र अंदर नियंत्रण हो सकता है, यही कारण है कि मैं अपने उदाहरण में एक पुनरावर्ती समाधान का इस्तेमाल किया है है।


आईडी

द्वारा नियंत्रण ढूँढना अब, अगर आप एक नियंत्रण अपने गोले में से एक में मैं एक ऐसी ही, पुनरावर्ती, दृष्टिकोण सुझाव है कि खोजने के लिए चाहते:

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; 
} 
साथ

विधि findControlById() आप आसानी से Control अपने आईडी द्वारा पा सकते हैं।

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

लिंक