jQuery live

กุมภาพันธ์ 9, 2011

Rebinding function to dynamic content with live !

Change from …
$(document).ready(function(){
$(‘#my-id’).click(function(){ dosomethink(); });
});

to …

$(‘#my-id’).live(‘click’, function() { dosomethink();});

References:
http://stackoverflow.com/questions/506764/rebinding-functions-on-dynamic-content-using-jquery-and-ajax
http://api.jquery.com/live

Java Interface

เมษายน 26, 2010

public class TestReadInterfaces {

 B b;
 C c;    

 public TestReadInterfaces()    {
 A a = new A();
 Class [] fs = a.getClass().getInterfaces();
 for (int i=0; i<fs.length; i++)    {
 System.out.println(fs[i].getName());
 }
 }

 public static void main(String[] args) {
 new TestReadInterfaces();
 }

}

class A implements B, C {

}

interface B {

}

interface C {

}

jQuery

เมษายน 12, 2010

jQuery is a JavaScript Library for improving web user interface by writing less code.
http://jquery.com

jQuery Tutorials for Designers
http://www.webdesignerwall.com/tutorials/jquery-tutorials-for-designers/

jQuery in Wikipedia
http://en.wikipedia.org/wiki/JQuery

jQuery Selectors
http://www.tutorialspoint.com/jquery/jquery-quick-guide.htm

JQuery Documentation & APIs
http://docs.jquery.com/Main_Page
http://api.jquery.com/category/effects/

Special Effects
PreLoader
http://dinolatoga.com/2009/04/26/how-to-create-a-visual-image-preloader-using-jquery/

Reel, Spinm Round
http://jquery.vostrel.cz/reel?utm_source=feedburner&utm_medium=twitter&utm_campaign=Feed%3A+annaroi+%28%E0%B9%81%E0%B8%AD%E0%B8%99%E0%B8%AD%E0%B8%A3%E0%B9%88%E0%B8%AD%E0%B8%A2%29

Lazy Image Loader
http://www.appelsiini.net/projects/lazyload

Unzip by Java

กุมภาพันธ์ 21, 2010

package zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;

public class UnZip {

	static final int BUFFER = 2048;

	public UnZip(String input)	{
		try {
	         BufferedOutputStream dest = null;
	         BufferedInputStream is = null;
	         ZipEntry entry;
	         java.util.zip.ZipFile zipfile = new java.util.zip.ZipFile(input);
	         Enumeration e = zipfile.entries();
	         while(e.hasMoreElements()) {
	            entry = (ZipEntry) e.nextElement();
	            System.out.println("Extracting: " +entry);
	            is = new BufferedInputStream(zipfile.getInputStream(entry));
	            int count;
	            byte data[] = new byte[BUFFER];
	            FileOutputStream fos = new FileOutputStream(entry.getName());
	            dest = new BufferedOutputStream(fos, BUFFER);
	            while ((count = is.read(data, 0, BUFFER))
	              != -1) {
	               dest.write(data, 0, count);
	            }
	            dest.flush();
	            dest.close();
	            is.close();
	         }
	      } catch(Exception e) {
	         e.printStackTrace();
	      }

	}

	public static void main(String[] args) {
		String input 	= ".\\output\\compress.zip";
		UnZip app = new UnZip(input);
	}
}

Reference: http://java.sun.com/developer/technicalArticles/Programming/compression/

Zip by Java

กุมภาพันธ์ 21, 2010

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFile {

	static final int BUFFER = 2048;	

	public ZipFile(String input, String output)	{
		try	{
	         BufferedInputStream origin = null;
	         FileOutputStream dest 	= new FileOutputStream(output);
	         ZipOutputStream out 	= new ZipOutputStream(new BufferedOutputStream(dest));
	         //out.setMethod(ZipOutputStream.DEFLATED);

	         byte data[] = new byte[BUFFER];

	         // get a list of files from current directory
	         File f = new File(input);
	         String files[] = f.list();

	         for (int i=0; i<files.length; i++) {
	            System.out.println("Adding: "+files[i]);
	            FileInputStream fi = new FileInputStream(input+files[i]);
	            origin = new BufferedInputStream(fi, BUFFER);
	            ZipEntry entry = new ZipEntry(input+files[i]);
	            out.putNextEntry(entry);
	            int count;
	            while((count = origin.read(data, 0,BUFFER)) != -1) {
	               out.write(data, 0, count);
	            }
	            origin.close();
	         }
	         out.close();
	     }
		catch(Exception e) {
	         e.printStackTrace();
	     }
	}

	public static void main(String[] args) {
		String input 	= "C:\\Program Files\\Mozilla Firefox\\";
		String output 	= ".\\output\\compress.zip";
		ZipFile app 	= new ZipFile(input,output);
	}
}

Reference: http://java.sun.com/developer/technicalArticles/Programming/compression/

Class diagrams

กุมภาพันธ์ 20, 2010

Double Listeners

กุมภาพันธ์ 20, 2010

This code was implemented for testing we can add two listeners to manage same event.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DoubleListener extends Applet {

	private static final long serialVersionUID = 2371255015431000864L;

	public void init()	{
		System.out.println("Init");
		this.addMouseListener(new MyMouseListener1());
		this.addMouseListener(new MyMouseListener2());
	}

	public DoubleListener()	{
		super();
		setSize(600,900);
		setVisible(true);
	}

	public void paint(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
	}

}

class MyMouseListener1 implements MouseListener	{
	public void mouseClicked(MouseEvent e) {
		System.out.println("MyMouseListener1::click()");
	}
	public void mouseEntered(MouseEvent e) {	}
	public void mouseExited(MouseEvent e)  {	}
	public void mousePressed(MouseEvent e) {	}
	public void mouseReleased(MouseEvent e) {	}
}

class MyMouseListener2 implements MouseListener	{
	public void mouseClicked(MouseEvent e) {
		System.out.println("MyMouseListener2::click()");
	}
	public void mouseEntered(MouseEvent e) {	}
	public void mouseExited(MouseEvent e)  {	}
	public void mousePressed(MouseEvent e) {	}
	public void mouseReleased(MouseEvent e) {	}
}

Events and EventListener

กุมภาพันธ์ 18, 2010

My Simple example with 4 classes for understanding how event and eventListener work together.

------------------------------
package myevents;
import java.util.EventObject;

public class MyEvent extends EventObject {

 private static final long serialVersionUID = 5736098458773344230L;
 private Object data;

 public MyEvent(Object source, Object _data)    {
   super(source);
   data = _data;
 }

 public Object getData()    {
   return data;
 }
}
-------------------------------
package myevents;
import java.util.EventListener;

public interface MyEventListener extends EventListener    {
  public void myEventHappend(MyEvent e);
}
------------------------------
package myevents;
public class PracticalInterface implements MyEventListener    {

 @Override
 public void myEventHappend(MyEvent e) {
    System.out.println((String) e.getData());
 }
}
------------------------------
package myevents;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class AppProgram extends JFrame {

private Vector listeners = new Vector();
private JTextArea _tar  = new JTextArea(22,40);
private JButton _bt1    = new JButton("Yeah");
private JButton _bt2    = new JButton("Yoo");

public AppProgram()    {
super("Application");
setLayout(new FlowLayout());
_bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shootEvent("Yeah !!");
}
});
_bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shootEvent("Yoo !?");
}
});
this.add(_tar);
this.add(_bt1);
this.add(_bt2);
}

public void addListener(MyEventListener l)    {
listeners.add(l);
}

public void removeListener(MyEventListener l)    {
listeners.remove(l);
}

public void shootEvent(String text)    {
MyEvent me = new MyEvent(this, text);
if (listeners.size() > 0)    {
((MyEventListener)listeners.firstElement()).myEventHappend(me);
}
}

public static void main(String[] args) {
AppProgram app = new AppProgram();
PracticalInterface lis = new PracticalInterface();
app.addListener(lis);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(400,600);
app.setVisible(true);
}

}
------------------------------
Hope you like it :) 
------------------------------

Java: Run instance from Abstract class

กุมภาพันธ์ 4, 2010

public abstract class CallAbstractClass {

public static CallAbstractClass getInstance()    {return new HiddenClass();}
public abstract String print();

public static void main(String[] args) {
CallAbstractClass instance = CallAbstractClass.getInstance();
System.out.println(instance.print());
}
}

class HiddenClass extends CallAbstractClass    {
public HiddenClass()  {    }
public String print() {  return “Yeah”;  }
}

- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - -
Concept : Singleton

class Singleton  {

  private static Singleton instance = null;

  private Singleton() { ... }

  public static Singleton getInstance() {
     if (instance==null){
           instance = new Singleotn();
     }
     return instance;
  }

  public void otherMethod()  { ... }

}

Java: How to use interface

ธันวาคม 15, 2009

interface GunInterface {
public void shot();
}

class Bomber implements GunInterface {
public void shot() {
System.out.println(“Bombbb !?”);
}
}

class Bazuka implements GunInterface {
public void shot() {
System.out.println(“Bazuka”);
}
}

public class RefreshInterface {
public RefreshInterface()    {
Bomber     bomb = new Bomber();
Bazuka    bazu = new Bazuka();
GunInterface gun = bomb;
gun.shot();
gun = bazu;
gun.shot();
((GunInterface) bomb).shot();
((GunInterface) bazu).shot();
}
public static void main(String [] argv)    {
new RefreshInterface();
}
}

Follow

Get every new post delivered to your Inbox.