2012-12-05 10 views

Odpowiedz

8

Możesz rekurencyjnie przypisać identyfikatory do wszystkich swoich powłok w aplikacji przy użyciu Display.getCurrent().getShells(); i Widget.setData();.

Ustawianie identyfikatorów

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

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

mieć dostęp do wszystkich aktywnych (nie) do zbiorników umieszczonych w aplikacji z metodą Display.getCurrent().getShells();. Możesz przechodzić przez wszystkie dzieci każdego z numerów Shell i przypisać identyfikator każdemu Control za pomocą metody Widget.setData();.

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

Jeśli Control jest Composite może mieć kontrole wewnątrz kompozytu, to jest powód Użyłem rekurencyjną rozwiązanie w moim przykładzie.


Znalezienie Sterowanie przez ID

Teraz, jeśli chcesz znaleźć kontrola w jednym ze swoich skorup Proponuję podobną, rekurencyjną, podejście:

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

Z metoda findControlById() można łatwo znaleźć Control według jej identyfikatora.

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

Linki

Powiązane problemy