/*
Copyright (C) 2010 maik.jablonski@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package jfix.zk;
import java.util.Arrays;
import java.util.List;
import org.zkoss.zk.ui.event.DropEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.SimpleListModel;
public class Sortbox extends Listbox {
public Sortbox() {
setNullable(false);
setMultiple(true);
setHflex("1");
setItemRenderer(new ItemRenderer() {
public String render(Object value) {
return String.valueOf(value);
}
});
}
public void setItemRenderer(final ItemRenderer renderer) {
setItemRenderer(new ListitemRenderer() {
public void render(Listitem item, Object data) throws Exception {
item.setValue(data);
item.setLabel(renderer.render(data));
item.setDraggable(Sortbox.this.toString());
item.setDroppable(Sortbox.this.toString());
item.addEventListener(Events.ON_DROP, new EventListener() {
public void onEvent(Event evt) {
DropEvent dropEvent = (DropEvent) evt;
Sortbox.this.addItemToSelection((Listitem) dropEvent.getDragged());
for (Object obj : Sortbox.this.getSelectedItems()) {
Listitem listitem = (Listitem) obj;
Sortbox.this.insertBefore(listitem, dropEvent.getTarget());
}
}
});
}
});
}
public void setValues(Object values[]) {
setValues(Arrays.asList(values));
}
public void setValues(List values) {
clearSelection();
setModel(new SimpleListModel(values));
renderAll();
}
public Object[] getValues() {
Object[] values = new Object[getItemCount()];
for (int i = 0; i < values.length; i++) {
values[i] = getItemAtIndex(i).getValue();
}
return values;
}
public boolean isEmpty() {
if (getValues() == null || getValues().length == 0) {
return true;
}
return false;
}
}