Is there a way to override the windowClosing function?
I did say no originally but I have managed to find a work arround
The solution to this particular problem (without changing the library code) would be to create a class that extends GWindow and in that class create an inner class that extends java.event.WindowAdapter. This inner class would then be modified to call a method immediately before the window is closed.
So in your sketch create a new tab called GWindowPlus.java note the extension because this will NOT be an inner class and making it a seperate class is better than the alternative - a
static inner class. In this tab copy and paste the following code.
GWindowPlus.java
- import g4p_controls.*;
- import processing.core.*;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.lang.reflect.Method;
- public class GWindowPlus extends GWindow {
- public Method callback = null;
- public Object callbackObj = null;
- public GWindowPlus(PApplet theApplet, String name, int x, int y, int w, int h, boolean noFrame, String mode) {
- super(theApplet, name, x, y, w, h, noFrame, mode);
- removeWindowListener(winAdapt);
- setActionOnClose(CLOSE_WINDOW);
- }
- public GWindowPlus(PApplet theApplet, String name, int x, int y, PImage image, boolean noFrame, String mode) {
- super(theApplet, name, x, y, image, noFrame, mode);
- removeWindowListener(winAdapt);
- setActionOnClose(CLOSE_WINDOW);
- }
- public void setCloseCallMethod(Object obj, String methodName) {
- try {
- callback = obj.getClass().getMethod(methodName, new Class<?>[] { GWindow.class } );
- callbackObj = obj;
- }
- catch (Exception e) {
- PApplet.println("Unable to find method" + methodName);
- }
- }
- public void setActionOnClose(int action) {
- switch(action) {
- case KEEP_OPEN:
- removeWindowListener(winAdapt);
- winAdapt = null;
- actionOnClose = action;
- break;
- case CLOSE_WINDOW:
- case EXIT_APP:
- if (winAdapt == null) {
- winAdapt = new GWindowAdapterPlus(this);
- addWindowListener(winAdapt);
- } // end if
- actionOnClose = action;
- break;
- } // end switch
- }
-
- public class GWindowAdapterPlus extends WindowAdapter {
- GWindow window;
- public GWindowAdapterPlus(GWindow window) {
- this.window = window;
- }
- public void windowClosing(WindowEvent evt) {
- switch(actionOnClose) {
- case CLOSE_WINDOW:
- window.papplet.noLoop();
- if (callback != null) {
- try {
- callback.invoke(callbackObj, window);
- }
- catch (Exception e) {
- PApplet.println("ooops \n" + e.getStackTrace());
- }
- }
- G4P.windowCloser.addWindow(window);
- break;
- case EXIT_APP:
- System.exit(0);
- break;
- }
- }
- }
- }
An example sketch to demonstrate the new class.
Main tab
- import g4p_controls.*;
- GWindowPlus wp;
- public void setup(){
- size(480, 320);
- wp = new GWindowPlus(this, "My window", 0, 0, 240, 120, false, JAVA2D);
- wp.addDrawHandler(this, "win_draw1");
- // Define the method tyo call on closing the winodw
- wp.setCloseCallMethod(this, "windowClose");
- }
- public void draw(){
- background(230);
-
- }
- // This method will be executed immediately before
- // the window closes
- public void windowClose(GWindow window){
- println("Window closed");
- }
- synchronized public void win_draw1(GWinApplet appc, GWinData data) {
- appc.background(230);
- }