Annotation of Examples/EnterpriseObjects/RadioMatrixAssociation/RadioAssociation.m, revision 1.1

1.1     ! root        1: /* RadioAssociation.m
        !             2:  *
        !             3:  * You may freely copy, distribute, and reuse the code in this example.
        !             4:  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
        !             5:  * fitness for any particular use.
        !             6:  *
        !             7:  *  
        !             8:  *
        !             9:  */
        !            10:  
        !            11: #import <appkit/appkit.h>
        !            12: #import <eointerface/eointerface.h>
        !            13: 
        !            14: #import "RadioAssociation.h"
        !            15:  
        !            16: // Add a category to ButtonCell class to recognize the RadioAssociation
        !            17: // for Radio Buttons.
        !            18: 
        !            19: @implementation ButtonCell (RadioAssociation)
        !            20: - (Class)matrixAssociationClass:matrix
        !            21: {
        !            22:     const char *iconname;
        !            23: 
        !            24:     if (([matrix mode] == NX_RADIOMODE)
        !            25:             && (iconname = [self icon])
        !            26:             && !strcmp(iconname, "NXradio"))
        !            27:         return [RadioAssociation class];
        !            28:     else
        !            29:         return [EOMatrixAssociation class];
        !            30: }
        !            31: @end
        !            32: 
        !            33: 
        !            34: // Override the standard matrix associationClass method to spot radio
        !            35: // associations
        !            36: 
        !            37: @implementation Matrix (RadioAssoc)
        !            38: - (Class)associationClass
        !            39: {
        !            40:     id acell;
        !            41: 
        !            42:     //  Determine if we're a matrix of radio buttons!
        !            43:     acell = [self cellAt:0:0];
        !            44:     if (acell && [acell respondsToSelector:@selector(matrixAssociationClass:)])
        !            45:         return [acell matrixAssociationClass:self];
        !            46:     
        !            47:     // Use the generic association 
        !            48:     return [EOMatrixAssociation class];
        !            49: }
        !            50: 
        !            51: @end
        !            52: 
        !            53: 
        !            54: @implementation RadioAssociation
        !            55: 
        !            56: - initWithController:(EOController *)aController
        !            57:        key: (NSString *)aKey destination: aDest
        !            58: {
        !            59:     [super initWithController:aController key: aKey destination:aDest];
        !            60: 
        !            61:     // This is needed to get rid of warning about multiple declarations. 
        !            62:     [(Matrix *)aDest setTarget:self];
        !            63:     [aDest setAction:@selector(controlActed:)];
        !            64: 
        !            65:     // Allow empty selection for instances when the EO value doesn't
        !            66:     // match any of the values
        !            67:        
        !            68:     [aDest setEmptySelectionEnabled:YES]; 
        !            69:     return self;
        !            70: }
        !            71: 
        !            72: - controlActed:sender
        !            73: {
        !            74:     [controller associationDidEdit: self];
        !            75:     return self;
        !            76: }
        !            77: 
        !            78: 
        !            79: // Return the selected cell string, or NULL if no cell is selected
        !            80: 
        !            81: - value
        !            82: {
        !            83:        const char *title = [[destination selectedCell] title];
        !            84:        if (title) 
        !            85:                return [NSString stringWithCString:title];
        !            86:         else  
        !            87:                return [EONull null];
        !            88: 
        !            89: }
        !            90: 
        !            91: - (NSString *)currentValue
        !            92: {
        !            93:     NSArray *selectedObjects = [controller selectedObjects];
        !            94:     NSString *stringValue;
        !            95:     
        !            96:     if ([selectedObjects count] >= 1) {
        !            97:         id selectedObject, aDict, val;
        !            98:                id array = [[[NSArray alloc] initWithObjects: key, 0] autorelease];
        !            99:        
        !           100:                selectedObject = [selectedObjects objectAtIndex: 0];
        !           101:                aDict = [selectedObject valuesForKeys: array];
        !           102:                val = [aDict objectForKey: key];
        !           103:                if (!val)
        !           104:                        stringValue = @"";
        !           105:                else
        !           106:             stringValue = [((NSObject *)val) description];      
        !           107:        } else {
        !           108:                stringValue = @"";
        !           109:     }
        !           110: 
        !           111:     return stringValue;
        !           112: }
        !           113: 
        !           114: - (void)selectionDidChange
        !           115: {
        !           116:     [self contentsDidChange];
        !           117: }
        !           118: 
        !           119: //
        !           120: // If no cell matches title, then an empty selection is set
        !           121: //
        !           122: - (void)selectCellWithTitle:(NSString *)title
        !           123:   {
        !           124:     int col,row,numrows,numcols;
        !           125:     const char *titleCString = [title cString];
        !           126: 
        !           127:     [destination getNumRows:&numrows numCols:&numcols];
        !           128:     for(col=0; col<numcols;col++) {
        !           129:         for(row=0; row<numrows; row++) {
        !           130:            id cell = [destination cellAt:row :col];
        !           131:            if (cell && !strcmp([cell title], titleCString)) {
        !           132:                [destination selectCellAt:row :col];
        !           133:                return;
        !           134:            }
        !           135:        }
        !           136:     }
        !           137:     
        !           138:        // Clear the selection if no matching cell is found.
        !           139:     [destination selectCellAt:-1 :-1];
        !           140: }
        !           141: 
        !           142: - (void)contentsDidChange
        !           143: {
        !           144:     [self selectCellWithTitle:[self currentValue]];
        !           145: 
        !           146:     // If there's not exactly one object selected, disable the control
        !           147:        
        !           148:     if ([[controller selectedObjects] count] == 1)
        !           149:         [destination setEnabled:YES];
        !           150:     else
        !           151:         [destination setEnabled:NO];
        !           152: }
        !           153: 
        !           154: @end
        !           155: 
        !           156: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.