Moose/Assistant.pm untested

filter: Statementstmt 0.0% 0 / 326 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 517 CC 28 SCAR 26.1
CC
28
Cov
0%
on_tree_selection_change
42.0
show_inspector
42.0
update_tree
30.0
CRAP 13.5
1 package Padre::Plugin::Moose::Assistant;
2  
3 use 5.008;
4 use Moose;
5 use Padre::Wx::Role::Dialog ();
6 use Padre::Plugin::Moose::FBP::Assistant ();
7  
8 our $VERSION = '0.21';
9 our @ISA = qw{
10 Padre::Wx::Role::Dialog
11 Padre::Plugin::Moose::FBP::Assistant
12 };
13  
14 sub new {
15 my $class = shift;
16 my $plugin = shift;
17  
18  
19 my $self = $class->SUPER::new( $plugin->main );
20  
21 # Store the plugin object for future usage
22 $self->{plugin} = $plugin;
23  
24 # Center & title
25 $self->CenterOnParent;
26 $self->SetTitle(
27 sprintf( Wx::gettext('Moose Assistant %s - Written for fun by Ahmad M. Zawawi (azawawi)'), $VERSION ) );
28  
29 # Restore defaults
30 $self->restore_defaults;
31  
32 # TODO Bug Alias to fix the wxFormBuilder bug regarding this one
33 $self->{inspector}->SetRowLabelSize(0);
34  
35 # Hide the inspector as needed
36 $self->show_inspector(undef);
37  
38 # Setup preview editor
39 my $preview = $self->{preview};
40 require Padre::Document;
41 $preview->{Document} = Padre::Document->new( mimetype => 'application/x-perl', );
42 $preview->{Document}->set_editor($preview);
43 $preview->SetLexer('application/x-perl');
44  
45 # Syntax highlight Moose keywords
46 require Padre::Plugin::Moose::Util;
47 Padre::Plugin::Moose::Util::add_moose_keywords_highlighting(
48 $plugin->{config}->{type}, $preview->{Document},
49 $preview
50 );
51  
52 $preview->Show(1);
53  
54 $self->show_code_in_preview(1);
55  
56 return $self;
57 }
58  
59 # This is called to start and show the dialog
60 sub run {
61 my $self = shift;
62  
63 # Apply the current theme to the preview editor
64 my $style = $self->main->config->editor_style;
65 my $theme = Padre::Wx::Theme->find($style)->clone;
66 $theme->apply( $self->{preview} );
67  
68 $self->ShowModal;
69 }
70  
71 # Set up the events
72 sub on_grid_cell_change {
73 my $self = shift;
74  
75 my $element = $self->{current_element} or return;
76  
77 if ( $element->does('Padre::Plugin::Moose::Role::CanHandleInspector') ) {
78 $element->read_from_inspector( $self->{inspector} );
79 }
80  
81 $self->show_code_in_preview(0);
82  
83 return;
84 }
85  
86 sub on_tree_selection_change {
87 my $self = shift;
88 my $event = shift;
89 my $tree = $self->{tree};
90 my $item = $event->GetItem or return;
91 my $element = $tree->GetPlData($item) or return;
92  
93 my $is_parent = $element->does('Padre::Plugin::Moose::Role::HasClassMembers');
94 my $is_program = $element->isa('Padre::Plugin::Moose::Program');
95  
96 # Show/Hide the inspector as needed
97 $self->show_inspector( $is_program ? undef : $element );
98  
99 # Display help about the current element
100 $self->{help}->SetValue( $element->provide_help );
101  
102 $self->{current_element} = $element;
103  
104 # Find parent element
105 if ( $element->does('Padre::Plugin::Moose::Role::ClassMember') ) {
106 $self->{current_parent} = $tree->GetPlData( $tree->GetItemParent($item) );
107 } else {
108 $self->{current_parent} = $element if $is_parent;
109 }
110  
111 $self->Layout;
112  
113 # TODO improve the crude workaround to positioning
114 unless ($is_program) {
115 my $preview = $self->{preview};
116 my $line_num = 0;
117 for my $line ( split /\n/, $preview->GetText ) {
118 my $name = quotemeta $element->name;
119 if ( $line =~ /$name/ ) {
120 my $position = $preview->PositionFromLine($line_num);
121 $preview->SetCurrentPos($position);
122 $preview->SetAnchor($position);
123 $preview->ScrollToLine($line_num);
124 last;
125 }
126 $line_num++;
127 }
128 }
129  
130 return;
131 }
132  
133 sub show_code_in_preview {
134 my $self = shift;
135 my $should_select_item = shift;
136  
137 eval {
138  
139 # Generate code
140 my $config = $self->{plugin}->{config};
141 my $code = $self->{program}->generate_code(
142 { type => $config->{type},
143 comments => $config->{comments},
144 sample_code => $config->{sample_code},
145 namespace_autoclean => $config->{namespace_autoclean},
146 }
147 );
148  
149 # And show it in preview editor
150 my $preview = $self->{preview};
151 $preview->SetReadOnly(0);
152 $preview->SetText($code);
153 $preview->SetReadOnly(1);
154  
155 # Update tree
156 $self->update_tree($should_select_item);
157 };
158 if ($@) {
159 $self->error( sprintf( Wx::gettext('Error:%s'), $@ ) );
160 }
161  
162 return;
163 }
164  
165 sub update_tree {
166 my $self = shift;
167 my $should_select_item = shift;
168 my $tree = $self->{tree};
169 my $lock = $self->lock_update($tree);
170  
171 $tree->DeleteAllItems;
172  
173 my $selected_item;
174  
175 my $program = $self->{program};
176 my $program_node = $tree->AddRoot(
177 Wx::gettext('Program'),
178 -1,
179 -1,
180 Wx::TreeItemData->new($program)
181 );
182  
183 if ( $program eq $self->{current_element} ) {
184 $selected_item = $program_node;
185 }
186  
187 for my $class ( @{ $program->roles }, @{ $program->classes } ) {
188 my $class_node = $tree->AppendItem(
189 $program_node,
190 $class->name,
191 -1, -1,
192 Wx::TreeItemData->new($class)
193 );
194 for my $class_item ( @{ $class->attributes }, @{ $class->subtypes }, @{ $class->methods } ) {
195 my $class_item_node = $tree->AppendItem(
196 $class_node,
197 $class_item->name,
198 -1, -1,
199 Wx::TreeItemData->new($class_item)
200 );
201 if ( $class_item == $self->{current_element} ) {
202 $selected_item = $class_item_node;
203 }
204 }
205  
206 if ( $class == $self->{current_element} ) {
207 $selected_item = $class_node;
208 }
209  
210 $tree->Expand($class_node);
211 }
212  
213 $tree->ExpandAll;
214  
215 # Select the tree node outside this event to
216 # prevent deep recurision
217 if ( $should_select_item and defined $selected_item ) {
218 Wx::Event::EVT_IDLE(
219 $self,
220 sub {
221 $tree->SelectItem($selected_item);
222 Wx::Event::EVT_IDLE( $self, undef );
223 }
224 );
225 }
226  
227 return;
228 }
229  
230 sub show_inspector {
231 my $self = shift;
232 my $element = shift;
233 my $inspector = $self->{inspector};
234 my $lock = $self->lock_update($inspector);
235  
236 $inspector->DeleteRows( 0, $inspector->GetNumberRows );
237 unless ( defined $element ) {
238 return;
239 }
240  
241 my $type = blessed($element);
242 unless ( defined $type and $type =~ /(Class|Role|Attribute|Subtype|Method|Constructor|Destructor)$/ ) {
243 $self->error("type: $element is not Class, Role, Attribute, Subtype or Method\n");
244 return;
245 }
246  
247 my $data = $element->get_grid_data;
248 $inspector->InsertRows( 0, scalar @$data );
249 $inspector->SetGridCursor( 0, 1 );
250 foreach my $i ( 0 .. $#$data ) {
251 my $row = $data->[$i];
252 $inspector->SetCellValue( $i, 0, $row->{name} );
253 $inspector->SetReadOnly( $i, 0 );
254 if ( defined $row->{is_bool} ) {
255 $inspector->SetCellEditor( $i, 1, Wx::GridCellBoolEditor->new );
256 $inspector->SetCellValue( $i, 1, 1 );
257 } elsif ( defined $row->{choices} ) {
258 $inspector->SetCellEditor( $i, 1, Wx::GridCellChoiceEditor->new( $row->{choices}, 1 ) );
259 }
260 }
261  
262 if ( $element->does('Padre::Plugin::Moose::Role::CanHandleInspector') ) {
263 $element->write_to_inspector($inspector);
264 }
265  
266 return;
267 }
268  
269 sub on_add_class_button {
270 my $self = shift;
271  
272 # Add a new class object to program
273 require Padre::Plugin::Moose::Class;
274 my $class = Padre::Plugin::Moose::Class->new;
275 $class->name( "Class" . $self->{class_count}++ );
276 $class->immutable(1);
277 push @{ $self->{program}->classes }, $class;
278  
279 $self->{current_element} = $class;
280 $self->show_inspector($class);
281 $self->show_code_in_preview(1);
282  
283 return;
284 }
285  
286 sub on_add_role_button {
287 my $self = shift;
288  
289 # Add a new role object to program
290 require Padre::Plugin::Moose::Role;
291 my $role = Padre::Plugin::Moose::Role->new;
292 $role->name( "Role" . $self->{role_count}++ );
293 push @{ $self->{program}->roles }, $role;
294  
295 $self->{current_element} = $role;
296 $self->show_inspector($role);
297 $self->show_code_in_preview(1);
298  
299 return;
300 }
301  
302 sub on_add_attribute_button {
303 my $self = shift;
304  
305 # Only allowed within a class/role element
306 unless ( defined $self->{current_element}
307 && defined $self->{current_parent}
308 && $self->{current_parent}->does('Padre::Plugin::Moose::Role::HasClassMembers') )
309 {
310 $self->error( Wx::gettext('You can only add an attribute to a class or role') );
311 $self->{palette}->SetSelection(0);
312 return;
313 }
314  
315 # Add a new attribute object to class
316 require Padre::Plugin::Moose::Attribute;
317 my $attribute = Padre::Plugin::Moose::Attribute->new;
318 $attribute->name( 'attribute' . $self->{attribute_count}++ );
319 push @{ $self->{current_parent}->attributes }, $attribute;
320  
321 $self->{current_element} = $attribute;
322 $self->show_inspector($attribute);
323 $self->show_code_in_preview(1);
324  
325 return;
326 }
327  
328 sub on_add_subtype_button {
329 my $self = shift;
330  
331 # Only allowed within a class/role element
332 unless ( defined $self->{current_element}
333 && defined $self->{current_parent}
334 && $self->{current_parent}->does('Padre::Plugin::Moose::Role::HasClassMembers') )
335 {
336 $self->error( Wx::gettext('You can only add a subtype to a class or role') );
337 $self->{palette}->SetSelection(0);
338 return;
339 }
340  
341 # Add a new subtype object to class
342 require Padre::Plugin::Moose::Subtype;
343 my $subtype = Padre::Plugin::Moose::Subtype->new;
344 $subtype->name( 'Subtype' . $self->{subtype_count}++ );
345 push @{ $self->{current_parent}->subtypes }, $subtype;
346  
347 $self->{current_element} = $subtype;
348 $self->show_inspector($subtype);
349 $self->show_code_in_preview(1);
350  
351 return;
352 }
353  
354 sub on_add_method_button {
355 my $self = shift;
356  
357 # Only allowed within a class/role element
358 unless ( defined $self->{current_element}
359 && defined $self->{current_parent}
360 && $self->{current_parent}->does('Padre::Plugin::Moose::Role::HasClassMembers') )
361 {
362 $self->error( Wx::gettext('You can only add a method to a class or role') );
363 $self->{palette}->SetSelection(0);
364 return;
365 }
366  
367 # Add a new method object to class
368 require Padre::Plugin::Moose::Method;
369 my $method = Padre::Plugin::Moose::Method->new;
370 $method->name( 'method_' . $self->{method_count}++ );
371 push @{ $self->{current_parent}->methods }, $method;
372  
373 $self->{current_element} = $method;
374 $self->show_inspector($method);
375 $self->show_code_in_preview(1);
376  
377 return;
378 }
379  
380 sub on_add_constructor_button {
381 my $self = shift;
382  
383 # Only allowed within a class/role element
384 unless ( defined $self->{current_element}
385 && defined $self->{current_parent}
386 && $self->{current_parent}->does('Padre::Plugin::Moose::Role::HasClassMembers') )
387 {
388 $self->error( Wx::gettext('You can only add a constructor to a class or role') );
389 $self->{palette}->SetSelection(0);
390 return;
391 }
392  
393 # Add a new constructor object to class/role
394 require Padre::Plugin::Moose::Constructor;
395 my $constructor = Padre::Plugin::Moose::Constructor->new;
396 $constructor->name('BUILD');
397 push @{ $self->{current_parent}->methods }, $constructor;
398  
399 $self->{current_element} = $constructor;
400 $self->show_inspector($constructor);
401 $self->show_code_in_preview(1);
402  
403 return;
404 }
405  
406 sub on_add_destructor_button {
407 my $self = shift;
408  
409 # Only allowed within a class/role element
410 unless ( defined $self->{current_element}
411 && defined $self->{current_parent}
412 && $self->{current_parent}->does('Padre::Plugin::Moose::Role::HasClassMembers') )
413 {
414 $self->error( Wx::gettext('You can only add a destructor to a class or role') );
415 $self->{palette}->SetSelection(0);
416 return;
417 }
418  
419 # Add a new destructor object to class/role
420 require Padre::Plugin::Moose::Destructor;
421 my $destructor = Padre::Plugin::Moose::Destructor->new;
422 $destructor->name('DEMOLISH');
423 push @{ $self->{current_parent}->methods }, $destructor;
424  
425 $self->{current_element} = $destructor;
426 $self->show_inspector($destructor);
427 $self->show_code_in_preview(1);
428  
429 return;
430 }
431  
432 sub on_reset_button_clicked {
433 my $self = shift;
434  
435 if ( $self->yes_no( Wx::gettext('Do you really want to reset?') ) ) {
436 $self->restore_defaults;
437 $self->show_code_in_preview(1);
438 }
439  
440 return;
441 }
442  
443 sub on_generate_code_button_clicked {
444 my $self = shift;
445  
446 Wx::Event::EVT_IDLE(
447 $self,
448 sub {
449 $self->main->new_document_from_string(
450 $self->{preview}->GetText => 'application/x-perl',
451 );
452 Wx::Event::EVT_IDLE( $self, undef );
453 }
454 );
455  
456 $self->EndModal(Wx::ID_OK);
457  
458 return;
459 }
460  
461 sub restore_defaults {
462 my $self = shift;
463  
464 $self->{class_count} = 1;
465 $self->{role_count} = 1;
466 $self->{attribute_count} = 1;
467 $self->{subtype_count} = 1;
468 $self->{method_count} = 1;
469  
470 require Padre::Plugin::Moose::Program;
471 $self->{program} = Padre::Plugin::Moose::Program->new;
472 $self->{current_element} = $self->{program};
473 $self->{current_parent} = $self->{program};
474  
475 return;
476 }
477  
478 # Called when a item context menu is requested.
479 sub on_tree_item_menu {
480 my $self = shift;
481 my $event = shift;
482 my $item = $event->GetItem;
483 my $tree = $self->{tree};
484 my $element = $tree->GetPlData($item) or return;
485 return if $element->isa('Padre::Plugin::Moose::Program');
486  
487 # Generate the context menu for this element
488 my $menu = Wx::Menu->new;
489  
490 Wx::Event::EVT_MENU(
491 $self,
492 $menu->Append( -1, Wx::gettext('Delete') ),
493 sub {
494 $self->delete_element($element);
495 }
496 );
497  
498 # Pops up the context menu
499 $tree->PopupMenu(
500 $menu,
501 $event->GetPoint->x,
502 $event->GetPoint->y,
503 );
504  
505 return;
506 }
507  
508 sub on_tree_key_up {
509 my $self = shift;
510 my $event = shift;
511 my $mod = $event->GetModifiers || 0;
512  
513 # see Padre::Wx::Main::key_up
514 $mod = $mod & ( Wx::MOD_ALT + Wx::MOD_CMD + Wx::MOD_SHIFT );
515  
516 my $tree = $self->{tree};
517 my $item_id = $tree->GetSelection;
518 my $element = $tree->GetPlData($item_id) or return;
519  
520 if ( $event->GetKeyCode == Wx::K_DELETE ) {
521 $self->delete_element($element);
522 }
523  
524 $event->Skip;
525  
526 return;
527 }
528  
529 sub delete_element {
530 my $self = shift;
531 my $element = shift or return;
532  
533 if ( $self->yes_no( sprintf( Wx::gettext('Do you want to delete %s?'), $element->name ) ) ) {
534  
535 #TODO do the actual item deletion
536 }
537  
538 return;
539 }
540  
541 sub on_preferences_button_clicked {
542 my $self = shift;
543  
544 # Create a new preferences dialog
545 require Padre::Plugin::Moose::Preferences;
546 my $prefs = Padre::Plugin::Moose::Preferences->new($self);
547  
548 # Update plugin variables from plugin's configuration hash
549 my $plugin = $self->{plugin};
550 my $config = $plugin->{config};
551 $prefs->{generated_code_combo}->SetValue( $config->{type} );
552 $prefs->{comments_checkbox}->SetValue( $config->{comments} );
553 $prefs->{sample_code_checkbox}->SetValue( $config->{sample_code} );
554 $prefs->{namespace_autoclean_checkbox}->SetValue( $config->{namespace_autoclean} );
555  
556 # Preferences: go modal!
557 if ( $prefs->ShowModal == Wx::wxID_OK ) {
558  
559 # Update configuration when the user hits the OK button
560 my $type = $prefs->{generated_code_combo}->GetValue;
561 $config->{type} = $type;
562 $config->{comments} = $prefs->{comments_checkbox}->IsChecked;
563 $config->{sample_code} = $prefs->{sample_code_checkbox}->IsChecked;
564 $config->{namespace_autoclean} = $prefs->{namespace_autoclean_checkbox}->IsChecked;
565 $plugin->config_write($config);
566  
567 # Update tree and preview editor
568 $self->show_code_in_preview(1);
569  
570 # Add moose et all keywords highlight to preview editor
571 require Padre::Plugin::Moose::Util;
572 Padre::Plugin::Moose::Util::add_moose_keywords_highlighting( $self->{preview}->{Document}, $type );
573  
574 # Add moose et all keywords highlight to current editor
575 my $doc = $self->current->document or return;
576 if ( $doc->isa('Padre::Plugin::Moose::Document') ) {
577 Padre::Plugin::Moose::Util::add_moose_keywords_highlighting( $doc, $type );
578 }
579  
580 }
581  
582 return;
583 }
584  
585 1;
586  
587 # Copyright 2008-2012 The Padre development team as listed in Padre.pm.
588 # LICENSE
589 # This program is free software; you can redistribute it and/or
590 # modify it under the same terms as Perl 5 itself.