summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Kropf <josh@slashdev.ca>2007-01-31 04:40:42 (GMT)
committerJosh Kropf <josh@slashdev.ca>2007-01-31 04:40:42 (GMT)
commit66eec2cf2d7508fb1ce5a85dd59f396c89ff19f8 (patch)
tree6a27db33245a0564e6534475124c1774ddb6a62e
parent447cc346843decd4375606f4d415b9d39ed6759e (diff)
downloadbb-bblib-66eec2cf2d7508fb1ce5a85dd59f396c89ff19f8.zip
bb-bblib-66eec2cf2d7508fb1ce5a85dd59f396c89ff19f8.tar.gz
bb-bblib-66eec2cf2d7508fb1ce5a85dd59f396c89ff19f8.tar.bz2
Initial import
-rw-r--r--.classpath6
-rw-r--r--.cvsignore2
-rw-r--r--.project17
-rw-r--r--build.xml20
-rw-r--r--rimjde/SDevBBFramework.jdp58
-rw-r--r--src/com/jiggak/mvc/AbstractController.java61
-rw-r--r--src/com/jiggak/mvc/AbstractModel.java52
-rw-r--r--src/com/jiggak/mvc/Controller.java19
-rw-r--r--src/com/jiggak/mvc/Model.java18
-rw-r--r--src/com/jiggak/mvc/ModelEvent.java32
-rw-r--r--src/com/jiggak/mvc/ModelListener.java15
-rw-r--r--src/com/jiggak/mvc/View.java17
-rw-r--r--src/com/jiggak/rim/Application.java46
-rw-r--r--src/com/jiggak/rim/ApplicationCallback.java15
-rw-r--r--src/com/jiggak/rim/ezmenu/EasyMenuScreenView.java111
-rw-r--r--src/com/jiggak/rim/ezmenu/MenuEventListener.java6
-rw-r--r--src/com/jiggak/rim/mvc/AbstractMainScreenView.java93
-rw-r--r--src/com/jiggak/rms/AbstractStorageCallback.java53
-rwxr-xr-xsrc/com/jiggak/rms/Settings.java167
-rw-r--r--src/com/jiggak/rms/SimpleStorage.java94
-rw-r--r--src/com/jiggak/rms/StorageCallback.java17
-rw-r--r--src/com/jiggak/task/RunnableTask.java35
-rw-r--r--src/com/jiggak/task/TaskCallback.java15
23 files changed, 969 insertions, 0 deletions
diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..afa552c
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/BlackBerry 4.0"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/.cvsignore b/.cvsignore
new file mode 100644
index 0000000..23e34ef
--- /dev/null
+++ b/.cvsignore
@@ -0,0 +1,2 @@
+bin
+.*
diff --git a/.project b/.project
new file mode 100644
index 0000000..1f3c0bc
--- /dev/null
+++ b/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>Slashdev Blackberry Framework</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/build.xml b/build.xml
new file mode 100644
index 0000000..d670cdd
--- /dev/null
+++ b/build.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<project name="project" default="default">
+ <property name="source.dir" value="src"/>
+ <property name="javadoc.dir" value="doc"/>
+ <property name="classes.dir" value="bin"/>
+
+ <target name="default" depends="init">
+ <javadoc destdir="${javadoc.dir}">
+ <fileset dir="${source.dir}"/>
+ </javadoc>
+ </target>
+
+ <target name="clean">
+ <delete dir="${classes.dir}" />
+ </target>
+
+ <target name="init">
+ <mkdir dir="${javadoc.dir}"/>
+ </target>
+</project>
diff --git a/rimjde/SDevBBFramework.jdp b/rimjde/SDevBBFramework.jdp
new file mode 100644
index 0000000..41b8dda
--- /dev/null
+++ b/rimjde/SDevBBFramework.jdp
@@ -0,0 +1,58 @@
+## RIM Java Development Environment
+# RIM Project file
+AlwaysBuild=0
+[AlxImports
+]
+AutoRestart=0
+[BuildOnlyFiles
+]
+[ClassProtection
+]
+[DefFiles
+]
+[DependencyFiles
+]
+[DependsOn
+]
+Description=BlackBerry Framework
+ExcludeFromBuildAll=0
+[Files
+..\src\com\jiggak\mvc\AbstractController.java
+..\src\com\jiggak\mvc\AbstractModel.java
+..\src\com\jiggak\mvc\Controller.java
+..\src\com\jiggak\mvc\Model.java
+..\src\com\jiggak\mvc\ModelEvent.java
+..\src\com\jiggak\mvc\ModelListener.java
+..\src\com\jiggak\mvc\View.java
+..\src\com\jiggak\rim\Application.java
+..\src\com\jiggak\rim\ApplicationCallback.java
+..\src\com\jiggak\rim\ezmenu\EasyMenuScreenView.java
+..\src\com\jiggak\rim\ezmenu\MenuEventListener.java
+..\src\com\jiggak\rim\mvc\AbstractMainScreenView.java
+..\src\com\jiggak\rms\AbstractStorageCallback.java
+..\src\com\jiggak\rms\Settings.java
+..\src\com\jiggak\rms\SimpleStorage.java
+..\src\com\jiggak\rms\StorageCallback.java
+..\src\com\jiggak\task\RunnableTask.java
+..\src\com\jiggak\task\TaskCallback.java
+]
+HaveAlxImports=0
+HaveDefs=0
+HaveImports=0
+[Icons
+]
+[Imports
+]
+Listing=0
+NoImport=0
+Options=-quiet
+OutputFileName=com_jiggak_sdev_SDevBBFramework
+[PackageProtection
+]
+RibbonPosition=0
+RunOnStartup=0
+StartupTier=7
+SystemModule=0
+Type=2
+Vendor=Slashdev.ca
+Version=1.0.001
diff --git a/src/com/jiggak/mvc/AbstractController.java b/src/com/jiggak/mvc/AbstractController.java
new file mode 100644
index 0000000..f791183
--- /dev/null
+++ b/src/com/jiggak/mvc/AbstractController.java
@@ -0,0 +1,61 @@
+/**
+ * Created Jul 21, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+import com.jiggak.rim.Application;
+import com.jiggak.task.RunnableTask;
+import com.jiggak.task.TaskCallback;
+
+/**
+ * @author josh
+ *
+ */
+public abstract class AbstractController
+ implements Controller, TaskCallback
+{
+ protected Model _model;
+ protected View _view;
+
+ /**
+ * @see com.jiggak.mvc.Controller#getModel()
+ */
+ public Model getModel() {
+ return _model;
+ }
+
+ /**
+ * @see com.jiggak.mvc.Controller#getView()
+ */
+ public View getView() {
+ return _view;
+ }
+
+ /**
+ * @see com.jiggak.mvc.Controller#setModel(com.jiggak.mvc.Model)
+ */
+ public void setModel(Model model) {
+ _model = model;
+ }
+
+ /**
+ * @see com.jiggak.mvc.Controller#setView(com.jiggak.mvc.View)
+ */
+ public void setView(View view) {
+ _view = view;
+ }
+
+ protected void backgroundTask(int taskID, Object cookie) {
+ new Thread(new RunnableTask(this, taskID, cookie)).start();
+ }
+
+ protected void foregroundTask(int taskID, Object cookie) {
+ Application.getInstance().invokeLater(new RunnableTask(this, taskID, cookie));
+ }
+
+ public void task(int taskID, Object cookie) {
+ //Empty implementation
+ }
+}
diff --git a/src/com/jiggak/mvc/AbstractModel.java b/src/com/jiggak/mvc/AbstractModel.java
new file mode 100644
index 0000000..2da6794
--- /dev/null
+++ b/src/com/jiggak/mvc/AbstractModel.java
@@ -0,0 +1,52 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+import java.util.Vector;
+
+/**
+ * @author josh
+ *
+ */
+public abstract class AbstractModel
+ implements Model
+{
+ protected Vector _listeners = new Vector();
+
+ /**
+ * @see com.jiggak.mvc.Model#addListener(com.jiggak.mvc.ModelListener)
+ */
+ public void addListener(ModelListener listener) {
+ if( !_listeners.contains(listener) ) {
+ _listeners.addElement(listener);
+ }
+ }
+
+ /**
+ * @see com.jiggak.mvc.Model#removeListener(com.jiggak.mvc.ModelListener)
+ */
+ public boolean removeListener(ModelListener listener) {
+ if( !_listeners.contains(listener) ) {
+ _listeners.removeElement(listener);
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * @see com.jiggak.mvc.Model#notifyListeners(com.jiggak.mvc.ModelEvent)
+ */
+ public void notifyListeners(ModelEvent event) {
+ ModelListener ml;
+ int size = _listeners.size();
+ for(int i=0; i<size; i++) {
+ ml = (ModelListener)_listeners.elementAt(i);
+ ml.notifyEvent(event);
+ }
+ }
+
+}
diff --git a/src/com/jiggak/mvc/Controller.java b/src/com/jiggak/mvc/Controller.java
new file mode 100644
index 0000000..2c6992d
--- /dev/null
+++ b/src/com/jiggak/mvc/Controller.java
@@ -0,0 +1,19 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+/**
+ * @author josh
+ *
+ */
+public interface Controller
+{
+ Model getModel();
+ void setModel(Model model);
+
+ View getView();
+ void setView(View view);
+}
diff --git a/src/com/jiggak/mvc/Model.java b/src/com/jiggak/mvc/Model.java
new file mode 100644
index 0000000..b3b4245
--- /dev/null
+++ b/src/com/jiggak/mvc/Model.java
@@ -0,0 +1,18 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+/**
+ * @author josh
+ */
+public interface Model
+{
+ void addListener(ModelListener listener);
+
+ boolean removeListener(ModelListener listener);
+
+ void notifyListeners(ModelEvent event);
+}
diff --git a/src/com/jiggak/mvc/ModelEvent.java b/src/com/jiggak/mvc/ModelEvent.java
new file mode 100644
index 0000000..679f4c7
--- /dev/null
+++ b/src/com/jiggak/mvc/ModelEvent.java
@@ -0,0 +1,32 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+/**
+ * @author josh
+ */
+public class ModelEvent
+{
+ public static final int EVENT_TYPE_UNKNOWN = 0;
+ private int _type;
+ private Object _data;
+
+ public ModelEvent(int type) {
+ this(type, null);
+ }
+
+ public ModelEvent(int type, Object data) {
+ _type = type; _data = data;
+ }
+
+ public int getType() {
+ return _type;
+ }
+
+ public Object getData() {
+ return _data;
+ }
+}
diff --git a/src/com/jiggak/mvc/ModelListener.java b/src/com/jiggak/mvc/ModelListener.java
new file mode 100644
index 0000000..7d10ee1
--- /dev/null
+++ b/src/com/jiggak/mvc/ModelListener.java
@@ -0,0 +1,15 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+/**
+ * @author josh
+ *
+ */
+public interface ModelListener
+{
+ void notifyEvent(ModelEvent event);
+}
diff --git a/src/com/jiggak/mvc/View.java b/src/com/jiggak/mvc/View.java
new file mode 100644
index 0000000..c77f7da
--- /dev/null
+++ b/src/com/jiggak/mvc/View.java
@@ -0,0 +1,17 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.mvc;
+
+/**
+ * @author josh
+ *
+ */
+public interface View
+ extends ModelListener
+{
+ Model getModel();
+ Controller getController();
+}
diff --git a/src/com/jiggak/rim/Application.java b/src/com/jiggak/rim/Application.java
new file mode 100644
index 0000000..88bc69b
--- /dev/null
+++ b/src/com/jiggak/rim/Application.java
@@ -0,0 +1,46 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rim;
+
+import net.rim.device.api.ui.UiApplication;
+
+/**
+ * @author josh
+ *
+ */
+public class Application
+ extends UiApplication
+{
+ protected static ApplicationCallback _callback;
+ protected static Application _instance = null;
+
+ protected Application() {
+ }
+
+ public static Application getInstance() {
+ if( _instance == null ) {
+ _instance = new Application();
+ _instance.onStartup();
+ _instance.enterEventDispatcher();
+ }
+
+ return _instance;
+ }
+
+ public ApplicationCallback getCallback() {
+ return _callback;
+ }
+
+ public static void setCallback(ApplicationCallback callback) {
+ _callback = callback;
+ }
+
+ public void onStartup() {
+ if( _callback != null ) {
+ _callback.startup();
+ }
+ }
+}
diff --git a/src/com/jiggak/rim/ApplicationCallback.java b/src/com/jiggak/rim/ApplicationCallback.java
new file mode 100644
index 0000000..9c8c985
--- /dev/null
+++ b/src/com/jiggak/rim/ApplicationCallback.java
@@ -0,0 +1,15 @@
+/**
+ * Created Jul 21, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rim;
+
+/**
+ * @author josh
+ *
+ */
+public interface ApplicationCallback
+{
+ void startup();
+}
diff --git a/src/com/jiggak/rim/ezmenu/EasyMenuScreenView.java b/src/com/jiggak/rim/ezmenu/EasyMenuScreenView.java
new file mode 100644
index 0000000..bdd5e3d
--- /dev/null
+++ b/src/com/jiggak/rim/ezmenu/EasyMenuScreenView.java
@@ -0,0 +1,111 @@
+/**
+ * Created Jul 26, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rim.ezmenu;
+
+import java.util.Vector;
+
+import net.rim.device.api.ui.MenuItem;
+import net.rim.device.api.ui.component.Menu;
+
+import com.jiggak.mvc.Controller;
+import com.jiggak.mvc.Model;
+import com.jiggak.rim.mvc.AbstractMainScreenView;
+
+/**
+ * @author josh
+ *
+ */
+public abstract class EasyMenuScreenView
+ extends AbstractMainScreenView
+{
+ protected int itemWithPrority = -1;
+ protected Vector menuItems = new Vector();
+ protected MenuEventListener menuListener;
+
+ public EasyMenuScreenView() {
+ super();
+ }
+
+ public EasyMenuScreenView(long style) {
+ super(style);
+ }
+
+ public EasyMenuScreenView(Model model, Controller controller, long style) {
+ super(model, controller, style);
+ }
+
+ public EasyMenuScreenView(Model model, Controller controller) {
+ super(model, controller);
+ }
+
+ public void addSeparator() {
+ menuItems.addElement(null);
+ }
+
+ public void addMenuItem(int itemID, String text) {
+ menuItems.addElement(new EasyMenuItem(itemID, text));
+ }
+
+ /**
+ * @see net.rim.device.api.ui.container.MainScreen#removeAllMenuItems()
+ */
+ public void removeAllMenuItems() {
+ super.removeAllMenuItems();
+ menuItems.removeAllElements();
+ }
+
+ public void removeMenuItem(int itemID) {
+ int size = menuItems.size();
+ for(int i=0; i<size; i++) {
+ Object item = menuItems.elementAt(i);
+ if( item != null ) {
+ if( ((EasyMenuItem)item).getItemID() == itemID ) {
+ menuItems.removeElementAt(i);
+ break;
+ }
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see net.rim.device.api.ui.container.MainScreen#makeMenu(net.rim.device.api.ui.component.Menu, int)
+ */
+ protected void makeMenu(Menu menu, int instance) {
+ super.makeMenu(menu, instance);
+ int size = menuItems.size();
+ for(int i=0; i<size; i++) {
+ Object item = menuItems.elementAt(i);
+ if( item != null ) {
+ menu.add((EasyMenuItem)item);
+ } else {
+ menu.addSeparator();
+ }
+ }
+ }
+
+ protected void menuItemPicked(int itemID) {
+ if( menuListener != null ) {
+ menuListener.onMenuItem(itemID);
+ }
+ }
+
+ protected class EasyMenuItem extends MenuItem {
+ private int _itemID;
+
+ public EasyMenuItem(int itemID, String text) {
+ super(text, 100, 0);
+ _itemID = itemID;
+ }
+
+ public int getItemID() {
+ return _itemID;
+ }
+
+ public void run() {
+ menuItemPicked(_itemID);
+ }
+ }
+}
diff --git a/src/com/jiggak/rim/ezmenu/MenuEventListener.java b/src/com/jiggak/rim/ezmenu/MenuEventListener.java
new file mode 100644
index 0000000..4123a78
--- /dev/null
+++ b/src/com/jiggak/rim/ezmenu/MenuEventListener.java
@@ -0,0 +1,6 @@
+package com.jiggak.rim.ezmenu;
+
+public interface MenuEventListener
+{
+ void onMenuItem(int itemID);
+}
diff --git a/src/com/jiggak/rim/mvc/AbstractMainScreenView.java b/src/com/jiggak/rim/mvc/AbstractMainScreenView.java
new file mode 100644
index 0000000..f0abf69
--- /dev/null
+++ b/src/com/jiggak/rim/mvc/AbstractMainScreenView.java
@@ -0,0 +1,93 @@
+/**
+ * Created Jul 20, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rim.mvc;
+
+import net.rim.device.api.ui.container.MainScreen;
+
+import com.jiggak.mvc.Controller;
+import com.jiggak.mvc.Model;
+import com.jiggak.mvc.View;
+import com.jiggak.rim.Application;
+
+/**
+ * @author josh
+ *
+ * BlackBerry MainScreen with implementation of View.
+ */
+public abstract class AbstractMainScreenView
+ extends MainScreen
+ implements View
+{
+ protected Model _model;
+ protected Controller _controller;
+
+ protected AbstractMainScreenView _parent;
+
+ public AbstractMainScreenView() {
+ this(0);
+ }
+
+ public AbstractMainScreenView(long style) {
+ this(null, null, style);
+ }
+
+ public AbstractMainScreenView(Model model, Controller controller) {
+ this(model, controller, 0);
+ }
+
+ public AbstractMainScreenView(Model model, Controller controller, long style) {
+ super(style);
+
+ _model = model;
+ _controller = controller;
+
+ if( _controller != null ) {
+ _controller.setModel(model);
+ }
+ }
+
+ /**
+ * @see com.jiggak.mvc.View#getModel()
+ */
+ public Model getModel() {
+ return _model;
+ }
+
+ /**
+ * @see com.jiggak.mvc.View#getController()
+ */
+ public Controller getController() {
+ return _controller;
+ }
+
+ protected void onDisplay() {
+ super.onDisplay();
+
+ if( getModel() != null ) {
+ getModel().addListener(this);
+ }
+ }
+
+ protected void onUndisplay() {
+ super.onUndisplay();
+
+ if( getModel() != null ) {
+ getModel().removeListener(this);
+ }
+ }
+
+ public void hide() {
+ if( isDisplayed() ) {
+ Application.getInstance().popScreen(this);
+ }
+ }
+
+ public void show() {
+ if( !isDisplayed() ) {
+ Application.getInstance().pushScreen(this);
+ }
+ }
+}
diff --git a/src/com/jiggak/rms/AbstractStorageCallback.java b/src/com/jiggak/rms/AbstractStorageCallback.java
new file mode 100644
index 0000000..fd074c6
--- /dev/null
+++ b/src/com/jiggak/rms/AbstractStorageCallback.java
@@ -0,0 +1,53 @@
+/**
+ * Created Jul 26, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rms;
+
+/**
+ * @author josh
+ *
+ */
+public abstract class AbstractStorageCallback
+ implements StorageCallback
+{
+ protected static class Part {
+ public Part(int pos, String data) {
+ this.pos = pos; this.data = data;
+ }
+ public int pos;
+ public String data;
+ }
+
+ private String parseBuffer;
+ private int partPos;
+ private int start;
+
+ protected void startParsing(byte[] data) {
+ start = 0;
+ partPos = 0;
+ parseBuffer = (data != null) ? new String(data) : null;
+ }
+
+ protected Part getNextPart(String delim) {
+ Part part = null;
+
+ if( parseBuffer != null ) {
+ //determain end of part
+ int end = parseBuffer.indexOf(delim, start);
+
+ //get part from buffer
+ String item = parseBuffer.substring(start, end);
+ part = new Part(partPos++, item);
+
+ start = end + delim.length();
+
+ if( start >= parseBuffer.length() ) {
+ parseBuffer = null;
+ }
+ }
+
+ return part;
+ }
+}
diff --git a/src/com/jiggak/rms/Settings.java b/src/com/jiggak/rms/Settings.java
new file mode 100755
index 0000000..254d1cc
--- /dev/null
+++ b/src/com/jiggak/rms/Settings.java
@@ -0,0 +1,167 @@
+/**
+ * Settings.java
+ */
+
+package com.jiggak.rms;
+
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.microedition.rms.RecordStoreException;
+
+
+/**
+ * A class for storing and retrieving application settings and properties. Class
+ * stores all settings into one Hashtable variable. Hashtable is loaded from
+ * RecordStore at initialization and it is stored back to the RecordStore with
+ * save method.
+ */
+public class Settings
+{
+ public static String KEY_VALUE_SEPERATOR = "|";
+
+ private static Settings m_instance;
+
+ private boolean m_valuesChanged = false;
+ private Hashtable m_properties = new Hashtable();
+ private SimpleStorage m_storage;
+
+ /**
+ * Singleton pattern is used to return only one instance of record store
+ */
+ public static synchronized Settings getInstance(String dbName)
+ throws RecordStoreException {
+ if (m_instance == null) {
+ m_instance = new Settings(dbName);
+ }
+ return m_instance;
+ }
+
+ /** Constructor */
+ private Settings(String dbName) throws RecordStoreException {
+ m_storage = new SimpleStorage(new KeyValueStorageCallback(), dbName);
+ load();
+ }
+
+ /** Get property from Hashtable */
+ private synchronized String getProperty(String name) {
+ return (String) m_properties.get(name);
+ }
+
+ /** Get boolean property */
+ public boolean getBooleanProperty(String name, boolean defaultValue) {
+ String value = getProperty(name);
+ if (value != null) {
+ return value.equals("true") || value.equals("1");
+ }
+ return defaultValue;
+ }
+
+ /** Get integer property */
+ public int getIntProperty(String name, int defaultValue) {
+ String value = getProperty(name);
+ if (value != null) {
+ try {
+ return Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ }
+ }
+ return defaultValue;
+ }
+
+ /** Get string property */
+ public String getStringProperty(String name, String defaultValue) {
+ Object value = getProperty(name);
+ return (value != null) ? value.toString() : defaultValue;
+ }
+
+ /** Load properties from record store */
+ private synchronized void load() throws RecordStoreException {
+ m_valuesChanged = false;
+ m_properties.clear();
+
+ Object[] settings = m_storage.load();
+ for(int i=0; i<settings.length; i++) {
+ KeyValuePair pair = (KeyValuePair)settings[i];
+ m_properties.put(pair.key, pair.value);
+ }
+ }
+
+ /** Save property Hashtable to record store */
+ public synchronized void save(boolean force) throws RecordStoreException {
+ if (!m_valuesChanged && !force)
+ return;
+
+ Enumeration e = m_properties.keys();
+ while (e.hasMoreElements()) {
+ String name = (String) e.nextElement();
+ String value = m_properties.get(name).toString();
+ m_storage.add(new KeyValuePair(name, value));
+ }
+ }
+
+ /** Set a boolean property */
+ public void setBooleanProperty(String name, boolean value) {
+ setStringProperty(name, value ? "true" : "false");
+ }
+
+ /** Set an integer property */
+ public void setIntProperty(String name, int value) {
+ setStringProperty(name, Integer.toString(value));
+ }
+
+ /** Set a string property */
+ public synchronized boolean setStringProperty(String name, String value) {
+ if (name == null && value == null)
+ return false;
+ m_properties.put(name, value);
+ m_valuesChanged = true;
+ return true;
+ }
+
+ private static class KeyValueStorageCallback extends AbstractStorageCallback {
+ public byte[] toBytes(Object obj) {
+ KeyValuePair pair = (KeyValuePair)obj;
+
+ StringBuffer buffer = new StringBuffer();
+ buffer.append(pair.key);
+ buffer.append(KEY_VALUE_SEPERATOR);
+ buffer.append(pair.value);
+ buffer.append(KEY_VALUE_SEPERATOR);
+
+ return buffer.toString().getBytes();
+ }
+
+ public Object toObject(byte[] data) {
+ KeyValuePair pair = new KeyValuePair();
+
+ startParsing(data);
+
+ Part part;
+ while( (part=getNextPart(KEY_VALUE_SEPERATOR)) != null ) {
+ switch(part.pos) {
+ case 0:
+ pair.key = part.data;
+ case 1:
+ pair.value = part.data;
+ }
+ }
+
+ return pair;
+ }
+ }
+
+ private static class KeyValuePair {
+ public String key;
+ public String value;
+
+ public KeyValuePair() {
+ this(null,null);
+ }
+
+ public KeyValuePair(String key, String value) {
+ this.key = key; this.value = value;
+ }
+ }
+
+}
diff --git a/src/com/jiggak/rms/SimpleStorage.java b/src/com/jiggak/rms/SimpleStorage.java
new file mode 100644
index 0000000..76aeb95
--- /dev/null
+++ b/src/com/jiggak/rms/SimpleStorage.java
@@ -0,0 +1,94 @@
+/**
+ * Created Jul 21, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rms;
+
+import javax.microedition.rms.RecordEnumeration;
+import javax.microedition.rms.RecordStore;
+import javax.microedition.rms.RecordStoreException;
+
+/**
+ * @author josh
+ *
+ */
+public class SimpleStorage
+{
+ private RecordStore _recordStore;
+
+ private StorageCallback _callback;
+ private String _name;
+
+ public SimpleStorage(String name) {
+ this(new DefaultCallback(), name);
+ }
+
+ public SimpleStorage(StorageCallback callback, String name) {
+ if( callback == null ) {
+ throw new IllegalArgumentException("callback cannot be null");
+ }
+
+ _callback = callback;
+ _name = name;
+ }
+
+ public void open() throws RecordStoreException {
+ if( _recordStore == null ) {
+ _recordStore = RecordStore.openRecordStore(_name, true);
+ }
+ }
+
+ public void close() {
+ if( _recordStore != null ) {
+ try {
+ _recordStore.closeRecordStore();
+ } catch(RecordStoreException e) {
+ //ignore
+ } finally {
+ _recordStore = null;
+ }
+ }
+ }
+
+ public int add(Object obj) throws RecordStoreException {
+ open();
+
+ try {
+ byte[] data = _callback.toBytes(obj);
+ return _recordStore.addRecord(data, 0, data.length);
+ } finally {
+ close();
+ }
+ }
+
+ public Object[] load() throws RecordStoreException {
+ open();
+
+ try {
+ int cnt = 0;
+ Object[] results = new Object[_recordStore.getNumRecords()];
+
+ RecordEnumeration e = _recordStore.enumerateRecords(null, null, false);
+ while( e.hasNextElement() ) {
+ results[cnt++] = _callback.toObject(e.nextRecord());
+ }
+
+ return results;
+ } finally {
+ close();
+ }
+ }
+
+ private static final class DefaultCallback
+ implements StorageCallback
+ {
+ public Object toObject(byte[] data) {
+ return new String(data);
+ }
+
+ public byte[] toBytes(Object obj) {
+ return obj.toString().getBytes();
+ }
+ }
+}
diff --git a/src/com/jiggak/rms/StorageCallback.java b/src/com/jiggak/rms/StorageCallback.java
new file mode 100644
index 0000000..37820a9
--- /dev/null
+++ b/src/com/jiggak/rms/StorageCallback.java
@@ -0,0 +1,17 @@
+/**
+ * Created Jul 21, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.rms;
+
+/**
+ * @author josh
+ *
+ */
+public interface StorageCallback
+{
+ byte[] toBytes(Object obj);
+
+ Object toObject(byte[] data);
+}
diff --git a/src/com/jiggak/task/RunnableTask.java b/src/com/jiggak/task/RunnableTask.java
new file mode 100644
index 0000000..3e4bbeb
--- /dev/null
+++ b/src/com/jiggak/task/RunnableTask.java
@@ -0,0 +1,35 @@
+/**
+ * Created Jul 21, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.task;
+
+/**
+ * @author josh
+ *
+ */
+public class RunnableTask
+ implements Runnable
+{
+ private TaskCallback _callback;
+ private int _taskID;
+ private Object _cookie;
+
+ public RunnableTask(TaskCallback callback, int taskID, Object cookie) {
+ _callback = callback;
+ _taskID = taskID;
+ _cookie = cookie;
+ }
+
+ /**
+ * @see java.lang.Runnable#run()
+ * Runs the task method in the callback
+ * if it is not null
+ */
+ public void run() {
+ if( _callback != null ) {
+ _callback.task(_taskID, _cookie);
+ }
+ }
+}
diff --git a/src/com/jiggak/task/TaskCallback.java b/src/com/jiggak/task/TaskCallback.java
new file mode 100644
index 0000000..9bbc34f
--- /dev/null
+++ b/src/com/jiggak/task/TaskCallback.java
@@ -0,0 +1,15 @@
+/**
+ * Created Jul 21, 2005
+ * By josh
+ * Copyright 2005 Slashdev.ca
+ */
+package com.jiggak.task;
+
+/**
+ * @author josh
+ *
+ */
+public interface TaskCallback
+{
+ void task(int taskID, Object cookie);
+}