Click here to Skip to main content
15,895,084 members
Articles / Mobile Apps / Android

A Domain Specific Language for Android touch events: Part 1: Description and usage of the DSL

Rate me:
Please Sign up or sign in to vote.
4.98/5 (14 votes)
6 Dec 2013CPOL9 min read 46.9K   467   31  
A DSL for creating touch gestures in Android.
package com.hfk.android.gestures;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import android.graphics.Point;

public class TouchGesture implements IResetable  {

	public TouchGesture(String id) 
	{
		this.id = id;
	}
	
	public String getId()
	{
		return id;
	}
	
	public void addEvent(TouchEvent event)
	{
		TouchEventExecution eventExecution = new TouchEventExecution();
		eventExecution.touchEvent = event;
		eventExecution.isExecuted = false;
		eventList.add(eventExecution);
	}
	
	public TouchEvent getEvent(int index)
	{
		return eventList.get(index).touchEvent;
	}
	
	public int size()
	{
		return eventList.size();
	}
	
	public void reset()
	{		
		for(IResetable resetable:resetableList)
		{
			resetable.reset();
		}
		
		if(onResetAction != null)
		{
			onResetAction.executeAction(null, this);
		}
		
		isValid = true;
		index = 0;
		context = new Hashtable<String, Object>();
	}
	
	public void addResetable(IResetable resetable)
	{
		resetableList.add(resetable);
	}
	
	public void setResetAction(IGestureAction action)
	{
		onResetAction = action;
	}
	
	public void invalidate()
	{
		if(contextExists("TIMER_INSTALLED"))
		{
			TouchHandler handler = (TouchHandler)getContext("TIMER_INSTALLED");
			handler.invalidateTimer();
			removeContext("TIMER_INSTALLED");
		}
		isValid = false;
	}
	
	public boolean isValid()
	{
		return ((index < eventList.size()) && isValid);
	}
	
	public TouchEvent current()
	{
		return eventList.get(index).touchEvent;
	}
	
	public boolean isCurrentExecuted()
	{
		return eventList.get(index).isExecuted;
	}
	
	public void currentIsExecuted()
	{
		eventList.get(index).isExecuted = true;
	}
	
	public void moveNext()
	{
		index++;
	}
	
	public boolean isCompleted()
	{
		if(!isValid)
			return false;
		
		return (index >= eventList.size());
	}
	
	public void setAllEventsProcessed()
	{
		index = eventList.size();
	}
	
	public boolean contextExists(String key)
	{
		return context.containsKey(key);
	}
	
	public void addContext(String key, Object data)
	{
		context.put(key, data);
	}
	
	public void setContext(String key, Object data)
	{
		context.put(key, data);
	}
	
	public void removeContext(String key)
	{
		context.remove(key);
	}
	
	public Object getContext(String key)
	{
		return context.get(key);
	}

	private String id;
	private List<TouchEventExecution> eventList = new ArrayList<TouchEventExecution>();	
	private List<IResetable> resetableList = new ArrayList<IResetable>();	
	private IGestureAction onResetAction = null;
	private boolean isValid = true;
	private int index = 0;
	
	private Runnable timerAction;
	
	private Hashtable<String, Object> context = new Hashtable<String, Object>();
	
	private class TouchEventExecution
	{
		TouchEvent touchEvent;
		boolean isExecuted;
	}
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions