51
EVENT PROCESSING MODEL IN C# AND JAVA

Java event processing model in c# and java

  • Upload
    techmx

  • View
    506

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Java  event processing model in c# and java

EVENT PROCESSING MODEL

IN C# AND JAVA

Page 2: Java  event processing model in c# and java

Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create Each Type Of Component

Page 3: Java  event processing model in c# and java

Introduction C# is a modern programming language supported

by an extensive set of API structures and classes.i.e., The .Net Framework

C# supports event-driven programming normally associated with Microsoft Windows applications.

One normally thinks of events as being generated by GUI components…but any object can generate an “event” if it’s

programmed to do so…

Page 4: Java  event processing model in c# and java

C# Event Processing Macro View Generally speaking, two logical components are

required to implement the event processing model:1) An event producer (or publisher)2) An event consumer (or subscriber)

Each logical components has assigned responsibilities

Consider the following diagram

Page 5: Java  event processing model in c# and java

C# Event Processing Macro View

Object A (Event Publisher)

Object B(Event Subscriber)

When an Event occurs notification is sent to all the subscribers on the list for that particular event…

Object B processes the event notification in itsevent handler code

Subscriber List(Object B)

Event Handler Code

Object A maintains a list of subscribers for

each publishable event

Object B subscribes to event (or events) generated by Object A.

Page 6: Java  event processing model in c# and java

C# Event Processing Macro View

Button (Event

Publisher)

When a Click event occurs notification is sent to all the subscribers on the list…

Button maintains a list of subscribers of

itsClick event

MainApp(Event Subscriber)

Subscriber List(MainApp.onButtonClick) onButtonClick

Page 7: Java  event processing model in c# and java

C# Event Processing Macro View

These two diagrams hide a lot of detailsHow is the subscriber list maintained?How is the event generated?How is notification sent to each subscriber?What is an event – really?How can you add custom event processing to

your programs? This presentation attempts to answer these

questions in a clear manner…

Page 8: Java  event processing model in c# and java

C# Event Processing Macro View

The .Net API contains lots of classes that generate different types of events…Most are GUI related

○ Can you think of a few? It also contains lots of Delegates

Can you name at least one? Let’s take a closer look at the C# event

processing model

Page 9: Java  event processing model in c# and java

C# Event Processing Macro View

To implement custom event processing in your programs you need to understand how to create the following component types:DelegatesEvent Generating Objects (publishers)

○ Events○ Event Notification Methods

Event Handling Objects (subscribers)○ Event Handler Methods

Page 10: Java  event processing model in c# and java

Required Components To implement custom event processing in your

programs you need to understand how to create the following component types: Delegates Event Generating Objects (publishers)

○ Events○ Event Notification Methods

Event Handling Objects (subscribers)○ Event Handler Methods

Page 11: Java  event processing model in c# and java

Role of Each Component- Delegate -

DelegateDelegate types represent references to methods

with a particular parameter list and return type○ Example

EventHandler(Object sender, EventArgs e) Represents a method that has two parameters, the

first one being of type Object and the second being of type EventArgs. Its return type is void.

Any method, so long as its signature matches that expected by the delegate, can be handled by the delegate.

Page 12: Java  event processing model in c# and java

Role of Each Component- Delegate -

But just what is a delegate?A delegate is a reference type object. A delegate extends either the System.Delegate

or MulticastDelegate class○ Depends on whether one (Delegate) or more

(MulticaseDelegate) subscribers are involved

You do not extend Delegate or MulticastDelegate○ The C# compiler does it for you

Page 13: Java  event processing model in c# and java

Role of Each Component- Delegate -

The delegate object contains the subscriber list.It is actually implemented as a linked list where

each node of the list contains a pointer to a subscriber’s event handler method

Delegates are types – like classesExcept – you declare them with the delegate

keyword and specify the types of methods they can reference

Page 14: Java  event processing model in c# and java

Role of Each Component- Publisher -

A publisher is any class that can fire an event and send event notifications to interested subscribers

A publisher class contains the following critical elements:An event field

○ This is what subscribers subscribe to…An event notification method

○ This activates the subscriber notification process when the event occurs

Page 15: Java  event processing model in c# and java

Role of Each Component- Subscriber -

A subscriber is a class that registers its interest in a publisher’s events

A subscriber class contains one or more event handler methods

- Event Handler Method – An event handler methods is an ordinary method

that is registered with a publisher’s event. The event handler method’s signature must match

the signature required by the publisher’s event delegate.

Page 16: Java  event processing model in c# and java

Role of Each Component- Event -

An event is a field in a class Events are declared with the event keyword Events must be a delegate type

Delegates, remember, are objects that contain a list of pointers to subscriber methods that delegate can process

An event field will be null until the first subscriber subscribes to that event

Page 17: Java  event processing model in c# and java

Role of Each Component- Event Notification Method - In addition to an event field a publisher will have a

method whose job is to start the subscriber notification process when the event occurs An event notification method is just a normal method It usually has a parameter of EventArgs or a user-defined

subtype of EventArgs.○ But it can have any number and type of parameters as

required.

Page 18: Java  event processing model in c# and java

An Custom Event Example- Elapsed Minute Timer - This example includes five separate source files:

Delegate.cs Publisher.cs Subscriber.cs MinuteEventArgs.cs MainApp.cs

Page 19: Java  event processing model in c# and java

using System;

namespace CustomEventExample {

public delegate void ElapsedMinuteEventHandler(Object sender, MinuteEventArgs e);

} // end CustomEventExample namespace

using System;

namespace CustomEventExample {

public class MinuteEventArgs : EventArgs {

private DateTime date_time;

public MinuteEventArgs(DateTime date_time){

this.date_time = date_time;

}

public int Minute {

get { return date_time.Minute; }

}

}

}

Page 20: Java  event processing model in c# and java

using System;

namespace CustomEventExample { public class Publisher { public event ElapsedMinuteEventHandler MinuteTick; public Publisher(){ Console.WriteLine("Publisher Created"); }

Page 21: Java  event processing model in c# and java

public void countMinutes(){

int current_minute = DateTime.Now.Minute;

while(true){

if(current_minute != DateTime.Now.Minute){

Console.WriteLine("Publisher: {0}", DateTime.Now.Minute);

onMinuteTick(new MinuteEventArgs(DateTime.Now));

current_minute = DateTime.Now.Minute;

}//end if

} // end while

} // end countMinutes method

public void onMinuteTick(MinuteEventArgs e){

if(MinuteTick != null){

MinuteTick(this, e);

}

}// end onMinuteTick method

} // end Publisher class definition

} // end CustomEventExample namespace

Page 22: Java  event processing model in c# and java

using System;

namespace CustomEventExample {

public class Subscriber {

private Publisher publisher;

public Subscriber(Publisher publisher){

this.publisher = publisher;

subscribeToPublisher();

Console.WriteLine("Subscriber Created");

}

public void subscribeToPublisher(){

publisher.MinuteTick += new ElapsedMinuteEventHandler(minuteTickHandler);

}

public void minuteTickHandler(Object sender, MinuteEventArgs e){

Console.WriteLine("Subscriber Handler Method: {0}", e.Minute);

}

} // end Subscriber class definition

} // end CustomEventExample namespace

Page 23: Java  event processing model in c# and java

using System;

namespace CustomEventExample {

public class MainApp {

public static void Main(){

Console.WriteLine("Custom Events are Cool!");

Publisher p = new Publisher();

Subscriber s = new Subscriber(p);

p.countMinutes();

} // end main

} //end MainApp class definition

} // end CustomEventExample namespace

Page 24: Java  event processing model in c# and java

OUTPUT

Page 25: Java  event processing model in c# and java

EVENT PROCESSING

MODEL IN JAVA

Page 26: Java  event processing model in c# and java

AGENDA

The Delegation Event model Event Listeners Event Classes Event Listener Interfaces Anonymous Inner Classes Examples

Page 27: Java  event processing model in c# and java

The Delegation Event Model

The concept of Delegation Event model is

A source generates an event and sends it to one or more listeners.

The listener simply waits until it receives an event. Once an event is received, the listener processes events and then returns.

Listeners must register with a source in order to receive an event notification.

Page 28: Java  event processing model in c# and java

Event Handling Model of AWT

Event source• Button• Textbox• Etc.,

Event listener• Methods

Event object• Event handling objects

Event handling methods

Page 29: Java  event processing model in c# and java

Events

o An event is an object that describes a state change in a source.

o Events are generated by

• Pressing a button

• Entering a character via the keyboard

• Selecting an item in a list

• Clicking the mouse

• And so on…………..

Page 30: Java  event processing model in c# and java

Event Sources

o A source is an object that generates an event.o A source must register listeners.

General form

Type - name of the event.

el - reference to the event listener.

public void addTypeListener(TypeListener el) public void removeTypeListener(TypeListener el)

Page 31: Java  event processing model in c# and java

Event Listenerso A listener is an object that is notified when an event

occurs.

o Two Requirements : It must have been registered with one or more sources to

receive notifications. It must implement methods to receive and process these

notifications.

Page 32: Java  event processing model in c# and java

EventObjecto The root of the java event class hierarchy is

EventObject, which is in java.util.o It is the super class of all the events.

Two methods:o getsource() - returns the source of the event.o toString() - returns the string equivalent of

the event.

Page 33: Java  event processing model in c# and java

AWTEvent class

o Defined within the java.awt package.o Subclass of EventObjecto Superclass of all AWT-based events that are handled by

the delegation event model.

Page 34: Java  event processing model in c# and java

Java.awt.eventEvent class Description

Action Event Generated when a button is pressed,a list item is double-clicked, or a menu item is selected.

AdjustmentEvent Generated when a scroll bar is manipulated.

ComponentEvent Generated when a component is hidden, moved,resized, or becomes visible.

ContainerEvent Generated when a component is added to or removed from a container.

FocusEvent Generated when a component gains or loses keyboard focus.

Page 35: Java  event processing model in c# and java

Event class Description

InputEvent Abstract superclass for all component input event classes.

ItemEvent Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected.

KeyEvent Generated when input is received from the keyboard.

MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component.

MousewheelEvent Generated when the mouse wheel is moved.

TextEvent Generated when the value of a text area or text field is changed.

WindowEvent Generated when a window is activated,closed, deactivated, deiconified, iconified, opened or quit.

Page 36: Java  event processing model in c# and java

Event Listener Interfaces The ActionListener Interface

void actionPerformed(ActionEvent ae)

The AdjustmentListener Interface

void adjustmentValueChanged(AdjustmentEvent ae)

The ComponentListener Interface

void componentResized(ComponentEvent ae)

void componentMoved(ComponentEvent ae)

void componentShown(ComponentEvent ae)

void componentHidden(ComponentEvent ae)

Page 37: Java  event processing model in c# and java

The ContainerListener Interface

void componentAdded(ContainerEvent ae)

void componentRemoved(ContainerEvent ae)

The FocusListener Interface

void focusGained(FocusEvent fe)

void focusLost(FocusEvent fe)

The ItemListener Interface

void itemStateChanged(ItemEvent ie)

The KeyListener Interface

void KeyPressed(KeyEvent ke)

void KeyReleased(KeyEvent ke)

void KeyTyped(KeyEvent ke)

Page 38: Java  event processing model in c# and java

The MouseListener Interface

void mouseClicked(MouseEvent me)

void mouseEntered(MouseEvent me)

void mouseExited(MouseEvent me)

void mousePressed(MouseEvent me)

void mouseReleased(MouseEvent me)

The MouseMotionListener Interface

void mouseDragged(MouseEvent me)

void mouseMoved(mouseEvent me)

The MouseWheelListener Interface

void mouseWheelMoved(MouseWheelEvent mwe)

Page 39: Java  event processing model in c# and java

The TextListener Interface

void textChanged(TextEvent te)

The WindowFocusListener Interface

