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

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

There is a newer version: 6.0.2
Show 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 java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Widget;

import at.spardat.enterprise.fmt.AParseException;
import at.spardat.enterprise.fmt.IFmt;
import at.spardat.xma.mdl.AttachmentExceptionClient;
import at.spardat.xma.mdl.ModelChangeEvent;
import at.spardat.xma.mdl.UIDelegateClient;
import at.spardat.xma.mdl.ValidationErrorClient;
import at.spardat.xma.mdl.WModel;
import at.spardat.xma.mdl.table.ITableWMClient;
import at.spardat.xma.page.EventAdapter;
import at.spardat.xma.page.PageClient;
/**
 * Implements the UI-related event handling and glues together the {@link PagingWMClient} and its
 * corresponding {@link PagingControlClient}.
 *
 * @author gub
 * @since 2.3.0
 */
public class PagingUIDelegateClient extends UIDelegateClient {
    /** the SWT control */
    private PagingControlClient control;
    /** the XMA model */
    private PagingWMClient model;
    /** true during updating SWT controls */
    private boolean updatingUI=false;
    /** true during updating the XMA model */
    private boolean updatingModel=false;
    /** enabled flag of the whole Composite of the control */
    private boolean enabled = true;    

    /**
     * Create the PagingUIDelegateClient.
     * @param model the attached model
     */
    public PagingUIDelegateClient(PagingWMClient model) {
        this.model=model;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.UIDelegateClient#createControl(Object)
     */
    public Object createControl(Object parent) {
        if(!(parent instanceof Composite)) throw new IllegalArgumentException("parent must be a composite");
        return new PagingControlClient((Composite)parent, model.getPage().getSession().getContext().getLocale(), PagingControlClient.DEFAULT, SWT.BORDER);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.UIDelegateClient#addListeners(Object,EventAdapter)
     */
    public void addListeners(Object control, EventAdapter adapter) {
        ((PagingControlClient)control).addSelecionListener(adapter);
        ((PagingControlClient)control).addModifyListener(adapter);
        ((PagingControlClient)control).addFocusListener(adapter);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.UIDelegateClient#attachUI(Object,Object)
     */
    public void attachUI(Object control, Object label) throws AttachmentExceptionClient {
        if (!(control instanceof PagingControlClient)) {
            throw new AttachmentExceptionClient ("cannot attach " + control.getClass().getName() + " to PagingModel");
        }
        this.control=(PagingControlClient) control;
        // attach this delegat to all controls of the pagingcontrol
        this.control.startW.setData(this);
        this.control.fastBackW.setData(this);
        this.control.backW.setData(this);
        this.control.reloadW.setData(this);
        this.control.jumpW.setData(this);
        this.control.nextW.setData(this);
        this.control.fastNextW.setData(this);
        this.control.endW.setData(this);
        this.control.customizeW.setData(this);
        this.control.exportW.setData(this);
        this.control.pageSizeW.setData(this);
        // move values to UI
        currentPage2UI();
        pageSizeList2UI();
        pageSize2UI();
        //if the control is already disabled, set this at this.enabled - otherwise do not change this.enabled
        //as in the Gen class the GUI designer disable property is set directly at the widget (only for some models)
        enabled = this.control.getEnabled()?enabled:false;
        this.control.setEnabled(enabled); //set the model's enabled state at the control
        updateErrorState();
        stateChanged();
        ((ITableWMClient)model.table).setExternalSorter(model);
    }

    /**
     * Show the index of the page containing the current offset in the jump combo.
     * Updates the choices of the jump combo.
     * Does nothing if SHOW_JUMP is not set.
     */
    private void currentPage2UI() {
        updatingUI = true;
        try {
            if((control.style&PagingControlClient.SHOW_JUMP)!=0) {
                String text = Integer.toString(model.getCurrentPage()+1);
                text = model.getJumpFmt().format(text);
                Point sel=null;
                if(text.equals(control.jumpW.getText())) {
                    sel=control.jumpW.getSelection();
                }
                List items = updateJumpList();
                control.jumpW.setText(text);
                if(items.contains(text)) {
                    control.jumpW.select(items.indexOf(text));
                }
                if(sel!=null) {
                    control.jumpW.setSelection(sel);
                }
            }
        } finally {
            updatingUI = false;
        }
    }

    /**
     * Show the pageSize in the pageSize combo.
     */
    private void pageSize2UI() {
        updatingUI = true;
        try {
            if((control.style&PagingControlClient.SHOW_PAGESIZE)!=0) {
                String text = Short.toString(model.getPageSize());
                text = model.getPageSizeFmt().format(text);
                control.pageSizeW.setText(text);
            }
        } finally {
            updatingUI = false;
        }
    }

    /**
     * Set the choice list of the pageSize combo.
     */
    void pageSizeList2UI() {
        updatingUI = true;
        try {
            if((control.style&PagingControlClient.SHOW_PAGESIZE)!=0) {
                control.pageSizeW.setItems(model.getPageSizeList());
            }
        } finally {
            updatingUI = false;
        }
    }

    /**
     * Enables/disables the buttons.
     * Updates the info label if SHOW_INFO is set.
     */
    private void stateChanged() {
        boolean hasNext=model.hasNext();
        int offset=model.getOffset();
        int resultSize=model.getResultSize();
        int tableSize=model.getTable().size();
        boolean isValid=!model.getPage().hasValidationError(control.pageSizeW);
        if((control.style&PagingControlClient.SHOW_INFO)!=0) {
            String text;
            Locale locale = model.getPage().getSession().getContext().getLocale();
            ResourceBundle messages = ResourceBundle.getBundle(PagingControlClient.resourceBundle,locale);
            if(resultSize==Integer.MAX_VALUE) {
                text = messages.getString("shortInfo");
            } else {
                text = messages.getString("info");
            }
            MessageFormat format = new MessageFormat(text, locale);
            if (tableSize >0) {
                control.info.setText(format.format(new Object[]{new Integer(offset+1),new Integer(offset+tableSize),new Integer(resultSize)}));
            } else {
                control.info.setText("");
            }

            format = new MessageFormat(messages.getString("fastBackWTip"),locale);
            control.fastBackW.setToolTipText(format.format(new Object[]{new Integer(model.getJumpSize())}));
            format = new MessageFormat(messages.getString("fastNextWTip"),locale);
            control.fastNextW.setToolTipText(format.format(new Object[]{new Integer(model.getJumpSize())}));
        }
        control.startW.setEnabled(isValid&&offset>0);
        control.fastBackW.setEnabled(isValid&&offset>0);
        control.backW.setEnabled(isValid&&offset>0);
        control.reloadW.setEnabled(isValid);
        control.jumpW.setEnabled(isValid&&(offset>0||hasNext));
        control.nextW.setEnabled(isValid&&hasNext);
        control.fastNextW.setEnabled(isValid&&hasNext);
        control.endW.setEnabled(isValid&&hasNext&&resultSize!=Integer.MAX_VALUE);
        control.customizeW.setEnabled(isValid);
        control.exportW.setEnabled(isValid);
    }

    /**
     * Updates the choices of the jump combo.
     */
    private List updateJumpList() {
        int page = model.getCurrentPage()+1;
        List pageList = new ArrayList(5);
        int additional=0;
        if(page-model.getJumpSize()>0) pageList.add(Integer.toString(page-model.getJumpSize()));
        else additional++;
        if(page-1>0) pageList.add(Integer.toString(page-1));
        else additional++;
        pageList.add(Integer.toString(page));
        int maxPage = (int) Math.ceil((float)model.getResultSize()/(float)model.getPageSize());
        if(page+1<=maxPage) pageList.add(Integer.toString(page+1));
        if(page+model.getJumpSize()<=maxPage) pageList.add(Integer.toString(page+model.getJumpSize()));
        String[] pageListArray=new String[pageList.size()];
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy