ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/SimpleUserInterface.pm
Revision: 1.7
Committed: Wed Mar 29 09:45:48 2000 UTC (25 years, 1 month ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: v102p1, V1_0_1, V1_0_0, V1_pre0, SCRAM_V1, SCRAMV1_IMPORT, V0_19_7, V0_19_6, V0_19_6p1, V0_19_5, SFATEST, V0_19_4, V0_19_4_pre3, V0_19_4_pre2, V0_19_4_pre1, V0_19_3, V0_19_2, V0_19_1, V0_19_0, V0_18_5, V0_18_4, V_18_3_TEST, VO_18_3, V0_18_2, V0_18_1, ProtoEnd
Branch point for: V1_pre1, SCRAM_V1_BRANCH, V0_19_4_B
Changes since 1.6: +2 -0 lines
Log Message:
tweaking

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 williamc 1.5 my $rv;
30 williamc 1.1 my $found=0;
31    
32     # print out informational
33     my $title=$userquery->title();
34     if ( $title ne "" ) {
35 williamc 1.6 print "-"x50;
36 williamc 1.1 print "\n";
37     print $title."\n";
38 williamc 1.6 print "-"x50;
39 williamc 1.1 print "\n";
40     }
41     print $userquery->intro();
42     print "\n\n";
43    
44     foreach $query ( $userquery->querylist() ) {
45 williamc 1.2 $rv=$self->_executequery($userquery,$query);
46 williamc 1.1 }
47 williamc 1.5 $userquery->OK();
48     return 1;
49 williamc 1.1 }
50    
51 williamc 1.2 sub _executequery {
52     my $self=shift;
53     my $userquery=shift;
54     my $query=shift;
55    
56 williamc 1.7 my $found='false';
57 williamc 1.2 # -- informational for each query
58 williamc 1.6 print "+"x30;
59 williamc 1.7 print "\n";
60 williamc 1.2 print $userquery->querymessage($query)."\n";
61    
62     # -- ship off to the correct routine based on type
63     foreach $command ( @{$self->{allowed_commands}} ) {
64     $type=$userquery->querytype($query);
65     if ( $command=~/^$type/ ) {
66     $rv=$self->_calltype($command, $userquery, $query);
67     $found='true';
68     last;
69     }
70     }
71     if ( $found ne "true" ) {
72     # default to basic
73     $rv=$self->_calltype("basic", $userquery, $query);
74     }
75     return $rv;
76     }
77    
78 williamc 1.3 # requires typehadler to return the response - undef assumed to be a cancel
79 williamc 1.1 sub _calltype {
80     my $self=shift;
81     my $command=shift;
82     my $userquery=shift;
83     my $query=shift;
84    
85     my @options=$userquery->queryoptions($query);
86     my $rv=$self->$command( $userquery->queryprompt($query), @options );
87    
88 williamc 1.3 if ( defined $rv ) {
89     # now process the response
90     my ($ret,$message,@vals)=$userquery->lodgevalue($query,$rv);
91     if ($ret ne 0 ) { # invalid response
92     print "Input not valid :\n";
93     print $message->read();
94     $rv=$self->_executequery($userquery,$query);
95     }
96 williamc 1.2 }
97 williamc 1.1 return $rv;
98     }
99    
100     sub freechoice {
101     my $self=shift;
102     my $prompt=shift;
103     my @options=@_;
104    
105     my $menuchoice;
106     my ($rv, $item);
107    
108     my $num=1;
109     foreach $item ( @options ) {
110     print $num.". ".$item."\n";
111     $num++;
112     }
113 williamc 1.4 print $num.". Enter".($num==1?"":" Alternative")." Value\n";
114     my $test=0;
115     # input from user
116     while ( ) {
117     $menuchoice=$self->basic("Please Enter a Number from".
118     " the list above (RETURN to cancel)>");
119     dp { $menuchoice=undef; last; } if ( $menuchoice eq "" );
120     do {print "Please Select a number Between 1 and $num :\n" ;
121     next; } if ( ($menuchoice!~/^\d+$/) ||
122     (($menuchoice > $num) || ( $menuchoice < 1)));
123     last;
124 williamc 1.1 }
125     # Deal with alternative value
126 williamc 1.6 if ( !defined $menuchoice ) {
127 williamc 1.5 print "Cancelled\n";
128 williamc 1.3 $rv=undef;
129     }
130 williamc 1.6 elsif ( $menuchoice == $num ) {
131     $rv=$self->basic($prompt);
132     }
133 williamc 1.1 else {
134     $rv=$options[$menuchoice-1];
135     print( $rv." selected\n");
136     }
137     return $rv;
138     }
139    
140     sub strictchoice {
141     my $self=shift;
142     my $prompt=shift;
143     my @options=@_;
144    
145     my $menuchoice;
146     my ($rv, $item);
147    
148     my $num=0;
149     foreach $item ( @options ) {
150     $num++;
151     print $num.". ".$item."\n";
152     }
153 williamc 1.3 if ( $num == 0 ) {
154     return $rv=$self->basic($prompt);
155 williamc 1.1 }
156 williamc 1.3 elsif ( $num==1 ) {
157     $rv=1;
158     }
159 williamc 1.1 else {
160     print "Please Enter a Number from the list above :";
161     while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
162     (($menuchoice > $num) || ( $menuchoice < 1)) ) {
163     print "Please Select a number Between 1 and $num :" ;
164     }
165     }
166     $rv=$options[$menuchoice-1];
167     return $rv;
168     }
169    
170     sub basic {
171     my $self=shift;
172     my $prompt;
173    
174     ((@_ && (defined $_[0]))?($prompt=shift):($prompt=">"));
175     print $prompt;
176     my $in=<STDIN>;
177     chomp $in;
178     return $in;
179     }