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

at.spardat.xma.mdl.paging.PagingWMClient Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2003, 2010 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id:  $
package at.spardat.xma.mdl.paging;

import at.spardat.enterprise.fmt.ABcdFmt;
import at.spardat.enterprise.fmt.IFmt;
import at.spardat.xma.mdl.ModelChangeEvent;
import at.spardat.xma.mdl.NewModelEvent;
import at.spardat.xma.mdl.UIDelegateClient;
import at.spardat.xma.mdl.UIDelegateFactoryClient;
import at.spardat.xma.mdl.WModel;
import at.spardat.xma.mdl.table.ITableWMClient;
import at.spardat.xma.mdl.table.TableExternalSorterClient;
import at.spardat.xma.mdl.table.TableWM;
import at.spardat.xma.mdl.table.TableWMClient;
import at.spardat.xma.page.Page;
import at.spardat.xma.page.PageClient;
/**
 * Implements the part of the paging model which is available on client side only.
 *
 * @author gub
 * @since 2.3.0
*/
public class PagingWMClient extends PagingWM implements IPagingWMClient, TableExternalSorterClient {
    /** The UIDelegate connecting this to the PagingControlClient. never null */
    PagingUIDelegateClient ui;
    /** The XMA-Page this model belongs to. never null */
    PageClient page;
    /** The current page, used to switch back if jump() does not succeed. */
    int oldJumpPage;
    /** The PagingListener responsible for changing the data in the table */
    PagingListener pagingListener;
    /** An optional customizer */
    Customizer customizer;
    /** An optional exporter */
    Exporter exporter;
    /** Export button can be disabled at runtime */
    boolean exportAllowed;
    /** The choices of the pageSize combo */
    String[] pageSizeList = new String[] { "10", "20", "50", "100", "200", "500", "1000" };

    /**
     * Creates the paging model on client side.
     * @param id a unique id identifying this WidgetModel within its page.
     * @param pm the {@link Page} this model belongs to.
     * @param table the corresponding table holding the data.
     */
    public PagingWMClient(short id, Page pm, ITableWMClient table) {
        super(id, pm, table);
        isAtServer=false;
        exportAllowed=true;
        page = (PageClient) pm;
        ui = (PagingUIDelegateClient) UIDelegateFactoryClient.getInstance((PageClient)pm).newUIDelegateFor(this);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.IWModelClient#getUIDelegate()
     */
    public UIDelegateClient getUIDelegate() {
        return ui;
    }

    /**
     * Returns always true.
     * @see at.spardat.xma.mdl.IWModelClient#isEditable()
     */
    public boolean isEditable() {
        return ui.isEditable();
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.IWModelClient#isEnabled()
     */
    public boolean isEnabled() {
        return ui.isEnabled();
    }

    /**
     * Does nothing.
     * @see at.spardat.xma.mdl.IWModelClient#setEditable(boolean)()
     */
    public void setEditable(boolean what) {
        ui.setEditable(what);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.IWModelClient#setEnabled()
     */
    public void setEnabled(boolean what) {
        ui.setEnabled(what);
    }

    /**
     * Returns the XMA-Page this model belongs to. never null
     */
    public PageClient getPage() {
        return page;
    }

    /**
     * Returns the formatter/validator used for the jump combo.
     */
    IFmt getJumpFmt() {
        int rs = getResultSize();
        int maxPage = rs == 0?1:(int) Math.ceil((float)rs/(float)getPageSize());
        return ABcdFmt.getInstance(5, 0, ABcdFmt.DEFAULT|ABcdFmt.NO_NEG, 1, maxPage, page.getSession().getContext().getLocale());
    }

    /**
     * Returns the formatter/validator used for the pageSize combo.
     */
    IFmt getPageSizeFmt() {
        return ABcdFmt.getInstance(5, 0, ABcdFmt.DEFAULT|ABcdFmt.NO_NEG|ABcdFmt.MANDATORY, 1, getMaxPageSize(), page.getSession().getContext().getLocale());
    }

    /**
     * Returns the choices of the pageSize combo
     */
    public String[] getPageSizeList() {
        return pageSizeList;
    }

    /**
     * Sets the choices of the pageSize combo
     */
    public void setPageSizeList(String[] pageSizeList) {
        this.pageSizeList = pageSizeList;
        if(ui.isUIAttached()) {
            ui.pageSizeList2UI();
        }
    }
    
    /** 
     * Returns whether exporting is currently allowed on the client
     */
    public boolean isExportAllowed() {
        return exportAllowed;
    }
    
    /**
     * Defines whether exporting is currently allowed on the client
     */
    public void setExportAllowed(boolean allowed) {
        this.exportAllowed = allowed;
        if(ui.isUIAttached()) {
            ui.exportAllowed2UI();
        }
    }
    
    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.WModel#handle(at.spardat.xma.mdl.ModelChangeEvent)
     */
    public boolean handle(ModelChangeEvent event) {
        boolean success = super.handle(event);
        if(success) ui.handleModelChangeEvent(event);
        return success;
    }


    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#setPagingListener(at.spardat.xma.mdl.paging.PagingListener)
     */
    public void setPagingListener(PagingListener listener) {
       pagingListener=listener;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#setCustomizer(at.spardat.xma.mdl.paging.Customizer)
     */
    public void setCustomizer(Customizer customizer) {
        this.customizer=customizer;
    }

    public void setExporter(Exporter exporter) {
        this.exporter=exporter;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#getCustomizer()
     */
    public Customizer getCustomizer() {
        return this.customizer;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#start()
     */
    public void start() {
        int oldOffset=getOffset();
        setOffset(0);
        doReload(oldOffset);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#fastBack()
     */
    public void fastBack() {
        int oldOffset=getOffset();
        int newOffset = getOffset() - (getPageSize()*getJumpSize());
        setOffset(newOffset>0?newOffset:0);
        doReload(oldOffset);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#back()
     */
    public void back() {
        int oldOffset=getOffset();
        int newOffset = getOffset() - getPageSize();
        setOffset(newOffset>0?newOffset:0);
        doReload(oldOffset);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#reload()
     */
    public void reload() {
        doReload(getOffset());
    }

    /**
     * Actually calls the reload-method of the paging listener.
     * Adjusts the resultSize if the table contains less data than ordered.
     * Resets the offset to oldOffset if the reload failed.
     */
    private void doReload(int oldOffset) {
        int pageSize=getPageSize();
        try {
            tableChanged=false;
            pagingListener.reload(getOffset(),pageSize);
            if(!tableChanged&&getOffset()==oldOffset+pageSize) {
                setResultSize(oldOffset+pageSize);
            } else {
               handle(new PagingModelChangeEvent(this, false));
            }
        } finally {
            if(!tableChanged) {
                setOffset(oldOffset);
            } else if(table.size()getOffset()+table.size();
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#next()
     */
    public void next() {
        int oldOffset=getOffset();
        int newOffset=getOffset()+table.size();
        setOffset(newOffset=getResultSize()) newOffset-=getPageSize();
        if(newOffset<0) newOffset=0;
        return newOffset;
    }

    /**
     * Returns the current page index. This is the number of the page starting with the
     * current offset. Zero based.
     */
    public int getCurrentPage() {
        if(table.size()<=0) return 0;
        int page = getOffset()/getPageSize();// TODO need oldpagesize here!
        return page;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#customize()
     */
    public void customize() {
        customizer.customize();
    }
    
    public void export() {
        exporter.export(); 
    }


    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.paging.IPagingWMClient#isValid()
     */
    public boolean isValid() {
        return ui.isValid();
    }

    public int getResultSize() {
        if (Integer.MAX_VALUE == resultSize.toInt()) {
            if (table.size() > 0 && table.size() < pageSize.toInt()) {
                return getOffset() + table.size();
            }
        }
        return super.getResultSize();
    }


    /**
     * Event class used to notify the dynamic registration of a new PagingWMClient.
     * @see Page#addWModel(WModel)
    */
    public static class NewPagingWMClientEvent extends NewPagingWMEvent {
        /** empty contructor for deserialization */
        public NewPagingWMClientEvent() {}

        /**
         * constructor which initializes the table reference
         * @param tableId the model id of the corresponding table
         */
        public NewPagingWMClientEvent(short tableId) {
            this.tableId = tableId;
        }

        // see at.spardat.xma.mdl.NewModelEvent.createModel()
        public WModel createModel(short id, Page page) {
            TableWMClient table = (TableWMClient) page.getWModel(tableId);
            return new PagingWMClient(id,page,table);
        }
    }

    // see at.sparda.xma.mdl.WModel.createNewModelEvent()
    public NewModelEvent createNewModelEvent() {
        return new NewPagingWMClientEvent(((TableWM)table).getId());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy