ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/UserInterface_basic.pm
Revision: 1.7
Committed: Fri Dec 17 08:36:23 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: ProtoEnd
Changes since 1.6: +8 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 # runcheckedquery(checkobj, type, parameter, args) : as runquery but input
10 # is checked on input
11 # startform() : Indicate a new form to start
12 # endform(parameterstore) : Close a form and transfer parameters
13 # (call setparam() on object)
14 # messageout(text) : send a message to the user
15 #
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 sub expandvars {
41 my $self=shift;
42 my $string=shift;
43 print "String in : $string\n";
44 $string=~s/\$\((.*?)\)/$self->{paramhash}{$1}/g;
45 $string=~s/\$(.*?)( |\/|\Z)/$self->{paramhash}{$1}$2/g;
46 print "String Out in : $string\n";
47 return $string;
48
49 }
50
51 sub runcheckedquery() {
52 my $self=shift;
53 my $checker=shift;
54 my @args=@_;
55 my $type=shift;
56 my $param=shift;
57
58 $self->runquery(@args);
59 while ( ($self->{paramhash}{$param} ne "" ) &&
60 ($checker->check($self->expandvars($self->{paramhash}{$param})))) {
61 $self->printout("Input Error : ".$checkobj->errormessage()."\n");
62 $self->runquery(@args);
63 }
64 }
65
66 sub runquery() {
67 my $self=shift;
68 my $type=shift;
69 my $param=shift;
70 my @args=@_;
71 my $found='false';
72
73 my $rv;
74
75 foreach $command ( @{$self->{allowed_commands}} ) {
76 if ( $command=~/^$type/ ) {
77 $rv=$self->$command( $param, @args ); $found='true';
78 last;
79 }
80 }
81 if ( ! ( $found=~/true/ ) ) {
82 # default to basic
83 $rv=$self->basic( $param, @args );
84 }
85 $self->{paramhash}{$param}=$rv;
86 $self->printout("\n");
87 }
88
89 sub messageout {
90 my $self=shift;
91 my $text=shift;
92
93 print $text;
94 }
95
96 # --------------- Request Implementations -----------------------------
97 #
98
99 sub basic {
100 my $self=shift;
101 my $param=shift;
102 my $string=shift;
103
104 my @args=shift;
105
106 $self->printout($string);
107 return $self->prompt("Please Enter Value :");
108 }
109
110 sub freechoice {
111 my $self=shift;
112 my $param=shift;
113 my $string=shift;
114 my $choiceref=shift;
115
116 my $menuchoice;
117 my $rv;
118
119 $self->printout($string."\n");
120 my $num=1;
121 foreach $item ( @{$choiceref} ) {
122 $self->printout($num.". ".$item."\n");
123 $num++;
124 }
125 if ( $num == 1 ) {
126 $rv=$self->basic($param,"Please enter a value\n");
127 }
128 else {
129 $self->printout($num.". Enter Alternative Value\n");
130 $self->printout("Please Enter a Number from the list above :");
131 # Error check
132 while ( (($menuchoice=$self->prompt()) > $num) || ( $menuchoice < 1) ) {
133 $self->printout("Please Select a number Between 1 and $num :");
134 }
135 # Deal with alternative value
136 if ( $menuchoice == $num ) {
137 $rv=$self->basic($param,"Please enter new value\n");
138 }
139 else {
140 $rv=$$choiceref[$menuchoice-1];
141 $self->printout( $rv." selected\n");
142 }
143 }
144 return $rv;
145 }
146
147 sub strictchoice {
148 my $self=shift;
149 my $param=shift;
150 my $string=shift;
151 my $choiceref=shift;
152
153 my $menuchoice;
154 my $rv;
155
156 $self->printout($string);
157 my $num=0;
158 foreach $item ( @{$choiceref} ) {
159 $num++;
160 $self->printout($num.". ".$item."\n");
161 }
162 if ( $num!=0 ) {
163 $self->printout("Please Enter a Number from the list above :");
164 # Error check
165 while ( (($menuchoice=$self->prompt()) > $num) || ( $menuchoice < 1) ) {
166 $self->printout("Please Select a number Between 1 and $num :");
167 }
168 $rv=$$choiceref[$menuchoice-1];
169 $self->printout( $rv." selected\n");
170 }
171 return $rv;
172 }
173 # ----------------- Support Routines ----------------------------------
174
175 sub printout {
176 my $self=shift;
177 my $string=shift;
178
179 print $string;
180 }
181
182 sub prompt {
183 my $self=shift;
184 my $prompt=shift;
185 my $input;
186
187 if ( defined $prompt ) {
188 print $prompt;
189 }
190 $input = <STDIN>;
191 chomp $input;
192 return $input;
193 }
194
195 sub startform {
196 my $self=shift;
197 my $string=shift;
198 undef %$self->{paramhash};
199 $self->printout("-----------------------------------------------------".
200 "---------------\n");
201 $self->printout($string."\n\n");
202 }
203
204 sub endform {
205 my $self=shift;
206 my $parameterobject=shift;
207
208 # copy the parameter hash across
209 foreach $key ( keys %{$self->{'paramhash'}} ) {
210 $parameterobject->setparam($key, $self->{paramhash}{$key});
211 }
212 }