Wednesday, May 20, 2009

Spring+Swing App

/**
*Add netbeans java project without the Main class
*Add lib spring
*create package todo and todo.ui
**/
package todo.ui;
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.WindowConstants;


public class MainFrame extends JFrame {
public void init() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(new Dimension(600, 400));

setVisible(true);
setState(Frame.NORMAL);
show();
}
}

//////////////////////////////////
package todo;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Launcher {
public void launch() {
String[] contextPaths = new String[] {"todo/app-context.xml"};
new ClassPathXmlApplicationContext(contextPaths);
}
}

////////////////////////////////

package todo;

public class ToDo {
public static void main(String[] args) {
Launcher launcher = new Launcher();
launcher.launch();
}
}

////////////////////////////////////////
Create an XML File path+named = src/todo/app-context.xml
///////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="mainFrame" class="todo.ui.MainFrame" method="init"></bean>
</beans>
<beans><bean id="mainFrame" class="todo.ui.MainFrame" method="init"> </bean>
</beans>

Thursday, May 14, 2009

Custom List component for J2ME

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ali.ui;

import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;

/**
*
* @author ali hassan
*/
public class List extends CustomItem {

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*/
public static final int DEFAULT_SELECTED_BGCOLOR = 0xFFFFFF;
/**
*
*/
public static final int DEFAULT_SELECTED_FGCOLOR = 0x0;
/**
*
*/
public static final int DEFAULT_FGCOLOR = 0xFFFFFF;
public static final int DEFAULT_BGCOLOR = 0x0;
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
private Vector items = new Vector();
private Vector itemFonts = new Vector();
private int selected = 0;
private int bgColor = DEFAULT_BGCOLOR;
private int fgColor = DEFAULT_FGCOLOR;
private int selectedBGColor = DEFAULT_SELECTED_BGCOLOR;
private int selectedFGColor = DEFAULT_SELECTED_FGCOLOR;
private int startPageItem = 0;
private int endPageItem = 0;
private boolean paintDown = true;

public List(String label) {
super(label);
}

public List(String label, int x, int y, int width, int height) {
this(label);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
int[] visRect_inout) {
switch (dir) {
case Canvas.DOWN:
this.next();
// this.getSelectedString();
notifyStateChanged();
repaint();
break;

case Canvas.UP:
this.prev();
// this.getSelectedString();

notifyStateChanged();
repaint();
break;
}
System.out.println(this.getSelected());
return true;
}



public List(String label, int x, int y, int width, int height, String[] items,
Font[] fonts) {

this(label, x, y, width, height);
if (items == null) {
throw new NullPointerException("items");
}
if (fonts == null) {
throw new NullPointerException("itemFonts");
}
int length = items.length;
int fontsLength = fonts.length;
if (fontsLength > length) {
throw new IllegalArgumentException(
"size of fonts > size of items: " + fontsLength + " > " + length);
}
for (int i = 0; i < length; i++) {
if (i < fontsLength) {
this.append(items[i], fonts[i]);
} else {
this.append(items[i]);
}
}
}

protected int getMinContentWidth() {
return this.width;
}

protected int getMinContentHeight() {
return this.height;
}

protected int getPrefContentWidth(int height) {
return getMinContentWidth();
}

protected int getPrefContentHeight(int width) {
return getMinContentHeight();
}

protected void paint(Graphics g, int w, int h) {

int clipX = g.getClipX();
int clipY = g.getClipY();
int clipW = g.getClipWidth();
int clipH = g.getClipHeight();
g.setClip(this.x, this.y, this.width, this.height);
int itemHeight;
g.setColor(this.bgColor);
g.fillRect(this.x, this.y, this.width, this.height);
g.setColor(this.fgColor);
if (this.paintDown) {
int iLenght = this.items.size();
int curY = this.y;
for (int i = this.startPageItem; (i < iLenght) && (curY - this.y + (itemHeight = ((Font) this.itemFonts.elementAt(i)).getHeight()) <= this.height); i++) {
String item = (String) this.items.elementAt(i);
Font font = (Font) this.itemFonts.elementAt(i);
g.setFont(font);
int itemWidth = font.stringWidth(item);
if (i == this.selected) {
g.setColor(this.selectedBGColor);
g.fillRect(this.x, curY, itemWidth + 4, itemHeight);
g.setColor(this.selectedFGColor);
g.drawString(item, this.x + 2, curY, Graphics.TOP | Graphics.LEFT);
g.setColor(this.fgColor);
} else {
g.drawString((String) this.items.elementAt(i), this.x + 2, curY,
Graphics.TOP | Graphics.LEFT);
}
curY += itemHeight;
this.endPageItem = i;
}
if (this.endPageItem < this.items.size() - 1) {
g.drawString(
(String) this.items.elementAt(this.endPageItem + 1), this.x + 2,
curY, Graphics.TOP | Graphics.LEFT);
}

} else {
int curY = this.y + this.height;
for (int i = this.endPageItem; i >= 0 && curY - (itemHeight = ((Font) this.itemFonts.elementAt(i)).getHeight()) >= this.y; i--) {
String item = (String) this.items.elementAt(i);
Font font = (Font) this.itemFonts.elementAt(i);
g.setFont(font);
int itemWidth = font.stringWidth(item);
if (i == this.selected) {
g.setColor(this.selectedBGColor);
g.fillRect(this.x, curY - itemHeight, itemWidth + 4, itemHeight);
g.setColor(this.selectedFGColor);
g.drawString((String) this.items.elementAt(i), this.x + 2, curY,
Graphics.BOTTOM | Graphics.LEFT);
g.setColor(this.fgColor);
} else {
g.drawString((String) this.items.elementAt(i), this.x + 2, curY,
Graphics.BOTTOM | Graphics.LEFT);
}
curY -= itemHeight;
this.startPageItem = i;
}
if (this.startPageItem > 0) {
g.drawString((String) this.items.elementAt(this.startPageItem - 1), this.x + 2, curY, Graphics.BOTTOM | Graphics.LEFT);
}
}
g.setClip(clipX, clipY, clipW, clipH);
}

//////My Main List
public void setBGColor(int color) {
this.bgColor = color;
}

public void setFGColor(int color) {
this.fgColor = color;
}

public void setSelectedBGColor(int color) {
this.selectedBGColor = color;
}

public void setSelectedFGColor(int color) {
this.selectedFGColor = color;
}

public void append(String item, Font font) {
if (item == null) {
throw new NullPointerException("item");
}
if (font == null) {
font = Font.getDefaultFont();
}
this.items.addElement(item);
this.itemFonts.addElement(font);
}

public void append(String item) {
this.append(item, null);
}

public void delete(int pos) {
this.items.removeElementAt(pos);
this.itemFonts.removeElementAt(pos);
if (pos <= this.startPageItem) {
this.startPageItem--;
this.paintDown = true;
}
if (pos <= this.selected) {
this.selected--;
this.paintDown = true;
}
if (pos <= this.endPageItem) {
this.endPageItem--;
this.paintDown = true;
}
}

public void deleteAll() {
this.items.removeAllElements();
this.itemFonts.removeAllElements();
this.selected = 0;
this.startPageItem = 0;
this.endPageItem = 0;
this.paintDown = true;
}

public void set(int pos, String item, Font font) {
if (item == null) {
throw new NullPointerException("item");
}
if (font == null) {
font = Font.getDefaultFont();
}
this.items.setElementAt(item, pos);
this.itemFonts.setElementAt(font, pos);
}

public boolean isSelected(int pos) {
return pos == this.selected;
}

public int getSelectedIndex() {
return (this.items.size() > 0) ? this.selected : -1;
}

public String getSelected() {
return (this.items.size() > 0) ? (String) this.items.elementAt(this.selected) : null;
}

public boolean next() {
boolean res = false;
if (this.selected < this.items.size() - 1) {
res = true;
if (++this.selected > this.endPageItem) {
this.paintDown = false;
this.endPageItem = this.selected;
}
}
return res;
}

public boolean prev() {
boolean res = false;
if (this.selected > 0) {
res = true;
if (--this.selected < this.startPageItem) {
this.paintDown = true;
this.startPageItem = this.selected;
}
}
return res;
}

public void previous() {
if (this.selected > 0) {
this.selected--;
}
}

public void show(Graphics g) {
}

/**
* @return the y
*/
public int getY() {
return this.y;
}

/**
* @param y
* the y to set
*/
public void setY(int y) {
this.y = y;
}

/**
* @return the height
*/
public int getHeight() {
return this.height;
}

/**
* @return the width
*/
public int getWidth() {
return this.width;
}

/**
* @return the x
*/
public int getX() {
return this.x;
}
}