void windowGainedFocus(WindowEvent we)

void windowLostFocus(WindowEvent we)

The WindowListener Interface

void windowActivated(WindowEvent we)

void windowClosed(WindowEvent we)

void windowClosing(WindowEvent we)

void windowDeactivated(WindowEvent we)

void windowDeiconified(WindowEvent we)

void windowIconified(WindowEvent we)

void windowOpened(WindowEvent we)

Page 40: Java  event processing model in c# and java

Anonymous Inner ClassesImport java.applet.*;

Import java.awt.event.*;

Public class AnonymousInnerClassDemo extends Applet

{

public void init()

{

addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent me) {

showStatus(“Mouse Pressed”); }

});

} }

Page 41: Java  event processing model in c# and java

Handling Mouse Events import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class MouseEvents extends Applet

implements MouseListener,MouseMotionListener

{

String msg = “ “;

int mouseX=0, mouseY=0;

public void init()

{

addMouseListener(this);

addMouseMotionListener(this);

}

Page 42: Java  event processing model in c# and java

public void mouseClicked(MouseEvent me)

{

mouseX=0;

mouseY=10;

msg = “Mouse clicked”;

repaint();

}

public void mouseEntered(MouseEvent me)

{

mouseX=0;

mouseY=10;

msg = “Mouse Entered”;

repaint();

}

Page 43: Java  event processing model in c# and java

public void mouseDragged(MouseEvent me)

{

mouseX= me.getX();

mouseY=me.getY();

msg=“ * “;

showStatus(“ Dragging mouse at “+ mouseX + “mouseY” +mouseY);

repaint();

}

public void paint(Graphics g)

{

g.drawString(msg,mouseX,mouseY);

}

}

Page 44: Java  event processing model in c# and java

DIFFERENCES WITH JAVA & C#

1. Differences in terms of syntax of Java and C#

2. Modification of concepts in C# that already exist in Java

3. Language features and concepts that do not exist in Java at all.

Page 45: Java  event processing model in c# and java

Differences in terms of Syntax

Java:public static void main(String[] args)

C#:static void Main(string[] args) string is shorthand for the System.String class in C#.

Another interesting point is that in C#, your Main method can actually be declared to be parameter-less

static void Main()

JAVA MAIN vs C# MAIN

Page 46: Java  event processing model in c# and java

Differences in terms of Syntax

Java:

System.out.println("Hello world!");

PRINT STATEMENTS

C#:System.Console.WriteLine("Hello world!");

or

Console.WriteLine("Hello again!");

Page 47: Java  event processing model in c# and java

Differences in terms of Syntax

DECLARING CONSTANTS

Java: In Java, compile-t ime constant values are declared inside a

class as

static final int K = 100;

C#: To declare constants in C# the const keyword is used for

compile t ime constants while the readonly keyword is used for runtime constants. The semantics of constant primit ives and object references in C# is the same as in Java.

const int K = 100;

Page 48: Java  event processing model in c# and java

Modified concepts from Java

IMPORTING LIBRARIES

Both the langugaes support this functionality and C# follows Java’s technique for importing libraries:

C#: using keywordusing System;

using System.IO; using System.Reflection;

Java: import keywordimport java.util.*; import java.io.*;

Page 49: Java  event processing model in c# and java

EnumerationsJava's lack of enumerated types leads to the use of

integers in situations that do not guarantee type safety.

C# code:public enum Direction {North=1, East=2, West=4, South=8};Usage:

Direction wall = Direction.North;

Java equivalent code will be:public class Direction {

public final static int NORTH = 1; public final static int EAST = 2; public final static int WEST = 3; public final static int SOUTH = 4;

}Usage:

int wall = Direction.NORTH;

Page 50: Java  event processing model in c# and java

Reference The complete Reference, Java2, Fifth edition.

Websites: Java vs. C#: Code to Code Comparison

http://www.javacamp.org/javavscsharp/ A Comparative Overview of C#:

http://genamics.com/developer/csharp_comparative.htm

Page 51: Java  event processing model in c# and java

THANK YOU…….