    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.events.KeyEvent;
    004import org.eclipse.swt.events.KeyListener;
    005import org.eclipse.swt.widgets.Control;
    006import org.vishia.gral.base.GralKeyListener;
    007import org.vishia.gral.base.GralKeySpecial_ifc;
    008import org.vishia.gral.base.GralTextField;
    009import org.vishia.gral.base.GralWidget;
    010import org.vishia.gral.ifc.GralUserAction;
    011import org.vishia.gral.ifc.GralWidget_ifc;
    012
    013/**A common key listener implementation for SWT. It is applied to all widgets.
    014 * Derived forms exists for special SWT-widgets.
    015 * This class is instanciated one time in the {@link SwtMng}.
    016 * A Widget can be completed with this key listener. On all keys
    017   * the {@link GralUserAction} given in the {@link GralWidget#getActionChange()} is called
    018   * <ul>
    019   * </ul>
    020   * If the method returns false, the central key action given in {@link GralMng#getRegisteredUserAction(String)}
    021   * for "keyAction" is tried to get and then invoked with cmd = "key" and the key code in params[0].
    022   * This central keyAction may be used for application centralized keys without association to the table itself.
    023 * @see GralKeyListener
    024 *
    025 * @author Hartmut Schorrig
    026 *
    027 */
    028public class SwtKeyListener implements GralKeySpecial_ifc, KeyListener// extends GralKeyListener
    029{
    030
    031  /**Version and history
    032   * <ul>
    033   * <li>2011-12-03 Hartmut created. Any widget may have the same key listener.
    034   * </ul>
    035   *
    036   */
    037  //@SuppressWarnings("hiding")
    038  public final static int version = 20120630;
    039
    040  //final SwtMng swtMng;
    041
    042  private final GralKeyListener keyAction;
    043  //final KeyListener swtListener;
    044
    045  public SwtKeyListener(GralKeyListener keyAction)
    046  {
    047    this.keyAction = keyAction;
    048  }
    049
    050  @Override
    051  public void keyPressed(KeyEvent keyEv)
    052  {
    053    final GralWidget_ifc widgetDescr;
    054    //System.out.println("" + keyEv.character + Integer.toHexString(keyEv.keyCode));
    055
    056    final Object source = keyEv.getSource();
    057    final Control swtControl;
    058    if(source instanceof Control){
    059      swtControl = ((Control)source);
    060      Object oData = swtControl.getData();
    061      if(oData instanceof GralTextField.GraphicImplAccess){
    062        GralTextField.GraphicImplAccess widgi = (GralTextField.GraphicImplAccess) oData;
    063        widgetDescr = widgi.widgg;
    064      } else if(oData instanceof GralWidget_ifc){
    065        widgetDescr = (GralWidget_ifc)oData;
    066      } else { widgetDescr = null;  }
    067    } else {
    068      widgetDescr = null;
    069      swtControl = null;
    070    }
    071    if((keyEv.keyCode & 0xffff) !=0){
    072      final int keyCode = SwtGralKey.convertFromSwt(keyEv.keyCode, keyEv.stateMask, keyEv.character);
    073      if(! specialKeysOfWidgetType(keyCode, widgetDescr, swtControl)){
    074        keyAction.keyPressed(keyCode, widgetDescr, swtControl);
    075      }
    076    }
    077    if(swtControl !=null){
    078      Control parent = swtControl.getParent();
    079      if(parent !=null){
    080        //KeyListener parentListener = parent.getListener(SWT.KEY_MASK);
    081        //parent.
    082      }
    083    }
    084  }
    085
    086  @Override
    087  public void keyReleased(KeyEvent arg0)
    088  {
    089    //basicListener.keyReleased(arg0);
    090
    091  }
    092
    093  @Override public boolean specialKeysOfWidgetType(int key, GralWidget_ifc widgg, Object widgImpl){ return false; }
    094
    095
    096}