home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / BraceMatchFeature.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  2.6 KB  |  118 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4.  
  5. import com.ms.ui.event.UIKeyEvent;
  6.  
  7. /**
  8. *    A feature plugin which matches parentheses. When the user types the closing paren,
  9. *    the cursor moves to the matching open paren. The next keystroke puts the caret back in 
  10. *    the expected place. Useful for coding.
  11. *
  12. *    @see    IFeature
  13. *    @see    JNoteKeyFeature
  14. *
  15. *    @version    1.0, 8/13/97
  16. */
  17.  
  18. public class BraceMatchFeature extends JNoteKeyFeature
  19. {
  20.     /**
  21.     *    Constant which indicates normal state - no open parenthese
  22.     */
  23.     static protected int STATE_NORMAL = 0;
  24.     
  25.     /**
  26.     *    Constant which indicates open state - an open parenthese
  27.     */
  28.     static protected int STATE_OPEN   = 1;
  29.     
  30.     /**
  31.     *    Constant which indicates closed state - a closing parenthese has been typed
  32.     */
  33.     static protected int STATE_CLOSED = 2;        // closed paren has been typed.
  34.     
  35.                                                 /**
  36.                                                 *    Current state of parenthese matching
  37.     */
  38.     int iState = STATE_NORMAL;
  39.     
  40.     /**
  41.     *    Character index of open parenthese
  42.     */
  43.     int iLastBrace = -1;
  44.     
  45.     /**
  46.     *    Character index of closed parenthese
  47.     */
  48.     int iEndPos = -1;
  49.     
  50.     
  51.     /**
  52.     *    Creates a new BraceMatchFeature()
  53.     */
  54.     public BraceMatchFeature()
  55.     {
  56.         super();
  57.     }
  58.     
  59.     /**
  60.     *    Called when a key is pressed but before it
  61.     *    is released. Part of IUIKeyListener.  If the state is STATE_CLOSED, remove the section 
  62.     *    around the open parenthese.
  63.     *
  64.     *    @param    ke    UIKeyEvent which is being processed
  65.     */
  66.     public synchronized void keyPressed(UIKeyEvent ke)
  67.     {
  68.         if (iState == STATE_CLOSED)
  69.         {
  70.             textTarget.setSelection(0,0);
  71.             textTarget.moveTheCaret(iEndPos);
  72.             iState = STATE_NORMAL;
  73.         }
  74.     }
  75.     
  76.     /**
  77.     *    Part of IUIKeyListener. Called when a key is typed (pressed and released).
  78.     *    Checks if the user just typed a close paren or an open paren and sets 
  79.     *    the state accordingly.
  80.     *
  81.     *    @param    ke    UIKeyEvent which is being processed
  82.     */
  83.     public synchronized void keyTyped(UIKeyEvent ke)
  84.     {
  85.         if (iState == STATE_CLOSED)
  86.         {
  87.             return;
  88.         }
  89.         
  90.         if (ke.getKeyChar() == '(') 
  91.         {
  92.             iLastBrace = textTarget.getCaretIndex()-1;
  93.             iState = STATE_OPEN;
  94.         }        
  95.         
  96.         if ((ke.getKeyChar() == ')') && (iState == STATE_OPEN))
  97.         {
  98.             iEndPos = textTarget.getCaretIndex();
  99.             //textTarget.moveCaret(iLastBrace);
  100.             textTarget.setSelection(iLastBrace, iLastBrace+1);
  101.             iState = STATE_CLOSED;                
  102.         }
  103.         
  104.     }        
  105.     
  106.     
  107.     /**
  108.     *    Resets the feature. Called when the feature is removed from an edit control.
  109.     *    Resets the state to STATE_NORMAL.
  110.     */
  111.     public void reset()
  112.     {
  113.         iState = STATE_NORMAL;
  114.         iEndPos = -1;
  115.     }
  116. }
  117.  
  118.