Re: CamelBones question
Re: CamelBones question
- Subject: Re: CamelBones question
- From: Pierre Vaudrey <email@hidden>
- Date: Tue, 11 May 2004 19:01:54 +0200
Le mardi, 11 mai 2004, ` 00:30 Europe/Paris, Sherm Pendley a icrit :
>
With that in mind, here's a translation of a constructor with the
>
above code in it
Ok it works fine, after I changed Action and DataSource methods with a
perl code close to ObjC one :
# Comments version 0.1, Copyright 2004 __MyCompanyName__.
use strict;
use warnings;
package CommentsWindowController;
use CamelBones qw(:All);
our @ISA = qw(Exporter);
outlet Window isa=>'NSWindow';
outlet Editfield;
outlet Table;
sub new {
my $self = bless({}, shift());
# Disable automatic conversion of NSString objects to Perl scalars
$CamelBones::ReturnStringsAsObjects = 1;
my $str =
NSString->stringWithString('~/Documents/perlTest/comment1.plist');
# Re-enable automatic conversion of NSString objects to Perl scalars
$CamelBones::ReturnStringsAsObjects = 0;
$self->{'recordsFile'} = $str->stringByExpandingTildeInPath();
my $records =
NSMutableArray->alloc()->initWithContentsOfFile($self- >{'recordsFile'});
unless (defined $records) {
$records = NSMutableArray->alloc()->init();
}
$self->{'comments'} = $records;
$self->{'NSWindowController'} =
NSWindowController->alloc()->initWithWindowNibName_owner('MainWindow',
$self);
$self->{'NSWindowController'}->window;
return $self;
}
# Private methods
sub saveData{
my ($self) = @_;
$self->{'comments'}->writeToFile_atomically($self->{'recordsFile'},1);
}
sub createRow{
my ($self) = @_;
my $comment = $self->{'Editfield'}->stringValue;
#$self->{'Editfield'}->setStringValue("");
my $row = {};
$row->{'comments'} = $comment;
return $row;
}
# Actions methods
sub addCommentAction
{
my ($self, $sender) = @_;
my $row = $self->createRow();
#push @{$self->{'comments'}},$row;
$self->{'comments'}->addObject($row);
$self->{'Table'}->reloadData();
$self->saveData();
}
sub insertCommentAction
{
my ($self, $sender) = @_;
my $row = $self->createRow();
my $index = $self->{'Table'}->selectedRow;
#splice @{$self->{'comments'}},$index,0,$row;
$self->{'comments'}->insertObject_atIndex($row,$index);
$self->{'Table'}->reloadData();
$self->saveData();
}
sub removeCommentAction
{
my ($self, $sender) = @_;
my $row = $self->createRow();
my $index = $self->{'Table'}->selectedRow;
splice @{$self->{'comments'}},$index,1;
$self->{'Table'}->reloadData();
$self->saveData();
}
# DataSource methods
sub numberOfRowsInTableView :
Selector(numberOfRowsInTableView:)
ArgTypes(@) ReturnType(i)
{
my $self = shift;
return $self->{'comments'}->count;
}
sub tableView_objectValueForTableColumn_row :
Selector(tableView:objectValueForTableColumn:row:)
ArgTypes(@@i) ReturnType(@)
{
my ($self, $tableView, $tableColumn, $row) = @_;
my $line = $self->{'comments'}->objectAtIndex($row);
my $value = $line->objectForKey($tableColumn->identifier);
return $value;
}
sub tableView_setObjectValue_forTableColumn_row :
Selector(tableView:setObjectValue:forTableColumn:row:)
ArgTypes(@@@i) ReturnType(v)
{
my ($self, $tableView, $newValue, $tableColumn, $row) = @_;
my $line = $self->{'comments'}->objectAtIndex($row);
$line->setObject_forKey($newValue, $tableColumn->identifier);
$self->saveData();
}
1;
I still need help for the conversion of the code of the
removeCommentAction method : the previous code using splice did not
work anymore.
There is probably a shorter solution in Perl compared to the following
ObjC code :
- (IBAction)deleteRecord:(id)sender
{
int status;
NSEnumerator *enumerator;
NSNumber *index;
NSMutableArray *tempArray;
id tempObject;
if ( [tableView numberOfSelectedRows] == 0 )
return;
NSBeep();
status = NSRunAlertPanel(@"Warning!", @"Are you sure that you want
to delete the selected record(s)?", @"OK", @"Cancel", nil);
if ( status == NSAlertDefaultReturn ) {
enumerator = [tableView selectedRowEnumerator];
tempArray = [NSMutableArray array];
while ( (index = [enumerator nextObject]) ) {
tempObject = [records objectAtIndex:[index intValue]];
[tempArray addObject:tempObject];
}
[records removeObjectsInArray:tempArray];
[tableView reloadData];
[self saveData];
}
}
Thanks for your help !
Pierre
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.