diff --git a/trunk/PlanosActuacao/src/pagination/Pagina.java b/trunk/PlanosActuacao/src/pagination/Pagina.java new file mode 100644 index 00000000..2a319c55 --- /dev/null +++ b/trunk/PlanosActuacao/src/pagination/Pagina.java @@ -0,0 +1,119 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package pagination; + +import com.sun.webui.jsf.component.Hyperlink; +import java.io.Serializable; +import javax.faces.context.FacesContext; +import planosactuacao.SessionBean1.PAGINATION_ENUM; + +/** + * + * @author lluis + */ +public class Pagina extends Hyperlink implements Serializable +{ + private PAGINATION_ENUM paginationType; + + private Integer risco_id; + private Integer valor; + private Integer pageNumber; + private Boolean previous = false; + private Boolean next = false; + + public Pagina() + { + super(); + } + + @Override + public Object saveState( FacesContext context ) + { + Object values[] = new Object[ 7 ]; + values[ 0 ] = super.saveState( context ); + values[ 1 ] = getRisco_id(); + values[ 2 ] = getValor(); + values[ 3 ] = getPageNumber(); + values[ 4 ] = isPrevious(); + values[ 5 ] = isNext(); + values[ 6 ] = getPaginationType(); + return values; + } + + @Override + public void restoreState( FacesContext context, Object state ) + { + Object values[] = ( Object[] ) state; + super.restoreState( context, values[ 0 ] ); + risco_id = ( Integer ) values[ 1 ]; + valor = ( Integer ) values[ 2 ]; + pageNumber = ( Integer ) values[ 3 ]; + previous = ( Boolean ) values[ 4 ]; + next = ( Boolean ) values[ 5 ]; + setPaginationType( ( PAGINATION_ENUM ) values[ 6 ] ); + } + + public Integer getRisco_id() + { + return risco_id; + } + + public void setRisco_id(Integer risco_id) + { + this.risco_id = risco_id; + } + + public Boolean isPrevious() + { + return previous; + } + + public void setPrevious(Boolean previous) + { + this.previous = previous; + } + + public Boolean isNext() + { + return next; + } + + public void setNext(Boolean next) + { + this.next = next; + } + + public Integer getPageNumber() + { + return pageNumber; + } + + public void setPageNumber( Integer page_nr ) + { + this.pageNumber = page_nr; + } + + public Integer getValor() + { + return valor; + } + + public void setValor( Integer valor ) + { + this.valor = valor; + } + + public PAGINATION_ENUM getPaginationType() + { + return paginationType; + } + + public void setPaginationType( PAGINATION_ENUM paginationType ) + { + this.paginationType = paginationType; + } + +} diff --git a/trunk/PlanosActuacao/src/pagination/Pagination.java b/trunk/PlanosActuacao/src/pagination/Pagination.java new file mode 100644 index 00000000..441e1df6 --- /dev/null +++ b/trunk/PlanosActuacao/src/pagination/Pagination.java @@ -0,0 +1,218 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package pagination; + +import db.entidades.PlanoActuacao; +import db.entidades.Risco; +import db.entidades.Utilizador; +import db.providers.RiscoLogic; +import utils.Global; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import planosactuacao.SessionBean1.PAGINATION_ENUM; + + +public abstract class Pagination implements Serializable +{ +// private static final String COLOR_TRATADO = "#000000"; + private static final String DEFAULT_COLOR_NAO_TRATADO = "#CC9966"; + + private static final Map< Integer, String > COLOR_NAO_TRATADO = new HashMap< Integer, String >(); + static + { + COLOR_NAO_TRATADO.put( Global.RESPONSAVEL_SEGURANCA, "#FF5858" ); + } + + + private final PAGINATION_ENUM pagination; + private final PlanoActuacao planoActuacao; + private final Utilizador user; + + private List< Risco > riscos = new LinkedList< Risco >(); + private List< Pagina > pages = new LinkedList< Pagina >(); + + private int currentPageNumber = 1; + + + public Pagination( PAGINATION_ENUM pagination, PlanoActuacao p, Utilizador user ) + { + this.pagination = pagination; + this.planoActuacao = p; + this.user = user; + setup(); + } + + public PAGINATION_ENUM getPagination() + { + return pagination; + } + + public PlanoActuacao getPlanoActuacao() + { + return planoActuacao; + } + + public abstract void setup(); + + public void addRisco( Risco r ) + { + if ( r != null ) + { + if ( riscos == null ) + { + riscos = new LinkedList< Risco >(); + } + riscos.add( r ); + } + } + + public void addPage( Pagina page ) + { + if ( page != null ) + { + if ( pages == null ) + { + pages = new LinkedList< Pagina >(); + } + pages.add( page ); + } + } + + public List< Pagina > getPages( Risco riscoActual ) + { + pages = new LinkedList< Pagina >(); + + if ( getCurrentPageNumber() > 1 ) + { + Pagina page = new Pagina(); + page.setId( "prev" ); + page.setPrevious( new Boolean( true ) ); + page.setText( "<<" ); + addPage( page ); + } + + Iterator< Risco > it = riscos.iterator(); + int pageNumber = 1; + while ( it.hasNext() ) + { + Risco risco = it.next(); + + Pagina page = new Pagina(); + page.setId( "valor" + risco.getId() ); + page.setPageNumber( pageNumber ); + page.setText( pageNumber ); + if ( isCurrentPage( page, risco, riscoActual ) ) + { + setDisabled( page ); + } + else + { + setColor( page, risco ); + } + addPage( page ); + + pageNumber++; + } + + if ( getCurrentPageNumber() < getTotalPages() ) + { + Pagina page = new Pagina(); + page.setId( "next" ); + page.setNext( new Boolean( true ) ); + page.setText( ">>" ); + addPage( page ); + } + + return pages; + } + + private boolean isCurrentPage( Pagina page, Risco risco, Risco riscoActual ) + { + return ( page.getPageNumber() == getCurrentPageNumber() ) && ( ( riscoActual.getIsPatrimonial() && risco.getIsPatrimonial() && getPagination().equals( PAGINATION_ENUM.PATRIMONIAIS ) ) + || ( ! riscoActual.getIsPatrimonial() && ! risco.getIsPatrimonial() && getPagination().equals( PAGINATION_ENUM.NORMAL ) ) ); + } + + private void setColor( Pagina page, Risco risco ) + { + String color = getColor( risco ); + if ( color != null ) + { + String style = page.getStyle(); + style = (style == null ? "" : style) + "; color: " + color + ";"; + page.setStyle( style ); + } + } + + private void setDisabled( Pagina page ) + { + String style = page.getStyle(); + style = ( style == null ? "" : style ) + "background: #FFFFFF; border: 1px solid #000000; padding-left: 5px; padding-right: 5px;"; + page.setStyle( style ); + page.setDisabled( true ); + } + + private String getColor( Risco risco ) + { + String result = null; + + boolean isRiscoTratado = RiscoLogic.getInstance().isRiscoTratado( risco, user, true ); + if ( ! isRiscoTratado ) + { + result = COLOR_NAO_TRATADO.get( user.getTipo() ); + if ( result == null ) + { + result = DEFAULT_COLOR_NAO_TRATADO; + } + } + + return result; + } + + public Risco getRisco( Pagina p ) + { + if ( p.isNext() ) + { + currentPageNumber++; + } + else if ( p.isPrevious() ) + { + currentPageNumber--; + } + else + { + currentPageNumber = p.getPageNumber().intValue(); + } + + return riscos.get( currentPageNumber - 1 ); + } + + public Risco getNextRisco() + { + Risco r = null; + + if ( currentPageNumber < getTotalPages() ) + { + currentPageNumber++; + r = riscos.get( currentPageNumber - 1 ); + } + + return r; + } + + public int getTotalPages() + { + return pages == null ? 0 : pages.size() - 1; + } + + public int getCurrentPageNumber() + { + return currentPageNumber; + } +} diff --git a/trunk/PlanosActuacao/src/pagination/PaginationNormais.java b/trunk/PlanosActuacao/src/pagination/PaginationNormais.java new file mode 100644 index 00000000..2e6c5daf --- /dev/null +++ b/trunk/PlanosActuacao/src/pagination/PaginationNormais.java @@ -0,0 +1,44 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package pagination; + +import db.entidades.Area; +import db.entidades.PlanoActuacao; +import db.entidades.Risco; +import db.entidades.Utilizador; +import planosactuacao.SessionBean1.PAGINATION_ENUM; + +/** + * + * @author dneves + */ +public class PaginationNormais extends Pagination +{ + public PaginationNormais( PlanoActuacao plano, Utilizador user ) + { + super( PAGINATION_ENUM.NORMAL, plano, user ); + } + + @Override + public void setup() + { + if ( getPlanoActuacao() != null ) + { + for ( Area a : getPlanoActuacao().getAreas() ) + { + for ( Risco r : a.getRiscos() ) + { + if ( ! r.getIsPatrimonial() ) + { + addRisco( r ); + } + } + } + } + } + +} + diff --git a/trunk/PlanosActuacao/src/pagination/PaginationPatrimoniais.java b/trunk/PlanosActuacao/src/pagination/PaginationPatrimoniais.java new file mode 100644 index 00000000..33884d1b --- /dev/null +++ b/trunk/PlanosActuacao/src/pagination/PaginationPatrimoniais.java @@ -0,0 +1,43 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package pagination; + +import db.entidades.Area; +import db.entidades.PlanoActuacao; +import db.entidades.Risco; +import db.entidades.Utilizador; +import planosactuacao.SessionBean1.PAGINATION_ENUM; + +/** + * + * @author dneves + */ +public class PaginationPatrimoniais extends Pagination +{ + public PaginationPatrimoniais( PlanoActuacao plano, Utilizador user ) + { + super( PAGINATION_ENUM.PATRIMONIAIS, plano, user ); + } + + @Override + public void setup() + { + if ( getPlanoActuacao() != null ) + { + for ( Area a : getPlanoActuacao().getAreas() ) + { + for ( Risco r : a.getRiscos() ) + { + if ( r.getIsPatrimonial() ) + { + addRisco( r ); + } + } + } + } + } + +}