ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/SimpleUserInterface.pm
Revision: 1.1
Committed: Wed Dec 15 15:56:17 1999 UTC (25 years, 4 months ago) by williamc
Content type: text/plain
Branch: MAIN
Log Message:
Basic testing passed

File Contents

# User Rev Content
1 williamc 1.1 #
2     # SimpleUserInterface.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     #
8     # Interface
9     # ---------
10     # new() : A new SimpleUserInterface object
11     # askuser(Query) : Get inputs from the user based on the given query
12     # returns 0 if cancelled or 1 if accepted
13    
14     package ActiveDoc::SimpleUserInterface;
15     use ActiveDoc::Query;
16     require 5.004;
17    
18     sub new {
19     my $class=shift;
20     $self={};
21     bless $self, $class;
22     @{$self->{allowed_commands}}=qw(freechoice basic strictchoice);
23     return $self;
24     }
25    
26     sub askuser {
27     my $self=shift;
28     my $userquery=shift;
29     my $rv=1;
30     my $found=0;
31    
32     # print out informational
33     my $title=$userquery->title();
34     if ( $title ne "" ) {
35     print "-"x80;
36     print "\n";
37     print $title."\n";
38     print "-"x80;
39     print "\n";
40     }
41     print $userquery->intro();
42     print "\n\n";
43    
44     foreach $query ( $userquery->querylist() ) {
45     # -- informational for each query
46     print $userquery->querymessage($query)."\n";
47    
48     # -- ship off to the correct routine based on type
49     foreach $command ( @{$self->{allowed_commands}} ) {
50     $type=$userquery->querytype($query);
51     if ( $command=~/^$type/ ) {
52     $rv=$self->_calltype($command, $userquery, $query);
53     $found='true';
54     last;
55     }
56     }
57     if ( $found ne "true" ) {
58     # default to basic
59     $rv=$self->_calltype("basic", $userquery, $query);
60     }
61     }
62    
63    
64     # --- confirm changes with user if we have input more than one value
65     if ( $#{$userquery->querylist()} > 1 ) {
66     print "\nDo you wish to Accept,Restart or Cancel these values? ".
67     "(a/r/c)\n";
68     my $in=<STDIN>;
69     if ( $in!~/^a/ ) {
70     $rv=0;
71     $userquery->cancel();
72     if ( $in=~/^r/ ) {
73     return $self->askuser($userquery);
74     }
75     }
76     }
77     if ( $rv ne "0" ) {
78     $userquery->OK();
79     }
80     return $rv;
81     }
82    
83     sub _calltype {
84     my $self=shift;
85     my $command=shift;
86     my $userquery=shift;
87     my $query=shift;
88    
89     my @options=$userquery->queryoptions($query);
90     my $rv=$self->$command( $userquery->queryprompt($query), @options );
91    
92     # now process the response
93     $userquery->lodgevalue($query,$rv);
94     return $rv;
95     }
96    
97     sub freechoice {
98     my $self=shift;
99     my $prompt=shift;
100     my @options=@_;
101    
102     my $menuchoice;
103     my ($rv, $item);
104    
105     my $num=1;
106     foreach $item ( @options ) {
107     print $num.". ".$item."\n";
108     $num++;
109     }
110     if ( $num == 1 ) {
111     $rv=$self->basic($prompt);
112     }
113     else {
114     print $num.". Enter Alternative Value\n";
115     print "Please Enter a Number from the list above :";
116     while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
117     (($menuchoice > $num) || ( $menuchoice < 1)) ) {
118     print "Please Select a number Between 1 and $num :" ;
119     }
120     }
121     # Deal with alternative value
122     if ( $menuchoice == $num ) {
123     $rv=$self->basic("Please enter new value $prompt");
124     }
125     else {
126     $rv=$options[$menuchoice-1];
127     print( $rv." selected\n");
128     }
129     return $rv;
130     }
131    
132     sub strictchoice {
133     my $self=shift;
134     my $prompt=shift;
135     my @options=@_;
136    
137     my $menuchoice;
138     my ($rv, $item);
139    
140     my $num=0;
141     foreach $item ( @options ) {
142     $num++;
143     print $num.". ".$item."\n";
144     }
145     if ( $num == 1 ) {
146     $rv=$self->basic($prompt);
147     }
148     else {
149     print "Please Enter a Number from the list above :";
150     while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
151     (($menuchoice > $num) || ( $menuchoice < 1)) ) {
152     print "Please Select a number Between 1 and $num :" ;
153     }
154     }
155     $rv=$options[$menuchoice-1];
156     return $rv;
157     }
158    
159     sub basic {
160     my $self=shift;
161     my $prompt;
162    
163     ((@_ && (defined $_[0]))?($prompt=shift):($prompt=">"));
164     print $prompt;
165     my $in=<STDIN>;
166     chomp $in;
167     return $in;
168     }