be.personify.iam.frontend.wicket.panel.common.MapCreatePanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of personify-frontend Show documentation
Show all versions of personify-frontend Show documentation
frontend library for different usages
package be.personify.iam.frontend.wicket.panel.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import be.personify.util.KeyValue;
import be.personify.util.StringUtils;
/**
* Panel for a Map
* @author vanderw
*
* @param the generic type
*/
public class MapCreatePanel extends Panel {
private static final String DATA_VIEW_CONTAINER = "dataViewContainer";
private static final String NEW_VALUE = "new_value";
private static final String NEW_KEY = "new_key";
private static final String DATA_VIEW = "dataView";
private static final long serialVersionUID = 4051148613584622985L;
List keyValueList = new ArrayList();
private MarkupContainer dataViewContainer;
private DataView dataView;
public MapCreatePanel( String id, final IModel model ) {
super(id, model);
}
@Override
protected void onInitialize() {
super.onInitialize();
dataViewContainer = new WebMarkupContainer(DATA_VIEW_CONTAINER);
dataViewContainer.setOutputMarkupId(true);
Map map = (Map)getDefaultModel().getObject();
if ( map != null ) {
for (String key : map.keySet()) {
keyValueList.add(new KeyValue(key, map.get(key)));
}
}
dataView = getDataView(DATA_VIEW,createDataProvider() );
dataViewContainer.add(dataView);
TextField newKeyTextField = new TextField(NEW_KEY, new Model(StringUtils.EMPTY_STRING));
TextField newValueTextField = new TextField(NEW_VALUE, new Model(StringUtils.EMPTY_STRING));
dataViewContainer.add(newKeyTextField);
dataViewContainer.add(newValueTextField);
dataViewContainer.add(new AjaxButton(PanelConstants.BUTTON_ADD) {
private static final long serialVersionUID = -5272734134708203645L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form> form) {
keyValueList.add(new KeyValue(newKeyTextField.getModelObject(),newValueTextField.getModelObject()));
((Map)MapCreatePanel.this.getDefaultModel().getObject()).put(newKeyTextField.getModelObject(), newValueTextField.getModelObject());
//clear
newKeyTextField.setModelObject(StringUtils.EMPTY_STRING);
newValueTextField.setModelObject(StringUtils.EMPTY_STRING);
dataView = getDataView(DATA_VIEW,createDataProvider() );
dataViewContainer.addOrReplace(dataView);
target.add(dataViewContainer);
super.onSubmit(target, form);
}
});
add(dataViewContainer);
}
/**
* Creates a dataprovider
* @return a list dataprovider
*/
private ListDataProvider createDataProvider() {
ListDataProvider dp = new ListDataProvider(keyValueList) {
private static final long serialVersionUID = -4570054503756352135L;
@Override
protected List getData() {
return super.getData();
}
@Override
public IModel model(KeyValue object) {
for ( KeyValue keyValue : keyValueList) {
if ( keyValue.getKey().equals(object.getKey())) {
keyValue.setValue(object.getValue());
return super.model(keyValue);
}
}
return super.model(object);
}
};
return dp;
}
/**
* Creates a dataview
* @param name the name of the dataview
* @param dp a list with the dataproviders
* @return a dataview
*/
public DataView getDataView(String name, ListDataProvider dp) {
DataView view = new DataView(name, dp) {
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(Item item) {
item.add(new Label(PanelConstants.KEY, new Model(item.getModelObject().getKey())));
item.add(new TextField(PanelConstants.VALUE, new Model(item.getModelObject().getValue()) {
private static final long serialVersionUID = -1765624247667019938L;
@Override
public void setObject(String object) {
String key = item.getModelObject().getKey();
for ( KeyValue keyValue : keyValueList) {
if ( keyValue.getKey().equals(key)) {
keyValue.setValue(object);
((Map)MapCreatePanel.this.getDefaultModel().getObject()).put(key, object);
}
}
super.setObject(object);
}
}));
item.add(new AjaxButton(PanelConstants.BUTTON_DELETE) {
private static final long serialVersionUID = -5272734134708203645L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form> form) {
keyValueList.remove(item.getModelObject());
((Map)MapCreatePanel.this.getDefaultModel().getObject()).remove(item.getModelObject().getKey());
dataView = getDataView(DATA_VIEW,createDataProvider() );
dataViewContainer.addOrReplace(dataView);
target.add(dataViewContainer);
super.onSubmit(target, form);
}
});
}
};
view.setOutputMarkupId(true);
view.setOutputMarkupPlaceholderTag(true);
view.setItemsPerPage(20);
return view;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy