view src/de/mpg/mpiwg/itgroup/digilib/digiImage/properties/DigiImagePropertyTabImageSize.java @ 30:207dae29cc09

new tabs
author dwinter
date Mon, 10 Oct 2011 13:51:14 +0200
parents
children abcce1110d84
line wrap: on
line source

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

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import de.mpg.mpiwg.itgroup.digilib.digiImage.DigiImage;
import de.mpg.mpiwg.itgroup.digilib.digiImage.DigiImageParameter;
import de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage;

/**
 * @author dwinter Shows the properties of a digiImage
 * 
 */
public class DigiImagePropertyTabImageSize extends AbstractPropertySection {

	private IDigiImage digiImage;
	private Map<String, Text> texts = new HashMap<String, Text>();

	private Map<String, ModifyListener> listeners = new HashMap<String, ModifyListener>();
	private Button button;

	
	//private String labelString = "wh:wh;rgba:rgba;dW:dw;mo:mo;rot:rot;ddpi:ddpi;ddpix:ddpix;ddpiy:ddpiy;brgt:brgt;ws:ws;rgbm:rgbm;wy:wy;wx:ww;dh:dh;pn:pn;cont:cont;fn:fn";
	public String labelString = "wh:wh;dw:dw;ws:ws;wy:wy;wx:ww;dh:dh";

	private Map<String, String> parseLabelString(String labelString) {
		HashMap<String, String> ret = new HashMap<String, String>();
		String[] splitted = labelString.split(";");
		for (int i = 0; i < splitted.length; i++) {
			String entry = splitted[i];
			String[] values = entry.split(":");
			if (values.length == 2)
				ret.put(values[0], values[1]);
		}
		return ret;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls
	 * (org.eclipse.swt.widgets.Composite,
	 * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		Map<String, String> labels = parseLabelString(labelString);
			
		DigiImageParameter dp = new DigiImageParameter("");
		GridLayout gridLayout = new GridLayout(1, false);
		parent.setLayout(gridLayout);
		
		for (String key : dp.createMapFromParameters(null).keySet()) {
			if (labels.containsKey(key)){
				
			Composite composite = getWidgetFactory().createFlatFormComposite(
					parent);
			FormData data;

			Text t = getWidgetFactory().createText(composite, "");
			texts.put(key, t);
			data = new FormData();
			data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
			data.right = new FormAttachment(100, 0);
			data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
			t.setLayoutData(data);

			CLabel labelLabel = getWidgetFactory().createCLabel(composite, labels.get(key)); //$NON-NLS-1$
			data = new FormData();
			data.left = new FormAttachment(0, 0);
			data.right = new FormAttachment(t, -ITabbedPropertyConstants.HSPACE);
			data.top = new FormAttachment(t, 0, SWT.CENTER);
			labelLabel.setLayoutData(data);
			}
		}
		Composite composite = getWidgetFactory()
				.createFlatFormComposite(parent);
		button = getWidgetFactory().createButton(composite, "redraw", SWT.None);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput
	 * (org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);

		// Assert.isTrue(selection instanceof IStructuredSelection);
		assert (IStructuredSelection.class.isInstance(selection) == true) : "wrong instance type";

		Object input = ((IStructuredSelection) selection).getFirstElement();
		// Assert.isTrue(input instanceof DigiImage);

		assert (DigiImage.class.isInstance(input) == true) : "wrong instance type for input";
		this.digiImage = (IDigiImage) input;

		// Map<String, String> pms =
		// digiImage.getParameter().createMapFromParameters(null);

		Map<String, String> labels = parseLabelString(labelString);

		for (String key : digiImage.getParameter()
				.createMapFromParameters(null).keySet()) {
			if (labels.containsKey(key)){
			ModifyListener listener = new DigiImagePropertyListener(key,
					digiImage);

			texts.get(key).addModifyListener(listener);
			listeners.put(key, listener);
			}
		}
		button.addMouseListener(new RedrawButtonListener(digiImage, texts));

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	public void refresh() {

		DigiImageProperties properties = (DigiImageProperties) digiImage
				.getAdapter(IPropertySource.class);

		for (String key : texts.keySet()) {

			Text labelText = texts.get(key);
			labelText.removeModifyListener(listeners.get(key));

			labelText.setText((String) properties.getPropertyValue(key));
			labelText.addModifyListener(listeners.get(key));

		}
		super.refresh();
	}

}