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

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. /**
  5. *    JNoteUIStatus
  6. *
  7. *    A status bar for JNotepad. Implemented as a feature, which makes it really
  8. *    easy to do. Implements both the key listener and the mouse listener interfaces,
  9. *    which allow it to intercept both mouse clicks and key clicks. When we get a 
  10. *    click that changes the position of the caret, well, we update ourselves!
  11. *
  12. *    @version    1.0, 8/6/97
  13. *    @see        com.ms.ui.IUIMouseListener
  14. *    @see        com.ms.ui.IUIKeyListener
  15. *    @see        ITextFeature
  16. */
  17.  
  18. import com.ms.ui.*;
  19. import com.ms.ui.event.*;
  20. import java.awt.*;
  21.  
  22. class JNoteUIStatus extends UIStatus implements ITextFeature, IUIMouseListener, IUIKeyListener
  23. {
  24.     protected ITextOperationTargetExt tTarget;                    // text target to listen to
  25.     protected UIText textBox;                                    // text field to display status information
  26.     protected UIText statusBox;                                    // text field to display caret position in
  27.     
  28.     /**
  29.     *    Creates a new JNoteUIStatus object.
  30.     */
  31.     public JNoteUIStatus()
  32.     {
  33.         super("", UIStatic.LEFT);
  34.         localInit(" ");
  35.     }
  36.     
  37.     /**
  38.     *    Creats a new JNoteUIStatus object initalized with a string.
  39.     *
  40.     *    @param text    Text to be displayed in status bar.
  41.     */
  42.     public JNoteUIStatus(String text)
  43.     {
  44.         super(text, UIStatic.LEFT);            
  45.         localInit(text);
  46.     }
  47.     
  48.     /**
  49.     *    Private init method. Creates text fields for the caret position and
  50.     *    for general text.
  51.     *
  52.     *    @param    text    Text to put into the status bar
  53.     */
  54.     private void localInit(String text)
  55.     {
  56.         //int iCaretPos = tTarget.getCaretIndex();
  57.         
  58.         textBox = new UIText(text);            
  59.         statusBox = new UIText("Line: 1 Position: 1", UIText.RIGHT);
  60.         
  61.         add(textBox, "east");
  62.         add(statusBox, "west");
  63.     }
  64.     
  65.     // IFeature and ITextFeature methods
  66.     
  67.     /**
  68.     *    Public IFeature initialization method. Called every time this feature
  69.     *    is attached to a new object. Sets the target object properly.
  70.     *
  71.     *    @param tObject Object to obtain position status from.
  72.     */
  73.     public void init(ITextOperationTargetExt tObject)
  74.     {
  75.         tTarget = tObject;
  76.     }
  77.     
  78.     /**
  79.     *    Public IFeature registration method. Called to attaches listeners to the 
  80.     *  target object so we can peek at events that happen to it and modify it 
  81.     *    accordingly.
  82.     */
  83.     public void register()
  84.     {
  85.         tTarget.addKeyListener(this);
  86.         tTarget.addMouseListener(this);
  87.     }
  88.     
  89.     /**
  90.     *    Public IFeature unregistration method. Called to detaches listeners from 
  91.     *  the target object.
  92.     */    
  93.     public void unregister()
  94.     {
  95.         tTarget.removeKeyListener(this);
  96.         tTarget.removeMouseListener(this);
  97.     }
  98.     
  99.     /**
  100.     *    Resets the feature. Called to clear any state we may have saved. Since
  101.     *    no state is kept, we do nothing.
  102.     */
  103.     public void reset()
  104.     {
  105.         // we keep no state, so nothing to reset            
  106.     }
  107.     
  108.     // private methods
  109.     
  110.     /**
  111.     *    Sets the status bar text field.    
  112.     *
  113.     *    @param    iLine    Current line number value.
  114.     *    @param    iPos    Current position in the line.
  115.     */
  116.     protected void printStatusLine(int iLine, int iPos)
  117.     {
  118.         statusBox.setName("Line: "+iLine+" Position: "+iPos);
  119.     }
  120.     
  121.     /**
  122.     *    Updates the status in the status bar. Displays the current location
  123.     *    of the caret in the text target.
  124.     */
  125.     protected void updateStatus()
  126.     {
  127.         int iCaretPos = tTarget.getCaretIndex();
  128.         
  129.         // grab the dimensions of the current font.
  130.         FontMetrics fontMetrics = getToolkit().getFontMetrics(tTarget.getFont());    
  131.         
  132.         // grab the location (in pixels) of the caret.
  133.         Point p = tTarget.getCharLocation(iCaretPos);
  134.         
  135.         // calculate current line by dividing current y position by the
  136.         // height of the font. Add one to make it one based.
  137.         int iLine = p.y / fontMetrics.getHeight() + 1;
  138.         
  139.         // grab index of the first character in current line by zeroing out
  140.         // the x coordinate of our positoin and grab the index at that point.
  141.         p.x = 0;
  142.         int iCharPos = tTarget.getCharFromScreen(p);
  143.         
  144.         // print the status bar.
  145.         printStatusLine(iLine, (iCaretPos-iCharPos+1));
  146.         
  147.         statusBox.repaint();
  148.     }    
  149.     
  150.     // Key listener methods
  151.     
  152.     /**
  153.     *    Public IUIKeyListener method. We do nothing here, all procesessing is
  154.     *    in <a href="#keyTyped">keyReleased()</a>
  155.     *
  156.     *    @param ke Key event that occurred     
  157.     */
  158.     public synchronized void keyPressed(UIKeyEvent ke)
  159.     {
  160.         // do it all in keyReleased
  161.     }
  162.     
  163.     /**
  164.     *    Public IUIKeyListener method. Update the status of the status bar
  165.     *    (i.e. current caret position) whenever a key is released.
  166.     *
  167.     *    @param ke Key event that occurred
  168.     */
  169.     public synchronized void keyReleased(UIKeyEvent ke)
  170.     {
  171.         updateStatus();
  172.     }
  173.     
  174.     /**
  175.     *    Public IUIKeyListener method. We do nothing here, all procesessing is
  176.     *    in <a href="#keyTyped">keyTyped()</a>
  177.     *
  178.     *    @param ke Key event that occurred     
  179.     */
  180.     public synchronized void keyTyped(UIKeyEvent ke)
  181.     {
  182.         // do it all in keyPressed
  183.     }
  184.     
  185.     
  186.     // Mouse listener methods
  187.     
  188.     /**
  189.     *    Public IUIMouseListener method. Update the status of the status bar
  190.     *    (i.e. current caret position) whenever the mouse is clicked.
  191.     *
  192.     *    @param evt Mouse event that occurred
  193.     */    
  194.     public void mouseClicked(UIMouseEvent evt)
  195.     {
  196.         updateStatus();
  197.     }
  198.     
  199.     /**
  200.     *    Public IUIMouseListener method. We do nothing here, all procesessing is
  201.     *    in <a href="#keyTyped">mouseClicked()</a>
  202.     *
  203.     *    @param evt Mouse event that occurred     
  204.     */
  205.     public void mousePressed(UIMouseEvent evt)
  206.     {
  207.         // we'll do it all in mouseClicked
  208.     }
  209.     
  210.     /**
  211.     *    Public IUIMouseListener method. We do nothing here, all procesessing is
  212.     *    in <a href="#keyTyped">mouseClicked()</a>
  213.     *
  214.     *    @param evt Mouse event that occurred     
  215.     */    
  216.     public void mouseReleased(UIMouseEvent evt)
  217.     {
  218.         // we'll do it all in mouseClicked
  219.     }
  220.     
  221.     /**
  222.     *    Public IUIMouseListener method. We do nothing here, all procesessing is
  223.     *    in <a href="#keyTyped">mouseClicked()</a>
  224.     *
  225.     *    @param evt Mouse event that occurred     
  226.     */    
  227.     public void mouseEntered(UIMouseEvent evt)
  228.     {
  229.         // don't care about this
  230.     }
  231.     
  232.     /**
  233.     *    Public IUIMouseListener method. We do nothing here, all procesessing is
  234.     *    in <a href="#keyTyped">mouseClicked()</a>
  235.     *
  236.     *    @param evt Mouse event that occurred     
  237.     */    
  238.     public void mouseExited(UIMouseEvent evt)
  239.     {
  240.         // don't care about this
  241.     }
  242.     
  243.     
  244. }
  245.  
  246.