/* Copyright (C) 2000-2005 SKYRIX Software AG This file is part of OpenGroupware.org. OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "SkyListView.h" #include "common.h" #include #include #include @interface SkyListView(PrivateMethodes) - (int)_rows; - (int)_itemIndex; - (id)_item; - (NSString *)_getValue; - (NSString *)_createId; @end @implementation SkyListView + (int)version { return 5; } + (void)initialize { NSAssert2([super version] == 2, @"invalid superclass (%@) version %i !", NSStringFromClass([self superclass]), [super version]); } - (id)init { if ((self = [super init])) { self->groupName = [[self _createId] copy]; self->columns = 3; self->showTableTag = YES; self->popUpValueKey = @"checkValue"; } return self; } - (void)dealloc { [self->action release]; [self->list release]; [self->item release]; [self->attributes release]; [self->selectedItems release]; [self->row release]; [self->column release]; [self->attribute release]; [self->groupName release]; [self->nilString release]; [self->labels release]; [self->popUpList release]; [self->popUpItem release]; [self->popUpValueKey release]; [self->itemTemplate release]; [super dealloc]; } /* notifications */ - (void)syncSleep { self->attribute = nil; [self->attributes release]; self->attributes = nil; [self->itemTemplate release]; self->itemTemplate = nil; [self->list release]; self->list = nil; [self->action release]; self->action = nil; [super syncSleep]; } /* accessors */ - (void)setLabels:(id)_labels { ASSIGN(self->labels, _labels); } - (id)labels { return self->labels; } - (void)setAction:(NSString *)_action { ASSIGNCOPY(self->action, _action); } - (NSString *)action { return self->action; } - (void)setList:(NSArray *)_list { if ((id)self->selectedItems == (id)_list) { [self->list release]; self->list = nil; self->list = [_list copy]; } else ASSIGN(self->list, _list); } - (NSArray *)list { return self->list; } - (void)setPopUpList:(NSArray *)_list { ASSIGN(self->popUpList, _list); } - (NSArray *)popUpList { return self->popUpList; } - (void)setPopUpValueKey:(NSString *)_key { ASSIGNCOPY(self->popUpValueKey, _key); } - (NSString *)popUpValueKey { return self->popUpValueKey; } - (void)setPopUpItem:(id)_item { ASSIGN(self->popUpItem, _item); } - (id)popUpItem { return self->popUpItem; } - (void)setItem:(id)_item { ASSIGN(self->item, _item); } - (id)item { return self->item; } - (void)setColumns:(unsigned int)_columns { if (_columns > 0) self->columns = _columns; } - (unsigned int)columns { return self->columns; } - (void)setSortHorizontal:(BOOL)_flag { self->sortHorizontal = _flag; } - (BOOL)sortHorizontal { return self->sortHorizontal; } - (void)setShowTableTag:(BOOL)_flag { self->showTableTag = _flag; } - (BOOL)showTableTag { return self->showTableTag; } - (void)setSelectedItems:(NSMutableArray *)_selectedItems { self->selectedItems = [_selectedItems mutableCopy]; } - (NSMutableArray *)selectedItems { return self->selectedItems; } - (void)setAttributes:(NSArray *)_attributes { ASSIGN(self->attributes, _attributes); } - (NSArray *)attributes { return self->attributes; } - (void)setItemTemplate:(NSString *)_template { ASSIGNCOPY(self->itemTemplate,_template); } - (NSString *)itemTemplate { return self->itemTemplate; } - (void)setSelectInverse:(BOOL)_flag { self->selectInverse = _flag; } - (BOOL)selectInverse { return self->selectInverse; } - (void)setUseRadioButtons:(BOOL)_flag { self->useRadioButtons = _flag; } - (BOOL)useRadioButtons { return self->useRadioButtons; } - (void)setUsePopUp:(BOOL)_flag { self->usePopUp = _flag; } - (BOOL)usePopUp { return self->usePopUp; } - (BOOL)useCheckBox { return !self->usePopUp && !self->useRadioButtons; } - (BOOL)withNil { return (self->nilString != nil) ? YES : NO; } - (NSString *)groupName { return self->groupName; } - (void)setNilString:(NSString *)_str { ASSIGNCOPY(self->nilString, _str); } - (NSString *)nilString { return (self->nilString != nil) ? self->nilString : (NSString *)@"no object"; } - (NSString *)popUpNilString { return self->nilString; } /* private accessors */ - (void)setColumn:(NSNumber *)_column { ASSIGN(self->column, _column); } - (NSNumber *)column { return self->column; } - (void)setRow:(NSNumber *)_row { ASSIGN(self->row, _row); } - (NSNumber *)row { return self->row; } - (void)setAttribute:(NSDictionary *)_attribute { ASSIGN(self->attribute, _attribute); } - (NSDictionary *)attribute { return self->attribute; } /* complex accessors */ - (NSArray *)rowList { unsigned rows, i; NSMutableArray *rowList; rows = [self _rows]; rowList = [NSMutableArray arrayWithCapacity:rows]; for (i = 0; i < rows; i++) [rowList addObject:[NSNumber numberWithInt:i]]; return (NSArray *)rowList; } - (NSArray *)columnList { NSMutableArray *columnList; unsigned i; columnList = [NSMutableArray arrayWithCapacity:self->columns]; for (i = 0; i < self->columns; i++) [columnList addObject:[NSNumber numberWithInt:i]]; return (NSArray *)columnList; } - (NSString *)valueString { NSMutableString *result; NSString *s; if ((self->attributes == nil) || ([self->attributes count] == 0)) return [self _item]; if ([self _item] == nil) return @""; result = [NSMutableString stringWithCapacity:128]; if ((s = [self->attribute objectForKey:@"prefix"])) [result appendString:s]; if ((s = [self _getValue]) != nil) [result appendString:s]; if ((s = [self->attribute objectForKey:@"suffix"])) [result appendString:s]; return (NSString *)result; } - (NSString *)templateValue { return [self->itemTemplate stringByReplacingVariablesWithBindings:[self _item] stringForUnknownBindings:@""]; } /* conditionals */ - (BOOL)isShowChecker { if ((int)[self->list count] <= [self _itemIndex]) return NO; return [self->selectedItems isKindOfClass:[NSArray class]]; } - (void)setIsChecked:(BOOL)_flag { BOOL status = (self->selectInverse) ? !_flag : _flag; id myItem = nil; myItem = [self _item]; if (myItem != nil) { if (status && ![self->selectedItems containsObject:myItem]) [self->selectedItems addObject:myItem]; else if (!status) [self->selectedItems removeObject:myItem]; } #if DEBUG else [self debugWithFormat:@"SkyListView list has changed"]; #endif } - (BOOL)isChecked { id myItem; if ((myItem = [self _item]) == nil) return YES; return (self->selectInverse) ? ![self->selectedItems containsObject:[self _item]] : [self->selectedItems containsObject:[self _item]]; } - (void)setPopUpSelection:(NSString *)_selection { if ([_selection isNotNull]) { [self setIsChecked:YES]; [[self _item] takeValue:_selection forKey:self->popUpValueKey]; } else { [[self _item] takeValue:[EONull null] forKey:self->popUpValueKey]; [self setIsChecked:NO]; } } - (NSString *)popUpSelection { NSString *sel; sel = [[self _item] valueForKey:self->popUpValueKey]; if ((![sel isNotNull]) && [self->popUpList isNotEmpty]) sel = [self->popUpList objectAtIndex:0]; return sel; } - (NSString *)popUpItemLabel { NSString *l; if (self->labels == nil) return self->popUpItem; l = [self->labels valueForKey:self->popUpItem]; return (l != nil) ? l : (NSString *)self->popUpItem; } - (void)setIsEmptyChecked:(BOOL)_flag { if (_flag) [self->selectedItems removeAllObjects]; } - (BOOL)isEmptyChecked { return [self->selectedItems isNotEmpty] ? NO : YES; } - (BOOL)isItalicStyle { NSString *key; key = [self->attribute objectForKey:@"key"]; return ([[key componentsSeparatedByString:@"."] count] > 1) ? YES : NO; } /* PrivateMethodes */ - (int)_rows { int r; r = [self->list count] / self->columns; return (([self->list count] % self->columns) > 0) ? r+1 : r; } - (int)_itemIndex { int r = [self->row intValue]; int c = [self->column intValue]; return (self->sortHorizontal) ? r*self->columns+c : c*[self _rows]+r; } - (int)nilColSpan { int attrCount; attrCount = [self->attributes isNotEmpty] ? [self->attributes count] : 1; return (self->columns * attrCount + self->columns - 1); } - (id)_item { int idx = [self _itemIndex]; return (int)[self->list count] > idx ? [self->list objectAtIndex:idx] : nil; } - (NSString *)_getValue { // TODO: split up id obj; NSString *key; NSArray *keyList; NSEnumerator *keyEnum; NSString *tmp; obj = [self _item]; key = [self->attribute valueForKey:@"key"]; if (key != nil && [key rangeOfString:@"."].length > 0) { keyList = [key componentsSeparatedByString:@"."]; keyEnum = [keyList objectEnumerator]; while ((key = [keyEnum nextObject]) != nil) { obj = [obj valueForKey:key]; // TODO: intended sideeffect on fallthrough? if ([obj isKindOfClass:[NSArray class]]) { NSMutableString *result; unsigned i, cnt; result = [NSMutableString stringWithCapacity:64]; key = [keyEnum nextObject]; for (i = 0, cnt = [(NSArray *)obj count]; i < cnt; i++) { NSString *s; if ([(s = [[obj objectAtIndex:i] valueForKey:key]) isNotNull]) [result appendString:s]; if (i < cnt - 1) { s = [self->attribute valueForKey:@"separator"]; if (s != nil) [result appendString:s]; } } return result; } } } if (key != nil && [key rangeOfString:@","].length > 0) { NSMutableString *result; BOOL first = YES; result = [NSMutableString stringWithCapacity:64]; keyList = [key componentsSeparatedByString:@","]; keyEnum = [keyList objectEnumerator]; while ((key = [keyEnum nextObject])) { NSString *s; if (![(s = [obj valueForKey:key]) isNotNull]) s = @""; if (!first && [s isNotEmpty]) { NSString *s; if ((s = [self->attribute valueForKey:@"separator"]) != nil) [result appendString:s]; } else first = NO; if (s != nil) [result appendString:s]; } return result; } if (key != nil) obj = [obj valueForKey:key]; if ((tmp = [self->attribute valueForKey:@"binding"]) != nil) { WOComponent *parent; SEL sel; NSString *result = nil; parent = [self parent]; sel = NSSelectorFromString(tmp); if ([parent respondsToSelector:sel]) { [self setValue:[self _item] forBinding:@"item"]; result = [parent performSelector:sel]; [self setValue:[self item] forBinding:@"item"]; } return (result != nil) ? result : (NSString *)@""; } return [obj stringValue]; } - (NSString *)_createId { #ifdef __MINGW32__ static unsigned cid = 0; cid++; return [NSString stringWithFormat:@"%d", cid]; #else char buf[16]; snprintf(buf, sizeof(buf), "%d", (int)random()); return [NSString stringWithCString:buf]; #endif } - (BOOL)hasAttributes { return !((self->attributes == nil) || ![self->attributes isNotEmpty]); }; - (BOOL)hasTemplate { return [self->itemTemplate isNotEmpty]; } @end /* SkyListView */