Difference between revisions of "Documentation/DevGuide/GUI/Events"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
 
Line 8: Line 8:
 
  {{DISPLAYTITLE:Events}}
 
  {{DISPLAYTITLE:Events}}
 
__NOTOC__
 
__NOTOC__
{{PRODUCTNAME}} dialogs are based on an event-oriented programming model where you can assign event handlers to the control elements. An event handler runs a predefined procedure when a particular action occurs. Event handlers are always added directly to the control (not to the control models). All dialog controls implement the interface <idl>com.sun.star.awt.XControl</idl> which extends the interface <idl>com.sun.star.awt.XWindow</idl>. Listeners are added to a control with a specific <code>add<ListenerName>Listener()</code> method like <code>addMouseListener( [in] XMouseListener xListener )</code>. Listeners are removed with a specific <code>remove<ListenerName>Listener()</code> method like <code>removeMouseListener( [in] XMouseListener xListener )</code>.  
+
{{AOo}} dialogs are based on an event-oriented programming model where you can assign event handlers to the control elements. An event handler runs a predefined procedure when a particular action occurs. Event handlers are always added directly to the control (not to the control models). All dialog controls implement the interface <idl>com.sun.star.awt.XControl</idl> which extends the interface <idl>com.sun.star.awt.XWindow</idl>. Listeners are added to a control with a specific <code>add<ListenerName>Listener()</code> method like <code>addMouseListener( [in] XMouseListener xListener )</code>. Listeners are removed with a specific <code>remove<ListenerName>Listener()</code> method like <code>removeMouseListener( [in] XMouseListener xListener )</code>.  
  
 
The methods of all listener interfaces have a parameter of a type derived from <idl>com.sun.star.lang.EventObject</idl>, for example <idl>com.sun.star.awt.MouseEvent</idl>, <idl>com.sun.star.awt.FocusEvent</idl> etc. This event object always carries a property <code>Source</code> by which it is possible to query the control an event has been triggered at.  
 
The methods of all listener interfaces have a parameter of a type derived from <idl>com.sun.star.lang.EventObject</idl>, for example <idl>com.sun.star.awt.MouseEvent</idl>, <idl>com.sun.star.awt.FocusEvent</idl> etc. This event object always carries a property <code>Source</code> by which it is possible to query the control an event has been triggered at.  
  
 
The following code example shows how to implement an <code>XActionListener</code>. You must remember to implement the <code>disposing()</code> method as dictated by <idl>com.sun.star.lang.XEventListener</idl>. <code>disposing()</code> is supposed to be triggered when a <code>dispose()</code> command at the control has been invoked.
 
The following code example shows how to implement an <code>XActionListener</code>. You must remember to implement the <code>disposing()</code> method as dictated by <idl>com.sun.star.lang.XEventListener</idl>. <code>disposing()</code> is supposed to be triggered when a <code>dispose()</code> command at the control has been invoked.
<source lang="java">
+
<syntaxhighlight lang="java">
 
   public void actionPerformed(ActionEvent rEvent){
 
   public void actionPerformed(ActionEvent rEvent){
 
   try{
 
   try{
Line 35: Line 35:
 
       ex.printStackTrace(System.out);
 
       ex.printStackTrace(System.out);
 
   }}
 
   }}
</source>
+
</syntaxhighlight>
 
=== Mouse Listeners ===
 
=== Mouse Listeners ===
  
Events that correspond to mouse actions are triggered by a <idl>com.sun.star.awt.XMouseListener</idl> that react to mouse movements over a control. Popular use-cases for a mouse listener are changing the mouse pointer when the mouse moves over the window or querying the click count of the event <code>mousePressed([in] com.sun.star.awt.MouseEvent e)</code> when you want to differentiate between a single-click and a double-click. For this purpose all methods carry a parameter <idls>com.sun.star.awt.MenuEvent</idls>, a structure that contains amongst other things, the member <code>ClickCount</code>. Other members (<code>PositionX</code> and <code>PositionY</code>) are to query the mouse position during the event invocation and <code>Buttons</code> that refers to the pressed mouse buttons.
+
Events that correspond to mouse actions are triggered by a <idl>com.sun.star.awt.XMouseListener</idl> that react to mouse movements over a control. Popular use-cases for a mouse listener are changing the mouse pointer when the mouse moves over the window or querying the click count of the event <code>mousePressed([in] com.sun.star.awt.MouseEvent e)</code> when you want to differentiate between a single-click and a double click. For this purpose all methods carry a parameter <idls>com.sun.star.awt.MenuEvent</idls>, a structure that contains amongst other things, the member <code>ClickCount</code>. Other members (<code>PositionX</code> and <code>PositionY</code>) are to query the mouse position during the event invocation and <code>Buttons</code> that refers to the pressed mouse buttons.
  
 
A <code>MouseMotionListener</code> that implements <idl>com.sun.star.awt.XMouseMotionListener</idl> can be used when a movement of the mouse pointer must be observed. The following example code shows a part of an implementation of a mouse motion listener that is executed when the mouse is entering a control. For further information about <code>WindowPeers</code>, see [[Documentation/DevGuide/GUI/Displaying Dialogs|Displaying Dialogs]].
 
A <code>MouseMotionListener</code> that implements <idl>com.sun.star.awt.XMouseMotionListener</idl> can be used when a movement of the mouse pointer must be observed. The following example code shows a part of an implementation of a mouse motion listener that is executed when the mouse is entering a control. For further information about <code>WindowPeers</code>, see [[Documentation/DevGuide/GUI/Displaying Dialogs|Displaying Dialogs]].
<source lang="java">
+
<syntaxhighlight lang="java">
 
   public void mouseEntered(MouseEvent _mouseEvent) {
 
   public void mouseEntered(MouseEvent _mouseEvent) {
 
       try {
 
       try {
Line 60: Line 60:
 
           throw new java.lang.RuntimeException("cannot happen...");
 
           throw new java.lang.RuntimeException("cannot happen...");
 
       }}
 
       }}
</source>
+
</syntaxhighlight>
 
=== Keyboard Listener ===
 
=== Keyboard Listener ===
  
 
Keyboard events can be captured by a <code>KeyListener</code> that implements <idl>com.sun.star.awt.XKeyListener</idl>. This allows you to verify each keyboard stroke. This listener is very useful for edit controls. The interface dictates the implementation of the two methods <code>keyPressed()</code> and <code>keyReleased()</code>.
 
Keyboard events can be captured by a <code>KeyListener</code> that implements <idl>com.sun.star.awt.XKeyListener</idl>. This allows you to verify each keyboard stroke. This listener is very useful for edit controls. The interface dictates the implementation of the two methods <code>keyPressed()</code> and <code>keyReleased()</code>.
<source lang="java">
+
<syntaxhighlight lang="java">
 
   public void keyReleased(KeyEvent keyEvent) {
 
   public void keyReleased(KeyEvent keyEvent) {
 
       int i = keyEvent.KeyChar;
 
       int i = keyEvent.KeyChar;
Line 70: Line 70:
 
       int m = keyEvent.KeyFunc;
 
       int m = keyEvent.KeyFunc;
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
=== Focus Listener ===
 
=== Focus Listener ===
  
Line 78: Line 78:
  
 
This example demonstrates how to use the <code>focusEvent</code>:
 
This example demonstrates how to use the <code>focusEvent</code>:
<source lang="java">
+
<syntaxhighlight lang="java">
 
   public void focusLost(FocusEvent _focusEvent) {
 
   public void focusLost(FocusEvent _focusEvent) {
 
       short nFocusFlags = _focusEvent.FocusFlags;
 
       short nFocusFlags = _focusEvent.FocusFlags;
Line 89: Line 89:
 
       }
 
       }
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
=== Paint Listener ===
 
=== Paint Listener ===
  

Latest revision as of 18:48, 21 December 2020



Apache OpenOffice dialogs are based on an event-oriented programming model where you can assign event handlers to the control elements. An event handler runs a predefined procedure when a particular action occurs. Event handlers are always added directly to the control (not to the control models). All dialog controls implement the interface com.sun.star.awt.XControl which extends the interface com.sun.star.awt.XWindow. Listeners are added to a control with a specific add<ListenerName>Listener() method like addMouseListener( [in] XMouseListener xListener ). Listeners are removed with a specific remove<ListenerName>Listener() method like removeMouseListener( [in] XMouseListener xListener ).

The methods of all listener interfaces have a parameter of a type derived from com.sun.star.lang.EventObject, for example com.sun.star.awt.MouseEvent, com.sun.star.awt.FocusEvent etc. This event object always carries a property Source by which it is possible to query the control an event has been triggered at.

The following code example shows how to implement an XActionListener. You must remember to implement the disposing() method as dictated by com.sun.star.lang.XEventListener. disposing() is supposed to be triggered when a dispose() command at the control has been invoked.

  public void actionPerformed(ActionEvent rEvent){
  try{
      // get the control that has fired the event, 
      XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, rEvent.Source);
      XControlModel xControlModel = xControl.getModel();
      XPropertySet xPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
      String sName = (String) xPSet.getPropertyValue("Name");
      // just in case the listener has been added to several controls,
      // we make sure we refer to the right one
      if (sName.equals("CommandButton1")){
          //...
      }
  }catch (com.sun.star.uno.Exception ex){
      /* perform individual exception handling here.
       * Possible exception types are:
       * com.sun.star.lang.WrappedTargetException,
       * com.sun.star.beans.UnknownPropertyException,
       * com.sun.star.uno.Exception
       */
      ex.printStackTrace(System.out);
  }}

Mouse Listeners

Events that correspond to mouse actions are triggered by a com.sun.star.awt.XMouseListener that react to mouse movements over a control. Popular use-cases for a mouse listener are changing the mouse pointer when the mouse moves over the window or querying the click count of the event mousePressed([in] com.sun.star.awt.MouseEvent e) when you want to differentiate between a single-click and a double click. For this purpose all methods carry a parameter MenuEvent, a structure that contains amongst other things, the member ClickCount. Other members (PositionX and PositionY) are to query the mouse position during the event invocation and Buttons that refers to the pressed mouse buttons.

A MouseMotionListener that implements com.sun.star.awt.XMouseMotionListener can be used when a movement of the mouse pointer must be observed. The following example code shows a part of an implementation of a mouse motion listener that is executed when the mouse is entering a control. For further information about WindowPeers, see Displaying Dialogs.

  public void mouseEntered(MouseEvent _mouseEvent) {
      try {
          // retrieve the control that the event has been invoked at...
          XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, _mouseEvent.Source);
          Object tk = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
          XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(XToolkit.class, tk);
          // create the peer of the control by passing the windowpeer of the parent 
          // in this case the windowpeer of the control
          xControl.createPeer(xToolkit, m_xWindowPeer);
          // create a pointer object "in the open countryside" and set the type accordingly...
          Object oPointer = this.m_xMCF.createInstanceWithContext("com.sun.star.awt.Pointer", this.m_xContext);
          XPointer xPointer = (XPointer) UnoRuntime.queryInterface(XPointer.class, oPointer);
          xPointer.setType(com.sun.star.awt.SystemPointer.REFHAND); 
          // finally set the created pointer at the windowpeer of the control
          xControl.getPeer().setPointer(xPointer);
      } catch (com.sun.star.uno.Exception ex) {
          throw new java.lang.RuntimeException("cannot happen...");
      }}

