    001package org.vishia.gral.swt;
    002
    003import org.eclipse.swt.events.FocusEvent;
    004import org.eclipse.swt.events.FocusListener;
    005import org.eclipse.swt.events.MouseEvent;
    006import org.eclipse.swt.events.MouseListener;
    007import org.eclipse.swt.widgets.Text;
    008import org.eclipse.swt.widgets.Widget;
    009import org.vishia.gral.base.GralWidget;
    010import org.vishia.gral.ifc.GralUserAction;
    011
    012public class SwtFocusAction
    013implements FocusListener
    014{
    015
    016        /**Reference to the users method. */
    017  private GralUserAction userAction;
    018
    019        protected final SwtMng guiMng;
    020
    021        String sCmdEnter, sCmdRelease;
    022
    023        private final SwtProperties propertiesGui;
    024
    025  /**Constructor.
    026   * @param guiMng The Gui-manager
    027   * @param userCmdGui The users method for the action.
    028   * @param sCmdPress command string provided as first parameter on mouse button press.
    029   * @param sCmdRelease
    030   * @param sCmdDoubleClick
    031   */
    032  public SwtFocusAction(
    033        SwtMng guiMng
    034  , GralUserAction userCmdGui
    035  , String sCmdEnter
    036  , String sCmdRelease
    037  )
    038  { this.guiMng = guiMng;
    039        this.propertiesGui = guiMng.propertiesGuiSwt;
    040        this.sCmdEnter = sCmdEnter;
    041    this.sCmdRelease = sCmdRelease;
    042    this.userAction = userCmdGui;
    043  }
    044
    045
    046  void setUserAction(GralUserAction userAction)
    047  {
    048        this.userAction = userAction;
    049  }
    050
    051
    052
    053
    054
    055        @Override public void focusGained(FocusEvent ev)
    056        { if(sCmdEnter !=null) { exec(ev, sCmdEnter); }
    057        }
    058
    059        @Override public void focusLost(FocusEvent ev)
    060        { if(sCmdRelease !=null) { exec(ev, sCmdRelease); }
    061        }
    062
    063
    064        private void exec(FocusEvent ev, String sCmd)
    065        {
    066                final Widget widget = ev.widget;
    067                final Object oInfo = widget.getData();
    068                final GralWidget widgetInfo;
    069                final String sContent;
    070                if(widget instanceof Text){ sContent = ((Text)widget).getText(); }
    071                else { sContent = null; }
    072                if(oInfo instanceof GralWidget){
    073                        widgetInfo = (GralWidget)oInfo;
    074                } else { widgetInfo = null; }
    075        userAction.userActionGui(sCmd, widgetInfo, sContent);
    076        }
    077
    078
    079}