Bivio::UI::HTML::Widget::PercentCell
# Copyright (c) 1999-2001 bivio Software, Inc. All rights reserved.
# $Id$
package Bivio::UI::HTML::Widget::PercentCell;
use strict;
# NAME
#
# Bivio::UI::HTML::Widget::PercentCell - formats a cell with a number
#
# RELEASE SCOPE
#
# bOP
#
# SYNOPSIS
#
# use Bivio::UI::HTML::Widget::PercentCell;
#
# EXTENDS
#
# Bivio::UI::HTML::Widget::String
#
use Bivio::UI::HTML::Widget::String;
@Bivio::UI::HTML::Widget::PercentCell::ISA = ('Bivio::UI::HTML::Widget::String');
# DESCRIPTION
#
#
Bivio::UI::HTML::Widget::PercentCell
formats a cell with a number.
# Sets the font to NUMBER_CELL
, alignment is RIGHT
.
#
# ATTRIBUTES
#
#
# decimals : int [1]
#
# Number of decimals to display.
#
# field : string (required)
#
# Name of the field to render.
#
#
#=IMPORTS
use Bivio::UI::HTML::Format::Printf;
#=VARIABLES
my($_IDI) = __PACKAGE__->instance_data_index;
# FACTORIES
#
#
# static new(hash_ref attributes) : Bivio::UI::HTML::Widget::PercentCell
#
# Creates a new PercentCell widget.
#
sub new {
my($self) = shift->SUPER::new(@_);
$self->[$_IDI] = {};
return $self;
}
# METHODS
#
#
# initialize()
#
# Initializes String attributes.
#
sub initialize {
my($self) = shift;
my($fields) = $self->[$_IDI];
return if $fields->{initialized};
my($field) = $self->get('field');
my($d) = $self->get_or_default('decimals', 1);
$self->put(
value => [$field, 'Bivio::UI::HTML::Format::Printf',
'%.'.$d.'f%%'],
column_align => 'E',
pad_left => 1,
column_nowrap => 1,
map(($_ => 'amount_cell'),
qw(column_data_class cell_class column_footer_class)),
);
$self->put(string_font => 'number_cell')
unless defined($self->unsafe_get('string_font'));
$fields->{initialized} = 1;
return $self->SUPER::initialize(@_);
}
#
# static internal_new_args(string field) : hash_ref
#
# static internal_new_args(string field, hash_ref attributes) : hash_ref
#
# Implements positional argument parsing for new.
#
sub internal_new_args {
my(undef, $field, $attributes) = @_;
return {
field => $field,
($attributes ? %$attributes : ()),
};
}
#
# render(any source, string_ref buffer)
#
# Draws the percent on the buffer.
#
sub render {
my($self, $source, $buffer) = @_;
# avoid rendering sign with "-0.0%"
my($str) = '';
$self->SUPER::render($source, \$str);
$str =~ s/\-(0\.0+\%)/$1/;
$$buffer .= $str;
return;
}
#=PRIVATE METHODS
# COPYRIGHT
#
# Copyright (c) 1999-2001 bivio Software, Inc. All rights reserved.
#
# VERSION
#
# $Id$
#
1;