We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › Adding your own library events
Page Index Toggle Pages: 1
Adding your own library events (Read 3719 times)
Adding your own library events
Jun 11th, 2005, 10:58pm
 
Quote:
ADDING YOUR OWN LIBRARY EVENTS

So that your library can notify the host applet that something
interesting has happened, this is how you implement an event
method in the style of serialEvent, serverEvent, etc.


public class FancyLibrary {
 Method fancyEventMethod;

 public YourLibrary(PApplet parent) {
   // your library init code here...

   // check to see if the host applet implements
   // public void fancyEvent(FancyLibrary f)
   try {
     fancyEventMethod =
       parent.getClass().getMethod("fancyEvent",
                                   new Class[] { FancyLibrary.class });
   } catch (Exception e) {
     // no such method, or an error.. which is fine, just ignore
   }
 }

 // then later, to fire that event
 public void makeEvent() {
   if (fancyEventMethod != null) {
   try {
     fancyEventMethod.invoke(parent, new Object[] { this });
   } catch (Exception e) {
     System.err.println("Disabling fancyEvent() for " + name +
                        " because of an error.");
     e.printStackTrace();
     fancyEventMethod = null;
   }
 }
}

I don't fully understand this example or how to implement it. I want to create a new event that will notify a function in the main code. E.g. you press a button so my libary creates a MyGUIActionEvent which notifies:
void actionPerformed(MyGUIActionEvent e) {
 ...
}
In my main code.

How do I incorporate something similar to the above code with my own class? What is meant by the comment "// you library init code here" - does that refer to the constructor or an init() method?

Kind regards,
- Markavian
Re: Adding your own library events
Reply #1 - Jun 12th, 2005, 12:59pm
 
Ok after some extensive and careful research I have acheived my goal. Below is a working example of creating a Synthetic Event based on this tutorial/glossary:
http://mindprod.com/jgloss/event11.html

Synthetic events:
http://mindprod.com/jgloss/event11.html#SYNTHETICEVENTS

Essentially, instead of creating an event and then using listener interfaces to receive the event I am passing the event directly to the parent where I want to use it.

I opted to use the all encompasing ActionEvent (!an action happened) as a basis for my event type.

The class stores the values required to create the event. Then, when ready to fire you call the eventName.sendEvent(Object parent); method which looks to see if ActionPeformed(ActionEvent e); exists in the specified parent. If it exists, then the method is called and the event object passed over.
In normal usage Object parent will be this passed down from your main code.

My implementation:
Code:
// Method class required to check method exists
import java.lang.reflect.Method;

public class MyGUIActionEvent {
 // values stored in an MyGUIActionEvent
 private String _command;
 private MyGUIObject _source;
 Method actionEventMethod;

 // construct data store for event
 MyGUIActionEvent(MyGUIObject source, String command) {
   // set variables
   this._command = command;
   this._source = source;
 }
 // send event directly to target (listener)
 public void sendEvent(Object parent) {
   // create ActionEvent  
   ActionEvent guiEvent = new ActionEvent(_source, ActionEvent.ACTION_PERFORMED, _command);

   // check to see if the host applet implements
   // public void actionPerformed(ActionEvent e)
   try {
     actionEventMethod = parent.getClass().getMethod("actionPerformed", new Class[] { guiEvent.getClass() });
     actionEventMethod.invoke(parent, new Object[] { guiEvent });
   }
   catch (Exception e) {
     // no such method, or an error.. which is fine, just ignore
     println("No method named actionPerformed was found in parent.");
   }
 }  
}


Sample usage:
Code:

MyGUI gui;
MyGUIButton helloButton;
MyGUIActionEvent guiEvent;
void setup() {
 gui = new MyGUI(this);
 helloButton = new MyGUIButton(40, 20, "hello");
 helloButton.setActionCommand("submit");
 gui.add(helloButton);
}

void draw() {

}

void actionPerformed(ActionEvent e) {
 // Check event based on its actionCommand
 // Adv: several objects can have the same actionCommand
 if(e.getActionCommand == "submit") {
   println("hello button was clicked");
 }
 // Alternatively check event based off the object
 if(e.getSource == helloButton) {
   println("hello button generated this event");
 }  
}

This all works, I've tested it - I'm currently finalising my first ALPHA release of MyGUI and the MyGUIObject adapter, then adding documentation.
Re: Adding your own library events
Reply #2 - Jun 13th, 2005, 2:35am
 
Proper working example: (test release)
http://mkv25.net/applets/MyGUI_a2/
Page Index Toggle Pages: 1