home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / WFC / SimpleOCXControl / SimpComp / SimpleControl.java < prev   
Encoding:
Java Source  |  2000-05-04  |  14.1 KB  |  463 lines

  1. package SimpComp ;
  2. import com.ms.wfc.core.*;
  3. import com.ms.wfc.ui.*;
  4. import com.ms.lang.Delegate;
  5.  
  6. /**
  7.  * SimpleControl
  8.  * 
  9.  * SimpleControl demonstrates how to build a simple control and how to 
  10.  * declare its properties and events so that they can be displayed and 
  11.  * manipulated in the Properties Window in the WFC Designer.
  12.  * 
  13.  * The control includes a simple custom property and a property editor 
  14.  * for that property. 
  15.  *
  16.  * The control hides those properties on com.ms.wfc.ui.Control that are not 
  17.  * appropriate
  18.  * 
  19.  * If the control was forms based then it would derive from UserControl 
  20.  * rather than Control.
  21.  * 
  22.  * The properties and events for controls can be built using the WFC 
  23.  * Component Builder
  24.  * 
  25.  * The entry point for class execution is a default constructor.
  26.  *  
  27.  * This class can be used as an ActiveX control. Check the checkbox 
  28.  * for this class on the Project Properties COM Classes tab, or 
  29.  * remove the // from the line:
  30.  * 
  31.  // * @com.register ( clsid=39DF5AA0-D3B1-11D1-8C52-00C04F8F3341, typelib=39DF5AA1-D3B1-11D1-8C52-00C04F8F3341 )
  32.  * @com.register ( clsid=2148925C-234F-11D2-8C4E-00C04F8F3341, typelib=2148925B-234F-11D2-8C4E-00C04F8F3341 )
  33.  */
  34. public class SimpleControl extends Control {
  35.     
  36.     /* Private data
  37.     */
  38.     
  39.     // The event handler which fires when numberData changes
  40.     private EventHandler m_NumberDataChanged ;
  41.  
  42.     // A read-only field derived from numberData
  43.     private double m_ReadOnlyData = 0.5 ;
  44.  
  45.     // The string data held and displayed by the control
  46.     private String m_StringData = "Default Data" ;
  47.  
  48.     // The numeric data held and displayed by the control
  49.     private short m_NumberData = 1 ;
  50.  
  51.     /*
  52.      * A flag to control the way the contents of the control are drawn.
  53.      * This contents of this property are set via an enum rather than
  54.      * being directly edited by the user - see property declaration in 
  55.      * the ClassInfo below
  56.      */
  57.     private int m_DrawingMode = DrawingMode.CALM ;
  58.  
  59.     // A "custom" property based on a user defined class 
  60.     private MyOwnType m_MyOwnType = MyOwnType.TYPE0  ;
  61.  
  62.     // Utility constants used by painting/invaldating the control
  63.     private static final int NUMBER_OF_DATA_PROPS = 4 ;
  64.     private static final int STRING_YPOS = 0 ;
  65.     private static final int NUM_YPOS = 1 ;
  66.     private static final int RO_YPOS = 2 ;
  67.     private static final int MOT_YPOS = 3 ;
  68.  
  69.  
  70.     /**
  71.      * Creates a new SimpleControl
  72.      * 
  73.      * Controls must have a default constructor 
  74.      */
  75.     public SimpleControl() {
  76.         
  77.         super();
  78.  
  79.         /*
  80.          * Control sets the style to STYLE_USERPAINT | STYLE_STDCLICK
  81.          * so we will add STYLE_RESIZE_REDRAW to make sure we get 
  82.          * redraw on resize
  83.          */
  84.         setStyle(STYLE_RESIZE_REDRAW, true) ;
  85.     }
  86.  
  87.     
  88.     /* Events
  89.     */
  90.  
  91.     /**
  92.      * Adds an event handler for the "numberDataChanged" event 
  93.      * notification. This event is fired whenever the value of 
  94.      * numberData changes
  95.      *
  96.      * @param value New Event Handler to install for this event.
  97.      */
  98.     public void addOnNumberDataChanged(EventHandler value) {
  99.         m_NumberDataChanged = (EventHandler)Delegate.combine(m_NumberDataChanged, value);
  100.     }
  101.  
  102.     /**
  103.      * Fires the event indicating that the numberData property has 
  104.      * been changed.
  105.      * 
  106.      * Inheriting controls should use this in favour of actually 
  107.      * listening to the event, but should not forget to call 
  108.      * super.onCheckedChanged() to ensure that the event is still 
  109.      * fired for external listeners.
  110.      *
  111.      * @param   e   Event to send
  112.      */
  113.     protected void onNumberDataChanged(Event event) {
  114.         if (m_NumberDataChanged != null) m_NumberDataChanged.invoke(this, event);
  115.     }
  116.  
  117.     /**
  118.      * Removes the given handler for the "numberDataChanged" event.
  119.      * If there are duplicate entries, ALL are removed.
  120.      *
  121.      * @param   value   Event handler to be removed.
  122.      */
  123.     public void removeOnNumberDataChanged(EventHandler value) {
  124.         m_NumberDataChanged = (EventHandler)Delegate.remove(m_NumberDataChanged, value);
  125.     }
  126.  
  127.     /* Property Get/Set Methods
  128.     */
  129.  
  130.     /**
  131.      * Get the flag that controls how the data values in the control 
  132.      * are rendered to the screen
  133.      *
  134.      * @return  int   A value from the control.DrawingMode enumeration
  135.      *                which indicates how the control is rendered to the 
  136.      *                screen
  137.      *
  138.      * @see control.DrawingMode
  139.      */
  140.     public int getDrawingMode() { 
  141.         return m_DrawingMode;
  142.     }
  143.  
  144.     /**
  145.      * Get the custom property
  146.      *
  147.      * @return  <b>MyOwnType</b> the value of the custom property 
  148.      *
  149.      * @see control.MyOwnType
  150.      */
  151.     public MyOwnType getMyOwnType() {
  152.         return m_MyOwnType;
  153.     }
  154.     
  155.     /**
  156.      * Answer the numeric data stored in the control
  157.      * 
  158.      * @return  <b>short</b> the numeric data property
  159.      */
  160.     public short getNumberData() {
  161.         return m_NumberData;
  162.     }
  163.     
  164.     /**
  165.      * Answer the readonly data derived from numberData
  166.      * 
  167.      * NOTE: ReadOnly Property so no "set" method
  168.      * 
  169.      * @return  <b>double</b> based on numberData
  170.      */
  171.     public double getReadOnlyData() { 
  172.         return m_ReadOnlyData;
  173.     }
  174.  
  175.     /**
  176.      * Answer the string data stored in the control
  177.      * 
  178.      * @return  <b>String</b>  the value of the StringData property
  179.      */
  180.     public String getStringData() {
  181.         return m_StringData;
  182.     }
  183.  
  184.  
  185.     /**
  186.      * Set the flag that controls how the data values in the control 
  187.      * are rendered to the screen
  188.      *
  189.      * @param value   A value from the control.DrawingMode enumeration
  190.      *                controlling how the control is rendered to the 
  191.      *                screen
  192.      *
  193.      * @see control.DrawingMode
  194.      */
  195.     
  196.     public void setDrawingMode(int value) {
  197.         
  198.         /*
  199.          * Verify that the value is one of the Drawing Mode enum values
  200.          * if not do not change the value
  201.          */
  202.         if (DrawingMode.valid(value)) {
  203.             m_DrawingMode = value;
  204.             invalidate() ;
  205.         }
  206.     }
  207.     
  208.  
  209.     /**
  210.      * Set the custom property
  211.      *
  212.      * @param   value   An instance of MyOwnType 
  213.      *
  214.      * @see control.MyOwnType
  215.      */
  216.     public void setMyOwnType(MyOwnType value) {
  217.         m_MyOwnType = value;
  218.         invalidateItemRect(MOT_YPOS) ;
  219.     }
  220.     
  221.     
  222.     /**
  223.      * Set the numeric data stored in the control
  224.      * 
  225.      * Whenever this value is changed the "numberDataChanged" event 
  226.      * will be fired
  227.      * 
  228.      * @param   value   The new numeric (short) data for the control
  229.      */
  230.     public void setNumberData(short value) {
  231.         m_NumberData = value;
  232.         m_ReadOnlyData = (double)value / 2  ;
  233.         invalidateItemRect(NUM_YPOS) ;
  234.         invalidateItemRect(RO_YPOS) ;
  235.         onNumberDataChanged(Event.EMPTY);
  236.     }
  237.  
  238.  
  239.     /**
  240.      * Set the string data stored in the control
  241.      * 
  242.      * @param value The new string data for the control
  243.      */
  244.     public void setStringData(String value) {
  245.         m_StringData = value;
  246.         invalidateItemRect(STRING_YPOS) ;
  247.     }
  248.     
  249.     
  250.     /* Methods
  251.     */
  252.  
  253.     /** 
  254.      * Override onPaint from control so that we can paint the contents
  255.      * of the control
  256.      */
  257.     protected void onPaint(PaintEvent e) {
  258.         
  259.         super.onPaint(e) ;
  260.  
  261.         Color bColor = Color.BLACK, 
  262.             fColor = Color.WHITE ; 
  263.  
  264.         int itemHeight = getItemHeight() ;
  265.         
  266.         /*
  267.          * As this is a simple control simply set the font 
  268.          * rather than giving the user the opportunity to 
  269.          * set it
  270.          */
  271.         Font controlFont = new Font( "Arial", itemHeight - 1
  272.                                     , FontSize.CELLHEIGHT
  273.                                     , FontWeight.BOLD
  274.                                     , false
  275.                                     , false
  276.                                     , false
  277.                                     );
  278.         
  279.         /*
  280.          * Work out background & foreground colors based on the 
  281.          * drawing mode
  282.          */
  283.         switch (m_DrawingMode) {
  284.             
  285.             case DrawingMode.AWKWARD:
  286.                 bColor = Color.YELLOW ;
  287.                 fColor = Color.BLACK ;
  288.                 break ;
  289.  
  290.             case DrawingMode.ANGRY:
  291.                 bColor = Color.RED ;
  292.                 fColor = Color.BLACK ;
  293.                 break ;
  294.  
  295.             default:
  296.                 bColor = Color.BLACK ;
  297.                 fColor = Color.WHITE ;
  298.         }
  299.  
  300.  
  301.         // Set colors & font
  302.         e.graphics.setBackColor(bColor);
  303.         e.graphics.setTextColor(fColor);
  304.         e.graphics.setFont(controlFont);
  305.  
  306.         // Draw the contents of the control
  307.         e.graphics.clearRect(e.clipRect) ;
  308.         e.graphics.drawString(m_StringData, 5, STRING_YPOS * itemHeight) ;
  309.         e.graphics.drawString((new Short(m_NumberData)).toString(), 5, NUM_YPOS * itemHeight) ;
  310.         e.graphics.drawString((new Double(m_ReadOnlyData)).toString(), 5, RO_YPOS * itemHeight) ;
  311.         e.graphics.drawString(m_MyOwnType.toString(), 5, MOT_YPOS * itemHeight) ;
  312.         
  313.     }
  314.  
  315.     /**
  316.      * @internalonly
  317.      * 
  318.      * Answer the height of each item in the control
  319.      */
  320.     private final int getItemHeight() {
  321.         return getClientRect().height/NUMBER_OF_DATA_PROPS ;
  322.     }
  323.  
  324.     /**
  325.      * @internalonly
  326.      * 
  327.      * Work out the area to invalidate based on the position of the item being updated
  328.      */
  329.     private final void invalidateItemRect(int itemYPos) { 
  330.         int itemHeight = getItemHeight() ;
  331.         Rectangle rect = new Rectangle(0, itemYPos * itemHeight, getClientRect().width, itemHeight) ;
  332.         invalidate(rect) ;
  333.     }
  334.  
  335.     /**
  336.      * SimpleControl.ClassInfo
  337.      * 
  338.      * Provides the component meta-information that cannot be 
  339.      * determined from the component itself. 
  340.      * 
  341.      */
  342.     public static class ClassInfo extends Control.ClassInfo {
  343.         
  344.         /* Events 
  345.         */
  346.         public static final EventInfo numberDataChanged 
  347.             = new EventInfo( SimpleControl.class        //Component Class
  348.                 , "numberDataChanged"                    //Event name
  349.                 , EventHandler.class                    //Event class
  350.                 //Event Attributes
  351.                 , CategoryAttribute.Action
  352.                 , new DescriptionAttribute("Fired when the number changes")
  353.                 );
  354.  
  355.         /* Properties
  356.         */
  357.         public static final PropertyInfo readOnlyData 
  358.             = new PropertyInfo ( SimpleControl.class    //Component Class
  359.                 , "readOnlyData"                        //Property name
  360.                 , double.class                            //Property class
  361.                 //Property Attributes
  362.                 , CategoryAttribute.Data
  363.                 , new DescriptionAttribute("A read only property derived from the numberData")
  364.                 );
  365.  
  366.         public static final PropertyInfo stringData 
  367.             = new PropertyInfo( SimpleControl.class
  368.                 ,  "stringData"
  369.                 , String.class
  370.                 , CategoryAttribute.Data
  371.                 , new DescriptionAttribute("The text contents of the control")
  372.                 );
  373.  
  374.         public static final PropertyInfo numberData 
  375.             = new PropertyInfo( SimpleControl.class
  376.                 , "numberData"
  377.                 , short.class
  378.                 , CategoryAttribute.Data
  379.                 , new DescriptionAttribute("The numeric contents of the control")
  380.                 );
  381.  
  382.         /*
  383.          * NOTE: enum Property - the property class is set to the 
  384.          * enum class NOT type int even though the get/set 
  385.          * methods are type int based
  386.          */
  387.         public static final PropertyInfo drawingMode 
  388.             = new PropertyInfo( SimpleControl.class
  389.                 , "drawingMode"
  390.                 , DrawingMode.class   //* NOTE enum class not int
  391.                 , CategoryAttribute.Behavior
  392.                 , new DescriptionAttribute("A flag to control the drawing mode - values from an enum")
  393.                 );
  394.  
  395.         /*
  396.          * NOTE: Custom Property - the property class is set to the 
  397.          * property class NOT an primitive type 
  398.          */
  399.         public static final PropertyInfo myOwnType 
  400.             = new PropertyInfo( SimpleControl.class
  401.                 , "myOwnType"
  402.                 , MyOwnType.class 
  403.                 , CategoryAttribute.Data
  404.                 , new DescriptionAttribute("A custom property - value should be one of the values defined in MyOwnType")
  405.                 );
  406.  
  407.  
  408.         /*
  409.          * NOTE: Remove the properties and events on Control 
  410.          * that are not appropriate for this control -
  411.          * we do not allow the user to set foreground & 
  412.          * background colors or the font or to handle
  413.          * the double click event
  414.          */
  415.         public static final PropertyInfo backColor = new PropertyInfo(
  416.             Control.ClassInfo.backColor, BrowsableAttribute.NO);
  417.  
  418.         public static final PropertyInfo foreColor = new PropertyInfo(
  419.             Control.ClassInfo.foreColor, BrowsableAttribute.NO);
  420.  
  421.         public static final PropertyInfo font = new PropertyInfo(
  422.             Control.ClassInfo.font, BrowsableAttribute.NO);
  423.  
  424.         public static final PropertyInfo text = new PropertyInfo(
  425.             Control.ClassInfo.text, BrowsableAttribute.NO);
  426.  
  427.         public static final EventInfo doubleClick = new EventInfo(
  428.             Control.ClassInfo.doubleClick, BrowsableAttribute.NO);
  429.  
  430.         /**
  431.          * Answer the set of events that this control responds to 
  432.          * - this will be the events that we trigger and any our 
  433.          * super classes trigger
  434.          * 
  435.          * @param events    The list of events for this control
  436.          */
  437.         public void getEvents(IEvents events) {
  438.             super.getEvents(events);
  439.             events.add(numberDataChanged);
  440.             events.add(doubleClick);
  441.         }
  442.  
  443.         /**
  444.          * Answer the set of properties for this control
  445.          * - this includes our parent class properties
  446.          * 
  447.          * @param properties    The list of properties for this control
  448.          */
  449.         public void getProperties(IProperties props) {
  450.             super.getProperties(props);
  451.             props.add(drawingMode);
  452.             props.add(numberData);
  453.             props.add(stringData);
  454.             props.add(readOnlyData);
  455.             props.add(myOwnType);
  456.             props.add(backColor);
  457.             props.add(foreColor);
  458.             props.add(font);
  459.             props.add(text);
  460.         }
  461.     }
  462. }
  463.