    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.SWT;
    004import org.eclipse.swt.events.TraverseEvent;
    005import org.eclipse.swt.events.TraverseListener;
    006import org.eclipse.swt.widgets.Composite;
    007import org.eclipse.swt.widgets.Control;
    008
    009/**This is a commonly use-able tranverse listener for all widgets in SWT.
    010 * Note that the system's (Windows-, Linux-) traverse mechanism is executed without this listener
    011 * with that operation systems standards.
    012 * @author Hartmut Schorrig
    013 *
    014 */
    015public class SwtTraverseListener implements TraverseListener
    016{
    017
    018  @Override public void keyTraversed(TraverseEvent e)
    019  {
    020    int key = e.keyCode;
    021    int traverse = e.detail;
    022    if(traverse == SWT.TRAVERSE_RETURN) return;
    023    Control widgs = (Control)e.widget;
    024    Composite panel = widgs.getParent();
    025    Control[] allwidg = panel.getChildren();
    026    for(int ix = 0; ix < allwidg.length; ++ix){
    027      if(allwidg[ix] == widgs){
    028        if(traverse == SWT.TRAVERSE_TAB_NEXT || traverse == SWT.TRAVERSE_ARROW_NEXT) {
    029          ix+=1; if(ix >= allwidg.length){ ix = 0; }
    030        } else if(traverse == SWT.TRAVERSE_TAB_PREVIOUS || traverse == SWT.TRAVERSE_ARROW_PREVIOUS) {
    031          ix-=1; if(ix <0){ ix =  allwidg.length-1; }
    032        }
    033        else return;  //other keys: do nothing.
    034        Control nextwidg = allwidg[ix];
    035        nextwidg.setFocus();
    036        break;
    037      }
    038    }
    039
    040  }
    041
    042}
    043
    044