MARC/Field.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 156 CC 12 SCAR 34.8
CC
12
Cov
0%
Populate
72.0
get
30.0
ClassInit
2.0
CRAP 32.4
1 $Tk::MARC::Field::VERSION = '0.11';
2
3 package Tk::MARC::Field;
4
5 =head1 NAME
6
7 Tk::MARC::Field - megawidget for editing MARC::Field objects.
8
9 =head1 SYNOPSIS
10
11 use Tk;
12 use Tk::MARC::Field;
13 use MARC::Field
14
15 my $mw = MainWindow->new;
16 my $field = MARC::Field->new('245','','',
17 'a' => 'The Case for Mars: ',
18 'b' => 'The plan to settle the red
19 planet, and why we must.'
20 );
21 $mw->MARC_Field(-field => $field
22 )->pack(-anchor => 'w');
23
24 MainLoop;
25
26 =head1 DESCRIPTION
27
28 This is a megawidget that allows editing of a MARC::Field object.
29 The widget itself does not change the MARC::Field - that is
30 up to the widget's parent.
31
32 The widget allows you to indicate your desire to delete/undelete
33 the field by clicking a button (which will change the color of
34 the tag).
35
36 It allows you to add new subfields by selecting from a list (you
37 can indicate your desire to delete a subfield by clicking a
38 button)... and it knows what subfields are valid for this field
39 (by using MARC::Descriptions).
40
41 You will likely never use a Tk::MARC::Field directly - it is simply
42 a component of Tk::MARC::Editor.
43
44 =cut
45
46 # Revision history for Tk::MARC::field.
47 # -------------------------------------
48 # 0.11 January 20, 2004
49 # - added check for missing -field
50 # - added check for missing -subfields
51 #
52 # 0.10 January 17, 2004
53 # - renamed to Tk::MARC::Field (capitalized 'Field') to better
54 # match MARC::Field and avoid confusion (thanks Andy!)
55 #
56 # 0.9 January 16, 2004
57 # - properly handle get() for fields < '010'
58 #
59 # 0.8 January 15, 2004
60 # - get() routine
61 #
62 # 0.7 January 14, 2004
63 # - first bit of POD
64 #
65 # 0.6 January 14, 2004
66 # - handle tags <= 010 (i.e. no subfields, just data)
67 #
68 # 0.5 January 14, 2004
69 # - proper _add_subfield routine
70 #
71 # 0.4 January 14, 2004
72 # - menu for adding subfields
73 #
74 # 0.3 January 12, 2004
75 # - built a couple of sample routines in pl/
76 # - t/01-use.t
77 #
78 # 0.2 January 09, 2004
79 # - now uses the newly-written MARC::Descriptions module
80 # (which is available on the CPAN)
81 #
82 # 0.1 January 06, 2004
83 # - original version
84 #
85
86 use Tk::widgets;
87 use base qw/Tk::Frame/;
88 use Tk::MARC::Subfield;
89 use Tk::MARC::Indicators;
90 use MARC::Descriptions;
91 use MARC::Field;
92 use Carp;
93 use strict;
94
95 Construct Tk::Widget 'MARC_Field';
96
97 our (
98 $TD,
99 );
100
101 sub ClassInit {
102 my ($class, $mw) = @_;
103 $TD = new MARC::Descriptions;
104 $class->SUPER::ClassInit($mw);
105 }
106
107 sub Populate {
108 my ($self, $args) = @_;
109
110 my $field = delete $args->{'-field'};
111 my $tag = delete $args->{'-tag'};
112 my $ind1 = delete $args->{'-ind1'};
113 my $ind2 = delete $args->{'-ind2'};
114 my $subfields = delete $args->{'-subfields'};
115 if (defined $field) {
116 croak "Not a MARC::Field" unless (ref($field) eq 'MARC::Field');
117 $self->{MARCField} = $field;
118 } else {
119 croak "Missing -tag" unless (defined $tag);
120 croak "Missing -subfields" unless (defined $subfields);
121 $self->{MARCField} = MARC::Field->new( $tag,
122 $ind1,
123 $ind2,
124 @$subfields
125 );
126 }
127
128 $self->SUPER::Populate($args);
129 $self->{"fixed_font"} = "-adobe-courier-medium-r-normal--14-100-75-75-m-60-iso8859-2";
130
131 my $FRAME = $self->Frame(-relief => 'ridge',
132 -borderwidth => 1,
133 )->pack(-side => 'top',
134 -expand => 1,
135 -fill => 'both');
136 my $FRAME_LABEL = $FRAME->Frame(-relief => 'raised',
137 -borderwidth => 1,
138 )->pack(-side => 'left',
139 -expand => 0,
140 -fill => 'y');
141 my $FRAME_TAG = $FRAME_LABEL->Frame()->pack(-side => 'top',
142 -expand => 0,
143 -fill => 'x');
144 my $FRAME_IND = $FRAME_LABEL->Frame()->pack(-side => 'top',
145 -expand => 1,
146 -fill => 'both');
147
148 # my $FRAME_EDIT = $self->Frame()->pack(-side => 'left',
149 # -expand => 1,
150 # -fill => 'both');
151 my $FRAME_EDIT = $FRAME->Frame()->pack(-side => 'left',
152 -expand => 1,
153 -fill => 'both');
154 my $FRAME_DESC = $FRAME_EDIT->Frame()->pack(-side => 'top',
155 -expand => 1,
156 -fill => 'x');
157 my $FRAME_SUBS = $FRAME_EDIT->Frame()->pack(-side => 'top',
158 -expand => 1,
159 -fill => 'both');
160
161 my $LABEL = $FRAME_TAG->Label(-text => $self->{'MARCField'}->tag(),
162 -anchor => 'nw',
163 -justify => 'left',
164 )->pack(-side => 'left',
165 -expand => 0,
166 -fill => 'both');
167 my $DEFAULT_TAG_COLOR = $LABEL->cget(-background);
168 $self->{DELETED} = "no";
169 my $B_DELETE = $FRAME_TAG->Button(-text => "X",
170 -font => $self->{"fixed_font"},
171 -width => 1,
172 -height => 1,
173 -padx => 0,
174 -pady => 0,
175 -command => sub {
176 $self->{DELETED} = "yes";
177 $LABEL->configure(-background => "red");
178 }
179 )->pack(-side => 'right');
180 my $B_UNDELETE = $FRAME_TAG->Button(-text => "O",
181 -font => $self->{"fixed_font"},
182 -width => 1,
183 -height => 1,
184 -padx => 0,
185 -pady => 0,
186 -command => sub {
187 $self->{DELETED} = "no";
188 $LABEL->configure(-background => $DEFAULT_TAG_COLOR);
189 }
190 )->pack(-side => 'right');
191 my $s;
192 $s = $TD->get($self->{'MARCField'}->tag(), "description");
193 $s = "No description from MARC::Descriptions" unless defined $s;
194 my $DESCRIPTION = $FRAME_DESC->Label(-text => $s,
195 -width => 42,
196 -anchor => 'nw',
197 -justify => 'left',
198 )->pack(-side => 'left',
199 -expand => 0,
200 -fill => 'x');
201
202
203 if ($self->{MARCField}->tag() ge '010') {
204 #
205 # Menu for adding subfields
206 #
207 my @menu_subfields;
208 my $ref_menu_subfields;
209 @menu_subfields = ();
210 my $href = $TD->get($self->{'MARCField'}->tag(), "subfields");
211 foreach my $subfield (sort keys %$href) {
212 push @menu_subfields,['command' => "[" . $subfield . "] " . $href->{$subfield}{description},
213 -command => sub {
214 $self->_add_subfield($self->{MARCField}->tag(), $FRAME_SUBS, $subfield);
215 }
216 ];
217 }
218 my $B_NEWSUBFLD = $FRAME_DESC->Menubutton(-text => "New subfield",
219 -font => $self->{"fixed_font"},
220 -padx => 0,
221 -pady => 0,
222 -tearoff => 0,
223 -indicatoron => 1,
224 -menuitems => \@menu_subfields,
225 )->pack(-side => 'left');
226
227 #
228 # Subfields
229 #
230 my @subfields = $self->{MARCField}->subfields();
231 $self->{subfields} = ();
232 my $i = 0;
233 foreach my $subfield (@subfields) {
234 $self->{subfields}[$i++] = $FRAME_SUBS->MARC_Subfield(-field => $self->{MARCField}->tag(),
235 -label => @$subfield[0],
236 -value => @$subfield[1]
237 )->pack(-anchor => 'w');
238 }
239 } else {
240 #
241 # Tag is <= 010
242 #
243 $self->{data} = $FRAME_SUBS->MARC_Subfield(-field => $self->{MARCField}->tag(),
244 -label => 'DATA',
245 -value => $self->{MARCField}->data()
246 )->pack(-anchor => 'w');
247 }
248
249 #
250 # indicators
251 #
252 if ($self->{MARCField}->tag() ge '010') {
253 $self->{indicators} = $FRAME_IND->MARC_Indicators(-field => $self->{MARCField}->tag(),
254 -ind1 => $self->{MARCField}->indicator(1),
255 -ind2 => $self->{MARCField}->indicator(2),
256 )->pack(-anchor => 'w');
257 }
258
259 $self->ConfigSpecs( '-field' => [ 'PASSIVE', , , undef],
260 '-tag' => [ 'PASSIVE', , , undef],
261 '-ind1' => [ 'PASSIVE', , , undef],
262 '-ind2' => [ 'PASSIVE', , , undef],
263 '-subfields' => [ 'PASSIVE', , , undef],
264 );
265 }
266
267
268 sub _add_subfield {
269 my $self = shift;
270 my ($tag, $f, $subfield) = @_;
271 push @{ $self->{subfields} }, $f->MARC_Subfield(-field => $tag,
272 -label => $subfield,
273 -value => "New subfield",
274 )->pack(-anchor => 'w');
275 }
276
277 sub get {
278 my $self = shift;
279
280 my $fld = undef;
281
282 if ($self->{DELETED} eq "no") {
283 if ($self->{MARCField}->tag() lt '010') {
284 $fld = MARC::Field->new($self->{MARCField}->tag(),
285 $self->{data}->get(),
286 );
287 } else {
288 #
289 # Tag >= 010
290 #
291 my $sf = undef;
292 my $i = -1;
293
294 # Find the first (non-deleted) subfield
295 while (($i < $#{ $self->{subfields} })
296 && (not defined $sf)
297 ) {
298 $i++;
299 $sf = $self->{subfields}[$i]->get();
300 }
301
302 # Did we find a valid subfield before falling off the end?
303 if (defined $sf) {
304 $fld = MARC::Field->new($self->{MARCField}->tag(),
305 $self->{indicators}->get(1),
306 $self->{indicators}->get(2),
307 @$sf[0] => @$sf[1],
308 );
309 $i++;
310 while ($i <= $#{ $self->{subfields} }) {
311 $sf = $self->{subfields}[$i++]->get();
312 if (defined $sf) {
313 $fld->add_subfields( @$sf );
314 }
315 }
316 } else {
317 # all subfields were deleted, so no Field is possible.
318 }
319 }
320 }
321 return $fld;
322 }
323
324 =head1 AUTHOR
325
326 David Christensen, <DChristensenSPAMLESS@westman.wave.ca>
327
328 =cut
329
330 =head1 COPYRIGHT AND LICENSE
331
332 Copyright 2004 by David Christensen
333
334 This library is free software; you can redistribute it and/or modify it
335 under the same terms as Perl itself.
336
337 =cut
338
339 1;