//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// MicroSafe source file
// Copyright (c) 2006 Elmar Sonnenschein / esoco GmbH
// Last Change: 05.11.2006 by eso
//
// MicroSafe is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// MicroSafe is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// MicroSafe; if not, write to the Free Software Foundation, Inc., 59 Temple
// Place, Suite 330, Boston, MA 02111-1307 USA or use the contact information
// from the GNU website http://www.gnu.org
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package de.esoco.microsafe;
import de.esoco.j2me.midlet.BasicMidlet;
import de.esoco.j2me.resource.ResourceBundle;
import de.esoco.j2me.storage.HierarchicalRecordStorage;
import de.esoco.j2me.ui.HierarchyView;
import de.esoco.j2me.ui.ProgressView;
import de.esoco.j2me.util.Log;
import de.esoco.j2me.util.ProgressMonitor;
import de.esoco.j2me.util.UserNotificationException;
import de.esoco.microsafe.model.MicroSafeModel;
import de.esoco.microsafe.ui.MicroSafeNodeController;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.RecordStore;
/********************************************************************
* The Midlet class of the MicroSafe application.
*
* @author eso
*/
public class MicroSafe extends BasicMidlet
{
//~ Static fields/initializers ---------------------------------------------
/** enable debugging */
public static final boolean DEBUG = false;
/** The standard name for the RecordStore that is used to store the model */
private static final String RECORDSTORE_NAME = "MicroSafeData";
//~ Instance fields --------------------------------------------------------
private MicroSafeModel aDataModel;
private HierarchyView aMainScreen;
private ProgressView aStartProgressView;
//~ Constructors -----------------------------------------------------------
/***************************************
* Constructor for the MicroSafeMidlet.
*/
public MicroSafe()
{
Log.setLogLevel(DEBUG ? Log.DEBUG : Log.OFF);
}
//~ Methods ----------------------------------------------------------------
/***************************************
* Saves the application's model and performs a cleanup of internal
* resources.
*
* @throws UserNotificationException If closing the midlet data model fails
*/
public void cleanup() throws UserNotificationException
{
super.cleanup();
closeModel();
aMainScreen = null;
}
/***************************************
* Returns the data model of the application.
*
* @return A MicroSafeModel instance
*/
public MicroSafeModel getDataModel()
{
return aDataModel;
}
/***************************************
* Internal method to close the data model and the underlying RecordStore.
*
* @throws UserNotificationException If closing the data model fails
*/
protected void closeModel() throws UserNotificationException
{
if (aDataModel != null)
{
try
{
aDataModel.close();
aDataModel = null;
}
catch (Exception e)
{
throw new UserNotificationException(getString("JL_MtError"),
getString("MsCloseMdl"), e);
}
}
}
/***************************************
* Displays the MicroSafe start screen with a progress view to display the
* progress of loading the model.
*
* @see BasicMidlet#getStartScreen()
*/
protected Displayable getStartScreen()
{
ResourceBundle.loadBundle("microsafe");
aStartProgressView = new ProgressView(getString("MtProgress"),
ResourceBundle.getCurrent()
.getImage("ImAbout"), null, true);
aStartProgressView.setProgressInfoText(getString("RsStarting"));
return aStartProgressView;
}
/***************************************
* Inititalizes the model and view.
*
* @throws An UserNotificationException if the initialization fails
*
* @see BasicMidlet#init()
*/
protected void init() throws UserNotificationException
{
initModel(aStartProgressView);
aStartProgressView = null;
if (aMainScreen == null)
{
aMainScreen = new HierarchyView(aDataModel,
new MicroSafeNodeController(aDataModel),
getStandardCommands());
}
}
/***************************************
* Internal method to initialize the data model.
*
* @param rMonitor The progress monitor to use for showing the progress of
* loading, saving, decrypting, and encrypting data
*
* @throws UserNotificationException If the initialization of the data model
* fails
*/
protected void initModel(ProgressMonitor rMonitor)
throws UserNotificationException
{
RecordStore aRS = null;
HierarchicalRecordStorage aHRS = null;
if (aDataModel == null)
{
try
{
aRS = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
aHRS = new HierarchicalRecordStorage(aRS);
aDataModel = new MicroSafeModel(aHRS);
aDataModel.setProgressMonitor(rMonitor);
aDataModel.initFromStorage();
// init will read the data completely; reset the storage to free
// allocated resources like streams and buffers
aHRS.reset();
}
catch (Exception e)
{
throw new UserNotificationException(getString("JL_MtError"),
getString("MsInitMdl"), e);
}
}
}
/***************************************
* This method will start the application's main loop by displaying the
* first screen of the HierarchyView instance used to naviagate the data
* model.
*
* @see de.esoco.j2me.midlet.BasicMidlet#runApp()
*/
protected void runApp()
{
aMainScreen.show();
}
}