Monday, April 13, 2009

SyncML

I'll not go in the theory. I start its coding here.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mypackage;

import com.funambol.storage.NamedObjectStore;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncConfig;
import com.funambol.syncml.spds.SyncManager;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.util.Log;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;


/**
*
* @author ali hassan
*/
public class MainMIDlet extends MIDlet implements CommandListener, Runnable{

private SourceConfig sc;
private SyncConfig conf;
private ContactSync cs ;

private Display display;
private Form mainFrm;

private Command cmd_exit;
private Command cmd_sync;


public MainMIDlet() {
Log.setLogLevel(Log.DEBUG);
sc = new SourceConfig();
conf = new SyncConfig();

NamedObjectStore store = new NamedObjectStore();

try{

store.create("DB");
sc.setRemoteUri("card");
sc.setType("text/x-vcard");
sc.setEncoding(SyncSource.ENCODING_B64);

conf.syncUrl = "http://myserver/funambol/ds";
conf.userName="username";
conf.password="password";
conf.compress=true;
conf.userAgent="NokiaN70";

store.store(SyncConfig.NAME, conf);
if(store.lookup("card")==0)
store.store("card", sc);
else
store.retrieve("card", sc);
}catch(Exception ex){
ex.printStackTrace();
}

mainFrm = new Form("MyForm");

this.cmd_exit = new Command("Exit", Command.EXIT, 0);
this.cmd_sync = new Command("Sync", Command.OK, 1);

mainFrm.addCommand(cmd_exit);
mainFrm.addCommand(cmd_sync);

mainFrm.setCommandListener(this);

display = Display.getDisplay(this);
}


protected void startApp(){
display.setCurrent(mainFrm);
}

protected void pauseApp() {

}

protected void destroyApp(boolean arg0){

}

public void commandAction(Command c, Displayable d) {
if(c==cmd_exit)
notifyDestroyed();
else if(c==cmd_sync)
{
new Thread(this).start();
}
}

public void run()
{
NamedObjectStore os = new NamedObjectStore();
try{
os.open("DB");
os.retrieve("card", sc);
os.retrieve(SyncConfig.NAME, conf);
}catch(Exception ex){
ex.printStackTrace();
}

SyncManager sm = new SyncManager(conf);
cs = new ContactSync(sc);
//Log.info(cs.+ "------------------------------");
try{
Log.info("Last Anchor Before Sync: "+sc.getLastAnchor());
sm.sync(cs, SyncML.ALERT_CODE_FAST);
Log.info("Last Anchor After Sync: "+sc.getLastAnchor());
os.store("card", sc);
os.close();
}catch(Exception ex){
ex.printStackTrace();
}

show();
}

private void show()
{

/*MyContacts mc = new MyContacts();
SyncItem si = cs.getNextItem();
Log.info("--------------------"+si.getType());*/
}

}

second step is:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package vopium;

import com.funambol.syncml.client.BaseSyncSource;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.util.Log;

/**
*
* @author ali hassan
*/
public class ContactSync extends BaseSyncSource{

public ContactSync(SourceConfig config) {
super(config);
}

public int addItem(SyncItem item) throws SyncException {

Log.info(item.getKey()+"--------------------------------------------"+new String(item.getContent()));
if (item.getType().equals(SourceConfig.VCARD_TYPE)) {
try{
MyContacts c = new MyContacts();
c.parse( new String(item.getContent()));
RMSContactManager cm = new RMSContactManager();
item.setKey(""+cm.addContactToRMS(c));
}catch(Exception ex)
{
ex.printStackTrace();
}
}
return 200;
}

public int updateItem(SyncItem item) throws SyncException {
Log.info("update");
return 200;
}

public int deleteItem(String key) throws SyncException {
Log.info("del");
return 200;
}

protected void initAllItems() throws SyncException {
Log.info("init All");
}

protected void initNewItems() throws SyncException {
Log.info("init New");
}

protected void initUpdItems() throws SyncException {
Log.info("init Update");
}

protected void initDelItems() throws SyncException {
Log.info("init Del");
}

protected SyncItem getItemContent(SyncItem item) throws SyncException {
Log.info("getItemContent");
return new SyncItem(item);
}

public void beginSync(int syncMode)
{
super.beginSync(syncMode);
}


}



Above class is responsible for the object serialization. Like hibernate.



we are using funambol J2ME API for this purpose.