Wiki

Clone wiki

ExperimentTool / C2 Integrating an online-survey into Brownie

Integrating an online-survey into Brownie

A survey in an experiment is often essential to obtain demographic data and experiment feedback. Here we will present one way to integrate an online-survey such as the LimeSurvey (https://www.limesurvey.org/) into Brownie.

  1. You need two additional classes to integrate a survey in your experiment. Two sample classes of the EnglishAuctio are shown at the end of this article: The "EnglishAuctionBrowserScreen" class and the "EnglishAuctionBrowserCanvas" class.

  2. Under your own client package, create two classes of your own, naming them “YourExperimentNameBrowserCanvas” and “YourExperimentNameBrowserScreen”

  3. You can simply take over the code from the BrowserCanvas class, but have to modify the BrowserScreen class in the following way:

    a. Define a subclass of ParamObject in order to define transfer parameters like a String parameter for the link of the survey and a ResponseType parameter.

    b. Modify the LocationListener regarding your own program logic in the run method

    c. Please change the boolean parameter „isTest“ to „false“ if running the real experiment. Set the parameter on “true” in the testing phase in order to avoid hours of clicking through the survey.

    d. In your Institution class of the server package, you can call up the BrowserScreen like all the other Screens, using the method showScreen(subject, screenId, parameter)

#!java

showScreen(
    subject.getIdClient(),
    BeautyContestBrowserScreen.class,
    new BeautyContestBrowserScreen.ParamObject(
                                beautyContestEnvironment.getLimesurveyLink3(),  
                        BeautyContestBrowserScreen.ResponseType.Q3));

The "EnglishAuctionBrowserScreen" class:

#!java

package edu.kit.exp.impl.englishauction.client;

import edu.kit.exp.impl.browserExperiment.client.BrowserCanvas;
import org.eclipse.swt.browser.LocationAdapter;
import org.eclipse.swt.browser.LocationEvent;


public class EnglishAuctionBrowserScreen extends EnglishAuctionScreen {
                private static final long serialVersionUID = -1686302797991338642L;

                public String link= "";
                public ResponseType response = ResponseType.Q1;
                public boolean isTest = true;                                  // change the parameter to "false" if it is not a test


                public EnglishAuctionBrowserScreen(String gameId, ParamObject parameter, String screenId, Long showUpTime) {
                                super(gameId, parameter, screenId, showUpTime);


                                link = parameter.getLink();
                                response = parameter.getResponseType();

                                if(isTest){
                                                //Nur für Testphase
                                                EnglishAuctionScreen.ResponseObject resp = new EnglishAuctionScreen.ResponseObject();
                                                resp.setResponse(response);
                                                guiController.sendClientResponse(resp, getGameId(), getScreenId());  

                                }else{

                                final BrowserCanvas browserCanvas = new BrowserCanvas();
                                mainFrame.add(browserCanvas, 0);

                                browserCanvas.connect();
                                browserCanvas.addNotify();

                                browserCanvas.getBrowser().getDisplay().asyncExec(new Runnable() {
                                                @Override
                                                public void run() {                                            

                                                                                                                                browserCanvas.getBrowser().setUrl(link);
                                                                browserCanvas.getBrowser().addLocationListener(
                                                                                                new LocationAdapter(){
                                                                                                    @Override public void changing(LocationEvent e){

                                                                                                      if (e.location.contains("im.iism.kit.edu")) {
                                                                                                        e.doit=false;
                                                                                                        EnglishAuctionScreen.ResponseObject resp = new EnglishAuctionScreen.ResponseObject();
                                                                                                                                resp.setResponse(response);
                                                                                                                                browserCanvas.removeNotify();
                                                                                                                                browserCanvas.disconnect();
                                                                                                                                guiController.sendClientResponse(resp, getGameId(), getScreenId());                                                                                                                    
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                );
                                                }
                                });
                }
                }
}

The "EnglishAuctionBrowserCanvas" class:

#!java

package edu.kit.exp.impl.englishauction.client;

import java.awt.Canvas;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**

* A simple canvas that encapsulates a SWT Browser instance. Add it to a AWT or

* Swing container and call "connect()" after the container has been made

* visible.

*/

public class EnglishAuctionBrowserCanvas extends Canvas {

                private static final long serialVersionUID = 767435237679519394L;



                private Thread swtThread;

                private Browser swtBrowser;

                public Shell shell;



                /**

                * Connect this canvas to a SWT shell with a Browser component and starts a

                * background thread to handle SWT events. This method waits until the

                * browser component is ready.

                */

                public void connect() {

                                if (this.swtThread == null) {

                                                final Canvas canvas = this;

                                                this.swtThread = new Thread() {

                                                                @Override

                                                                public void run() {

                                                                                try {

                                                                                                Display display = new Display();



                                                                                                shell = SWT_AWT.new_Shell(display, canvas);

                                                                                                shell.setLayout(new FillLayout());



                                                                                                synchronized (this) {

                                                                                                                swtBrowser = new Browser(shell, SWT.NONE);

                                                                                                                this.notifyAll();

                                                                                                }



                                                                                                shell.open();

                                                                                                while (!isInterrupted() && !shell.isDisposed()) {

                                                                                                                if (!display.readAndDispatch()) {

                                                                                                                                display.sleep();

                                                                                                                }

                                                                                                }

                                                                                                shell.dispose();

                                                                                                display.dispose();

                                                                                } catch (Exception e) {

                                                                                                interrupt();

                                                                                }

                                                                }

                                                };

                                                this.swtThread.start();

                                }



                                // Wait for the Browser instance to become ready

                                synchronized (this.swtThread) {

                                                while (this.swtBrowser == null) {

                                                                try {

                                                                                this.swtThread.wait(100);

                                                                } catch (InterruptedException e) {

                                                                                this.swtBrowser = null;

                                                                                this.swtThread = null;

                                                                                break;

                                                                }

                                                }

                                }

                }



                /**

                * Returns the Browser instance. Will return "null" before "connect()" or

                * after "disconnect()" has been called.

                */

                public Browser getBrowser() {

                                return this.swtBrowser;

                }



                /**

                * Stops the swt background thread.

                */

                public void disconnect() {

                                if (swtThread != null) {

                                                swtBrowser = null;

                                                swtThread.interrupt();

                                                swtThread = null;

                                                shell.setVisible(false);



                                }

                }



                /**

                * Ensures that the SWT background thread is stopped if this canvas is

                * removed from it's parent component (e.g. because the frame has been

                * disposed).

                */

                @Override

                public void removeNotify() {

                                super.removeNotify();

                                disconnect();

                }
}

Updated