Monday, March 11, 2013

how to remove list item in zk

package test.viewmodel;

import java.util.Set;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Messagebox;

public class ListModelListViewModel {
    private Set<PeopleData> selectedListItem;

    public Set<PeopleData> getSelectedListItem() {
        return selectedListItem;
    }

    public void setSelectedListItem(Set<PeopleData> selectedListItem) {
        this.selectedListItem = selectedListItem;
    }

    ListModelList<PeopleData> peopleModelList = new ListModelList<PeopleData>();

    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
        System.out.println("inside init");
        loadData();
        peopleModelList.setMultiple(true);
    }

    public class PeopleData extends Object {

        private String firstName;
        private String lastName;
        private String city;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }
    }

    @Command
    public void doRemove(@ContextParam(ContextType.VIEW) Component view) {
        if (selectedListItem == null || (selectedListItem != null && selectedListItem.size() == 0)) {
            Messagebox.show("Please select a Row to delete.");
        } else {
            for (PeopleData peopleData : selectedListItem) {
                for (int i = 0; i < peopleModelList.size(); i++) {
                    PeopleData temppeopleData = peopleModelList.get(i);
                    if (temppeopleData.equals(peopleData)) {
                        try {
                            peopleModelList.remove(peopleData);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    }

                }

            }
        }
    }

    public void loadData() {
        PeopleData peopleData = new PeopleData();
        peopleData.setFirstName("Himanshu");
        peopleData.setLastName("Sekhar");
        peopleData.setCity("Odisha");
        peopleModelList.add(peopleData);
        peopleData = new PeopleData();
        peopleData.setFirstName("Subodth");
        peopleData.setLastName("Joshi");
        peopleData.setCity("UK");
        peopleModelList.add(peopleData);
        peopleData = new PeopleData();
        peopleData.setFirstName("Himanshu");
        peopleData.setLastName("");
        peopleData.setCity("Noida");
        peopleModelList.add(peopleData);

    }

    public ListModelList<PeopleData> getPeopleModelList() {
        return peopleModelList;
    }

    public void setPeopleModelList(ListModelList<PeopleData> peopleModelList) {
        this.peopleModelList = peopleModelList;
    }
}


<zk>
    <window border="normal" title="hello"
        apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('test.viewmodel.ListModelListViewModel')">

        <listbox multiple="true" emptyMessage="No Rows Found"
            checkmark="true" model="@bind(vm.peopleModelList)" height="310px"
            span="true" width="860px"
            selectedItems="@bind(vm.selectedListItem)">

            <listhead>
                <listheader label="First Name"></listheader>
                <listheader label="Last Name"></listheader>
                <listheader label="City"></listheader>

            </listhead>

            <template name="model" var="PeopleData">
                <listitem>
                    <listcell>
                        <label value="${PeopleData.firstName}" />
                    </listcell>
                    <listcell>
                        <label value="${PeopleData.lastName}" />
                    </listcell>
                    <listcell>
                        <label value="${PeopleData.city}" />
                    </listcell>
                </listitem>
            </template>
        </listbox>

        <button id="btn" label="Delete People"
            onClick="@command('doRemove')" />

    </window>
</zk>

How to Calculate the Future Investment Value of Investment.

Below java program for calculate future investment value. Here calculate s using the formula futureInvestmentValue =investmentAmount x (...