Source

Padre::Plugin::HG / lib / Padre / Plugin / HG / StatusTree.pm

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package Padre::Plugin::HG::StatusTree;
=pod

=head1  NAME

Padre::Plugin::HG::StatusTree
Displays the status of the current hg project in a tree format

=head1 SYNOPSIS

 my $object = Padre::Plugin::HG::StatusTree->new($self,$project_root);
 
=head1 DESCRIPTION

This module diplays a tree in the left panel of the ide that shows
the mecurial status of each file.  
Right clicking on a file will give you options to perform actions.  Right clicking the
project root will give you project wide options. 

=head1 METHODS

=cut

use strict;
use warnings;

our $VERSION = '0.01';

use Padre::Wx;
use Padre::Util qw/_T/;
use Wx qw/WXK_UP WXK_DOWN wxTR_HAS_BUTTONS  wxTR_HIDE_ROOT  /;
use base 'Wx::Panel';
use Padre::Plugin::My;
use File::Spec;
use File::Basename;
use Data::Dumper;
my $HG;
my %dirTree;
my $project_name;
my %WxTree;
my $ThisTree;

my $image = qq(
    iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0
    RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAQaSURBVHjaAEEAvv8BAEsbAAAE
    +AAACPEAAAr8BwDxAD4A7w9lFgUiPhYKGQ4BAgEB6+7v8+rq78j+/ACbAAEDvAAFA/cACAUA
    AP//AAKIEW4A+wcZBo6/XbqyWpHRFokMnroeDJKCkgyMDAwMLz+8Zthz5gjDog2bGM6duL2O
    gYWtlIGH7R7Dv/8MAAHEADbAV0iOMZLleNBC7//H3xz5//3/1//o4CdQ7Oz7k/9jJmT9ZzHQ
    u8qgY6jMYGTEABAAQQC+/wMAJQ4IAAD3kDc6LUpiS2QAEgUUAPj6+QD+/v4A/wD/APr++gDx
    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bqCgJwcHR5aIFLcRjxAwuQEj4tu7Pz9fP/ty5fu3H9OBWXgj0ObPDEDv
    gAwACDAAvIZw301II8cAAAAASUVORK5CYII= );


  #setup the image list 
  my $images = Wx::ImageList->new( 16, 16 );
        my $file_types = {
                M => $images->Add(
	                        Wx::ArtProvider::GetBitmap( 'wxART_TIP', 'wxART_OTHER_C', [ 16, 16 ] ),
                ),
                dir => $images->Add(
                        Wx::ArtProvider::GetBitmap( 'wxART_FOLDER', 'wxART_OTHER_C', [ 16, 16 ] ),
                ),
                C => $images->Add(
                        Wx::ArtProvider::GetBitmap( 'wxART_TICK_MARK', 'wxART_CMN_DIALOG', [ 16, 16 ] ),
                ),
                '?' => $images->Add(
                        Wx::ArtProvider::GetBitmap( 'wxART_MISSING_IMAGE', 'wxART_OTHER_C', [ 16, 16 ] ),
                ),
          };
  



my @root = qw (root root);

=pod

=head2 new

 Creates and displays the status tree
 $tree = Padre::Plugin::HG::StatusTree->new($self, $project_root);

=cut

sub new
{
  my ($class, $hg, $root) = @_;
  $project_name = $root;


  my $self       = $class->SUPER::new( Padre::Current->main->left );
  my $box        = Wx::BoxSizer->new(Wx::wxVERTICAL);
  my $treectrl = Wx::TreeCtrl->new( $self, -1 );
  $treectrl->AssignImageList($images);
  
  $HG = $hg; #assign this the hg object. 
 
  $self->drawTree($treectrl);
  Wx::Event::EVT_TREE_ITEM_MENU(
         $treectrl, $treectrl,
         \&_on_tree_item_menu,
  );
  
  #Double Click
  Wx::Event::EVT_TREE_ITEM_ACTIVATED(
            $treectrl, $treectrl,
             \&_on_tree_item_activated
  );
  
  $box->Add( $treectrl, 1, Wx::wxGROW );

  $self->SetSizer($box);  
  Padre::Current->main->left->show($self);
  $self->{treeCtrl} = $treectrl;
  $ThisTree = $self;
  return $self;
   
}





=pod

