home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / International / sampleime / SampleIME.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  10.8 KB  |  309 lines

  1. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  
  3. import java.awt.*;
  4. import com.ms.lang.SystemX ;
  5. import com.ms.util.* ;
  6. import java.awt.event.KeyEvent ;
  7.  
  8. import com.ms.util.InputMethod.*;
  9. import com.ms.ui.* ;
  10. import com.ms.fx.* ;
  11.  
  12. // This is a sample IME which allows user to input unicode characters
  13. // by typing the hex value of unicode characters.
  14. //
  15. // It demostrate that how to use the InputMethodCallback.handleIMEChar()
  16. // to put a converted unicode character( composition character )
  17. // back into the Java side and InputMethodCallback.handleIMEMessage() to
  18. // send IME message to the system.
  19. //
  20. // This SampleIME also demostrates how to write a level-2 IME, whose composition
  21. // window will be shown near the position of input caret.
  22.  
  23. public class SampleIME implements com.ms.util.InputMethod.InputMethodListener
  24. {
  25.     SampleIMEWindow m_uiInput ;
  26.         // UI window for Sample IME.
  27.     StringBuffer m_CompStr ;
  28.         // Composition string.
  29.     boolean m_bActivate = false ;
  30.         // the flag to indicate if the IME is activated or not.
  31.         
  32.     IUIComponent   uiComp ;
  33.         // Visible componet for UI.
  34.     Component       awtComp ;
  35.         // Visible componet for AWT.
  36.     Component topContainer ;
  37.         // The top container in the hierarchy of the visible component.
  38.         // We will use this as a parent when we create the composition window.
  39.     Font font ;        
  40.         // Font used in the composition window.
  41.         
  42.     public SampleIME()
  43.     {
  44.         m_CompStr = new StringBuffer( 20 ) ;
  45.     }
  46.  
  47.     protected void finalize() throws Throwable
  48.     {
  49.         if ( m_uiInput != null )
  50.             m_uiInput.dispose() ;
  51.     }
  52.  
  53.     /**
  54.      * This will be called by InputManagerListener.handledKey(). When activated,
  55.      * Java IME should use this method to take keystroke from system, and decides 
  56.      * if it wants to take this keystroke.
  57.      */
  58.     public boolean handledKey( InputMethodCallback imeCallback, long when, int keyCode, int keyChar, int state )
  59.     {
  60.         if ( !m_bActivate || ( ( state & InputManagerListener.DOWN ) != InputManagerListener.DOWN ) )
  61.         {
  62.             // If not activated or this is a KeyUp, just return.
  63.             return false ;
  64.         }
  65.  
  66.         char ch = (char)keyChar ;
  67.             // Get the input from user.
  68.  
  69.         if ( ( m_CompStr.length() > 0 ) && ( keyCode == KeyEvent.VK_BACK_SPACE ) )
  70.         {
  71.             // Processing of BackSpace.
  72.             StringBuffer newCompStr = new StringBuffer();
  73.  
  74.             // Delete the last character.
  75.             for ( int i = 0 ; i < m_CompStr.length() - 1 ; i++ )
  76.                 newCompStr.append( m_CompStr.charAt( i ) ) ;
  77.             m_CompStr = newCompStr ;
  78.             
  79.             if ( m_uiInput != null )
  80.                 m_uiInput.setCompositionString( m_CompStr ) ;
  81.  
  82.             // Generate an IMECompositionMessage to inform imeCallback that
  83.             // the composition is changed.
  84.             imeCallback.handleIMEMessage( 
  85.                 new IMECompositionMessage( IMECompositionMessage.CHANGE_COMPOSITION, keyCode, keyChar, m_CompStr.toString() ) ) ;
  86.             return true ;   
  87.                 // Return true to tell InputManagerListener not to pass this keystroke
  88.                 // back to system.                
  89.         }
  90.  
  91.         if ( ( m_CompStr.length() > 0 ) && ( keyCode == KeyEvent.VK_ENTER ) )
  92.         {
  93.             // When there are characters in m_CompStr, and the user hits
  94.             // the Enter key, it means that a composition is completed.
  95.  
  96.             // Converted the composition string into a unicode character.
  97.             int unicode = Integer.valueOf( m_CompStr.toString(), 16 ).intValue() ;
  98.             char unicodeChar = ( char ) unicode ;
  99.  
  100.             // Pass the composed character back to the system.
  101.             imeCallback.handleIMEChar( when, unicodeChar, state ) ;
  102.  
  103.             // Redraw the composition window.
  104.             m_CompStr = new StringBuffer() ;
  105.             if ( m_uiInput != null )
  106.                 m_uiInput.setCompositionString( m_CompStr ) ;
  107.                 
  108.             // Composition is completed.
  109.             imeCallback.handleIMEMessage( 
  110.                 new IMECompositionMessage( IMECompositionMessage.END_COMPOSITION ) ) ;
  111.  
  112.             return true ;
  113.                 // Return true to tell InputManagerListener not to pass this keystroke
  114.                 // back to system.                            
  115.         }
  116.                 
  117.         if ( ( ch >= 'a' && ch <= 'f' ) ||  
  118.              ( ch >= 'A' && ch <= 'F' ) || 
  119.              ( ch >= '0' && ch <= '9' ) )
  120.         {
  121.             if ( m_CompStr.length() == 0 )
  122.             {
  123.                 // Generate an IMECompositionMessage to inform imeCallback that a new
  124.                 // composition is started.
  125.                 imeCallback.handleIMEMessage( new IMECompositionMessage( IMECompositionMessage.START_COMPOSITION ) ) ;
  126.             }
  127.             // Update the composition string and the display of compositon window.
  128.             if ( m_CompStr.length() < 4 )
  129.             {
  130.                 m_CompStr.append( ch ) ;
  131.                 // Generate an IMECompositionMessage to inform imeCallback that
  132.                 // the composition is changed. Note that we pass the composition string
  133.                 // in the last parameter of IMECompositionMessage.
  134.                 imeCallback.handleIMEMessage( 
  135.                     new IMECompositionMessage( IMECompositionMessage.CHANGE_COMPOSITION, keyCode, keyChar, m_CompStr.toString() ) ) ;
  136.             }           
  137.             return true ;   // End of processing.
  138.         }
  139.         // SampleIME is not interested in this keystroke, just return it to the system
  140.         // as a normal keyboard input.
  141.         return false ;  // Pass it back to component.
  142.     }
  143.  
  144.     // Handle InputMethodMessage: to draw default IME UI.
  145.     // When we use InputMethodCallback.handleIMEMessage() to pass the IME messages to
  146.     // the system, and the target application is not interested in IME messages( either
  147.     // by not implementing InputMethodMessageListener, or return false in 
  148.     // InputMethodMessageListener.handleIMEMessage() ), all of the IME messages 
  149.     // will fall back here.
  150.     public void handleIMEMessage( InputMethodMessage IMEMessage )
  151.     {
  152.         if ( m_uiInput != null )
  153.            m_uiInput.repaint() ;
  154.     }
  155.  
  156.     public void paint(Graphics g)
  157.     {
  158.         // Don't worry about this right now.
  159.         // This is intended to support inline IME.
  160.     }
  161.  
  162.     public void setVisibleComponent(Component c)
  163.     {    
  164.         awtComp = c ;
  165.  
  166.         topContainer = c ;
  167.         
  168.         Component temp = topContainer ;
  169.         while ( temp != null )
  170.         {
  171.             topContainer = temp ;
  172.             temp = temp.getParent() ;
  173.         }
  174.         
  175.         if ( topContainer instanceof Frame )
  176.             if ( m_uiInput == null )
  177.             {
  178.                 m_uiInput = new SampleIMEWindow( this, ( Frame ) topContainer ) ;
  179.                 m_uiInput.setCompositionString( m_CompStr ) ;
  180.                 m_uiInput.show() ;
  181.                 m_uiInput.resize( 200, 100 ) ;
  182.                 m_uiInput.setActivatedStr( "U" ) ;
  183.                     // Create the UI for this IME.
  184.             }
  185.     }
  186.  
  187.     // Sets the component c as the component where the IME is seen on the 
  188.     // screen. In Microsoft's VM implementation, java.awt.TextField and 
  189.     // java.awt.TextArea will make use of this to provide IME level 2 support.
  190.     public void setVisibleComponent( IUIComponent c)
  191.     {
  192.         uiComp = c ;
  193.         
  194.         topContainer = c.getRoot().getHost() ;  // Get the host in the AWT side.
  195.  
  196.         // Calcuate the insets of the top level guy in AWT.
  197.  
  198.         // Find the top container.
  199.         Component temp = topContainer ;
  200.         while ( temp != null )
  201.         {
  202.             topContainer = temp ;
  203.             temp = temp.getParent() ;
  204.         }
  205.  
  206.         if ( m_uiInput == null )
  207.             if ( topContainer instanceof Container )
  208.             {
  209.                 // Create an IME composition window, which will be a child of
  210.                 // the topContainer.
  211.                 m_uiInput = new SampleIMEWindow( this, ( Frame ) topContainer ) ;
  212.                 m_uiInput.setCompositionString( m_CompStr ) ;
  213.                 m_uiInput.show() ;
  214.                 m_uiInput.resize( 200, 100 ) ;
  215.                 m_uiInput.setActivatedStr( "U" ) ;
  216.             }               
  217.     }    
  218.  
  219.     // Set the position of composition window.
  220.     // In Microsoft's VM implementation, java.awt.TextField and 
  221.     // java.awt.TextArea will make use of this to provide IME level 2 support.
  222.     public void setPos(int x, int y)
  223.     {
  224.         if ( m_uiInput == null )
  225.             return ;
  226.             
  227.         Point scrnPos ;
  228.         if ( uiComp != null )
  229.         {           
  230.             // We have a visible component, set the composition window
  231.             // near the visible component( IME level 2 support ).
  232.             scrnPos = uiComp.getLocationOnScreen() ;
  233.                 // Get the screen position of the current component.                
  234.             m_uiInput.setPos( x + scrnPos.x , y + scrnPos.y ) ;
  235.                 // Before we pass the position to IME, we have to translate
  236.                 // the ( x,y ) position into the client area of the top container.
  237.         } 
  238.         else if ( awtComp != null )
  239.         {
  240.             scrnPos = awtComp.getLocationOnScreen() ;
  241.             m_uiInput.setPos( x, y ) ;
  242.         }
  243.         else
  244.             m_uiInput.setPos( x, y ) ;
  245.         
  246.     }
  247.  
  248.     public Dimension size(Graphics g)
  249.     {
  250.         // Don't worry about this right now.
  251.         // This is intended to support inline IME.
  252.         return null ;
  253.     }
  254.     
  255.     // Acticate the IME.
  256.     public void activate()
  257.     {
  258.         show() ;
  259.         m_bActivate = true ;
  260.     }
  261.  
  262.     // Deactivate the IME.
  263.     public void deactivate()
  264.     {
  265.         hide() ;
  266.         m_bActivate = false ;
  267.     }        
  268.  
  269.     // Show the IME UI.
  270.     public void show()
  271.     {
  272.         if ( m_uiInput != null )
  273.             m_uiInput.show() ;
  274.     }
  275.  
  276.     // Hide the IME UI.
  277.     public void hide()
  278.     {
  279.         if ( m_uiInput != null )
  280.             //m_uiInput.setVisible( false ) ;
  281.             m_uiInput.hide() ;
  282.     }
  283.  
  284.     // Return true if the IME is activated.
  285.     public boolean isActivated()
  286.     {
  287.         return m_bActivate ;
  288.     }
  289.  
  290.     // Return the name of this IME.
  291.     public String getName()
  292.     {
  293.         return "Sample IME" ;
  294.     }
  295.  
  296.     // Set the font used in the composition window.
  297.     public void setFont( Font font )
  298.     {
  299.         if ( this.font == font )
  300.             return ;
  301.         this.font = font ;
  302.         if ( m_uiInput != null )
  303.         {
  304.             if ( uiComp != null )
  305.                 m_uiInput.setFont( uiComp.getGraphics().getBaseGraphics(), font ) ;
  306.         }
  307.     }
  308. }
  309.