Home  | My Account  |  Membership
WorldDeveloper.org
Forum Details
Topics
Posts
Categories
 
577 member(s)
718,905 hit(s)
14 categories
51 topics
26 messages
0 online
Forums: Code Post
Paging utility for displaying lists in web UI
hostj2me
43 topic(s)
23 post(s)

January 02, 2008 6:29 PM


/*
* Copyright (C) 2005 Astrient Labs, LLC Licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* Astrient Labs, LLC.
* www.astrientlabs.com
* rashid@astrientlabs.com
* Rashid Mayes 2005
*/
package com.astrientlabs.webui;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.astrientlabs.logging.LogWriter;


public class BasicUIListable extends LinkedList implements UIListable
{
/**
*
*/
private static final long serialVersionUID = -7369036906003073472L;
private int pageSize = 10;
private int pageNumber = -1;
private Random random = new Random();


public List getItems(int pageNumber)
{
int start = pageNumber * pageSize;
int end = start + pageSize;

return getItems(start,end);
}

public List items(int pageNumber, int pageSize)
{
int start = (pageNumber * pageSize);
int end = start + pageSize;

return getItems(start,end);
}


public List getItems(int start, int end)
{
try
{
if ( start > -1 && start size() )
{
if ( end > size() )
{
end = size();
}

return this.subList(start,end);
}
}
catch (Exception e)
{
LogWriter.system.log(getClass(),e);
}

return this.subList(0,0);//(List)EMPTY_LIST;
}


public void resetPageNumber()
{
pageNumber = -1;
}

public List next()
{
return getItems(++pageNumber);
}


public List current()
{
return ( pageNumber == -1 ) ? next() : getItems(pageNumber);
}


public List previous()
{
return getItems(--pageNumber);
}

/**
* Returns the pageNumber.
* @return int
*/
public int getPageNumber()
{
return pageNumber;
}

/**
* Returns the pageSize.
* @return int
*/
public int getPageSize()
{
return pageSize;
}

/**
* Sets the pageNumber.
* @param pageNumber The pageNumber to set
*/
public void setPageNumber(int pageNumber)
{
this.pageNumber = pageNumber;
}

/**
* Sets the pageSize.
* @param pageSize The pageSize to set
*/
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}


public int totalPages()
{
int size = size();
int pageSize = getPageSize();

int total = size / pageSize;
if ( (size % pageSize ) > 0 )
{
total++;
}

return total;
}

public int totalPages(int pageSize)
{
int size = size();

int total = size / pageSize;
if ( (size % pageSize ) > 0 )
{
total++;
}

return total;
}

/*
public synchronized void buildIndexes(Indexer indexer)
{
indexes.clear();


Object o;
Object index;
for ( Iterator iter = this.iterator(); iter.hasNext(); )
{
o = iter.next();
index = indexer.index(indexes,this);
}
}*/

/*
public static void main(String[] args)
{
BasicUIListable list = new BasicUIListable();
list.setPageSize(2);
for(int i = 0; i 5;list.add(String.valueOf(i++)));
for (String s : list)
{
System.out.println(s);
}

System.out.println("\n\n");
List l = list.getItems(2);
for (String s : l)
{
System.out.println(s);
}

System.exit(0);
}*/

public E random()
{
return this.get(random.nextInt(this.size()));
}
}



edited by hostj2me 1/2/08 1:31 PM


©2008 WorldDeveloper.org - PO Box 310575 Miami, FL 33231

WorldDeveloper.org is a managed social network for application developers around the world.