| 1 | package Gtk2::Ex::FormFactory::Proxy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | my $NAME_CNT = 0; | ||
| 7 | |||
| 8 | sub get_context { shift->{context} } | ||
| 9 | sub get_name { shift->{name} } | ||
| 10 | sub get_aggregated_by { shift->{aggregated_by} } | ||
| 11 | sub get_set_prefix { shift->{set_prefix} } | ||
| 12 | sub get_get_prefix { shift->{get_prefix} } | ||
| 13 | sub get_attr_activity_href { shift->{attr_activity_href} } | ||
| 14 | sub get_attr_accessors_href { shift->{attr_accessors_href} } | ||
| 15 | sub get_attr_aggregate_href { shift->{attr_aggregate_href} } | ||
| 16 | sub get_accessor { shift->{accessor} } | ||
| 17 | sub get_changes_attr_filter { shift->{changes_attr_filter} } | ||
| 18 | |||
| 19 | sub get_buffered { 0 } | ||
| 20 | |||
| 21 | sub get_object_changed { shift->{object_changed} } | ||
| 22 | sub set_object_changed { shift->{object_changed} = $_[1] } | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $class = shift; | ||
| 26 | my %par = @_; | ||
| 27 | my ($context, $object, $name, $set_prefix, $get_prefix) = | ||
| 28 | @par{'context','object','name','set_prefix','get_prefix'}; | ||
| 29 | my ($attr_accessors_href, $attr_activity_href, $aggregated_by) = | ||
| 30 | @par{'attr_accessors_href','attr_activity_href','aggregated_by'}; | ||
| 31 | my ($accessor, $changes_attr_filter) = | ||
| 32 | @par{'accessor','changes_attr_filter'}; | ||
| 33 | |||
| 34 | $attr_accessors_href ||= {}, | ||
| 35 | $attr_activity_href ||= {}; | ||
| 36 | $name ||= "object_".$NAME_CNT++; | ||
| 37 | |||
| 38 | my $self = bless { | ||
| 39 | context => $context, | ||
| 40 | object => $object, | ||
| 41 | name => $name, | ||
| 42 | aggregated_by => $aggregated_by, | ||
| 43 | set_prefix => $set_prefix, | ||
| 44 | get_prefix => $get_prefix, | ||
| 45 | attr_activity_href => $attr_activity_href, | ||
| 46 | attr_accessors_href => $attr_accessors_href, | ||
| 47 | accessor => $accessor, | ||
| 48 | changes_attr_filter => $changes_attr_filter, | ||
| 49 | attr_aggregate_href => {}, | ||
| 50 | }, $class; | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_object { | ||
| 56 | my $self = shift; | ||
| 57 | my $object = $self->{object}; | ||
| 58 | ref $object eq 'CODE' ? &$object() : $object; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub update_by_aggregation { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | my $aggregated_by = $self->get_aggregated_by; | ||
| 65 | |||
| 66 | my $object = $self->get_attr($aggregated_by); | ||
| 67 | |||
| 68 | $self->set_object($object); | ||
| 69 | |||
| 70 | 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub set_object { | ||
| 74 | my $self = shift; | ||
| 75 | my ($object) = @_; | ||
| 76 | |||
| 77 | #-- nothing to do if it's the same object | ||
| 78 | #-- (eval{} is for catching Class::DBI exceptions if | ||
| 79 | #-- $self->{object} was deleted in the meantime) | ||
| 80 | return if eval { $object eq $self->{object} }; | ||
| 81 | |||
| 82 | #-- reset changed status | ||
| 83 | $self->set_object_changed(0); | ||
| 84 | |||
| 85 | #-- set object | ||
| 86 | $self->{object} = $object; | ||
| 87 | |||
| 88 | #-- Update all object widgets | ||
| 89 | my $context = $self->get_context; | ||
| 90 | $context->update_object_widgets ($self->get_name); | ||
| 91 | |||
| 92 | #-- Update aggregated objects | ||
| 93 | my $attr_aggregate_href = $self->get_attr_aggregate_href; | ||
| 94 | my ($attr, $child_object_name, $child_object); | ||
| 95 | |||
| 96 | while ( ($attr, $child_object_name) = each %{$attr_aggregate_href} ) { | ||
| 97 | $child_object = $self->get_attr($attr); | ||
| 98 | $context->set_object($child_object_name, $child_object); | ||
| 99 | } | ||
| 100 | |||
| 101 | return $object; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub get_attr { | ||
| 105 | my $self = shift; | ||
| 106 | my ($attr_name) = @_; | ||
| 107 | |||
| 108 | if ( $attr_name =~ /^([^.]+)\.(.*)$/ ) { | ||
| 109 | $self = $self->get_context->get_proxy($1); | ||
| 110 | $attr_name = $2; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $accessor = $self->get_accessor; | ||
| 114 | my $object = $self->get_object; | ||
| 115 | |||
| 116 | return &$accessor($object, $attr_name) if $accessor; | ||
| 117 | |||
| 118 | my $method = $self->get_get_prefix.$attr_name; | ||
| 119 | $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 120 | |||
| 121 | return if not $object; | ||
| 122 | return &$accessor($object) if $accessor; | ||
| 123 | return $object->$method(); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub set_attr { | ||
| 127 | my $self = shift; | ||
| 128 | my ($attr_name, $attr_value, $no_widget_update) = @_; | ||
| 129 | |||
| 130 | if ( $attr_name =~ /^([^.]+)\.(.*)$/ ) { | ||
| 131 | $self = $self->get_context->get_proxy($1); | ||
| 132 | $attr_name = $2; | ||
| 133 | } | ||
| 134 | |||
| 135 | $self->object_changed($attr_name); | ||
| 136 | |||
| 137 | my $accessor = $self->get_accessor; | ||
| 138 | my $object = $self->get_object; | ||
| 139 | my $name = $self->get_name; | ||
| 140 | |||
| 141 | my $rc; | ||
| 142 | if ( $accessor ) { | ||
| 143 | $rc = &$accessor($object, $attr_name, $attr_value); | ||
| 144 | } else { | ||
| 145 | my $set_prefix = $self->get_set_prefix; | ||
| 146 | my $method = $set_prefix.$attr_name; | ||
| 147 | $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 148 | $rc = $accessor ? | ||
| 149 | &$accessor($object, $attr_value) : | ||
| 150 | $object->$method($attr_value); | ||
| 151 | } | ||
| 152 | |||
| 153 | return $rc if $no_widget_update; | ||
| 154 | |||
| 155 | $self->get_context | ||
| 156 | ->update_object_attr_widgets($name, $attr_name, $object); | ||
| 157 | |||
| 158 | my $child_object_name = $self->get_attr_aggregate_href->{$attr_name}; | ||
| 159 | |||
| 160 | $self->get_context->set_object($child_object_name, $attr_value) | ||
| 161 | if $child_object_name; | ||
| 162 | |||
| 163 | return $rc; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub set_attrs { | ||
| 167 | my $self = shift; | ||
| 168 | my ($attrs_href, $no_widget_update) = @_; | ||
| 169 | |||
| 170 | my $set_prefix = $self->get_set_prefix; | ||
| 171 | my $object = $self->get_object; | ||
| 172 | my $name = $self->get_name; | ||
| 173 | my $context = $self->get_context; | ||
| 174 | my $accessors = $self->get_attr_accessors_href; | ||
| 175 | |||
| 176 | my ($method, $attr_name, $attr_value, $accessor, $child_object_name); | ||
| 177 | |||
| 178 | $accessor = $self->get_accessor; | ||
| 179 | |||
| 180 | while ( ($attr_name, $attr_value) = each %{$attrs_href} ) { | ||
| 181 | $self->object_changed($attr_name); | ||
| 182 | if ( $accessor ) { | ||
| 183 | &$accessor($object, $attr_name, $attr_value); | ||
| 184 | } else { | ||
| 185 | $method = $set_prefix.$attr_name; | ||
| 186 | $accessor = $accessors->{$method}; | ||
| 187 | $accessor ? | ||
| 188 | &$accessor($object, $attr_value) : | ||
| 189 | $object->$method($attr_value); | ||
| 190 | } | ||
| 191 | $accessor = undef; | ||
| 192 | next if $no_widget_update; | ||
| 193 | $context->update_object_attr_widgets( | ||
| 194 | $name, $attr_name, $object | ||
| 195 | ); | ||
| 196 | $child_object_name = $self->get_attr_aggregate_href->{$attr_name}; | ||
| 197 | $context->set_object($child_object_name, $attr_value) | ||
| 198 | if $child_object_name; | ||
| 199 | } | ||
| 200 | |||
| 201 | 1; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub get_attr_presets { | ||
| 205 | my $self = shift; | ||
| 206 | my ($attr_name) = @_; | ||
| 207 | |||
| 208 | my $method = $self->get_get_prefix.$attr_name."_presets"; | ||
| 209 | my $object = $self->get_object; | ||
| 210 | my $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 211 | |||
| 212 | return &$accessor($object) if ref $accessor eq 'CODE'; | ||
| 213 | return $accessor if $accessor; | ||
| 214 | return $object->$method(); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub get_attr_rows { | ||
| 218 | my $self = shift; | ||
| 219 | my ($attr_name) = @_; | ||
| 220 | |||
| 221 | my $method = $self->get_get_prefix.$attr_name."_rows"; | ||
| 222 | my $object = $self->get_object; | ||
| 223 | my $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 224 | |||
| 225 | return &$accessor($object) if ref $accessor eq 'CODE'; | ||
| 226 | return $accessor if $accessor; | ||
| 227 | return $object->$method(); | ||
| 228 | } | ||
| 229 | |||
| 230 | sub get_attr_list { | ||
| 231 | my $self = shift; | ||
| 232 | my ($attr_name, $widget_name) = @_; | ||
| 233 | |||
| 234 | my $method = $self->get_get_prefix.$attr_name."_list"; | ||
| 235 | my $object = $self->get_object; | ||
| 236 | my $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 237 | |||
| 238 | return &$accessor($object, $widget_name) if ref $accessor eq 'CODE'; | ||
| 239 | return $accessor if $accessor; | ||
| 240 | return $object->$method($widget_name); | ||
| 241 | } | ||
| 242 | |||
| 243 | sub get_attr_presets_static { | ||
| 244 | my $self = shift; | ||
| 245 | my ($attr_name) = @_; | ||
| 246 | |||
| 247 | my $method = $self->get_get_prefix.$attr_name."_presets_static"; | ||
| 248 | my $object = $self->get_object; | ||
| 249 | my $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 250 | |||
| 251 | return &$accessor($object) if ref $accessor eq 'CODE'; | ||
| 252 | return $accessor if $accessor; | ||
| 253 | return 1 if not $object->can($method); | ||
| 254 | return $object->$method(); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub get_attr_rows_static { | ||
| 258 | my $self = shift; | ||
| 259 | my ($attr_name) = @_; | ||
| 260 | |||
| 261 | my $method = $self->get_get_prefix.$attr_name."_rows_static"; | ||
| 262 | my $object = $self->get_object; | ||
| 263 | my $accessor = $self->get_attr_accessors_href->{$method}; | ||
| 264 | |||
| 265 | return &$accessor($object) if ref $accessor eq 'CODE'; | ||
| 266 | return $accessor if $accessor; | ||
| 267 | return 1 if not $object->can($method); | ||
| 268 | return $object->$method(); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub get_attr_list_static { | ||
| 272 | my $self = shift; | ||
| 273 | my ($attr_name) = @_; | ||
| 274 | |||
| 275 | my $method = $self->get_get_prefix.$attr_name."_list_static"; | ||
| 276 | my $object = $self->get_object; | ||
| 277 | |||
| 278 | return 1 if not $object->can($method); | ||
| 279 | return $object->$method(); | ||
| 280 | } | ||
| 281 | |||
| 282 | sub get_attr_activity { | ||
| 283 | my $self = shift; | ||
| 284 | my ($attr_name) = @_; | ||
| 285 | |||
| 286 | $Gtk2::Ex::FormFactory::DEBUG && | ||
| 287 | print " proxy->get_attr_activity($attr_name)\n"; | ||
| 288 | |||
| 289 | my $object = $self->get_object; | ||
| 290 | return 0 if not defined $object; | ||
| 291 | |||
| 292 | my $attr_activity_href = $self->get_attr_activity_href; | ||
| 293 | |||
| 294 | return 1 if not $attr_activity_href or | ||
| 295 | not exists $attr_activity_href->{$attr_name}; | ||
| 296 | |||
| 297 | my $attr_activity = $attr_activity_href->{$attr_name}; | ||
| 298 | |||
| 299 | return $attr_activity if not ref $attr_activity eq 'CODE'; | ||
| 300 | return &$attr_activity($object); | ||
| 301 | } | ||
| 302 | |||
| 303 | sub object_changed { | ||
| 304 | my $self = shift; | ||
| 305 | my ($attr_name) = @_; | ||
| 306 | |||
| 307 | my $changes_attr_filter = $self->get_changes_attr_filter; | ||
| 308 | |||
| 309 | if ( !$changes_attr_filter || | ||
| 310 | $attr_name !~ $changes_attr_filter ) { | ||
| 311 | $self->set_object_changed(1); | ||
| 312 | my $aggregated_by = $self->get_aggregated_by; | ||
| 313 | if ( $aggregated_by ) { | ||
| 314 | my $context = $self->get_context; | ||
| 315 | my ($object, $attr) = $context->norm_object_attr($aggregated_by); | ||
| 316 | $context->get_proxy($object)->object_changed($attr); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | 1; | ||
| 321 | } | ||
| 322 | |||
| 323 | 1; | ||
| 324 | |||
| 325 | __END__ |