#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#
# Author: Paul mooney
# Date:   28 July 2000
#
# Call the new function with a clone name and it will show all the
# plate names linked to that clone.
#
# $Id: Show_Plates.pm,v 1.1 2000/07/31 13:13:32 pjm Exp $

package Show_Plates;

sub new {
    my ($type, $options) = @_; #$parent_window, $clone_name) = @_;

    my $self = {
        clone_name    => $options->{clone_name},
        project_name  => $options->{project_name},
        parent_window => $options->{parent_window},
    };

    bless($self, $type);

    my $ref_to_show = $self->get_plates_for($self->{clone_name});
    $self->create_window($self->{parent_window},
                         $self->{clone_name},
                         $ref_to_show);
}

sub get_plates_for {
    my ($self, $clone_name) = @_;
    my @to_show;
    my $dbh;

    eval {
        if( $clone_name ){

            $dbh = WrapDBI->connect('reports', {RaiseError => 1});

            if( !defined $dbh ) {
                die "Could not connect to database";
            }

            $dbh->{AutoCommit} = 0;

            my $sth =
                $dbh->prepare(q[select distinct l.ID_LIGATION, bb.NAME
                                from   LIGATION l, SUBCLONE sc,
                                       BB_MAP bbm, BECKMAN_BOX bb
                                where  l.CLONENAME       = ?
                                and    l.ID_LIGATION     = sc.ID_LIGATION
                                and    sc.ID_SUBCLONE    = bbm.ID_SUBCLONE
                                and    bbm.ID_BECKMANBOX = bb.ID_BECKMANBOX
                                order by l.ID_LIGATION, bb.NAME ]);
            $sth->execute($clone_name);
            my $ref = $sth->fetchall_arrayref();

            if( @{$ref} ){
                push @to_show, sprintf("%10s %-20s", "Ligation", "Plate");
                push @to_show, sprintf("%10s %-20s", "-"x10, "-"x20);

                foreach my $row (@{$ref}){
                    push @to_show, sprintf("%10d %-20s", $row->[0], $row->[1]);
                }
            }
        }
    };

    my $error = $@;
    $dbh->disconnect() if $dbh;

    die $error if $error;

#    print join("\n", @to_show), "\n";

    return \@to_show;
}

sub create_window {
    my ($self, $parent_window, $clone_name, $ref_to_show) = @_;

    my $mw = $parent_window->Toplevel();
    $self->{mw} = $mw;

    $mw->title("Plates for clone $clone_name");
    my $fixed_width_font =
        '-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso8859-1',;

    # LISTBOX
    my $list_box =
        $mw->ScrlListbox(-font            => $fixed_width_font,
                         -width           => 35,
                         -height          => 25,
                         -scrollbars      => 'e',
                         -selectmode      => 'single',
                         -exportselection => 0
                         )->pack(-expand => 'y',
                                 -fill   => 'both');

    foreach my $text (@{$ref_to_show}){
        $list_box->insert('end', $text);
    }
    
    # Ok
    $mw->Button(-text    => 'Ok',
                -command => sub{ $self->{button_pressed} = 'Yes'; },
                )->pack();


    # make sure whole window appears on screen, not just the title.

    my $f = $self->{mw}->Frame(-width => 10, -height => 20);
    $f->pack(-side => 'top');
    $self->{mw}->idletasks();
    $self->{mw}->packPropagate(0);
    $f->packForget(); 

    # center window on screen

    my $x = int(($self->{mw}->screenwidth  - $self->{mw}->reqwidth)  / 2);
    my $y = int(($self->{mw}->screenheight - $self->{mw}->reqheight) / 2);
    
    $self->{mw}->geometry("+$x+$y");

    # grap focus and wait till the user has pressed one of the buttons

    $self->{mw}->raise();
    $self->{mw}->grab();
    $self->{mw}->waitVisibility;
    $self->{mw}->update;

    $self->{mw}->waitVariable(\$self->{button_pressed});
    $self->{mw}->grabRelease;
    $self->{mw}->withdraw;

    return $self->{button_pressed};
}

#End of module
1;

#
#
# $Log: Show_Plates.pm,v $
# Revision 1.1  2000/07/31  13:13:32  pjm
# Initial revision
#
#
# 