=head2 gettext_label

        Works out what to name the tab for the project.

=cut

sub gettext_label {
        my ($self) = @_;
        my @dirs = File::Spec->splitdir( $project_name);
        my $name = File::Spec->catdir(('..',$dirs[-2], $dirs[-1]));
         chomp $name;
        return "(HG) ".$name;
}

=pod

=head2 getWxNode

        Add the nodes to the tree. 
        getWxNode($treectrl,[name,parent,type],path);
=cut

sub getWxNode
{
    my ($treectrl,$node,$path) = @_;
    
    my $name = $node->[0];
    my $parent = $node->[1];
    my $type = $node->[2];
    chomp $path;
    chomp $name;
    if (exists($WxTree{$path}))
    {
      return $WxTree{$path};
      # $WxTree{$path} = $treectrl->AppendItem( $WxTree{$parent}, $name);
      
    }else
    {
        $WxTree{$path} = $treectrl->AppendItem( 
                  getWxNode($treectrl, $dirTree{$parent}, $parent), 
                            $name, 
                            $file_types->{$type},
                             -1,
                             Wx::TreeItemData->new(
                                            {   name => $name,
                                                path  => $path,
	                                        type => $type,
	                                }));
    }
  
  
}

=pod

=head2 parseHgStatus

 $self->parseHgstatus(@hgstatus);
 parses the output of HGstatus and calls create branch for each item.
 
=cut

sub parseHgStatus
{
      my (@hgstatus) = @_;
      
      foreach my $line (@hgstatus)
      {
        my ($filestatus, $path) = split(/\s/,$line);
        chomp $path;
        
        my @dir = File::Spec->splitdir( $path );

        createBranch(\@dir,$filestatus);
               
      }
  
  
  
}

=pod

=head2 createBranch

 $self->parseHgstatus(@hgstatus);
 parses the output of HGstatus and calls create branch for each item.
 
=cut


sub createBranch
{
  
  my ($dirRef, $status) = @_;
  my @dir = @$dirRef;
  my $count = 0;
  my %parentChild;
  foreach my $item (@dir)
  {
    my $type;
    my $parent = '';
    if ($count == 0 ) 
    { 
      $parent = $dirTree{root}->[0]; 
    }
    else
    {
      $parent= File::Spec->catfile(@dir[0..$count -1 ]);
    }
    
    my $node = File::Spec->catfile(@dir[0..$count]);

    if (!exists($dirTree{$node}))
    {

	     if ($count < (scalar(@dir) -1))
	     {
	       $type = 'dir';
	     }
	     else
	     {
	       $type = $status;
	     }
	     $dirTree{$node} = [$item, $parent, $type];
    }
    
     $count ++;
  }
  #return %dirTree;
}

=pod

=head2 drawTree

 $self->drawTree($treectrl);
 creates the Tree from the output of the vcs status
 
=cut

sub drawTree {
  my ($self,$treectrl) = @_;
  
  %WxTree = ();
  $WxTree{root} = $treectrl->AddRoot( 
                        $project_name, 
                        $file_types->{dir},
                        -1,
                        Wx::TreeItemData->new(
                                            {   name => 'root',
                                                path  => '',
	                                        type => 'root',
	                                })  ); 
    
 
  %dirTree = ();
  $dirTree{root} = ['root', 'root', 'dir'];
  chdir ($project_name);
  
  my @hgStatus = `hg status --all`;
  chomp (@hgStatus);
  parseHgStatus(@hgStatus);
  foreach my $file  (keys(%dirTree))
  {
       my $path = File::Spec->catdir($project_name,$file);
       my $dir = File::Basename::dirname($path);
      
       getWxNode($treectrl, $dirTree{$file}, $file); 
     #  print " getWxNode($treectrl, ". $dirTree{$file}." , $file)\n";    
  }
  #expand the root as a work around for windows bug. where
  #root does not expand. 
  $treectrl->Expand($WxTree{root});

}
=pod

=head2 _on_tree_item_activated

        Performs actions when the users double clicks on a tree node
        at the moment opens a file when it is double clicked. 
        
 
=cut
 
