Package jfix.zk

Source Code of jfix.zk.Listbox

/*
    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.ArrayList;
import java.util.List;

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.ListModel;
import org.zkoss.zul.SimpleListModel;

public class Listbox extends org.zkoss.zul.Listbox {

  private boolean nullable = true;

  public Listbox() {
    setSeltype("single");
    setHflex("1");
  }

  public Listbox(List values) {
    setValues(values);
  }

  public Listbox(Object[] values) {
    setValues(values);
  }

  public Listbox doPaged(int pageSize) {
    setMold("paging");
    setPageSize(pageSize);
    return this;
  }

  public void setValues(Object[] values) {
    if (values != null) {
      setModel(new SimpleListModel(values));
    }
  }

  public void setValues(List values) {
    if (values != null) {
      setModel(new SimpleListModel(values));
    }
  }

  public void setModel(ListModel model) {
    if (nullable) {
      super.setModel(new NullableListModel(model));
    } else {
      super.setModel(model);
    }
  }

  public void setSelection(List values, Object selected) {
    setValues(values);
    setSelectedValue(selected);
  }

  public void setModel(Object[] values, Object selected) {
    setValues(values);
    setSelectedValue(selected);
  }

  public Object getSelectedValue() {
    if (getSelectedIndex() != -1) {
      return getModel().getElementAt(getSelectedIndex());
    }
    return null;
  }

  public Object[] getValues() {
    List values = new ArrayList();
    for (int i = 0; i < getItemCount(); i++) {
      Object value = getItemAtIndex(i).getValue();
      if (value != null) {
        values.add(getItemAtIndex(i).getValue());
      }
    }
    return values.toArray(new Object[] {});
  }

  public void setSelectedValue(Object value) {
    setSelectedIndex(-1);
    if (value != null) {
      for (int i = 0; i < getModel().getSize(); i++) {
        if (value.equals(getModel().getElementAt(i))) {
          setSelectedIndex(i);
          return;
        }
      }
    }
  }

  public void addSelectListener(final ActionListener actionListener) {
    addEventListener(Events.ON_SELECT, new EventListener() {
      public void onEvent(Event e) throws Exception {
        actionListener.actionPerformed(e);
      }
    });
  }

  public void addDropListener(final ActionListener actionListener) {
    addEventListener(Events.ON_DROP, new EventListener() {
      public void onEvent(Event e) throws Exception {
        actionListener.actionPerformed(e);
      }
    });
  }

  public boolean isNullable() {
    return nullable;
  }

  public void setNullable(boolean nullable) {
    this.nullable = nullable;
  }
 
  public boolean isEmpty() {
    return getSelectedValue() == null;
  }
}
TOP

Related Classes of jfix.zk.Listbox

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.