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

eu.future.earth.gwt.client.ui.panels.AbstractLabelFieldPanel Maven / Gradle / Ivy

The newest version!
package eu.future.earth.gwt.client.ui.panels;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

import eu.future.earth.gwt.client.logic.UUID;
import eu.future.earth.gwt.client.ui.general.EeGwtFrameworkCss;

public abstract class AbstractLabelFieldPanel extends Composite implements HasWidgets {

	protected List ids = new ArrayList();

	protected HashMap labels = new HashMap();

	protected HashMap req = new HashMap();

	protected HashMap edits = new HashMap();

	protected HashMap extraFields = new HashMap();

	protected void setElements(String name, Widget label, Widget edit, Widget extraItem) {
		if (name != null) {
			ids.add(name);
			if (label != null) {
				labels.put(name, label);
			}
			if (edit != null) {
				edits.put(name, edit);
			}
			if (extraItem != null) {
				extraFields.put(name, extraItem);
			}
		}
	}

	public boolean hasRequiredShowing() {
		for (String label : ids) {
			Widget cur = edits.get(label);
			if (cur != null && cur.isVisible()) {
				Boolean setToValue = req.get(label);
				if (setToValue != null && setToValue) {
					return true;
				}
			}
		}
		return false;
	}

	public void checkRequiredLegendas() {
		showRequired(hasRequiredShowing());
	}

	public abstract String getLabel(String name);

	public abstract void setLabel(String id, String newText);

	public abstract void setRequired(String name, boolean newState);

	public abstract void setError(String name, boolean newState);

	@Override
	public void add(Widget w) {
		if (w instanceof Field) {
			Field real = (Field) w;
			addField(real);
			if (real.isRequired()) {
				setRequired(real.getLabel(), true);
			}
		} else {
			addRow(w);
		}
	}

	protected abstract void addFieldInternal(Field real);

	public void setRequiredForLabel(String id, boolean newState) {
		if (id != null) {
			req.put(id, newState);
			Widget label = labels.get(id);
			if (label != null) {
				if (newState) {
					setRequiredLabel(label);
				} else {
					clearRequiredLabel(label);
				}
			}
			Widget edit = edits.get(id);
			if (edit != null) {
				if (newState) {
					setRequiredToTextField(edit);
				} else {
					clearRequiredToTextField(edit);
				}
			}
		}

	}

	public void setRequiredForLabel(Field name) {
		setRequired(name.getId(), name.isRequired());
	}

	public void setVisibleForLabel(Field name, boolean newState) {
		setVisible(name.getId(), newState);
	}

	public void setErrorForLabel(String id, boolean newState) {
		Widget label = labels.get(id);
		if (label != null) {
			if (newState) {
				setErrorLabel(label);
			} else {
				clearErrorLabel(label);
			}
		}
		Widget edit = edits.get(id);
		if (edit != null) {
			if (newState) {
				setError(edit);
			} else {
				clearError(edit);
			}
		}
	}

	public static void setRequiredLabel(Widget element) {
		if (element != null) {
			element.addStyleName(EeGwtFrameworkCss.REQUIRED_LABEL);
		}
	}

	public static void clearRequiredLabel(Widget element) {
		if (element != null) {
			element.removeStyleName(EeGwtFrameworkCss.REQUIRED_LABEL);
		}
	}

	public static void setRequiredToTextField(Widget element) {
		if (element != null) {
			element.addStyleName(EeGwtFrameworkCss.REQUIRED_TEXT_FIELD);
		}
	}

	public static void clearRequiredToTextField(Widget element) {
		if (element != null) {
			element.removeStyleName(EeGwtFrameworkCss.REQUIRED_TEXT_FIELD);
		}
	}

	// Error handling
	public static void setErrorLabel(Widget element) {
		if (element != null) {
			element.addStyleName(EeGwtFrameworkCss.ERROR_LABEL);
		}
	}

	public static void clearErrorLabel(Widget element) {
		if (element != null) {
			element.removeStyleName(EeGwtFrameworkCss.ERROR_LABEL);
		}
	}

	public static void setError(Widget element) {
		if (element != null) {
			element.addStyleName(EeGwtFrameworkCss.ERROR_TEXT_FIELD);
		}
	}

