All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.mdl.NewModelEventParams Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2009 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

package at.spardat.xma.mdl;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import at.spardat.enterprise.fmt.FmtFactory;
import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;
import at.spardat.xma.serializer.XmaSerializable;

/**
 * Additional parameters to send with {@link NewModelEvent}s.
 * These parameters can be used to send informations, like which validator to use,
 * name of the label, tooltip etc, along with the event.
 * For the most probably used parameters - validator/formatter, label, tooltip - keys are defined here.
 *
 * @author gub
 * @since 2.1.0
 */
public class NewModelEventParams implements XmaSerializable {
	/** key for validator/formatter string */
	public static final short PARAM_FORMAT = 1;
	/** key for label text */
	public static final short PARAM_LABEL = 2;
	/** key for tooltip text */
	public static final short PARAM_TOOLTIP = 3;

	/** map containing all prameters */
	private HashMap params;

	/** Constructs an empty parameter set */
	public NewModelEventParams() {}

	/**
	 * Constructs a parameter set with given valiator/formatter string.
	 * The validator/formatter string is interpreted automatically be the
	 * runtime and the corresponding validator will be set on the synchronized model.
	 * @param format validator/formatter string understandable by {@link FmtFactory}.
	 */
	public NewModelEventParams(String format) {
		if(format!=null) setParameter(PARAM_FORMAT, format);
	}

	/**
	 * Constructs a parameter set with given valiator/formatter string and label text.
	 * The validator/formatter string is interpreted automatically be the
	 * runtime and the corresponding validator will be set on the synchronized model.
	 * The label text is not interpreted automatically be the runtime.
	 * It is up the the programmer if the translated text or a key into a resource bundle is used here.
	 * @param format validator/formatter string understandable by {@link FmtFactory}.
	 * @param label the text for the label
	 */
	public NewModelEventParams(String format,String label) {
		if(format!=null) setParameter(PARAM_FORMAT, format);
		if(label!=null) setParameter(PARAM_LABEL, label);
	}

	/**
	 * Constructs a parameter set with given valiator/formatter string, label and tooltip.
	 * The validator/formatter string is interpreted automatically be the
	 * runtime and the corresponding validator will be set on the synchronized model.
	 * The label and tooltip texts are not interpreted automatically be the runtime.
	 * It is up the the programmer if the translated text or a key into a resource bundle is used here.
	 * @param format validator/formatter string understandable by {@link FmtFactory}.
	 * @param label the text for the label
	 * @param tooltip the text for the tooltip
	 */
	public NewModelEventParams(String format,String label,String tooltip) {
		if(format!=null) setParameter(PARAM_FORMAT, format);
		if(label!=null) setParameter(PARAM_LABEL, label);
		if(tooltip!=null) setParameter(PARAM_TOOLTIP, tooltip);
	}

	/**
	 * Set a parameter value.
	 * @param key can be one of the keys defined in this class, or an additional one
	 * @param value The parameter value. It must not be null.
	 */
	public void setParameter(short key,Serializable value) {
        if (value == null) throw new IllegalArgumentException();
        if(params==null) params = new HashMap();
        params.put(new Short(key), value);
	}

	/**
	 * Get a parameter value.
	 * @param key of the parameter to read. It can be one of the keys defined in this class, or an additional one.
	 * @return the parameter value set by {@link NewModelEventParams#setParameter(short, Serializable)}
	 * 		with the same key
	 */
	public Serializable getParameter(short key) {
		if(params!=null) return (Serializable) params.get(new Short(key));
		else return null;
	}

	/**
	 * Removes all parameter values.
	 */
	public void clear() {
		if(params!=null) params.clear();
	}

	// see at.spardat.xma.serializer.XmaSerializable.serialize(XmaOutput)
	public void serialize(XmaOutput out) throws IOException {
        short numParameters = (params == null ? 0 : (short) params.size());
        out.writeShort ("numParams", numParameters);
        if (numParameters > 0) {
            Iterator    iter = params.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry e = (Map.Entry) iter.next();
                short id = ((Short)e.getKey()).shortValue();
                out.writeShort ("pId", id);
                // warning: the following line will throw IOExceptions if there is something
                // that cannot be serialized. This may be a common exception if user supplied
                // parameters are used and they are not Serializable.
                out.writeObject ("pValue", e.getValue());
            }
        }
    }

	// see at.spardat.xma.serializer.XmaSerializable.deserialize(XmaInput)
	public void deserialize(XmaInput in) throws IOException, ClassNotFoundException {
        short numParameters = in.readShort();
        if(numParameters>0) {
			params = new HashMap();
	        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy