Package net.sf.nfp.mini.data

Source Code of net.sf.nfp.mini.data.MucusRegistry

package net.sf.nfp.mini.data;


import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Vector;

import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;

import net.sf.log.mobile.Log;
import net.sf.nfp.mini.misc.MucusUtils;

/**
* Does not change while the NFP midlet is running
* @author Krzysztof Rzymkowski
*
*/
public class MucusRegistry {
  public static final String MUCUS_REGISTRY = "mucus-registry";
  private static MucusRegistry instance;
  private Vector registry = null;
  public static final int[] MUCUS_COLORS = { 0x00ff00, 0x0000ff, 0xff0000 };
  private static final String[] MUCUS_IMAGES_NAME = {
    "green.png","blue.png""red.png"
  };
  private static final String MUCUS_IMAGES_BASE = "/icons/";
  public static Image[] MUCUS_IMAGES = null;
 
  private MucusRegistry() throws IOException, RecordStoreException {
    Log.log("MucusRegistry.MucusRegistry()");
    try {
      RecordStore rms = RecordStore.openRecordStore(MUCUS_REGISTRY, false);
      registry = MucusUtils.getAll(rms);
      rms.closeRecordStore();
     
    }catch(RecordStoreNotFoundException ex){
      registry = MucusUtils.createDefaultsVector();
    }
    Log.log("MUCUS_IMAGES 1="+MUCUS_IMAGES);
    if(MUCUS_IMAGES == null) {
      MUCUS_IMAGES = new Image[MUCUS_IMAGES_NAME.length];
      for(int i=0;i<MUCUS_IMAGES.length;++i)
        MUCUS_IMAGES[i] = Image.createImage(MUCUS_IMAGES_BASE + MUCUS_IMAGES_NAME[i]);
    }
    Log.log("MUCUS_IMAGES 2="+MUCUS_IMAGES);
  }
 

  public Mucus find(int id) {
    for(int i=0;i<registry.size();++i) {
      Mucus m = (Mucus) registry.elementAt(i);
      if(m.getId() == id)
        return m;
    }
    throw new NoSuchElementException("Mucus id:"+id);
  }
 
  public Vector getAll() {
    return registry;
  }
 
  public static MucusRegistry instance() {
    try {
      if(instance == null)
        instance = new MucusRegistry();
      return instance;
    }catch(Exception ex){
      throw new RuntimeException(ex.toString());
    }
  }


  public static void close() {
    instance = null;
  }


  public Image getImage(Mucus mucus) {
    Log.log("MucusRegistry.getImage("+mucus+")");
    Log.log("mucus.getCategory()="+mucus.getCategory());
    Log.log("MUCUS_IMAGES="+MUCUS_IMAGES);
    return MUCUS_IMAGES[mucus.getCategory()];
  }

}
TOP

Related Classes of net.sf.nfp.mini.data.MucusRegistry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.