HG/StatusTree.pm untested

filter: Statementstmt 0.0% 0 / 182 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 243 CC 8 SCAR 17.6
CC
8
Cov
0%
createBranch
20.0
_on_tree_item_menu
12.0
getWxNode
6.0
CRAP 5.8
1 package Padre::Plugin::HG::StatusTree;
2 =pod
3  
4 =head1 NAME
5  
6 Padre::Plugin::HG::StatusTree
7 Displays the status of the current hg project in a tree format
8  
9 =head1 SYNOPSIS
10  
11 my $object = Padre::Plugin::HG::StatusTree->new($self,$project_root);
12
13 =head1 DESCRIPTION
14  
15 This module diplays a tree in the left panel of the ide that shows
16 the mecurial status of each file.
17 Right clicking on a file will give you options to perform actions. Right clicking the
18 project root will give you project wide options.
19  
20 =head1 METHODS
21  
22 =cut
23  
24 use strict;
25 use warnings;
26  
27 our $VERSION = '0.01';
28  
29 use Padre::Wx;
30 use Padre::Util qw/_T/;
31 use Wx qw/WXK_UP WXK_DOWN wxTR_HAS_BUTTONS wxTR_HIDE_ROOT /;
32 use base 'Wx::Panel';
33 use Padre::Plugin::My;
34 use File::Spec;
35 use File::Basename;
36 use Data::Dumper;
37 my $HG;
38 my %dirTree;
39 my $project_name;
40 my %WxTree;
41 my $ThisTree;
42  
43 my $image = qq(
44 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0
45 RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAQaSURBVHjaAEEAvv8BAEsbAAAE
46 +AAACPEAAAr8BwDxAD4A7w9lFgUiPhYKGQ4BAgEB6+7v8+rq78j+/ACbAAEDvAAFA/cACAUA
47 AP//AAKIEW4A+wcZBo6/XbqyWpHRFokMnroeDJKCkgyMDAwMLz+8Zthz5gjDog2bGM6duL2O
48 gYWtlIGH7R7Dv/8MAAHE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j8MjP/YGF7v5mc4s+Dj/a9/v8cwsDM/AQggZgZeNWBmYn3/+9uPHU/O
63 /FT6do9bnYedn0FQiBsoDIzav4wMP9+zMTw9ycxwds4vhtOr3+76+vNrLAM38w2G/4wMAAHE
64 yGBiyADOct+B+PdfbqCgJwcHR5aIFLcRjxAwuQEj4tu7Pz9fP/ty5fu3H9OBWXgj0ObPDEDv
65 gAwACDAAvIZw301II8cAAAAASUVORK5CYII= );
66  
67  
68 #setup the image list
69 my $images = Wx::ImageList->new( 16, 16 );
70 my $file_types = {
71 M => $images->Add(
72 Wx::ArtProvider::GetBitmap( 'wxART_TIP', 'wxART_OTHER_C', [ 16, 16 ] ),
73 ),
74 dir => $images->Add(
75 Wx::ArtProvider::GetBitmap( 'wxART_FOLDER', 'wxART_OTHER_C', [ 16, 16 ] ),
76 ),
77 C => $images->Add(
78 Wx::ArtProvider::GetBitmap( 'wxART_TICK_MARK', 'wxART_CMN_DIALOG', [ 16, 16 ] ),
79 ),
80 '?' => $images->Add(
81 Wx::ArtProvider::GetBitmap( 'wxART_MISSING_IMAGE', 'wxART_OTHER_C', [ 16, 16 ] ),
82 ),
83 };
84
85  
86  
87  
88 my @root = qw (root root);
89  
90 =pod
91  
92 =head2 new
93  
94 Creates and displays the status tree
95 $tree = Padre::Plugin::HG::StatusTree->new($self, $project_root);
96  
97 =cut
98  
99 sub new
100 {
101 my ($class, $hg, $root) = @_;
102 $project_name = $root;
103  
104  
105 my $self = $class->SUPER::new( Padre::Current->main->left );
106 my $box = Wx::BoxSizer->new(Wx::wxVERTICAL);
107 my $treectrl = Wx::TreeCtrl->new( $self, -1 );
108 $treectrl->AssignImageList($images);
109
110 $HG = $hg; #assign this the hg object.
111
112 $self->drawTree($treectrl);
113 Wx::Event::EVT_TREE_ITEM_MENU(
114 $treectrl, $treectrl,
115 \&_on_tree_item_menu,
116 );
117
118 #Double Click
119 Wx::Event::EVT_TREE_ITEM_ACTIVATED(
120 $treectrl, $treectrl,
121 \&_on_tree_item_activated
122 );
123
124 $box->Add( $treectrl, 1, Wx::wxGROW );
125  
126 $self->SetSizer($box);
127 Padre::Current->main->left->show($self);
128 $self->{treeCtrl} = $treectrl;
129 $ThisTree = $self;
130 return $self;
131
132 }
133  
134  
135  
136  
137  
138 =pod
139  
140 =head2 gettext_label
141  
142 Works out what to name the tab for the project.
143  
144 =cut
145  
146 sub gettext_label {
147 my ($self) = @_;
148 my @dirs = File::Spec->splitdir( $project_name);
149 my $name = File::Spec->catdir(('..',$dirs[-2], $dirs[-1]));
150 chomp $name;
151 return "(HG) ".$name;
152 }
153  
154 =pod
155  
156 =head2 getWxNode
157  
158 Add the nodes to the tree.
159 getWxNode($treectrl,[name,parent,type],path);
160 =cut
161  
162 sub getWxNode
163 {
164 my ($treectrl,$node,$path) = @_;
165
166 my $name = $node->[0];
167 my $parent = $node->[1];
168 my $type = $node->[2];
169 chomp $path;
170 chomp $name;
171 if (exists($WxTree{$path}))
172 {
173 return $WxTree{$path};
174 # $WxTree{$path} = $treectrl->AppendItem( $WxTree{$parent}, $name);
175
176 }else
177 {
178 $WxTree{$path} = $treectrl->AppendItem(
179 getWxNode($treectrl, $dirTree{$parent}, $parent),
180 $name,
181 $file_types->{$type},
182 -1,
183 Wx::TreeItemData->new(
184 { name => $name,
185 path => $path,
186 type => $type,
187 }));
188 }
189
190
191 }
192  
193 =pod
194  
195 =head2 parseHgStatus
196  
197 $self->parseHgstatus(@hgstatus);
198 parses the output of HGstatus and calls create branch for each item.
199
200 =cut
201  
202 sub parseHgStatus
203 {
204 my (@hgstatus) = @_;
205
206 foreach my $line (@hgstatus)
207 {
208 my ($filestatus, $path) = split(/\s/,$line);
209 chomp $path;
210
211 my @dir = File::Spec->splitdir( $path );
212  
213 createBranch(\@dir,$filestatus);
214
215 }
216
217
218
219 }
220  
221 =pod
222  
223 =head2 createBranch
224  
225 $self->parseHgstatus(@hgstatus);
226 parses the output of HGstatus and calls create branch for each item.
227
228 =cut
229  
230  
231 sub createBranch
232 {
233
234 my ($dirRef, $status) = @_;
235 my @dir = @$dirRef;
236 my $count = 0;
237 my %parentChild;
238 foreach my $item (@dir)
239 {
240 my $type;
241 my $parent = '';
242 if ($count == 0 )
243 {
244 $parent = $dirTree{root}->[0];
245 }
246 else
247 {
248 $parent= File::Spec->catfile(@dir[0..$count -1 ]);
249 }
250
251 my $node = File::Spec->catfile(@dir[0..$count]);
252  
253 if (!exists($dirTree{$node}))
254 {
255  
256 if ($count < (scalar(@dir) -1))
257 {
258 $type = 'dir';
259 }
260 else
261 {
262 $type = $status;
263 }
264 $dirTree{$node} = [$item, $parent, $type];
265 }
266
267 $count ++;
268 }
269 #return %dirTree;
270 }
271  
272 =pod
273  
274 =head2 drawTree
275  
276 $self->drawTree($treectrl);
277 creates the Tree from the output of the vcs status
278
279 =cut
280  
281 sub drawTree {
282 my ($self,$treectrl) = @_;
283
284 %WxTree = ();
285 $WxTree{root} = $treectrl->AddRoot(
286 $project_name,
287 $file_types->{dir},
288 -1,
289 Wx::TreeItemData->new(
290 { name => 'root',
291 path => '',
292 type => 'root',
293 }) );
294
295
296 %dirTree = ();
297 $dirTree{root} = ['root', 'root', 'dir'];
298 chdir ($project_name);
299
300 my @hgStatus = `hg status --all`;
301 chomp (@hgStatus);
302 parseHgStatus(@hgStatus);
303 foreach my $file (keys(%dirTree))
304 {
305 my $path = File::Spec->catdir($project_name,$file);
306 my $dir = File::Basename::dirname($path);
307
308 getWxNode($treectrl, $dirTree{$file}, $file);
309 # print " getWxNode($treectrl, ". $dirTree{$file}." , $file)\n";
310 }
311 #expand the root as a work around for windows bug. where
312 #root does not expand.
313 $treectrl->Expand($WxTree{root});
314  
315 }
316 =pod
317  
318 =head2 _on_tree_item_activated
319  
320 Performs actions when the users double clicks on a tree node
321 at the moment opens a file when it is double clicked.
322
323
324 =cut
325
326 sub _on_tree_item_activated
327 {
328
329 my ( $self, $event,$me ) = @_;
330 my $node = $event->GetItem;
331
332 my $node_data = $self->GetPlData($node);
333 my $selected_path = $node_data->{path};
334 my $selected_file = $node_data->{name};
335 my $selected_type = $node_data->{type};
336 my $full_path = File::Spec->catdir(($project_name,$selected_path));
337
338 if ($selected_type ne 'dir' and $selected_type ne 'root')
339 {
340 open_file($full_path);
341 }
342
343  
344 }
345  
346 =pod
347  
348 =head2 _on_tree_item_menu
349  
350 Called when a user right clicks a node in the tree
351 Shows different options depending if a file/dir or root
352 is selected.
353
354
355 =cut
356 sub _on_tree_item_menu {
357 my ( $self, $event,$me ) = @_;
358 my $node = $event->GetItem;
359 my $node_data = $self->GetPlData($node);
360 my $menu = Wx::Menu->new;
361 # Default action - same when the item is activated
362 my $selected_path = $node_data->{path};
363 my $selected_file = $node_data->{name};
364 my $selected_type = $node_data->{type};
365 my $full_path = File::Spec->catdir(($project_name,$selected_path));
366
367 my $parent_dir = File::Basename::dirname($full_path);
368 #Commit
369 if ($selected_type eq 'root')
370 {
371
372 my $default = $menu->Append(
373 -1,
374 Wx::gettext( 'Refresh' ));
375
376 Wx::Event::EVT_MENU(
377 $self, $default,
378 sub { $ThisTree->refresh($self) }
379 );
380 my $pull = $menu->Append(
381 -1,
382 Wx::gettext( 'Pull & Update' ));
383
384 Wx::Event::EVT_MENU(
385 $self, $pull,
386 sub { $HG->pull_update_project($selected_file, $project_name) }
387 );
388 my $push = $menu->Append(
389 -1,
390 Wx::gettext( 'Push' ));
391
392 Wx::Event::EVT_MENU(
393 $self, $push,
394 sub { $HG->push_project($selected_file, $project_name) }
395 );
396 my $commit = $menu->Append(
397 -1,
398 Wx::gettext( 'Commit' ));
399
400 Wx::Event::EVT_MENU(
401 $self, $commit,
402 sub { require Padre::Plugin::HG::ProjectCommit;
403 Padre::Plugin::HG::ProjectCommit->showList($HG);
404 $ThisTree->refresh($self) }
405 );
406 }
407
408 elsif ($selected_type ne 'dir' and $selected_type ne 'root')
409 {
410 my $default = $menu->Append(
411 -1,
412 Wx::gettext( 'Commit' ));
413
414 Wx::Event::EVT_MENU(
415 $self, $default,
416 sub { $HG->vcs_commit($selected_file, $parent_dir ); $ThisTree->refresh($self)}
417 );
418
419 #Add
420 my $add = $menu->Append(
421 -1,
422 Wx::gettext( 'Add' ));
423
424 Wx::Event::EVT_MENU(
425 $self, $add,
426 sub { $HG->vcs_add($selected_file, $parent_dir);$ThisTree->refresh($self)}
427 );
428
429 #diff (just diffs current file to Tip)
430 my $diff = $menu->Append(
431 -1,
432 Wx::gettext( 'Diff to Tip' ));
433
434 Wx::Event::EVT_MENU(
435 $self, $diff,
436 sub { $HG->show_diff($selected_file, $parent_dir);}
437 );
438 #diff to a revision
439 my $diff2 = $menu->Append(
440 -1,
441 Wx::gettext( 'Diff to Revision' ));
442
443 Wx::Event::EVT_MENU(
444 $self, $diff,
445 sub { $HG->show_diff_revision($selected_file, $parent_dir);}
446 );
447
448 #open
449 my $open = $menu->Append(
450 -1,
451 Wx::gettext( 'Open' ));
452
453 Wx::Event::EVT_MENU(
454 $self, $open,
455 sub{open_file($full_path)}
456 );
457
458
459 }
460
461
462 my $x = $event->GetPoint->x;
463 my $y = $event->GetPoint->y;
464 $self->PopupMenu( $menu, $x, $y );
465
466  
467 }
468  
469  
470 =pod
471  
472 =head2 view_close
473  
474 Called by Padre when the X is clicked on the tree view in the left .
475
476 =cut
477 sub view_close(){
478 #$HG->close_statusTree();
479 return 1;
480 }
481  
482 sub view_stop(){
483  
484 }
485  
486  
487 =pod
488  
489 =head2 view_panel
490  
491 Called by Padre when the X is clicked on the tree view in the left .
492
493 =cut
494 sub view_panel(){
495 return 'left';
496 }
497  
498 =pod
499  
500 =head2 open_file
501  
502 open_file($path)
503 opens the file in the editor.
504
505
506 =cut
507 sub open_file
508 {
509 my ($path) = @_;
510 my $main = Padre->ide->wx->main;
511 $main->setup_editors($path);
512
513 }
514  
515 =pod
516  
517 =head2 refresh
518  
519 $self->refresh();
520 refreshes the tree control by deleteing all items and
521 readding them.
522
523 =cut
524  
525 sub refresh {
526 my ($self, $treeCtrl) = @_;
527 $treeCtrl->DeleteAllItems;
528 $self->drawTree($treeCtrl);
529 return ();
530 }
531 1;