Java Q&A
by Andrew Wilson


Example 1:

public class Foo
{
private int exitVal;
public WindowAdapter createAdapter()
{
	WindowAdapter a = new WindowAdapter() 
	{ /* Start of anonymous class definition */
		// anonymous class derived from WindowAdapter
		public void windowClosing()
{
	exitVal = 1;
}
	}; /* End of anonymous class definition */
	return a;
	}
}


Listing One
Button sortButton = new Button();
private void initForm()
{
        sortButton.setDock(ControlDock.BOTTOM);
        sortButton.setLocation(new Point(0, 277));
        sortButton.setSize(new Point(300, 23));
        sortButton.setTabIndex(0);
        sortButton.setText("Sort");
        sortButton.addOnClick(new EventHandler(this.sortClick));
        /*Download complete sample for full code */       
}
private void sortClick(Object source, Event e)
{
        m_pDlg = new ProgressDialog();
        m_pDlg.show();
        m_pDlg.progressBar.setMaximum( m_items.length );
        
        m_items = m_app.sortItems( m_items );
        
        showItems();
}

Listing Two
public delegate 
void WFCSortEventDelegate ( Object sender, WFCSortEvent e ); 

Listing Three
package WFCExtensions;

import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.lang.Delegate;
public class WFCApp extends App
{
    WFCSortEventDelegate m_SortCompletionEvent;
    WFCSortEventDelegate m_SortProgressEvent;
    public WFCApp ( int items )
    {
        super ( items );
    }
    public void addOnSortCompletionEvent(WFCSortEventDelegate value)
    {
        m_SortCompletionEvent = (WFCSortEventDelegate)Delegate.
                                 combine(m_SortCompletionEvent, value);
    }
    public void addOnSortProgressEvent(WFCSortEventDelegate value)
    {
        m_SortProgressEvent = (WFCSortEventDelegate)Delegate.
                                 combine(m_SortProgressEvent, value);
    }
    protected void onSortCompletionEvent(WFCSortEvent event)
    {
        if (m_SortCompletionEvent != null) m_SortCompletionEvent.
                                                 invoke(this, event);
    }
    protected void onSortProgressEvent(WFCSortEvent event)
    {
        if (m_SortProgressEvent != null) m_SortProgressEvent.
                                                 invoke(this, event);
    }
    public void removeOnSortCompletionEvent(WFCSortEventDelegate value)
    {
        m_SortCompletionEvent = (WFCSortEventDelegate)Delegate.
                                   remove(m_SortCompletionEvent, value);
    }
    public void removeOnSortProgressEvent(WFCSortEventDelegate value)
    {
        m_SortProgressEvent = (WFCSortEventDelegate)Delegate.
                                    remove(m_SortProgressEvent, value);
    }
    protected void sortCompleteEvent( )
    {
        WFCSortEvent e = new WFCSortEvent();
        onSortCompletionEvent( e );
    }
    protected void sortProgressEvent( int position, int total )
    {
        WFCSortEvent e = new WFCSortEvent ( position, total );
        onSortProgressEvent( e );
    }
    public static class ClassInfo extends com.ms.wfc.core.ClassInfo
    {
        public static final EventInfo sortCompletionEvent = new EventInfo(
            WFCApp.class, "sortCompletionEvent", EventHandler.class);
        public static final EventInfo sortProgressEvent = new EventInfo(
            WFCApp.class, "sortProgressEvent", EventHandler.class);
        public void getEvents(IEvents events)
        {
            super.getEvents(events);
            events.add(sortProgressEvent);
            events.add(sortCompletionEvent);
        }
        public void getProperties(IProperties props)
        {
            super.getProperties(props);
        }
    }
}

Listing Four
import java.util.*;

public abstract class App
{
    protected int[] m_items;
    public App(int items)
    {
        m_items = new int[ items ];
        fillItems( m_items );
    }
    protected void fillItems( int[] items )
    {
        Random rnd = new Random();
        for ( int i = 0; i < items.length; i++ )
        {
            int tmp;
            do 
            {
                tmp = rnd.nextInt(); 
            } while ( tmp < 0 );
            m_items[i] = tmp; 
        }
    }
    public int[] sortItems( int[] items )
    {
        int[] sortedList = new int[ items.length ];
        System.arraycopy( m_items, 0, sortedList, 0, m_items.length );
        for ( int i = 0; i < sortedList.length; i++ )
        {
            for( int j = i; j < sortedList.length; j++ )
            {
                if (  sortedList[i] > sortedList[j] )
                {
                    int tmp = sortedList[j];
                    sortedList[j] = sortedList[i];
                    sortedList[i] = tmp;
                }
            }
           sortProgressEvent( i, m_items.length );
        }
        sortCompleteEvent();
        return sortedList;
    }
    public int[] getItems( )
    {
        return m_items;
    }
    protected abstract void sortCompleteEvent( );
    protected abstract void sortProgressEvent( int position, int total );
}

Listing Five
public class Example1 extends Form
{
    WFCApp m_app;
    public Example1()
    {
        super();
        m_app = new WFCApp ( 2500 );
        m_items = m_app.getItems();
        m_sortedItems = m_app.sortItems( m_items );
        
        // bind custom delegate classes to WFCApp class
        m_app.addOnSortCompletionEvent( 
                   new WFCSortEventDelegate( this.sortCompletionEvent ) );
        m_app.addOnSortProgressEvent( 
                   new WFCSortEventDelegate( this.sortProgressEvent ) );
        initForm(); 
    }
}

Listing Six
public class Example2 extends Frame
{
    public void initForm ( )
    {
    /** Download complete source for full example */
    
        this.addWindowListener( 
            new WindowDelegate( this,
            "windowClosing",
            WindowDelegate.CLOSING ) ); 
    }
    public void windowClosing ( WindowEvent e )
    {
        System.exit(1);
    }
}

5