sub _on_tree_item_activated
{
	
	my ( $self, $event,$me ) = @_;
	my $node      = $event->GetItem;
            
        my $node_data = $self->GetPlData($node);
	my $selected_path  = $node_data->{path};
        my $selected_file = $node_data->{name};
        my $selected_type = $node_data->{type};
        my $full_path = File::Spec->catdir(($project_name,$selected_path));
        
        if ($selected_type ne 'dir' and $selected_type ne 'root')
        {
                open_file($full_path);
        }
        

}

=pod

=head2  _on_tree_item_menu

        Called when a user right clicks a node in the tree
        Shows different options depending if a file/dir or root
        is selected. 
        
 
=cut
sub _on_tree_item_menu {
        my ( $self, $event,$me ) = @_;
        my $node      = $event->GetItem;
        my $node_data = $self->GetPlData($node);
        my $menu          = Wx::Menu->new;
        # Default action - same when the item is activated
        my $selected_path  = $node_data->{path};
        my $selected_file = $node_data->{name};
        my $selected_type = $node_data->{type};
        my $full_path = File::Spec->catdir(($project_name,$selected_path));
        
        my $parent_dir = File::Basename::dirname($full_path);
        #Commit
        if ($selected_type eq 'root')
        {
              
                my $default = $menu->Append(
                        -1,
                       Wx::gettext( 'Refresh' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $default,
                       sub { $ThisTree->refresh($self) }
               );   
               my $pull = $menu->Append(
                        -1,
                       Wx::gettext( 'Pull & Update' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $pull,
                        sub {  $HG->pull_update_project($selected_file, $project_name) }
               );    
               my $push = $menu->Append(
                        -1,
                       Wx::gettext( 'Push' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $push,
                       sub {  $HG->push_project($selected_file, $project_name) }
               ); 
              my $commit = $menu->Append(
                        -1,
                       Wx::gettext( 'Commit' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $commit,
                       sub {  require Padre::Plugin::HG::ProjectCommit;
                               Padre::Plugin::HG::ProjectCommit->showList($HG);
                               $ThisTree->refresh($self) }
               );     
        }
        
        elsif ($selected_type ne 'dir' and $selected_type ne 'root')
        {
                my $default = $menu->Append(
                        -1,
                       Wx::gettext( 'Commit' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $default,
                       sub { $HG->vcs_commit($selected_file, $parent_dir ); $ThisTree->refresh($self)}
               );
               
               #Add
               my $add = $menu->Append(
                         -1,
                       Wx::gettext( 'Add' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $add,
                       sub { $HG->vcs_add($selected_file, $parent_dir);$ThisTree->refresh($self)}
               );   
               
               #diff (just diffs current file to Tip) 
               my $diff = $menu->Append(
                         -1,
                       Wx::gettext( 'Diff to Tip' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $diff,
                       sub { $HG->show_diff($selected_file, $parent_dir);}
               );  
               #diff to a revision  
               my $diff2 = $menu->Append(
                         -1,
                       Wx::gettext( 'Diff to Revision' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $diff,
                       sub { $HG->show_diff_revision($selected_file, $parent_dir);}
               );  
               
               #open 
               my $open = $menu->Append(
                         -1,
                       Wx::gettext( 'Open' ));
                    
                Wx::Event::EVT_MENU(
                       $self, $open,
                       sub{open_file($full_path)}
               ); 
               
                 
       }
       
       
        my $x = $event->GetPoint->x;
        my $y = $event->GetPoint->y;
        $self->PopupMenu( $menu, $x, $y );
        

}


=pod

=head2  view_close

    Called by Padre when the X is clicked on the tree view in the left . 
 
=cut
sub view_close(){
   #$HG->close_statusTree();
   return 1;
}

sub view_stop(){

}


=pod

=head2  view_panel

    Called by Padre when the X is clicked on the tree view in the left . 
 
=cut
sub view_panel(){
  return 'left';
}

=pod

=head2  open_file

        open_file($path)
        opens the file in the editor. 
        
 
=cut
sub open_file
{ 
     	 my ($path) = @_;
     	        my $main = Padre->ide->wx->main;
	        $main->setup_editors($path);
	        
}

=pod

=head2  refresh

        $self->refresh();
        refreshes the tree control by deleteing all items and 
        readding them. 
 
=cut

sub refresh {
    my ($self, $treeCtrl) = @_;
        $treeCtrl->DeleteAllItems;
        $self->drawTree($treeCtrl); 
    return ();
}
1;