/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.rubis.admin.controller.user;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import org.xulfaces.rubis.TreeController;
import org.xulfaces.rubis.admin.dao.UserDAO;
import org.xulfaces.rubis.model.User;
public class UsersTreeController extends TreeController {
private String searchCriteria = "%";
private UserDAO userDAO;
private int startOffset = 1;
private int rows = 100;
private boolean showPreviousDisabled = true;
private boolean showNextDisabled = true;
private boolean ascendingOrderForFirstName;
public UsersTreeController(){
super();
}
public String getSearchCriteria() {
return searchCriteria;
}
public void setSearchCriteria(String searchCriteria) {
this.searchCriteria = searchCriteria;
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void find(){
if(this.searchCriteria != null){
this.startOffset = 1;
Collection results = userDAO.findByFirstname(searchCriteria,startOffset,rows);
if( (results!=null) && (!results.isEmpty())){
this.showNextDisabled = !(results.size() == rows);
}
else {
this.showNextDisabled = true;
}
setTreeModel(buildTreeModelFromCollection(results));
}
}
public void showNext(){
if(this.searchCriteria != null){
startOffset = startOffset + 100 ;
Collection results = userDAO.findByFirstname(searchCriteria,startOffset,rows);
if( (results!=null) && (!results.isEmpty())){
this.showNextDisabled = !(results.size() == rows);
}
else {
this.showNextDisabled = true;
}
setTreeModel(buildTreeModelFromCollection(results));
}
}
public void showPrevious(){
if(this.searchCriteria != null){
if(startOffset > 1){
startOffset = startOffset - 100 ;
Collection results = userDAO.findByFirstname(searchCriteria,startOffset,rows);
if( (results!=null) && (!results.isEmpty())){
this.showNextDisabled = !(results.size() == rows);
}
else {
this.showNextDisabled = true;
}
setTreeModel(buildTreeModelFromCollection(results));
}
}
}
public void clear(){
this.searchCriteria = "%";
this.startOffset = 1;
this.showPreviousDisabled = true;
this.showNextDisabled = true;
setTreeModel(buildTreeModelFromCollection(new ArrayList()));
}
public void refresh(){
Collection results = userDAO.findByFirstname(searchCriteria,startOffset,rows);
if( (results!=null) && (!results.isEmpty())){
this.showNextDisabled = !(results.size() == rows);
}
else {
this.showNextDisabled = true;
}
setTreeModel(buildTreeModelFromCollection(results));
}
public void removeUser(){
User user = (User) this.getSelectedObject();
if(user != null){
this.userDAO.deleteUser(user.getId());
refresh();
}
}
public boolean isShowNextDisabled() {
return showNextDisabled;
}
public void setShowNextDisabled(boolean showNextDisabled) {
this.showNextDisabled = showNextDisabled;
}
public boolean isShowPreviousDisabled() {
if(this.startOffset == 1){
return true;
}
return false;
}
public void setShowPreviousDisabled(boolean showPreviousDisabled) {
this.showPreviousDisabled = showPreviousDisabled;
}
public void sortByFirstName(){
sortTreeModel(new FirstNameComparator());
this.ascendingOrderForFirstName = !this.ascendingOrderForFirstName;
}
class FirstNameComparator implements Comparator {
public int compare(Object o1, Object o2) {
DefaultMutableTreeNode n1 = (DefaultMutableTreeNode) o1;
DefaultMutableTreeNode n2 = (DefaultMutableTreeNode) o2;
User u1 = (User) n1.getUserObject();
User u2 = (User ) n2.getUserObject();
if(ascendingOrderForFirstName){
return u1.getFirstname().compareTo(u2.getFirstname());
}
else {
return u2.getFirstname().compareTo(u1.getFirstname());
}
}
}
}