home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-05-04 | 14.1 KB | 463 lines |
- package SimpComp ;
- import com.ms.wfc.core.*;
- import com.ms.wfc.ui.*;
- import com.ms.lang.Delegate;
-
- /**
- * SimpleControl
- *
- * SimpleControl demonstrates how to build a simple control and how to
- * declare its properties and events so that they can be displayed and
- * manipulated in the Properties Window in the WFC Designer.
- *
- * The control includes a simple custom property and a property editor
- * for that property.
- *
- * The control hides those properties on com.ms.wfc.ui.Control that are not
- * appropriate
- *
- * If the control was forms based then it would derive from UserControl
- * rather than Control.
- *
- * The properties and events for controls can be built using the WFC
- * Component Builder
- *
- * The entry point for class execution is a default constructor.
- *
- * This class can be used as an ActiveX control. Check the checkbox
- * for this class on the Project Properties COM Classes tab, or
- * remove the // from the line:
- *
- // * @com.register ( clsid=39DF5AA0-D3B1-11D1-8C52-00C04F8F3341, typelib=39DF5AA1-D3B1-11D1-8C52-00C04F8F3341 )
- * @com.register ( clsid=2148925C-234F-11D2-8C4E-00C04F8F3341, typelib=2148925B-234F-11D2-8C4E-00C04F8F3341 )
- */
- public class SimpleControl extends Control {
-
- /* Private data
- */
-
- // The event handler which fires when numberData changes
- private EventHandler m_NumberDataChanged ;
-
- // A read-only field derived from numberData
- private double m_ReadOnlyData = 0.5 ;
-
- // The string data held and displayed by the control
- private String m_StringData = "Default Data" ;
-
- // The numeric data held and displayed by the control
- private short m_NumberData = 1 ;
-
- /*
- * A flag to control the way the contents of the control are drawn.
- * This contents of this property are set via an enum rather than
- * being directly edited by the user - see property declaration in
- * the ClassInfo below
- */
- private int m_DrawingMode = DrawingMode.CALM ;
-
- // A "custom" property based on a user defined class
- private MyOwnType m_MyOwnType = MyOwnType.TYPE0 ;
-
- // Utility constants used by painting/invaldating the control
- private static final int NUMBER_OF_DATA_PROPS = 4 ;
- private static final int STRING_YPOS = 0 ;
- private static final int NUM_YPOS = 1 ;
- private static final int RO_YPOS = 2 ;
- private static final int MOT_YPOS = 3 ;
-
-
- /**
- * Creates a new SimpleControl
- *
- * Controls must have a default constructor
- */
- public SimpleControl() {
-
- super();
-
- /*
- * Control sets the style to STYLE_USERPAINT | STYLE_STDCLICK
- * so we will add STYLE_RESIZE_REDRAW to make sure we get
- * redraw on resize
- */
- setStyle(STYLE_RESIZE_REDRAW, true) ;
- }
-
-
- /* Events
- */
-
- /**
- * Adds an event handler for the "numberDataChanged" event
- * notification. This event is fired whenever the value of
- * numberData changes
- *
- * @param value New Event Handler to install for this event.
- */
- public void addOnNumberDataChanged(EventHandler value) {
- m_NumberDataChanged = (EventHandler)Delegate.combine(m_NumberDataChanged, value);
- }
-
- /**
- * Fires the event indicating that the numberData property has
- * been changed.
- *
- * Inheriting controls should use this in favour of actually
- * listening to the event, but should not forget to call
- * super.onCheckedChanged() to ensure that the event is still
- * fired for external listeners.
- *
- * @param e Event to send
- */
- protected void onNumberDataChanged(Event event) {
- if (m_NumberDataChanged != null) m_NumberDataChanged.invoke(this, event);
- }
-
- /**
- * Removes the given handler for the "numberDataChanged" event.
- * If there are duplicate entries, ALL are removed.
- *
- * @param value Event handler to be removed.
- */
- public void removeOnNumberDataChanged(EventHandler value) {
- m_NumberDataChanged = (EventHandler)Delegate.remove(m_NumberDataChanged, value);
- }
-
- /* Property Get/Set Methods
- */
-
- /**
- * Get the flag that controls how the data values in the control
- * are rendered to the screen
- *
- * @return int A value from the control.DrawingMode enumeration
- * which indicates how the control is rendered to the
- * screen
- *
- * @see control.DrawingMode
- */
- public int getDrawingMode() {
- return m_DrawingMode;
- }
-
- /**
- * Get the custom property
- *
- * @return <b>MyOwnType</b> the value of the custom property
- *
- * @see control.MyOwnType
- */
- public MyOwnType getMyOwnType() {
- return m_MyOwnType;
- }
-
- /**
- * Answer the numeric data stored in the control
- *
- * @return <b>short</b> the numeric data property
- */
- public short getNumberData() {
- return m_NumberData;
- }
-
- /**
- * Answer the readonly data derived from numberData
- *
- * NOTE: ReadOnly Property so no "set" method
- *
- * @return <b>double</b> based on numberData
- */
- public double getReadOnlyData() {
- return m_ReadOnlyData;
- }
-
- /**
- * Answer the string data stored in the control
- *
- * @return <b>String</b> the value of the StringData property
- */
- public String getStringData() {
- return m_StringData;
- }
-
-
- /**
- * Set the flag that controls how the data values in the control
- * are rendered to the screen
- *
- * @param value A value from the control.DrawingMode enumeration
- * controlling how the control is rendered to the
- * screen
- *
- * @see control.DrawingMode
- */
-
- public void setDrawingMode(int value) {
-
- /*
- * Verify that the value is one of the Drawing Mode enum values
- * if not do not change the value
- */
- if (DrawingMode.valid(value)) {
- m_DrawingMode = value;
- invalidate() ;
- }
- }
-
-
- /**
- * Set the custom property
- *
- * @param value An instance of MyOwnType
- *
- * @see control.MyOwnType
- */
- public void setMyOwnType(MyOwnType value) {
- m_MyOwnType = value;
- invalidateItemRect(MOT_YPOS) ;
- }
-
-
- /**
- * Set the numeric data stored in the control
- *
- * Whenever this value is changed the "numberDataChanged" event
- * will be fired
- *
- * @param value The new numeric (short) data for the control
- */
- public void setNumberData(short value) {
- m_NumberData = value;
- m_ReadOnlyData = (double)value / 2 ;
- invalidateItemRect(NUM_YPOS) ;
- invalidateItemRect(RO_YPOS) ;
- onNumberDataChanged(Event.EMPTY);
- }
-
-
- /**
- * Set the string data stored in the control
- *
- * @param value The new string data for the control
- */
- public void setStringData(String value) {
- m_StringData = value;
- invalidateItemRect(STRING_YPOS) ;
- }
-
-
- /* Methods
- */
-
- /**
- * Override onPaint from control so that we can paint the contents
- * of the control
- */
- protected void onPaint(PaintEvent e) {
-
- super.onPaint(e) ;
-
- Color bColor = Color.BLACK,
- fColor = Color.WHITE ;
-
- int itemHeight = getItemHeight() ;
-
- /*
- * As this is a simple control simply set the font
- * rather than giving the user the opportunity to
- * set it
- */
- Font controlFont = new Font( "Arial", itemHeight - 1
- , FontSize.CELLHEIGHT
- , FontWeight.BOLD
- , false
- , false
- , false
- );
-
- /*
- * Work out background & foreground colors based on the
- * drawing mode
- */
- switch (m_DrawingMode) {
-
- case DrawingMode.AWKWARD:
- bColor = Color.YELLOW ;
- fColor = Color.BLACK ;
- break ;
-
- case DrawingMode.ANGRY:
- bColor = Color.RED ;
- fColor = Color.BLACK ;
- break ;
-
- default:
- bColor = Color.BLACK ;
- fColor = Color.WHITE ;
- }
-
-
- // Set colors & font
- e.graphics.setBackColor(bColor);
- e.graphics.setTextColor(fColor);
- e.graphics.setFont(controlFont);
-
- // Draw the contents of the control
- e.graphics.clearRect(e.clipRect) ;
- e.graphics.drawString(m_StringData, 5, STRING_YPOS * itemHeight) ;
- e.graphics.drawString((new Short(m_NumberData)).toString(), 5, NUM_YPOS * itemHeight) ;
- e.graphics.drawString((new Double(m_ReadOnlyData)).toString(), 5, RO_YPOS * itemHeight) ;
- e.graphics.drawString(m_MyOwnType.toString(), 5, MOT_YPOS * itemHeight) ;
-
- }
-
- /**
- * @internalonly
- *
- * Answer the height of each item in the control
- */
- private final int getItemHeight() {
- return getClientRect().height/NUMBER_OF_DATA_PROPS ;
- }
-
- /**
- * @internalonly
- *
- * Work out the area to invalidate based on the position of the item being updated
- */
- private final void invalidateItemRect(int itemYPos) {
- int itemHeight = getItemHeight() ;
- Rectangle rect = new Rectangle(0, itemYPos * itemHeight, getClientRect().width, itemHeight) ;
- invalidate(rect) ;
- }
-
- /**
- * SimpleControl.ClassInfo
- *
- * Provides the component meta-information that cannot be
- * determined from the component itself.
- *
- */
- public static class ClassInfo extends Control.ClassInfo {
-
- /* Events
- */
- public static final EventInfo numberDataChanged
- = new EventInfo( SimpleControl.class //Component Class
- , "numberDataChanged" //Event name
- , EventHandler.class //Event class
- //Event Attributes
- , CategoryAttribute.Action
- , new DescriptionAttribute("Fired when the number changes")
- );
-
- /* Properties
- */
- public static final PropertyInfo readOnlyData
- = new PropertyInfo ( SimpleControl.class //Component Class
- , "readOnlyData" //Property name
- , double.class //Property class
- //Property Attributes
- , CategoryAttribute.Data
- , new DescriptionAttribute("A read only property derived from the numberData")
- );
-
- public static final PropertyInfo stringData
- = new PropertyInfo( SimpleControl.class
- , "stringData"
- , String.class
- , CategoryAttribute.Data
- , new DescriptionAttribute("The text contents of the control")
- );
-
- public static final PropertyInfo numberData
- = new PropertyInfo( SimpleControl.class
- , "numberData"
- , short.class
- , CategoryAttribute.Data
- , new DescriptionAttribute("The numeric contents of the control")
- );
-
- /*
- * NOTE: enum Property - the property class is set to the
- * enum class NOT type int even though the get/set
- * methods are type int based
- */
- public static final PropertyInfo drawingMode
- = new PropertyInfo( SimpleControl.class
- , "drawingMode"
- , DrawingMode.class //* NOTE enum class not int
- , CategoryAttribute.Behavior
- , new DescriptionAttribute("A flag to control the drawing mode - values from an enum")
- );
-
- /*
- * NOTE: Custom Property - the property class is set to the
- * property class NOT an primitive type
- */
- public static final PropertyInfo myOwnType
- = new PropertyInfo( SimpleControl.class
- , "myOwnType"
- , MyOwnType.class
- , CategoryAttribute.Data
- , new DescriptionAttribute("A custom property - value should be one of the values defined in MyOwnType")
- );
-
-
- /*
- * NOTE: Remove the properties and events on Control
- * that are not appropriate for this control -
- * we do not allow the user to set foreground &
- * background colors or the font or to handle
- * the double click event
- */
- public static final PropertyInfo backColor = new PropertyInfo(
- Control.ClassInfo.backColor, BrowsableAttribute.NO);
-
- public static final PropertyInfo foreColor = new PropertyInfo(
- Control.ClassInfo.foreColor, BrowsableAttribute.NO);
-
- public static final PropertyInfo font = new PropertyInfo(
- Control.ClassInfo.font, BrowsableAttribute.NO);
-
- public static final PropertyInfo text = new PropertyInfo(
- Control.ClassInfo.text, BrowsableAttribute.NO);
-
- public static final EventInfo doubleClick = new EventInfo(
- Control.ClassInfo.doubleClick, BrowsableAttribute.NO);
-
- /**
- * Answer the set of events that this control responds to
- * - this will be the events that we trigger and any our
- * super classes trigger
- *
- * @param events The list of events for this control
- */
- public void getEvents(IEvents events) {
- super.getEvents(events);
- events.add(numberDataChanged);
- events.add(doubleClick);
- }
-
- /**
- * Answer the set of properties for this control
- * - this includes our parent class properties
- *
- * @param properties The list of properties for this control
- */
- public void getProperties(IProperties props) {
- super.getProperties(props);
- props.add(drawingMode);
- props.add(numberData);
- props.add(stringData);
- props.add(readOnlyData);
- props.add(myOwnType);
- props.add(backColor);
- props.add(foreColor);
- props.add(font);
- props.add(text);
- }
- }
- }
-