| 1 | # | ||
| 2 | # This file is part of Config-Model-Itself | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2007-2026 by Dominique Dumont. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||
| 9 | # | ||
| 10 | package Config::Model::Itself::TkEditUI 2.032; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings ; | ||
| 14 | use Carp ; | ||
| 15 | use 5.10.0; | ||
| 16 | |||
| 17 | use Config::Model::TkUI 1.381; | ||
| 18 | |||
| 19 | use base qw/Config::Model::TkUI/; | ||
| 20 | |||
| 21 | Construct Tk::Widget 'ConfigModelEditUI'; | ||
| 22 | |||
| 23 | sub ClassInit { | ||
| 24 | my ($class, $mw) = @_; | ||
| 25 | # ClassInit is often used to define bindings and/or other | ||
| 26 | # resources shared by all instances, e.g., images. | ||
| 27 | |||
| 28 | |||
| 29 | # cw->Advertise(name=>$widget); | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub Populate { | ||
| 34 | my ($cw, $args) = @_; | ||
| 35 | |||
| 36 | my $cm_lib_dir = (delete $args->{-cm_lib_dir})."/models" ; | ||
| 37 | my $model_name = delete $args->{-model_name} || ''; | ||
| 38 | my $root_dir = delete $args->{-root_dir} ; # used to test the edited model | ||
| 39 | |||
| 40 | $args->{'-title'} ||= "cme meta edit $model_name" ; | ||
| 41 | |||
| 42 | $cw->SUPER::Populate($args) ; | ||
| 43 | |||
| 44 | my $model_menu = $cw->{my_menu}->cascade( | ||
| 45 | -label => 'Model', | ||
| 46 | -menuitems => $cw->build_menu() , | ||
| 47 | ) ; | ||
| 48 | |||
| 49 | $cw->{cm_lib_dir} = $cm_lib_dir ; | ||
| 50 | $cw->{model_name} = $model_name ; | ||
| 51 | $cw->{root_dir} = $root_dir ; | ||
| 52 | |||
| 53 | $cw->show_message("Add a name in Class to create your model") unless $model_name; | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub build_menu { | ||
| 58 | my $cw = shift ; | ||
| 59 | |||
| 60 | # search for config_dir override | ||
| 61 | my $root = $cw->{instance}->config_root; | ||
| 62 | my $items = []; | ||
| 63 | my %app; | ||
| 64 | |||
| 65 | my $found_app = 0; | ||
| 66 | foreach my $app ($root->fetch_element('application')->fetch_all_indexes) { | ||
| 67 | push @$items, [ command => "test $app", '-command' => sub{ $cw->test_model($app) }]; | ||
| 68 | $app{$app} = $root->grab_value("application:$app config_dir"); | ||
| 69 | } | ||
| 70 | |||
| 71 | push @$items, [ qw/command test -command/, sub{ $cw->test_model }] unless @$items ; | ||
| 72 | |||
| 73 | return $items; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub test_model { | ||
| 77 | my $cw = shift ; | ||
| 78 | my $app = shift; | ||
| 79 | |||
| 80 | if ( $cw->{instance}->needs_save ) { | ||
| 81 | my $answer = $cw->Dialog( | ||
| 82 | -title => "save model before test", | ||
| 83 | -text => "Save model ?", | ||
| 84 | -buttons => [ qw/yes no cancel/, 'show changes' ], | ||
| 85 | -default_button => 'yes', | ||
| 86 | )->Show; | ||
| 87 | |||
| 88 | if ( $answer eq 'yes' ) { | ||
| 89 | $cw->save( sub {$cw->_launch_test($app);}); | ||
| 90 | } | ||
| 91 | elsif ( $answer eq 'no' ) { | ||
| 92 | $cw->_launch_test($app); | ||
| 93 | } | ||
| 94 | elsif ( $answer =~ /show/ ) { | ||
| 95 | $cw->show_changes( sub { $cw->test_model } ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $cw->_launch_test($app); | ||
| 100 | } | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _launch_test { | ||
| 105 | my $cw = shift ; | ||
| 106 | my $app = shift; | ||
| 107 | |||
| 108 | my $testw = $cw -> {test_widget} ; | ||
| 109 | $testw->destroy if defined $testw and Tk::Exists($testw); | ||
| 110 | |||
| 111 | # need to read test model from where it was written... | ||
| 112 | my $model = Config::Model -> new(model_dir => $cw->{cm_lib_dir}) ; | ||
| 113 | |||
| 114 | # keep a reference on this object, otherwise it will vanish at the end of this block. | ||
| 115 | $cw->{test_model} = $model ; | ||
| 116 | |||
| 117 | my %args = ( root_dir => $cw->{root_dir} ); | ||
| 118 | my $root = $cw->{instance}->config_root; | ||
| 119 | |||
| 120 | $args{root_class_name} = $app ? $root->grab_value("application:$app model") : $cw->{model_name}; | ||
| 121 | $args{instance_name} = $app ? "test $app" : $cw->{model_name}; | ||
| 122 | |||
| 123 | if ($app) { | ||
| 124 | $args{application} = $app; | ||
| 125 | $args{config_dir} = $root->grab_value("application:$app config_dir"); | ||
| 126 | } | ||
| 127 | |||
| 128 | my $instance_to_test = $model->instance ( %args ) ; | ||
| 129 | |||
| 130 | $cw -> {test_widget} = $cw->ConfigModelUI (-instance => $instance_to_test, -quit => 'soft') ; | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | 1; |