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

  1. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  
  3. import java.awt.*;
  4. import java.awt.event.* ;
  5. import com.ms.util.* ;
  6. import com.ms.fx.* ;
  7.  
  8. /**
  9. * SampleIMEWindow is the UI for SampleIME.
  10. * SampleIMEWindow is a composition window. You can set the composition
  11. * string, and SampleIMEWindow will display it.
  12. * SampleIMEWindow also contains a button for activating/deactivating
  13. * the SampleIME. 
  14. */
  15. public class SampleIMEWindow extends Window
  16.     implements MouseListener, MouseMotionListener
  17. {
  18.     Frame frame ;
  19.         // The attached frame, used for creating Window.
  20.     StringBuffer m_str ;
  21.         // The composition string to be displayed.
  22.     SampleIME m_ime ;
  23.         // The attached SampleIME.
  24.     Rectangle m_ButtonRect ;
  25.     Font font ;
  26.     FontMetrics fm ;
  27.  
  28.     int preferedWidth ;
  29.     int preferedHeight ;
  30.  
  31.     int m_PressX ;
  32.     int m_PressY ;
  33.  
  34.     String activatedStr ;
  35.         // The string to be displayed on the button when
  36.         // the attached IME is activated.
  37.     String deactivatedStr ;
  38.         // The string to be displayed on the button when
  39.         // the attached IME is deactivated.
  40.     /**
  41.      * @param ime, the IME which attached to the SampleIMEWindow
  42.      * @param c, the attached Frame.
  43.      */
  44.     public SampleIMEWindow( SampleIME ime, Frame c )
  45.     {       
  46.         super( c ) ;
  47.         
  48.         m_ime = ime ;
  49.         this.frame = c ;
  50.         addMouseListener( this ) ;
  51.         addMouseMotionListener( this ) ;
  52.         
  53.         m_ButtonRect = new Rectangle( 4, 4, 21, 21 ) ;
  54.         preferedWidth = 100 ;
  55.         preferedHeight = 60 ;
  56.  
  57.         activatedStr = "U" ;    // stands for "Unicode"
  58.         deactivatedStr = "E" ;  // stands for "English"
  59.         setBackground( Color.lightGray ) ;
  60.     }
  61.  
  62.     /**
  63.      * Set the string to be displayed in the composition window.
  64.      * @param str, the string to be displayed.
  65.      */
  66.     public void setCompositionString( StringBuffer str )
  67.     {
  68.         m_str = str ;
  69.     }
  70.  
  71.     /**
  72.      * Make the window a top-level one and show the window.
  73.      */
  74.     public void show()
  75.     {
  76.         IFxSystemInterface systemInterface = FxToolkit.getSystemInterface() ;
  77.         systemInterface.setOnTop( this, true ) ;    
  78.         super.show() ;
  79.     }
  80.     
  81.     /**
  82.      * Display the status of composition.
  83.      */
  84.     public void paint( Graphics g )
  85.     {
  86.         // Draw the background of the IME composition window.
  87.         g.drawRect( 0,0, preferedWidth - 1, preferedHeight - 1 ) ;
  88.         g.drawRect( 2,2, preferedWidth - 5, preferedHeight - 5 ) ;
  89.         g.drawRect( 4,4, 21, 21 ) ;
  90.         g.setColor( Color.white ) ;
  91.         g.drawLine( 1,1, preferedWidth - 2, 1 ) ;
  92.         g.drawLine( 1,1, 1, preferedHeight - 2 ) ;
  93.         g.setColor( Color.gray ) ;
  94.         g.drawLine( 1, preferedHeight - 2, preferedWidth-2, preferedHeight - 2 ) ;
  95.         g.drawLine( preferedWidth - 2, 1, preferedWidth-2, preferedHeight - 2 ) ;
  96.  
  97.         // Draw the IME activate/deactivate button.
  98.         g.setColor( Color.white ) ;
  99.         g.drawLine( 4,4, 25, 4 ) ;
  100.         g.drawLine( 4,4, 4, 25 ) ;
  101.         g.setColor( Color.gray ) ;
  102.         g.drawLine( 25, 4, 25, 25 ) ;
  103.         g.drawLine( 4, 25, 25, 25 ) ;
  104.  
  105.         g.setColor( Color.red ) ;
  106.         if ( m_ime.isActivated() )
  107.             g.drawString( activatedStr, 10, 20 ) ;
  108.         else
  109.             g.drawString( deactivatedStr, 10, 20 ) ;
  110.         g.setColor( Color.black ) ;
  111.         if ( m_str != null )
  112.             g.drawString( m_str.toString() , 30, 20 ) ;
  113.     }
  114.  
  115.     /**
  116.      * Set the position of the SampleIMEWindow.
  117.      */
  118.     public void setPos(int x, int y)
  119.     {
  120.         // If font information is available, we will make use of
  121.         // it to position the window in the lower right corner
  122.         // of the caret.
  123.         if ( fm != null )
  124.             setLocation( x + 10, y + fm.getHeight() * 3 / 2 ) ;
  125.         else        
  126.             setLocation( x, y ) ;
  127.         resize( preferedWidth, preferedHeight ) ;
  128.     }
  129.  
  130.     /**
  131.     * Set the size of the SampleIMEWindow.
  132.     */
  133.     public void setSize( int width, int height )
  134.     {
  135.         preferedWidth = width ;
  136.         preferedHeight = height ;
  137.         resize( width, height ) ;
  138.     }
  139.  
  140.     /**
  141.     * Set the string to be displayed on the button when
  142.     * the attached IME is activated. This should be
  143.     * an one character string.
  144.     */
  145.     public void setActivatedStr( String str )
  146.     {
  147.         activatedStr = str ;
  148.     }
  149.  
  150.     /**
  151.     * Set the string to be displayed on the button when
  152.     * the attached IME is deactivated. This should be
  153.     * an one character string.
  154.     */
  155.     public void setDeactivatedStr( String str )
  156.     {
  157.         deactivatedStr = str ;
  158.     }
  159.  
  160.     public void mouseClicked( MouseEvent event ) {}
  161.  
  162.     public void mouseEntered( MouseEvent event ) {}
  163.  
  164.     public void mouseExited( MouseEvent event ) {}
  165.  
  166.     public void mousePressed( MouseEvent event )
  167.     {
  168.         if ( m_ButtonRect.contains( event.getX(), event.getY() ) )
  169.         {
  170.             if ( m_ime.isActivated() )
  171.                 m_ime.deactivate() ;
  172.             else
  173.                 m_ime.activate() ;
  174.             repaint() ;
  175.         } else
  176.         {
  177.             m_PressX = event.getX() ;
  178.             m_PressY = event.getY() ;
  179.         }
  180.     }
  181.  
  182.     public void mouseReleased( MouseEvent event ) {}
  183.  
  184.     public void mouseDragged( MouseEvent event )
  185.     {
  186.         if ( m_ButtonRect.contains( event.getX(), event.getY() ) )
  187.             return ;
  188.  
  189.         Point curPos = getLocation() ;
  190.         setLocation( curPos.x + event.getX() - m_PressX , curPos.y + event.getY() - m_PressY ) ;
  191.     }
  192.  
  193.     public void mouseMoved( MouseEvent event ) {}
  194.  
  195.     public void setFont( Graphics g, Font font )
  196.     {
  197.         this.font = font ;
  198.         fm = g.getFontMetrics( font ) ;
  199.     }    
  200. }
  201.