ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/UserInterface_basic.pm
Revision: 1.5
Committed: Mon Oct 25 15:03:05 1999 UTC (25 years, 6 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.4: +13 -4 lines
Log Message:
Bugfix - actually transfer params at endform

File Contents

# User Rev Content
1 williamc 1.1 #
2     # User Interface --- very basic command line input interface
3     #
4     # Interface
5     # ---------
6     # new() : a new interface object
7     # runquery(type, parameter,args) : Call the appropriate query handling
8     # routine for the given type
9 williamc 1.4 # runcheckedquery(checkobj, type, parameter, args) : as runquery but input
10     # is checked on input
11 williamc 1.1 # startform() : Indicate a new form to start
12     # endform(parameterstore) : Close a form and transfer parameters
13     # (call setparam() on object)
14 williamc 1.3 # messageout(text) : send a message to the user
15 williamc 1.1 #
16     # ------------------------------------------------------------
17     # ---- required processing routines ---
18     # freechoice( param , commentstring, optionref )
19     # : display and ask user for input
20     # strictchoice( param , commentstring, optionref )
21     # : display and ask user for input
22     # basic(param, commentstring) : Simply user input query
23    
24     package ActiveDoc::UserInterface_basic;
25     require 5.001;
26    
27     sub new {
28     my $class=shift;
29     $self={};
30     bless $self, $class;
31     $self->init(@_); # override if reqd
32     return $self;
33     }
34    
35     sub init {
36     my $self=shift;
37     @{$self->{allowed_commands}}=qw(freechoice basic strictchoice);
38     }
39    
40 williamc 1.5 sub expandvars {
41     my $self=shift;
42     my $string=shift;
43     $string=~s/\$(.*?)( |\/|\Z)/$self->{paramhash}{$1}$2/g;
44     return $string;
45    
46     }
47    
48 williamc 1.4 sub runcheckedquery() {
49     my $self=shift;
50     my $checkobj=shift;
51     my @args=@_;
52     my $type=shift;
53     my $param=shift;
54    
55     $self->runquery(@args);
56     while ( ($self->{paramhash}{$param}) ne "" &&
57 williamc 1.5 ($checkobj->check($self->expandvars($self->{paramhash}{$param})))) {
58 williamc 1.4 $self->printout("Input Error : ".$checkobj->errormessage()."\n");
59     $self->runquery(@args);
60     }
61     }
62    
63 williamc 1.1 sub runquery() {
64     my $self=shift;
65     my $type=shift;
66     my $param=shift;
67     my @args=@_;
68     my $found='false';
69    
70     my $rv;
71    
72     foreach $command ( @{$self->{allowed_commands}} ) {
73     if ( $command=~/^$type/ ) {
74     $rv=$self->$command( $param, @args ); $found='true';
75     last;
76     }
77     }
78     if ( ! ( $found=~/true/ ) ) {
79     # default to basic
80     $rv=$self->basic( $param, @args );
81     }
82     $self->{paramhash}{$param}=$rv;
83 williamc 1.5 $self->printout("\n");
84 williamc 1.1 }
85    
86 williamc 1.3 sub messageout {
87     my $self=shift;
88     my $text=shift;
89    
90     print $text;
91     }
92 williamc 1.1
93     # --------------- Request Implementations -----------------------------
94     #
95    
96     sub basic {
97     my $self=shift;
98     my $param=shift;
99     my $string=shift;
100 williamc 1.4
101     my @args=shift;
102 williamc 1.1
103     $self->printout($string);
104 williamc 1.4 return $self->prompt("Please Enter Value :");
105 williamc 1.1 }
106    
107     sub freechoice {
108     my $self=shift;
109     my $param=shift;
110     my $string=shift;
111     my $choiceref=shift;
112    
113     my $menuchoice;
114     my $rv;
115    
116 williamc 1.5 $self->printout($string."\n");
117 williamc 1.1 my $num=1;
118     foreach $item ( @{$choiceref} ) {
119     $self->printout($num.". ".$item."\n");
120     $num++;
121     }
122 williamc 1.2 if ( $num == 1 ) {
123     $rv=$self->basic($param,"Please enter a value\n");
124     }
125     else {
126 williamc 1.1 $self->printout($num.". Enter Alternative Value\n");
127     $self->printout("Please Enter a Number from the list above :");
128     # Error check
129     while ( (($menuchoice=$self->prompt()) > $num) || ( $menuchoice < 1) ) {
130     $self->printout("Please Select a number Between 1 and $num :");
131     }
132     # Deal with alternative value
133     if ( $menuchoice == $num ) {
134     $rv=$self->basic($param,"Please enter new value\n");
135     }
136     else {
137     $rv=$$choiceref[$menuchoice-1];
138     $self->printout( $rv." selected\n");
139     }
140 williamc 1.2 }
141 williamc 1.1 return $rv;
142     }
143    
144     sub strictchoice {
145     my $self=shift;
146     my $param=shift;
147     my $string=shift;
148     my $choiceref=shift;
149    
150     my $menuchoice;
151     my $rv;
152    
153     $self->printout($string);
154     my $num=0;
155     foreach $item ( @{$choiceref} ) {
156     $num++;
157     $self->printout($num.". ".$item."\n");
158     }
159 williamc 1.2 if ( $num!=0 ) {
160 williamc 1.1 $self->printout("Please Enter a Number from the list above :");
161     # Error check
162     while ( (($menuchoice=$self->prompt()) > $num) || ( $menuchoice < 1) ) {
163     $self->printout("Please Select a number Between 1 and $num :");
164     }
165     $rv=$$choiceref[$menuchoice-1];
166     $self->printout( $rv." selected\n");
167 williamc 1.2 }
168 williamc 1.1 return $rv;
169     }
170     # ----------------- Support Routines ----------------------------------
171    
172     sub printout {
173     my $self=shift;
174     my $string=shift;
175    
176     print $string;
177     }
178    
179     sub prompt {
180     my $self=shift;
181 williamc 1.4 my $prompt=shift;
182 williamc 1.1 my $input;
183    
184 williamc 1.4 if ( defined $prompt ) {
185     print $prompt;
186     }
187 williamc 1.1 $input = <STDIN>;
188     chomp $input;
189     return $input;
190     }
191    
192     sub startform {
193     my $self=shift;
194 williamc 1.4 my $string=shift;
195 williamc 1.1 undef %$self->{paramhash};
196     $self->printout("-----------------------------------------------------".
197     "---------------\n");
198 williamc 1.4 $self->printout($string."\n\n");
199 williamc 1.1 }
200    
201     sub endform {
202     my $self=shift;
203     my $parameterobject=shift;
204    
205     # copy the parameter hash across
206 williamc 1.5 foreach $key ( %{$self->{'paramhash'}} ) {
207     $parameterobject->setparam($key, $self->{paramhash}{$key});
208 williamc 1.1 }
209     }