Saving GML files

July 09, 2011

As for loading a Gml file, you have two ways to save Gml files.

  • The simple way : the execution of your sketch will hang until the file is saved.
  • The threaded way : won’t block your sketch.

Simple way

import gml4u.brushes.*;
import gml4u.recording.*;
import gml4u.test.*;
import gml4u.utils.*;
import gml4u.drawing.*;
import gml4u.events.*;
import gml4u.model.*;

// Gml object
Gml gml;

void setup() {
	gml = new Gml();
}

void draw() {
	// Add strokes, ... to your Gml object
}

void keyPressed() {
	if (key == 's' || key == 'S') {
		GmlSavingHelper.save(gml, sketchPath+"/gml.xml");
	}
}

The saving is done through the GMLSavingHelper’s save() static method.
This method has two arguments:
– The Gml object to be saved
– The path where is should be saved

Threaded way

import gml4u.brushes.*;
import gml4u.recording.*;
import gml4u.test.*;
import gml4u.utils.*;
import gml4u.drawing.*;
import gml4u.events.*;
import gml4u.model.*;

// Declare a Gml object and GmlParser
Gml gml;
GmlSaver saver;

void setup() {
	// Create and start the thread with a 500ms waiting time, a name and a reference to your sketch (this)
	saver = new GmlSaver(500, "mySaver", this);
	saver.start();
}

void draw() {
	// Add strokes, ... to your Gml object
}

void keyPressed() {
	if (key == 's' || key == 'S') {
		saver.save(gml, sketchPath+"/gml.xml");
	}
}

// Callback function used by the GmlSaver to send the result of the saving
public void gmlEvent(GmlEvent event) {
	if (event instanceof GmlSavingEvent) {
		GmlSavingEvent savingEvent = (GmlSavingEvent) event;
		boolean result = savingEvent.successful;
		String location = savingEvent.location;
		println((result ? "Succeeded" : "Failed")+ " saving Gml file to " + location);
	}
}

Explainations
When the GmlSaver is created, il will try to find a public gmlEvent method with a GmlEvent argument.
If missing or incorrect, depending on your log level (something that we will see later on), the GmlSaver will log an error and nothing more will happen.
This gmlEvent method will be called at the end of the saving.
This method takes a generic argument, which is the GmlEvent class.
This is generic to keep a single entry point and avoid the need to have as many callbacks as event types.
To know which event type was returned, you have to test it using the instanceof comparator against one of the expected classes extending GmlEvent.
Here, we’re expecting a GmlSaving event, so we test if this is correct and create a copy of it to get the results.

if (event instanceof GmlParsingEvent) {
	if (event instanceof GmlSavingEvent) {
		GmlSavingEvent savingEvent = event;
		boolean result = savingEvent.successful;
		String location = savingEvent.location;
		println((result ? "Succeeded" : "Failed") + " saving Gml file to " + location);
	}
}

As seen before for the GmlParsingEvent, this can also be achieved by casting the GmlEvent to a GmlSavingEvent

if (event instanceof GmlSavingEvent) {
	boolean result = ((GmlSavingEvent) event).successful;
	String location = ((GmlSavingEvent) event).location;
	println((result ? "Succeeded" : "Failed")+ " saving Gml file to " + location);
}

| Tags: , | More: Tutorials, Updates