Keyboard Listener

Keyboard events can be captured by a KeyListener that implements com.sun.star.awt.XKeyListener. This allows you to verify each keyboard stroke. This listener is very useful for edit controls. The interface dictates the implementation of the two methods keyPressed() and keyReleased().

  public void keyReleased(KeyEvent keyEvent) {
      int i = keyEvent.KeyChar;
      int n = keyEvent.KeyCode;
      int m = keyEvent.KeyFunc;
  }

Focus Listener

A focus listener implementing com.sun.star.awt.XFocusListener is notified when the focus is entering (focusGained()) or leaving (focusLost()) a control.

The FocusListener is usually used to verify the user input when the control loses the focus.

This example demonstrates how to use the focusEvent:

  public void focusLost(FocusEvent _focusEvent) {
      short nFocusFlags = _focusEvent.FocusFlags;
      int nFocusChangeReason = nFocusFlags & FocusChangeReason.TAB;
      if (nFocusChangeReason == FocusChangeReason.TAB){
          // get the window of the Window that has gained the Focus...
          // Note that the xWindow is just a representation of the controlwindow
          // but not of the control itself
          XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, _focusEvent.NextFocus);
      }
  }

Paint Listener

Paint Listeners implementing com.sun.star.awt.XPaintListener are used to repaint areas that have become invalid.

Control element-specific events

Control element-specific events are events that only occur in relation to certain control elements.

The When initiating event is implemented in some control-button models. It is particularly useful because it is sent by either a key-press or a mouse-down action. Thus, it provides a consistent interface for users who navigate by mouse or by keyboard. If the model implements the Repeat capability, When initiating is the event that is repeatedly sent.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages