view src/de/mpg/mpiwg/itgroup/digilib/digiImage/DigiImage.java @ 9:e63a64652f4d

added comments
author dwinter
date Mon, 03 Jan 2011 16:53:48 +0100
parents 83c58ea33792
children 2f47c136b8a6
line wrap: on
line source

package de.mpg.mpiwg.itgroup.digilib.digiImage;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import de.mpg.mpiwg.itgroup.digilib.manipulator.extensions.ZoomAreaRectangleListener;




/**
 * Refers to an Digilib-Scaler Object either an image or an folder. The object contains an swt-label object, so that it can be embedded e.g. in Eclipse.
 * @author dwinter
 *
 */
public class DigiImage implements IDigiImage {
	
	/** 
	 * Implemens a connection to Digilib.
	 */
    
	
	/* some parameters should not occur in the URL constructed by {@link #createUrlFromParameter(DigiImageParameter)} if set to negative values.
	 * 
	 */
	private static String[] omitIfNegativeArray = {"dw","dh","ddpi","ddpix","ddpiy"}; 
	private static List<String> omitIfNegative = Arrays.asList(omitIfNegativeArray);
	
	
	private String baseUrl="http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler";
		
	/**
	 * Get the Url of the digilib scaler, set to http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler, if not changed. 
	 * TODO should be part of a configuration/preference file
	 * @return
	 */
	public String getBaseUrl() {
		return baseUrl;
	}

	/**
	 * Setze die Basis url des Digilib-Scalers
	 * @param baseUrl
	 */
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	private Label label;
	
	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getLabel()
	 */
	public Label getLabel() {
		return label;
	}

	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setLabel(org.eclipse.swt.widgets.Label)
	 */
	public void setLabel(Label label) {
		this.label = label;
	}

	//private URL url;
	private Composite parent;
	
	private DigiImageParameter dp;
	
	
	/**
	 * 
	 * @param parent Composite where the image should be become a child of.
	 * @param style SWT Style how to display the image as a composite. For more the the style parameter at @see import org.eclipse.swt.widgets.Label
	 * @param dp Set of Parameters which define how the image should be displayed.
	 */
	public DigiImage(Composite parent, int style, DigiImageParameter dp) {
		//super(parent,style);
		label = new Label(parent, style);
		this.parent = parent;
		this.dp=dp;
		try {
			setNewURL(createUrlFromParameter(dp));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * Sets the complete URL of the manipulated image and changes the image object in the composite.
	 * @param url
	 */
	private void setNewURL(URL url) {
		//this.url = url;
		
	
		InputStream is;
		try {
			is = url.openStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		Image img = new Image(parent.getDisplay(),is);
		//Label label = new Label(parent, SWT.None);
		label.setImage(img);
		
		label.setVisible(true);
	}

	
	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getParent()
	 */
	public Composite getParent() {
		return parent;
	}


	/**
	 * Creates the URL to digilib from the given parameter set. For the documentation @see {@link http://developer.berlios.de/docman/display_doc.php?docid=106&group_id=251}.
	 * @param dp
	 * @return
	 * @throws MalformedURLException
	 */
	private URL createUrlFromParameter(DigiImageParameter dp) throws MalformedURLException{
		
		Map<String,String> qa= new HashMap<String,String>();
		
		setPm(qa,"fn",dp.getFn());
		setPm(qa,"pn",dp.getPn());
		
		setPm(qa,"dw",dp.getDw());
		setPm(qa,"dh",dp.getDh());
		
		setPm(qa,"wx",dp.getWx());
		setPm(qa,"wy",dp.getWy());
		setPm(qa,"ws",dp.getWs());
		setPm(qa,"ww",dp.getWw());
		setPm(qa,"wh",dp.getWh());
		
		
		setPm(qa,"mo",dp.getMo());
		
		setPm(qa,"cont",dp.getCont());
		setPm(qa,"brgt",dp.getBrgt());
		
		setPm(qa,"rot",dp.getRot());
		
		setPm(qa,"rgba",dp.getRgba());
		setPm(qa,"rgbm",dp.getRgbm());
		
		setPm(qa,"ddpi",dp.getDdpi());
		setPm(qa,"ddpix",dp.getDdpix());
		setPm(qa,"ddpiy",dp.getDdpiy());
		
		List<String> queryArray = new ArrayList<String>();
		for(String key:qa.keySet()){
			queryArray.add(key+"="+qa.get(key));
		}
		String queryString = StringUtils.join(queryArray,"&");
		
		String stringUrl=baseUrl+"?"+queryString;
		return new URL(stringUrl);
	}
	
	/**
	 * Puts the parameter as key-value  into the map repecting the setting in @see {@link #omitIfNegativeArray}.
	 * @param qa
	 * @param key
	 * @param value
	 */
	private void setPm(Map<String, String> qa, String key, Object value){
		String v;
		if (String.class.isInstance(value)){
			v=(String)value;
			qa.put(key, v);
		} else if (Integer.class.isInstance(value)){
			if (!((Integer)value==-1 & omitIfNegative.indexOf(key)>-1)) { // nicht vernachlaessigen
				v=String.valueOf(value);
				qa.put(key, v);
			}
		} else {
			v=String.valueOf(value);
			qa.put(key, v);
		}
		
	}

	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getParameter()
	 */
	public DigiImageParameter getParameter() {
		
		return dp;
	}

	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setParameter(de.mpg.mpiwg.itgroup.digilib.digiImage.DigiImageParameter)
	 */
	public void setParameter(DigiImageParameter dp) {
		this.dp=dp;
		
	}
	
	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#redraw()
	 */
	public void redraw(){
		try {
			setNewURL(createUrlFromParameter(dp));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private ZoomAreaRectangleListener rectangleListener=null;
	
	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getRectangleListener()
	 */
	public ZoomAreaRectangleListener getRectangleListener() {
	
		return rectangleListener;
	}

	/* (non-Javadoc)
	 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setRectangleListener(de.mpg.mpiwg.itgroup.digilib.manipulator.extensions.RectangleListener)
	 */
	public void setRectangleListener(ZoomAreaRectangleListener rectangleListener) {
		this.rectangleListener = rectangleListener;
	}

}