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

  1. //
  2. //  SampleBean.java
  3. //
  4. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  5. //
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.util.Vector;
  10. import java.util.Date;
  11.  
  12. interface SampleBeanEventListener extends java.util.EventListener {
  13.     void somethingHappened();
  14. }
  15.  
  16. public class SampleBean extends Panel {
  17.  
  18.     public SampleBean()
  19.     {
  20.         enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  21.     }
  22.  
  23.     public void paint(Graphics g) {
  24.         g.setColor(textColor);
  25.         g.drawString(textToDraw, 20, 20);
  26.     }
  27.  
  28.     //
  29.     //  Examples of events using standard design patterns.
  30.     //
  31.  
  32.     transient Vector listeners = new Vector();
  33.  
  34.     synchronized public void addSampleBeanEventListener(SampleBeanEventListener l) {
  35.  
  36.         if(listeners == null)
  37.             listeners = new Vector();
  38.  
  39.         listeners.addElement(l);
  40.     }
  41.  
  42.     synchronized public void removeSampleBeanEventListener(SampleBeanEventListener l) {        
  43.         listeners.removeElement(l);
  44.     }
  45.  
  46.     protected void processMouseEvent(MouseEvent e)
  47.     {
  48.         if((e.getID()) == MouseEvent.MOUSE_PRESSED)
  49. fire_event:
  50.         {
  51.             synchronized(this)
  52.             {
  53.                 if(listeners == null)
  54.                     break fire_event;
  55.  
  56.                 SampleBeanEventListener snapshot[];
  57.                 
  58.                 snapshot = new SampleBeanEventListener[listeners.size()];
  59.                 listeners.copyInto(snapshot);
  60.             
  61.                 for (int i = 0; i < snapshot.length; i++)
  62.                     snapshot[i].somethingHappened();
  63.  
  64.             }
  65.  
  66.         }
  67.  
  68.          super.processMouseEvent(e);
  69.     }
  70.  
  71.     //
  72.     //  Examples of properties using standard design patterns.
  73.     //
  74.  
  75.     String textToDraw = "Some text...";
  76.  
  77.     public String getTextToDraw() {
  78.         return textToDraw;
  79.     }
  80.  
  81.     public void setTextToDraw(String textToDraw) {
  82.         this.textToDraw = textToDraw;
  83.         repaint();
  84.     }
  85.  
  86.     Color textColor = Color.black;
  87.  
  88.     public Color getTextColor() {
  89.         return textColor;
  90.     }
  91.  
  92.     public void setTextColor(Color textColor) {
  93.         this.textColor = textColor;
  94.         repaint();
  95.     }
  96.  
  97.     //
  98.     //  Examples of methods using standard design patterns.
  99.     //
  100.  
  101.     public String returnTheDate() {
  102.         return (new Date()).toString();
  103.     }
  104. }
  105.