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 |
$rv=$self->_executequery($userquery,$query);
|
46 |
}
|
47 |
|
48 |
|
49 |
# --- confirm changes with user if we have input more than one value
|
50 |
# if ( $#{$userquery->querylist()} > 1 ) {
|
51 |
# print "\nDo you wish to Accept,Restart or Cancel these values? ".
|
52 |
# "(a/r/c)\n";
|
53 |
# my $in=<STDIN>;
|
54 |
# if ( $in!~/^a/ ) {
|
55 |
# $userquery->cancel();
|
56 |
# if ( $in=~/^r/ ) {
|
57 |
# return $self->askuser($userquery);
|
58 |
# }
|
59 |
# }
|
60 |
# }
|
61 |
if ( defined $rv ) {
|
62 |
$userquery->OK();
|
63 |
}
|
64 |
return $rv;
|
65 |
}
|
66 |
|
67 |
sub _executequery {
|
68 |
my $self=shift;
|
69 |
my $userquery=shift;
|
70 |
my $query=shift;
|
71 |
|
72 |
# -- informational for each query
|
73 |
print $userquery->querymessage($query)."\n";
|
74 |
|
75 |
# -- ship off to the correct routine based on type
|
76 |
foreach $command ( @{$self->{allowed_commands}} ) {
|
77 |
$type=$userquery->querytype($query);
|
78 |
if ( $command=~/^$type/ ) {
|
79 |
$rv=$self->_calltype($command, $userquery, $query);
|
80 |
$found='true';
|
81 |
last;
|
82 |
}
|
83 |
}
|
84 |
if ( $found ne "true" ) {
|
85 |
# default to basic
|
86 |
$rv=$self->_calltype("basic", $userquery, $query);
|
87 |
}
|
88 |
return $rv;
|
89 |
}
|
90 |
|
91 |
# requires typehadler to return the response - undef assumed to be a cancel
|
92 |
sub _calltype {
|
93 |
my $self=shift;
|
94 |
my $command=shift;
|
95 |
my $userquery=shift;
|
96 |
my $query=shift;
|
97 |
|
98 |
my @options=$userquery->queryoptions($query);
|
99 |
my $rv=$self->$command( $userquery->queryprompt($query), @options );
|
100 |
|
101 |
if ( defined $rv ) {
|
102 |
# now process the response
|
103 |
my ($ret,$message,@vals)=$userquery->lodgevalue($query,$rv);
|
104 |
if ($ret ne 0 ) { # invalid response
|
105 |
print "Input not valid :\n";
|
106 |
print $message->read();
|
107 |
$rv=$self->_executequery($userquery,$query);
|
108 |
}
|
109 |
}
|
110 |
return $rv;
|
111 |
}
|
112 |
|
113 |
sub freechoice {
|
114 |
my $self=shift;
|
115 |
my $prompt=shift;
|
116 |
my @options=@_;
|
117 |
|
118 |
my $menuchoice;
|
119 |
my ($rv, $item);
|
120 |
|
121 |
my $num=1;
|
122 |
foreach $item ( @options ) {
|
123 |
print $num.". ".$item."\n";
|
124 |
$num++;
|
125 |
}
|
126 |
if ( $num == 1 ) {
|
127 |
return $rv=$self->basic($prompt);
|
128 |
}
|
129 |
else {
|
130 |
print $num.". Enter Alternative Value\n";
|
131 |
while ( ((($menuchoice=$self->basic("Please Enter a Number from".
|
132 |
" the list above >"))!~/^\d+$/) ||
|
133 |
(($menuchoice > $num) || ( $menuchoice < 1)))) {
|
134 |
print "Please Select a number Between 1 and $num :\n" ;
|
135 |
}
|
136 |
}
|
137 |
# Deal with alternative value
|
138 |
if ( $menuchoice == $num ) {
|
139 |
$rv=$self->basic($prompt);
|
140 |
}
|
141 |
elsif ( $menuchoice eq "" ) {
|
142 |
$rv=undef;
|
143 |
}
|
144 |
else {
|
145 |
$rv=$options[$menuchoice-1];
|
146 |
print( $rv." selected\n");
|
147 |
}
|
148 |
return $rv;
|
149 |
}
|
150 |
|
151 |
sub strictchoice {
|
152 |
my $self=shift;
|
153 |
my $prompt=shift;
|
154 |
my @options=@_;
|
155 |
|
156 |
my $menuchoice;
|
157 |
my ($rv, $item);
|
158 |
|
159 |
my $num=0;
|
160 |
foreach $item ( @options ) {
|
161 |
$num++;
|
162 |
print $num.". ".$item."\n";
|
163 |
}
|
164 |
if ( $num == 0 ) {
|
165 |
return $rv=$self->basic($prompt);
|
166 |
}
|
167 |
elsif ( $num==1 ) {
|
168 |
$rv=1;
|
169 |
}
|
170 |
else {
|
171 |
print "Please Enter a Number from the list above :";
|
172 |
while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
|
173 |
(($menuchoice > $num) || ( $menuchoice < 1)) ) {
|
174 |
print "Please Select a number Between 1 and $num :" ;
|
175 |
}
|
176 |
}
|
177 |
$rv=$options[$menuchoice-1];
|
178 |
return $rv;
|
179 |
}
|
180 |
|
181 |
sub basic {
|
182 |
my $self=shift;
|
183 |
my $prompt;
|
184 |
|
185 |
((@_ && (defined $_[0]))?($prompt=shift):($prompt=">"));
|
186 |
print $prompt;
|
187 |
my $in=<STDIN>;
|
188 |
chomp $in;
|
189 |
return $in;
|
190 |
}
|