Archive

Archives pour la catégorie ‘Source Code’

Paint a linear gradient

28/01/2009
Commentaires fermés

/*
 *
 */
public void paint(Graphics g, Color c1, Color c2) {
        Graphics2D g2 = (Graphics2D)g;
        Paint oldPainter = g2.getPaint();
        g2.setPaint(new GradientPaint(new Point2D.Double(0, 0), c1, new Point2D.Double(getWidth(), 0), c2));
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setPaint(oldPainter);
}

/*
 * Without gradiant paint
 */
public void paint(Graphics g, Color c1, Color c2) {
    // Add gradient (This might be done before !!)
    Graphics2D g2 = (Graphics2D)g;
    for (int i = -1; i <= this.getWidth(); ++i) {
      float ratio = (float)i / (float)this.getWidth();
      int red = (int)(c2.getRed() * ratio + c1.getRed() * (1 - ratio));
      int green = (int)(c2.getGreen() * ratio + c1.getGreen() * (1 - ratio));
      int blue = (int)(c2.getBlue() * ratio + c1.getBlue() * (1 - ratio));
      Color c = new Color(red, green, blue);
      Rectangle2D r = new Rectangle2D.Float(1 + i, 0, this.getWidth(), this.getHeight());
      g2.setPaint(c);
      g2.fill(r);
    }
    // Paint using super methods
    super.paint(g);
}

Java

MySplitPaneUI (A split pane with 3 dots)

28/01/2009
Commentaires fermés

/*
 * Created on 14 avr. 2006
 */
import java.awt.Graphics;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;

/**
 * MySplitPaneUI is a UI implementation of BasicSplitPaneUI
 * that does not show any divider.
 *
<pre>
 * +----------+
 * | Changes: |
 * +----------+
 *    [19/09/2006]
 *       - Added
 * </pre>
<p> */<br />
public class MySplitPaneUI extends BasicSplitPaneUI {</p>
<p>    public static ComponentUI createUI(JComponent x) {<br />
        return new MySplitPaneUI();<br />
    }</p>
<p>    public BasicSplitPaneDivider createDefaultDivider() {<br />
        return new MySplitPaneDivider(this);<br />
    }<br />
}</p>
<p>class MySplitPaneDivider extends BasicSplitPaneDivider {<br />
    float strokeThickness = 2f;<br />
    float miterLimit = 2f;<br />
    float[] dashPattern = {2f};<br />
    float dashPhase = 2f;</p>
<p>    public MySplitPaneDivider(BasicSplitPaneUI ui) {<br />
        super(ui);<br />
        ui.getSplitPane().setContinuousLayout(true);<br />
    }</p>
<p>    protected class DragController extends BasicSplitPaneDivider.DragController {<br />
        public DragController(MouseEvent event) {<br />
            super(event);<br />
        }<br />
    }</p>
<p>    public void paint(Graphics g) {<br />
        // do not paint any thing ! if you want an invisible split pane (like Eclipse)<br />
        // Paint 3 dots<br />
        Graphics2D gg = (Graphics2D)g;<br />
        gg.setColor(Color.darkGray);<br />
        gg.setStroke(new BasicStroke(strokeThickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, miterLimit, dashPattern, dashPhase));        </p>
<p>        if(this.splitPane.getOrientation() == JSplitPane.VERTICAL_SPLIT) {<br />
            g.drawLine(getWidth()/2-7, getHeight()/2, getWidth()/2+7, getHeight()/2);<br />
        } else {<br />
            g.drawLine(getWidth()/2, getHeight()/2-7, getWidth()/2, getHeight()/2+7);<br />
        }<br />
    }<br />
}<br />

Java

Paint an arrow extremity in Java 2D

28/01/2009
Commentaires fermés

protected void paintArrowExtremity(Graphics2D g, int x1, int y1, int x2, int y2, double factor) {
        // Angles (téta)
        double theta;
        double thetabis = 0;
        // Angle de la pointe
        double alpha = Math.toRadians(90-30);
        // Longueur de la fl?che
        int l = (int) (7*factor);  

        int x[] = new int[3];
        int y[] = new int[3];

        theta = (x2-x1)/(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
        thetabis = Math.acos(theta);

        if (y1<y2)
        {
             x[0]=x2;
             x[1]=(int) (l*Math.cos(thetabis-Math.PI/2-alpha)+x2);
             x[2]=(int) (l*Math.cos(thetabis+Math.PI/2+alpha)+x2);
             y[0]=y2;
             y[1]=(int) (l*Math.sin(thetabis-Math.PI/2-alpha)+y2);
             y[2]=(int) (l*Math.sin(thetabis+Math.PI/2+alpha)+y2);
        }
        else
        {
             x[0]=x2;
             x[1]=(int) (l*Math.cos(thetabis-Math.PI/2-alpha)+x2);
             x[2]=(int) (l*Math.cos(thetabis+Math.PI/2+alpha)+x2);
             y[0]=y2;
             y[1]=y2+(y2-(int) (l*Math.sin(thetabis-Math.PI/2-alpha)+y2));
             y[2]=y2+(y2-(int) (l*Math.sin(thetabis+Math.PI/2+alpha)+y2));
        }

        g.fillPolygon(x, y, 3);
}

Java

Litle RMI howto

28/01/2009
Commentaires fermés

Dans le main du serveur:


LocateRegistry.createRegistry(9999);
System.setSecurityManager(new RMISecurityManager());
CalculatorImpl calc = new CalculatorImpl();
Naming.rebind("//localhost:9999/calculator", calc);

Coté client:


Calculator calc = (Calculator) Naming.lookup("//" + serverIP + ":" + portServer + "/calculator");
System.out.println(calc.add(2, 5));

Exécution:


java -cp $CLASSPATH -Djava.security.policy=policy.txt ServerMainClass

Java

Add a tag extractor to string prototype

28/01/2009
Commentaires fermés

String.prototype.extractTags = function(tag) {
       var matchAll = new RegExp('(?:<'+tag+'[^>]*?>)((\n|\r|.)*?)(?:</'+tag+'>)', 'img');
       var matchOne = new RegExp('(?:<'+tag+'[^>]*?>)((\n|\r|.)*?)(?:</'+tag+'>)', 'im');
       var tags = [];
       var matches = this.match(matchAll) || [];
       for (var i=0; i<matches.length; ++i) {
               tags.push((matches[i].match(matchOne) || ['', ''])[1]);
       }
       return tags;
}

JavaScript

cssStylesheetManager.js # Javascript library that manage alternate CSS stylesheet.

28/01/2009
Commentaires fermés

/**
 * Javascript library that manage alternate CSS stylesheet.
 * FileName: cssstylesheetmanager.js
 * Date:     15/11/2006
 * Author:   Gabriel DROMARD
 * Require:  Prototype.js
 */

/**
 * Set current CSS Stylesheet.
 */
Event.observe(window, 'load', setCurrentCSSStylesheet, false);

/**
 * Set active CSS Stylesheet.
 * @param title The CSS stylesheet to activate
 */
function setActiveCSSStylesheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
	setCookie('PingusActiveStyleSheet', title);
}

/**
 * Active an already set CSS Stylesheet.
 * (This function use cookies)
 */
function setCurrentCSSStylesheet() {
	var current = getCookie('PingusActiveStyleSheet');
	if(current != undefined && current != null)
		setActiveCSSStylesheet(current);
}

JavaScript

Manage special characters

28/01/2009
Commentaires fermés

/**
 * This library is design to manage characters, replacements, encoding ...
 *
 * This javascript needs prototype.js v1.4
 */
var characters = [
//	{ decimal: '&#34', symbol: 'A DOUBLE QUOTE HERE', entity: '"', octal: '42', hex: '0x22' },
	{ decimal: '&#38', symbol: '&', entity: '&', octal: '46', hex: '0x26' },
	{ decimal: '&#60', symbol: '<', entity: '<', octal: '74', hex: '0x3C' },
	{ decimal: '&#62', symbol: '>', entity: '>', octal: '76', hex: '0x3E' },
	{ decimal: '&#160', symbol: ' ', entity: '&nbsp;', octal: '240', hex: '0xA0' },
	{ decimal: '&#161', symbol: '?', entity: '&iexcl;', octal: '241', hex: '0xA1' },
	{ decimal: '&#162', symbol: '?', entity: '&cent;', octal: '242', hex: '0xA2' },
	{ decimal: '&#163', symbol: '?', entity: '&pound;', octal: '243', hex: '0xA3' },
	{ decimal: '&#164', symbol: '¤', entity: '&curren;', octal: '244', hex: '0xA4' },
	{ decimal: '&#165', symbol: '?', entity: '&yen;', octal: '245', hex: '0xA5' },
	{ decimal: '&#166', symbol: '?', entity: '&brvbar;', octal: '246', hex: '0xA6' },
	{ decimal: '&#167', symbol: '§', entity: '&sect;', octal: '247', hex: '0xA7' },
	{ decimal: '&#168', symbol: '¨', entity: '&uml;', octal: '250', hex: '0xA8' },
	{ decimal: '&#169', symbol: '?', entity: '&copy;', octal: '251', hex: '0xA9' },
	{ decimal: '&#170', symbol: '?', entity: '&ordf;', octal: '252', hex: '0xAA' },
	{ decimal: '&#171', symbol: '?', entity: '&laquo;', octal: '253', hex: '0xAB' },
	{ decimal: '&#172', symbol: '?', entity: '&not;', octal: '254', hex: '0xAC' },
	{ decimal: '&#173', symbol: '­', entity: '&shy;', octal: '255', hex: '0xAD' },
	{ decimal: '&#174', symbol: '?', entity: '&reg;', octal: '256', hex: '0xAE' },
	{ decimal: '&#175', symbol: '?', entity: '&macr;', octal: '257', hex: '0xAF' },
	{ decimal: '&#176', symbol: '°', entity: '&deg;', octal: '260', hex: '0xB0' },
	{ decimal: '&#177', symbol: '?', entity: '&plusmn;', octal: '261', hex: '0xB1' },
	{ decimal: '&#178', symbol: '?', entity: '&sup2;', octal: '262', hex: '0xB2' },
	{ decimal: '&#179', symbol: '?', entity: '&sup3;', octal: '263', hex: '0xB3' },
	{ decimal: '&#180', symbol: '´', entity: '&acute;', octal: '264', hex: '0xB4' },
	{ decimal: '&#181', symbol: '?', entity: '&micro;', octal: '265', hex: '0xB5' },
	{ decimal: '&#182', symbol: '?', entity: '&para;', octal: '266', hex: '0xB6' },
	{ decimal: '&#183', symbol: '?', entity: '&middot;', octal: '267', hex: '0xB7' },
	{ decimal: '&#184', symbol: '¸', entity: '&cedil;', octal: '270', hex: '0xB8' },
	{ decimal: '&#185', symbol: '?', entity: '&sup1;', octal: '271', hex: '0xB9' },
	{ decimal: '&#186', symbol: '?', entity: '&ordm;', octal: '272', hex: '0xBA' },
	{ decimal: '&#187', symbol: '?', entity: '&raquo;', octal: '273', hex: '0xBB' },
	{ decimal: '&#188', symbol: '?', entity: '&frac14;', octal: '274', hex: '0xBC' },
	{ decimal: '&#189', symbol: '?', entity: '&frac12;', octal: '275', hex: '0xBD' },
	{ decimal: '&#190', symbol: '?', entity: '&frac34;', octal: '276', hex: '0xBE' },
	{ decimal: '&#191', symbol: '?', entity: '&iquest;', octal: '277', hex: '0xBF' },
	{ decimal: '&#192', symbol: '?', entity: '&Agrave;', octal: '300', hex: '0xC0' },
	{ decimal: '&#193', symbol: '??', entity: '&Aacute;', octal: '301', hex: '0xC1' },
	{ decimal: '&#194', symbol: '??', entity: '&Acirc;', octal: '302', hex: '0xC2' },
	{ decimal: '&#195', symbol: '?', entity: '&Atilde;', octal: '303', hex: '0xC3' },
	{ decimal: '&#196', symbol: '??', entity: '&Auml;', octal: '304', hex: '0xC4' },
	{ decimal: '&#197', symbol: '?', entity: '&Aring;', octal: '305', hex: '0xC5' },
	{ decimal: '&#198', symbol: '?', entity: '&AElig;', octal: '306', hex: '0xC6' },
	{ decimal: '&#199', symbol: '??', entity: '&Ccedil;', octal: '307', hex: '0xC7' },
	{ decimal: '&#200', symbol: '?', entity: '&Egrave;', octal: '310', hex: '0xC8' },
	{ decimal: '&#201', symbol: '??', entity: '&Eacute;', octal: '311', hex: '0xC9' },
	{ decimal: '&#202', symbol: '?', entity: '&Ecirc;', octal: '312', hex: '0xCA' },
	{ decimal: '&#203', symbol: '??', entity: '&Euml;', octal: '313', hex: '0xCB' },
	{ decimal: '&#204', symbol: '?', entity: '&Igrave;', octal: '314', hex: '0xCC' },
	{ decimal: '&#205', symbol: '??', entity: '&Iacute;', octal: '315', hex: '0xCD' },
	{ decimal: '&#206', symbol: '??', entity: '&Icirc;', octal: '316', hex: '0xCE' },
	{ decimal: '&#207', symbol: '?', entity: '&Iuml;', octal: '317', hex: '0xCF' },
	{ decimal: '&#208', symbol: '?', entity: '&ETH;', octal: '320', hex: '0xD0' },
	{ decimal: '&#209', symbol: '?', entity: '&Ntilde;', octal: '321', hex: '0xD1' },
	{ decimal: '&#210', symbol: '?', entity: '&Ograve;', octal: '322', hex: '0xD2' },
	{ decimal: '&#211', symbol: '??', entity: '&Oacute;', octal: '323', hex: '0xD3' },
	{ decimal: '&#212', symbol: '??', entity: '&Ocirc;', octal: '324', hex: '0xD4' },
	{ decimal: '&#213', symbol: '?', entity: '&Otilde;', octal: '325', hex: '0xD5' },
	{ decimal: '&#214', symbol: '??', entity: '&Ouml;', octal: '326', hex: '0xD6' },
	{ decimal: '&#215', symbol: '??', entity: '&times;', octal: '327', hex: '0xD7' },
	{ decimal: '&#216', symbol: '?', entity: '&Oslash;', octal: '330', hex: '0xD8' },
	{ decimal: '&#217', symbol: '?', entity: '&Ugrave;', octal: '331', hex: '0xD9' },
	{ decimal: '&#218', symbol: '??', entity: '&Uacute;', octal: '332', hex: '0xDA' },
	{ decimal: '&#219', symbol: '?', entity: '&Ucirc;', octal: '333', hex: '0xDB' },
	{ decimal: '&#220', symbol: '??', entity: '&Uuml;', octal: '334', hex: '0xDC' },
	{ decimal: '&#221', symbol: '??', entity: '&Yacute;', octal: '335', hex: '0xDD' },
	{ decimal: '&#222', symbol: '?', entity: '&THORN;', octal: '336', hex: '0xDE' },
	{ decimal: '&#223', symbol: '??', entity: '&szlig;', octal: '337', hex: '0xDF' },
	{ decimal: '&#224', symbol: '?', entity: '&agrave;', octal: '340', hex: '0xE0' },
	{ decimal: '&#225', symbol: 'á', entity: '&aacute;', octal: '341', hex: '0xE1' },
	{ decimal: '&#226', symbol: 'â', entity: '&acirc;', octal: '342', hex: '0xE2' },
	{ decimal: '&#227', symbol: '?', entity: '&atilde;', octal: '343', hex: '0xE3' },
	{ decimal: '&#228', symbol: 'ä', entity: '&auml;', octal: '344', hex: '0xE4' },
	{ decimal: '&#229', symbol: '?', entity: '&aring;', octal: '345', hex: '0xE5' },
	{ decimal: '&#230', symbol: '?', entity: '&aelig;', octal: '346', hex: '0xE6' },
	{ decimal: '&#231', symbol: 'ç', entity: '&ccedil;', octal: '347', hex: '0xE7' },
	{ decimal: '&#232', symbol: '?', entity: '&egrave;', octal: '350', hex: '0xE8' },
	{ decimal: '&#233', symbol: 'é', entity: '&eacute;', octal: '351', hex: '0xE9' },
	{ decimal: '&#234', symbol: '?', entity: '&ecirc;', octal: '352', hex: '0xEA' },
	{ decimal: '&#235', symbol: 'ë', entity: '&euml;', octal: '353', hex: '0xEB' },
	{ decimal: '&#236', symbol: '?', entity: '&igrave;', octal: '354', hex: '0xEC' },
	{ decimal: '&#237', symbol: 'í', entity: '&iacute;', octal: '355', hex: '0xED' },
	{ decimal: '&#238', symbol: 'î', entity: '&icirc;', octal: '356', hex: '0xEE' },
	{ decimal: '&#239', symbol: '?', entity: '&iuml;', octal: '357', hex: '0xEF' },
	{ decimal: '&#240', symbol: '?', entity: '&eth;', octal: '360', hex: '0xF0' },
	{ decimal: '&#241', symbol: '?', entity: '&ntilde;', octal: '361', hex: '0xF1' },
	{ decimal: '&#242', symbol: '?', entity: '&ograve;', octal: '362', hex: '0xF2' },
	{ decimal: '&#243', symbol: 'ó', entity: '&oacute;', octal: '363', hex: '0xF3' },
	{ decimal: '&#244', symbol: 'ô', entity: '&ocirc;', octal: '364', hex: '0xF4' },
	{ decimal: '&#245', symbol: '?', entity: '&otilde;', octal: '365', hex: '0xF5' },
	{ decimal: '&#246', symbol: 'ö', entity: '&ouml;', octal: '366', hex: '0xF6' },
	{ decimal: '&#247', symbol: '÷', entity: '&divide;', octal: '367', hex: '0xF7' },
	{ decimal: '&#248', symbol: '?', entity: '&oslash;', octal: '370', hex: '0xF8' },
	{ decimal: '&#249', symbol: '?', entity: '&ugrave;', octal: '371', hex: '0xF9' },
	{ decimal: '&#250', symbol: 'ú', entity: '&uacute;', octal: '372', hex: '0xFA' },
	{ decimal: '&#251', symbol: '?', entity: '&ucirc;', octal: '373', hex: '0xFB' },
	{ decimal: '&#252', symbol: 'ü', entity: '&uuml;', octal: '374', hex: '0xFC' },
	{ decimal: '&#253', symbol: 'ý', entity: '&yacute;', octal: '375', hex: '0xFD' },
	{ decimal: '&#254', symbol: '?', entity: '&thorn;', octal: '376', hex: '0xFE' },
	{ decimal: '&#255', symbol: '?', entity: '&yuml;', octal: '377', hex: '0xFF' }
);

var escapeCharacters = function(string) {
	var toReturn = string;
	for(charIndex=0; charIndex<characters.length; ++charIndex)
		toReturn = toReturn.replace( characters[ charIndex ].symbol, characters[ charIndex ].entity );
	return toReturn;
};

var encodeHtml = function(htmlString) {
	var encodedHtml = escape(htmlString);
	encodedHtml = encodedHtml.replace(///g,"%2F");
	encodedHtml = encodedHtml.replace(/?/g,"%3F");
	encodedHtml = encodedHtml.replace(/=/g,"%3D");
	encodedHtml = encodedHtml.replace(/&/g,"%26");
	encodedHtml = encodedHtml.replace(/@/g,"%40");
	return encodedHtml;
};

JavaScript

CookieManager.js # Cookies management Javascript tool library.

28/01/2009
Commentaires fermés

/**
 * Cookies management Javascript tool library.
 * FileName: cookiemanager.js
 * Date:     15/11/2006
 * Author:   Gabriel DROMARD
 */

/**
 * Retreive a cookie.
 * @param name The cookie name.
 */
var getCookie = function(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			var endstr = document.cookie.indexOf (";", j);
			if (endstr == -1) endstr = document.cookie.length;
			return unescape(document.cookie.substring(j, endstr));
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
};

/**
 * Store a cookie.
 * @param name    Cookie name
 * @param value   Cookie value
 * @param expires (optional) the date on which the cookie will expire (default is current date + 300 days)
 * @param path    (optional) the cookie path
 * @param domain  (optional) the cookie domain
 * @param secure  (optional) Is the cookie secure ? (default value: false)
 */
var setCookie = function(name, value) {
	//alert('setCookie('+name+' = '+value+')');
	var expDays = 300;
	var exp = new Date();
	exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : exp;
	var path    = (argc > 3) ? argv[3] : null;
	var domain  = (argc > 4) ? argv[4] : null;
	var secure  = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" :    ("; path=" + path)) +
	((domain == null) ? "" :  ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
};

/**
 * Delete an existing cookie.
 * @param the cookie name.
 */
var deleteCookie = function (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = getCookie (name);
	if(cval != undefined) document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
};

JavaScript

PicassXMLAlbum.js # Javascript library that handle Picassa XML output

28/01/2009
Commentaires fermés

/*
 * --------------------------------------------------------------------------
 * PicassaXMLAlbum Javascript library, version 1.0
 * (c) 2006 Gabriel DROMARD <gabriel_dromard@yahoo.fr>
 *
 *
 * This library is build under album XML output generated by Picassa v2.1.0
 * This library require prototype.js v1.4.0
 * --------------------------------------------------------------------------
 */

/*
 * Exemple of use:
 *
 * --------------------------------------------------------------------------
myAjaxrequest = new Ajax.Request( 'http://localhost/MyAlbum/index.xml', { method: 'get', onFailure: displayThumbnailsError, onComplete: setThumbnails});

function setThumbnails() {
	var album = new PicassaXMLAlbum(request.responseText);
	var testElmt = document.getElementById('test');
	testElmt.innerHTML = 'Album Name: '+album.getName()+'<br>';
	testElmt.innerHTML += 'Album nb images: '+album.getImageCount();
	var images = album.getImages();
	for(i=0; i<images.length; ++i) {
		testElmt.innerHTML += '<br>Images n°'+i+' '+images[i].getThumbnail();
	}
}

function displayThumbnailsError(error) {
	document.getElementById('test').innerHTML = '<font color="darkred"><b>Sorry. There was an error while loading thumbnails. </b>('+error+')</font>';
}
 * --------------------------------------------------------------------------
 *
 *
 */

/**
 * PicassaXMLAlbum Object that represent the album object.
 */
var PicassaXMLAlbum = Class.create();

PicassaXMLAlbum.prototype = {

	/**
	 * Constructor
	 * @param xml The XML string representing the Picassa XML album file content.
	 */
	initialize: function(url, options) {
		this.xml = url;
		this.options = options;
	},

	/**
	 * Retreive the album name.
	 */
	dispatchError: function(error) {
		try {
			if(typeof(this.options.onFailure) == 'function') {
				this.options.onFailure(error);
			} else {
				alert(error);
			}
		} catch(e) {
			alert(error);
		}
	},

	/**
	 * Retreive the album name.
	 */
	getName: function() {
		try {
			if(this.name == undefined) {
				this.name = RegExp('<albumName>([^<>]*)</albumName>').exec(this.xml)[1];
			}
			return this.name;
		} catch(e) {
			this.dispatchError(e);
		}
	},

	/**
	 * Retreive the image count of the album.
	 */
	getImageCount: function() {
		try {
			if(this.imageCount == undefined) {
				this.imageCount = RegExp('<albumItemCount>([^<>]*)</albumItemCount>').exec(this.xml)[1];
			}
			return this.imageCount;
		} catch(e) {
			this.dispatchError(e);
		}
	},

	/**
	 * Get images of the albums.
	 * @return an array of PicassaXMLImage objects.
	 */
	getImages: function() {
		try {
			if(this.images == undefined) {
				var imagesArray = new Array();
				var xmlTmp = this.xml;
				for(i=0; i<this.getImageCount(); ++i) {
					var begin = xmlTmp.indexOf('<image>');
					xmlTmp = xmlTmp.substr(begin);
					var end = xmlTmp.indexOf('</image>');
					imagesArray[i] = xmlTmp.substr(0, end);
					xmlTmp = xmlTmp.substr(end);
				}
				this.images = new Array();
				for(i=0; i<imagesArray.length; ++i) {
					var image = new PicassaXMLImage(imagesArray[i], {onFailure: this.dispatchError});
					this.images[i] = image;
				}
			}
			return this.images;
		} catch(e) {
			this.dispatchError(e);
		}
	}
};

/**
 * PicassaXMLImage object that represent one image.
 */
var PicassaXMLImage = Class.create();

PicassaXMLImage.prototype = {

	/**
	 * Constructor
	 */
	initialize: function(xml, options) {
		this.xml = xml;
		this.options = options;
	},

	/**
	 * Is it the first image ?
	 */
	isFirstImage: function() {
		try {
			if(this.firstImage == undefined) {
				this.firstImage = RegExp('<isFirstImage>([^<>]*)</isFirstImage>').exec(this.xml)[1];
			}
			return this.firstImage;
		} catch(e) {
			this.dispatchError(e);
		}
	},

	/**
	 * Is it the last image ?
	 */
	isLastImage: function() {
		try {
			if(this.lastImage == undefined) {
				this.lastImage = RegExp('<isLastImage>([^<>]*)</isLastImage>').exec(this.xml)[1];
			}
			return this.lastImage;
		} catch(e) {
			this.dispatchError(e);
		}
	},

	/**
	 * Get the image url.
	 */
	getImage: function() {
		if(this.image == undefined) {
			this.image = RegExp('<itemLargeImage>([^<>]*)</itemLargeImage>').exec(this.xml)[1];
		}
		return this.image;
	},

	/**
	 * Get the image name.
	 */
	getName: function() {
		if(this.name == undefined) {
			this.name = RegExp('<itemName>([^<>]*)</itemName>').exec(this.xml)[1];
		}
		return this.name;
	},

	/**
	 * Get the image caption.
	 */
	getCaption: function() {
		if(this.caption == undefined) {
			this.caption = RegExp('<itemCaption>([^<>]*)</itemCaption>').exec(this.xml)[1];
		}
		return this.caption;
	},

	/**
	 * Get the width of image.
	 */
	getImageWidth: function() {
		if(this.imageWidth == undefined) {
			this.imageWidth = RegExp('<itemWidth>([^<>]*)</itemWidth>').exec(this.xml)[1];
		}
		return this.imageWidth;
	},

	/**
	 * Get the width of image.
	 */
	getImageHeight: function() {
		if(this.imageHeight == undefined) {
			this.imageHeight = RegExp('<itemHeight>([^<>]*)</itemHeight>').exec(this.xml)[1];
		}
		return this.imageHeight;
	},

	/**
	 * Get the name of the previous image.
	 */
	getPrevImageName: function() {
		if(this.prevImageName == undefined) {
			this.prevImageName = RegExp('<prevImage>([^<>]*)</prevImage>').exec(this.xml)[1];
		}
		return this.prevImageName;
	},

	/**
	 * Get the name of the next image.
	 */
	getNextImageName: function() {
		if(this.nextImageName == undefined) {
			this.nextImageName = RegExp('<nextImage>([^<>]*)</nextImage>').exec(this.xml)[1];
		}
		return this.nextImageName;
	},

	/**
	 * Get the url of the image thumbnail.
	 */
	getThumbnail: function() {
		if(this.thumbnail == undefined) {
			this.thumbnail = RegExp('<itemThumbnailImage>([^<>]*)</itemThumbnailImage>').exec(this.xml)[1];
		}
		return this.thumbnail;
	},

	/**
	 * Get the width of image.
	 */
	getThumbnailWidth: function() {
		if(this.thumbnailWidth == undefined) {
			this.thumbnailWidth = RegExp('<itemThumbnailWidth>([^<>]*)</itemThumbnailWidth>').exec(this.xml)[1];
		}
		return this.thumbnailWidth;
	},

	/**
	 * Get the width of image.
	 */
	getThumbnailHeight: function() {
		if(this.thumbnailHeight == undefined) {
			this.thumbnailHeight = RegExp('<itemThumbnailHeight>([^<>]*)</itemThumbnailHeight>').exec(this.xml)[1];
		}
		return this.thumbnailHeight;
	},

	/**
	 * Retreive the album name.
	 */
	dispatchError: function(error) {
		try {
			if(typeof(this.options.onFailure) == 'function') {
				this.options.onFailure(error);
			} else {
				alert(error);
			}
		} catch(e) {
			alert(error);
		}
	}
};

JavaScript

A IE script to make it compatible with CSS hover

28/01/2009
Commentaires fermés

<attach event="ondocumentready" handler="parseStylesheets" />
<script language="JScript">
	/**
	 *	Pseudos - V1.30.050121 - hover & active
	 *	---------------------------------------------
	 *	Peterned - http://www.xs4all.nl/~peterned/
	 *	(c) 2005 - Peter Nederlof
	 *
	 *	Credits  - Arnoud Berendsen
	 *	         - Martin Reurings
	 *           - Robert Hanson
	 *
	 *	howto: body { behavior:url("csshover.htc"); }
	 *	---------------------------------------------
	 */

	var currentSheet, doc = window.document, activators = {
		onhover:{on:'onmouseover', off:'onmouseout'},
		onactive:{on:'onmousedown', off:'onmouseup'}
	}

	function parseStylesheets() {
		var sheets = doc.styleSheets, l = sheets.length;
		for(var i=0; i<l; i++)
			parseStylesheet(sheets[i]);
	}

	function parseStylesheet(sheet) {
		if(sheet.imports) {
			try {
				var imports = sheet.imports, l = imports.length;
				for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
			} catch(securityException) {}
		}

		try {
			var rules = (currentSheet = sheet).rules, l = rules.length;
			for(var j=0; j<l; j++) parseCSSRule(rules[j]);
		} catch(securityException){}
	}

	function parseCSSRule(rule) {
		var select = rule.selectorText, style = rule.style.cssText;
		if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;

		var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
		var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
		var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
		var affected = select.replace(/:hover.*$/, '');
		var elements = getElementsBySelect(affected);

		currentSheet.addRule(newSelect, style);
		for(var i=0; i<elements.length; i++) new HoverElement(elements[i], className, activators[pseudo]);
	}

	function HoverElement(node, className, events) {
		if(!node.hovers) node.hovers = {};
		if(node.hovers[className]) return;
		node.hovers[className] = true;
		node.attachEvent(events.on,
			function() { node.className += ' ' + className; });
		node.attachEvent(events.off,
			function() { node.className = node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
	}

	function getElementsBySelect(rule) {
		var parts, nodes = [doc];
		parts = rule.split(' ');
		for(var i=0; i<parts.length; i++) {
			nodes = getSelectedNodes(parts[i], nodes);
		}
		return nodes;
	}

	function getSelectedNodes(select, elements) {
		var result, node, nodes = [];
		var classname = (/\.([a-z0-9_-]+)/i).exec(select);
		var identify = (/\#([a-z0-9_-]+)/i).exec(select);
		var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
		for(var i=0; i<elements.length; i++) {
			result = tagName? elements[i].all.tags(tagName):elements[i].all;
			for(var j=0; j<result.length; j++) {
				node = result[j];
				if((identify && node.id != identify[1]) || (classname && !(new RegExp('\\b' + classname[1] + '\\b').exec(node.className)))) continue;
				nodes[nodes.length] = node;
			}
		}
		return nodes;
	}
</script>

JavaScript