AssistantFrames

 view release on metacpan or  search on metacpan

AssistantFrames.pm  view on Meta::CPAN

package Mac::AssistantFrames;

$VERSION = "0.1";
sub Version { $VERSION; }

require 5.004;
use strict;
use Mac::AFDialogs;

sub new {
   my $class = shift;
   my $self = {};
   $self->{'answers'} = [];
   $self->{'frames'} = [];
   $self->{'sequence'} = [];
   $self->{'running'} = 1;
   bless $self, $class;
   return $self;
}

sub backupOne {
   my $self = shift;
   my $dlgs = $self->{'frames'};
   my $answers = $self->{'answers'};
   while ($self->getLastFrameRaw() =~ /^\[.*\]$/) {
      pop @$dlgs;
      pop @$answers;
   }
   if (@$dlgs > 0) {
      pop @$dlgs;
      pop @$answers;
   }
}

sub continueOne {
   my $self = shift;
   my $name = shift;
   my $value = shift;
   push @{$self->{'frames'}}, $name;
   push @{$self->{'answers'}}, $value;
}

sub abort {
   my $self = shift;
   my $text = shift;
   $self->{'running'} = 0;
   $self->{'frames'} = [];
   $self->{'answers'} = [];
   if ($text) {
      MacPerl::Answer($text);
   }
}

sub standardAction {
   my $self = shift;
   my $name = shift;
   my $dlg = shift;
   if ($dlg->{'cont'}) {
      $self->continueOne($name, $dlg->{'value'});
      if ($name eq "<finish>") {
         $self->{'running'} = 0;
      }
   } elsif ($dlg->{'back'}) {
      $self->backupOne();
      if ($name eq "<start>") {
         $self->{'running'} = 0;
      }
   } elsif ($dlg->{'abort'}) {
      $self->abort();
   }
}

sub startFrame {
   my $self = shift;
   my $title = shift;
   my $description = shift;
   my $dlg = Mac::AFDialogs->new();
   $dlg->simpleDialog($title, $description);
   $dlg->{'value'} = "";
   $self->standardAction("<start>", $dlg);
}

sub finishFrame {
   my $self = shift;
   my $title = shift;
   my $description = shift;
   my $dlg = Mac::AFDialogs->new();
   $dlg->simpleDialog($title, $description);
   $dlg->{'value'} = "";
   $self->standardAction("<finish>", $dlg);
}

sub constantFrame {
   my $self = shift;
   my $name = shift;
   my $title = shift;
   my $description = shift;
   my $value = shift;
   my $dlg = Mac::AFDialogs->new();
   $dlg->simpleDialog($title, $description);
   $dlg->{'value'} = $value;
   $self->standardAction($name, $dlg);
}

sub yesnoFrame {
   my $self = shift;
   my $name = shift;
   my $title = shift;
   my $description = shift;
   my $dlg = Mac::AFDialogs->new();
   $dlg->yesnoDialog($title, $description);
   $self->standardAction($name, $dlg);
}

sub radioFrame {
   my $self = shift;
   my $name = shift;
   my $title = shift;
   my $description = shift;
   my $values = shift;
   my $labels = shift;
   my $offset = shift;
   my $dlg = Mac::AFDialogs->new();
   $dlg->radioDialog($title, $description, $values, $labels, $offset);

AssistantFrames.pm  view on Meta::CPAN

     my $last = $assist->getLastFrame();
     if ($last eq "<empty>") {
        $assist->startFrame("title", "description");
     } elsif ($last eq "<start>") {
        $assist->constantFrame("dlg1", "title", 
                               "description", "value");
     } elsif ($last eq "dlg1") {
        $assist->singleSelectionFrame("dlg2", "title", 
                                      "description", 
                                      ["v1", "v2", "v3"]);
     } elsif ($last eq "dlg2") {
        $assist->constantHiddenFrame("dlg3", "value");
     } elsif ($last eq "dlg3") {
        $assist->finishFrame("title", "description");
     }
  }
  if ($assist->getLastFrame eq "<finish>") {
     print $assist->joinedAnswersr(":"), "\n";
  } else {
     print "aborted\n";
  }

=head1 DESCRIPTION

This Module implements simple assistant style frames for MacPerl. You can use it
if you need to ask the user some questions in sequence to accomplish a task. This
could be done with one dialogbox, too. But the assistant approach is often easier
to understand, especially if the user doesn't know much about the subject at hand.
This is the case most often with configuration of new software packages, where
the user has to be given much more detailed descriptions of what to do as would
fit on a simple dialog.

The AssistantFrame allows backward navigation in case the user erred on some of
his answers. It allows complete backwind of the frames (abortion of the process)
and it has a uniform userinterface that resembles a lot the Mac-style assistants.

=head1 CLASS METHODS

=over 4

=item new()

This constructor creates a new frame sequence object.

=item Version()

This method returns the version of the module.

=back

=head1 METHODS

=over 4

=item running()

This checks if the assistant object is in running state. If the "<start>" frame
is aborted, this is reset to 0. If the "<finish>" frame is accepted, this is
reset to 0.

=item backupOne()

This method backs up one frame. This is internally used, you seldom have to
invoke it yourself.

=item continueOne(name, value)

This method adds a successfull frame to the sequence. This is used internally,
so you shouldn't need to call this yourself.

=item abort([text])

This method stops the assistant. If "text" is given, it creates an alert dialog.
If it is not given, the assistant is just canceled. This can be used to react on
errors in a assistant processing.

=item standardAction(name, dlg)

This method is internally used to process the events from the dialog. Just ignore
it.

=item getLastFrame()

This method returns the last frame the user completed. This is needed to allow
backing up and sequencing correctly. If the sequence of frames is empty, it
returns "<empty>".

=item getLastFrameRaw()

This method is used to get the last frame, as is getLastFrame. Only difference:
getLastFrameRaw delivers the frame-name in "raw" format - names of hidden constant
elements is bracketed in []. This function is used internally to distinguish
frames from constants.

=item getLastAnswer()

This returns the last answer given. It is usefull to make frames dependend on
older frames.

=item getAnswers()

This returns a reference to the array of the answer-strings.

=item joinedAnswers(sep)

This returns a string created by joining all answers together, separated by sep.

=item getNumAnswers()

This returns the number of accumulated answers.

=item addToSequence(closure)

This adds the closure to the sequence array of the assistant. The sequence array
allows a simple processing of a static sequence of frames.

=item processSequence()

This processes the sequence array and returns a reference to the array of answers.

=back



( run in 2.603 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )