Changeset 12399


Ignore:
Timestamp:
08/30/10 02:39:48 (18 months ago)
Author:
adamk
Message:

Change Padre::Cache to use methods, for cleanliness.
Trying to fix second-order issues in the directory tree.

Location:
trunk/Padre/lib/Padre
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Padre/lib/Padre/Cache.pm

    r12251 r12399  
    1414 
    1515sub stash { 
     16    my $class = shift; 
    1617    my $owner = shift; 
    1718    my $key   = shift; 
     
    2930 
    3031    $DATA{$key}->{$owner} 
    31         or $DATA{$key}->{$owner} = {}; 
     32    or $DATA{$key}->{$owner} = {}; 
    3233} 
    3334 
    3435sub release { 
    35     delete $DATA{ $_[0] }; 
     36    delete $DATA{ $_[1] }; 
    3637} 
    3738 
  • trunk/Padre/lib/Padre/Document.pm

    r12283 r12399  
    343343sub DESTROY { 
    344344    if ( defined $_[0]->{filename} ) { 
    345         Padre::Cache::release( $_[0]->{filename} ); 
     345        Padre::Cache->release( $_[0]->{filename} ); 
    346346    } 
    347347} 
  • trunk/Padre/lib/Padre/Project.pm

    r12251 r12399  
    66use strict; 
    77use warnings; 
    8 use File::Spec (); 
     8use File::Spec   (); 
     9use Padre::Cache (); 
    910 
    1011our $VERSION = '0.69'; 
     
    317318    if ( defined $_[0]->{root} ) { 
    318319        require Padre::Cache; 
    319         Padre::Cache::release( $_[0]->{root} ); 
     320        Padre::Cache->release( $_[0]->{root} ); 
    320321    } 
    321322} 
  • trunk/Padre/lib/Padre/Wx/Directory.pm

    r12378 r12399  
    5656    # Where is the current root directory of the tree 
    5757    $self->{root} = ''; 
    58  
    59     # The list of all files to build into the tree 
    60     $self->{files} = []; 
    61  
    62     # The directories in the tree that should be expanded 
    63     $self->{expand} = {}; 
    6458 
    6559    # Create the search control 
     
    194188            # Leaving search mode 
    195189            $self->{searching} = 0; 
    196             $search->ShowCancelButton(0); 
    197190            $self->task_reset; 
    198191            $self->clear; 
     192            $self->refill; 
    199193            $self->browse; 
    200194        } else { 
     
    208202        } else { 
    209203            # Entering search mode 
    210             $self->{expand}    = $self->tree->expanded; 
     204            $self->{expand}    = $self->tree->GetExpandedPlData; 
    211205            $self->{searching} = 1; 
    212206            $search->ShowCancelButton(1); 
     
    260254} 
    261255 
     256# Refill the tree from storage 
     257sub refill { 
     258    my $self   = shift; 
     259    my $tree   = $self->{tree}; 
     260    my $files  = delete $self->{files}; 
     261    my $expand = delete $self->{expand} or return; 
     262    my @stack  = $tree->GetRootItem; 
     263    foreach my $i ( 0 .. $#$files ) { 
     264        my $path  = $files->[$i]; 
     265        my $image = $path->type ? 'folder' : 'package'; 
     266        unless ( $tree->GetPlData($stack[-1])->GetData->is_parent($path) ) { 
     267            if ( $files->[$i - 1]->is_parent($path) ) { 
     268                # Descending 
     269                push @stack, $tree->GetLastChild($stack[-1]); 
     270            } else { 
     271                # Ascending 
     272                while ( @stack ) { 
     273                    pop @stack; 
     274                    if ( $tree->GetPlData($stack[-1])->GetData->is_parent($path) ) { 
     275                        last; 
     276                    } 
     277                } 
     278            }                    
     279        } 
     280        $tree->AppendItem( 
     281            $stack[-1],                   # Parent node 
     282            $path->name,                  # Label 
     283            $tree->{images}->{$image},    # Icon 
     284            -1,                           # Wx Identifier 
     285            Wx::TreeItemData->new($path), # Embedded data 
     286        ); 
     287    } 
     288} 
     289         
    262290 
    263291 
     
    285313    ); 
    286314 
    287     # Before we change anything, store the expansion state 
    288     unless ( $self->searching ) { 
    289         $self->{expand} = $self->tree->expanded; 
    290     } 
    291  
    292315    # Switch project states if needed 
    293316    unless ( $self->{root} eq $root ) { 
     
    297320        # if we potentially need it again later. 
    298321        if ( $ide->project_exists( $self->{root} ) ) { 
    299             my $stash = Padre::Cache::stash( 
     322            my $stash = Padre::Cache->stash( 
    300323                __PACKAGE__ => $ide->project( $self->{root} ), 
    301324            ); 
    302             %$stash = ( 
    303                 root   => $self->{root}, 
    304                 files  => $self->{files}, 
    305                 expand => $self->{expand}, 
    306             ); 
    307         } 
    308  
    309         # Flush the now-unusable state 
     325            if ( $self->{searching} ) { 
     326                # Save the stored browse state 
     327                %$stash = ( 
     328                    root   => $self->{root}, 
     329                    files  => $self->{files}, 
     330                    expand => $self->{expand}, 
     331                ); 
     332            } else { 
     333                # Capture the browse state fresh. 
     334                %$stash = ( 
     335                    root   => $self->{root}, 
     336                    files  => $self->tree->GetChildrenPlData, 
     337                    expand => $self->tree->GetExpandedPlData, 
     338                ); 
     339            } 
     340        } 
     341 
     342        # Flush the now-unusable local state 
     343        $self->clear; 
    310344        $self->{root}   = $root; 
    311         $self->{files}  = []; 
    312         $self->{expand} = {}; 
     345        $self->{files}  = undef; 
     346        $self->{expand} = undef; 
    313347 
    314348        # Do we have an (out of date) cached state we can use? 
    315         # If so, display it immediately and update it later. 
    316         if ($project) { 
    317             my $stash = Padre::Cache::stash( 
     349        # If so, display it immediately and update it later on. 
     350        if ( $project ) { 
     351            my $stash = Padre::Cache->stash( 
    318352                __PACKAGE__ => $project, 
    319353            ); 
    320354            if ( $stash->{root} ) { 
    321  
    322355                # We have a cached state 
    323                 $self->{files}  = $stash->{files}; 
    324356                $self->{expand} = $stash->{expand}; 
    325357            } 
     
    327359 
    328360        # Flush the search box and start the rerender 
    329         $self->clear; 
    330         $self->browse; 
     361        if ( $self->{expand} ) { 
     362            $self->browse( @{$self->{expand}} ); 
     363        } else { 
     364            $self->browse; 
     365        } 
    331366    } 
    332367 
     
    334369} 
    335370 
    336 sub refresh_finish { 
     371 
     372 
     373 
     374 
     375###################################################################### 
     376# Browse Methods 
     377 
     378sub browse { 
    337379    TRACE( $_[0] ) if DEBUG; 
    338380    my $self = shift; 
    339     my $task = shift; 
    340     $self->{files} = $task->{model}; 
    341     $self->refresh_render; 
    342 } 
    343  
    344 # This is a primitive first attempt to get familiar with the tree API 
    345 sub refresh_render { 
    346     TRACE( $_[0] ) if DEBUG; 
    347     my $self   = shift; 
    348     my $tree   = $self->tree; 
    349     my $root   = $tree->GetRootItem; 
    350     my $expand = $self->{expand}; 
    351  
    352     # Flush the old tree contents 
    353     # TO DO: This is inefficient, upgrade to something that does the 
    354     # equivalent of a treewise diff application, modifying the tree 
    355     # to get the result we want instead of rebuilding it entirely. 
    356     my $lock = $self->main->lock('UPDATE'); 
    357     $tree->DeleteChildren($root); 
    358  
    359     # Fill the new tree 
    360     my @stack = (); 
    361     my @files = @{$self->{files}}; 
    362     while (@files) { 
    363         my $path  = shift @files; 
    364         my $image = $path->type ? 'folder' : 'package'; 
    365         while (@stack) { 
    366  
    367             # If we are not the child of the deepest element in 
    368             # the stack, move up a level and try again 
    369             last if $tree->GetPlData( $stack[-1] )->is_parent($path); 
    370  
    371             # We have finished filling the directory. 
    372             # Now it (maybe) has children, we can expand it. 
    373             my $complete = pop @stack; 
    374             if ( $expand->{ $tree->GetPlData($complete)->unix } ) { 
    375                 $tree->Expand($complete); 
    376             } 
    377         } 
    378  
    379         # If there is anything left on the stack it is our parent 
    380         my $parent = $stack[-1] || $root; 
    381  
    382         # Add the next item to that parent 
    383         my $item = $tree->AppendItem( 
    384             $parent,                      # Parent node 
    385             $path->name,                  # Label 
    386             $tree->{images}->{$image},    # Icon 
    387             -1,                           # Wx Identifier 
    388             Wx::TreeItemData->new($path), # Embedded data 
    389         ); 
    390  
    391         # If it is a folder, it goes onto the stack 
    392         if ( $path->type == 1 ) { 
    393             push @stack, $item; 
    394         } 
    395     } 
    396  
    397     # Apply the same Expand logic above to any remaining stack elements 
    398     while (@stack) { 
    399         my $complete = pop @stack; 
    400         if ( $expand->{ $tree->GetPlData($complete)->unix || 0 } ) { 
    401             $tree->Expand($complete); 
    402         } 
    403     } 
    404  
    405     return 1; 
    406 } 
    407  
    408  
    409  
    410  
    411  
    412 ###################################################################### 
    413 # Browse Methods 
    414  
    415 sub browse { 
    416     TRACE( $_[0] ) if DEBUG; 
    417     my $self = shift; 
    418     my $path = shift || Padre::Wx::Directory::Path->directory; 
    419381    return if $self->searching; 
    420382 
     
    425387        on_message => 'browse_message', 
    426388        on_finish  => 'browse_finish', 
    427         list       => [ $path ], 
     389        list       => [ @_ ? @_ : Padre::Wx::Directory::Path->directory ], 
    428390    ); 
    429391 
     
    475437    my $task = shift; 
    476438} 
     439 
    477440 
    478441 
  • trunk/Padre/lib/Padre/Wx/TreeCtrl.pm

    r12378 r12399  
    3535} 
    3636 
     37# Fetch a list of all Perl data elements for all nodes 
     38# in depth-first top-to-bottom order. 
     39sub GetChildrenPlData { 
     40    my $self  = shift; 
     41    my @queue = $self->GetRootItem; 
     42    my @data  = (); 
     43    while ( @queue ) { 
     44        my $item = shift @queue; 
     45        push @data, $self->GetPlData($item)->GetData; 
     46 
     47        # Continue if we have no child nodes 
     48        next unless $self->GetChildrenCount( $item, 0 ); 
     49 
     50        # Processing children last to first and unshifting onto the 
     51        # queue, lets us achieve depth-first top-down within the need 
     52        # for intermediate storage or grepping. 
     53        my ( $child, $cookie ) = $self->GetLastChild($item); 
     54        while ( $cookie ) { 
     55            unshift @queue, $child; 
     56            ( $child, $cookie ) = $self->GetPreviousChild( $item, $cookie ); 
     57        } 
     58    } 
     59 
     60    return \@data; 
     61} 
     62 
     63# Fetch a list of the Perl data elements for expanded nodes 
     64# in depth-first top to bottom order. 
     65sub GetExpandedPlData { 
     66    my $self  = shift; 
     67    my @queue = $self->GetRootItem; 
     68    my @data  = (); 
     69    while ( @queue ) { 
     70        my $item = shift @queue; 
     71        push @data, $self->GetPlData($item)->GetData; 
     72 
     73        # Processing children last to first and unshifting onto the 
     74        # queue, lets us achieve depth-first top-down within the need 
     75        # for intermediate storage or grepping. 
     76        my ( $child, $cookie ) = $self->GetLastChild($item); 
     77        while ( $cookie ) { 
     78            if ( $self->IsExpanded ) { 
     79                unshift @queue, $child; 
     80            } 
     81            ( $child, $cookie ) = $self->GetPreviousChild( $item, $cookie ); 
     82        } 
     83    } 
     84 
     85    return \@data; 
     86} 
     87 
    37881; 
Note: See TracChangeset for help on using the changeset viewer.