	public static void clearError(Widget element) {
		if (element != null) {
			element.removeStyleName(EeGwtFrameworkCss.ERROR_TEXT_FIELD);
		}
	}

	public abstract void setVisible(String name, boolean newState);

	public void showFilled() {
		Iterator labelsName = labels.keySet().iterator();
		while (labelsName.hasNext()) {
			String labelName = labelsName.next();
			Widget item = edits.get(labelName);
			if (item instanceof HasText) {
				HasText real = (HasText) item;
				if (real.getText() != null && real.getText().length() > 0) {
					setVisibleElements(labelName, true);
				} else {
					setVisibleElements(labelName, false);
				}
			}
		}
	}

	public abstract void showRequired(boolean hasRequired);

	public void setVisibleElements(String id, boolean newState) {
		Widget label = labels.get(id);
		if (label != null) {
			label.setVisible(newState);
		}
		Widget edit = edits.get(id);
		if (edit != null) {
			edit.setVisible(newState);
		}
		Widget extras = extraFields.get(id);
		if (extras != null) {
			extras.setVisible(newState);
		}
	}

	public final Field addRow(Widget item) {
		if (item instanceof Field) {
			return addField((Field) item);
		} else {
			return addTwoRows(null, item);
		}
	}

	public final Field addRow(String labelName, Widget item) {
		return addRow(labelName, item, null, false);
	}

	public final Field addRow(String labelName, Widget item, boolean alignTop) {
		return addRow(labelName, item, null, alignTop);
	}

	public final Field addRow(String labelName, Widget item, Widget extraItem) {
		return addRow(labelName, item, extraItem, false);
	}

	public Field addField(Field real) {
		if (real.getLabel() == null) {
			if (real.getId() == null) {
				real.setId(UUID.uuid());
			}
		} else {
			if (real.getId() == null) {
				real.setId(real.getLabel());
			}
		}
		real.setLabelFieldHolder(this);
		addFieldInternal(real);
		return real;
	}

	public Field addRawRow(Widget item, Widget extraItem) {
		return addRawRow(null, item, extraItem, false);
	}

	public Field addRawRow(Widget item, Widget extraItem, boolean alignTop) {
		return addRawRow(null, item, extraItem, alignTop);
	}

	public Field addRawRow(String id, Widget item) {
		return addRawRow(id, item, null, false);
	}

	public Field addRawRow(String id, Widget item, Widget extraItem) {
		return addRawRow(id, item, extraItem, false);
	}

	public Field addRowNoLabel(String id, Widget item) {
		return addTwoRows(id, null, item, null);
	}

	public Field addTwoRows(String labelName, Widget item) {
		return addTwoRows(labelName, item, null);
	}

	public Field addTwoRows(String labelName, Widget managedItem, Widget extra) {
		return addTwoRows(null, labelName, managedItem, extra);
	}

	public Field addTwoRows(String id, String labelName, Widget managedItem, Widget extra) {
		Field result = new Field();
		result.setId(id);
		result.setManager(managedItem);
		result.setExtraWidget(extra);
		result.setLabel(labelName);
		if (labelName != null && result.getId() == null) {
			result.setId(labelName);
		}
		return addTwoRows(result);
	}

	public abstract Field addTwoRows(Field result);

	private Field addRawRow(String id, Widget item, Widget extraItem, boolean alignTop) {
		Field res = new Field();
		res.setId(id);
		res.setShift(true);
		res.setAlignTop(alignTop);
		res.setManager(item);
		res.setExtraWidget(extraItem);
		return addField(res);
	}

	public Field addRow(String labelName, Widget item, Widget extraItem, boolean alignTop) {
		Field res = new Field();
		res.setLabel(labelName);
		res.setAlignTop(alignTop);
		res.setManager(item);
		res.setExtraWidget(extraItem);
		return addField(res);
	}

	public abstract void setWordWrapForLabel(String labelName, boolean b);

	public Widget getEdit(String name) {
		return edits.get(name);
	}

	public void setFieldRequired(Field real, boolean newState) {
		setRequired(real.getLabel(), newState);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy