Documentation

ETController

ETController : ETNibOwner <NSCopying, ETTemplateProvider, ETResponder>

A generic controller layer interfaced with the layout item tree.

AuthorsQuentin Mathe
Declared inETController.h

Overview

ETController provides a generic controller layer, usually to be used a replacement for both NSArrayController and NSTreeController.

This reusable controller fills the same purpose than the previously mentionned NSController subclasses. The difference lies in the fact the tree structure is already abstracted in the layout item tree, as such there is no need for a special tree controller class with EtoileUI. The mediation with a collection of model objects is also abstracted in ETLayoutItemGroup class, whose instances play the role of lightweight mediators between View and Model in MVC term, hence NSArrayController isn't really needed either.

ETController extends the traditional facilities of NSController subclasses by allowing to make a distinction between Object class and Group class (leaf vs branches) as very often needed by applications in the Object Manager style (see also CoreObject) at both UI and model levels.

For the UI, you can specify items templates to be cloned when a new element has to be inserted/added. On the model side, you can specify the class of the model objects to be instantiated.

This whole facility can be used at any levels of the UI.

For example, for supporting multiple windows in a file manager, you can create an ETLayoutItemGroup instance, that encapsulates a layout item tree which is the file manager UI. Then by simply setting this item group as an item group template on the window layer, and wiring File->New Window... to -add: action of this controller, new file manager windows will be created when New Window... is clicked in the menu.

If you want your file managers to automatically open on a default directory, a model class can be set on the on the controller (-setGroupClass:) or alternatively a model instance can be set on the template item group (by calling -[ETLayoutItem setRepresentedObject:] ).

That ability to clone entire UI is detailed in -deepCopy of ETLayoutItem class and subclasses.

When a controller is set, it's very important to ensure its layout item is a base item, otherwise the wrong template items may be look up in the ancestor layout item instead of using the one declared in the controller bound to the container.

ETController directly sorts object of the content and doesn't maintain arranged objects as a collection distinct from the content.


Default

- (ETLayoutItemGroup *) content

Returns the content object which is either a layout item group or nil.

Can be overriden to return a descendant item of the originally set content. For example:

- (ETLayoutItemGroup *) content
{
	return [[super content] itemForIdentifier: @"theChildItemWhichWeMutateSortAndFilter"];
}

See also -setContent: , -[ETLayoutItemGroup controller] and -nextResponder .

    - (void) didChangeContent: (ETLayoutItemGroup *)oldContent toContent: (ETLayoutItemGroup *)newContent

    Tells the receiver the item group that owns the controller has changed.

    You can override this method in subclasses to adjust controller to the new content. A common use case is to reset the observation using -stopObserveObject:forNotificationName: for some object in the old content and -startObserveObject:forNotificationName:selector: for the some object in the new content.

    if (oldContent != nil)
    {
    	[self stopObserveObject: [oldContent itemForIdentifier: @"browser"]
    		forNotificationName: ETItemGroupSelectionDidChangeNotification];
    }
    if (newContent != nil)
    {
    	[self startObserveObject: [newContent itemForIdentifier: @"browser"]
    		 forNotificationName: ETItemGroupSelectionDidChangeNotification
    					selector: @selector(browserSelectionDidChange:)];
    }
    
      - (id) nibMainContent

      Returns the Nib top level object (e.g. a window or a view) that provides the item group expected to become the controller content and owner when -loadNibAndReturnContent is invoked.

      You shouldn't need to use this method. See -setNibMainContent: .

        - (void) setNibMainContent: (id)anObject

        Sets the Nib top level object (e.g. a window or a view) that provides the item group expected to become the controller content and owner when -loadNibAndReturnContent is invoked.

        When no item can be retrieved, -loadNibAndReturnContent renders the Nib main content with -builder and uses the resulting item as the controller content.

        You usally don't use this method but set the related outlet in IB/Gorm.

          - (ETLayoutItemGroup *) loadNibAndReturnContent

          Loads the Nib file with the receiver as the File's Owner and returns the new controller content on success, otherwise returns nil and logs a failure message.

          You must retain the returned item which owns the receiver once the method has returned. Later this item can be released to release the controller.

          During the Nib loading phase, the controller owns the Nib (precisely the top level objects), but just before returning it transfers the ownership back to the returned content (identical to -content) and releases the top level objects. Which means every top level objects have to inserted into the item tree in -didLoadNib or retained with outlets (if you write a subclass).

          Raises an exception if the bundle or the Nib itself cannot be found.

          Also raises an exception when the new controller content cannot be determined, -nibMainContent or -content returns nil.

            - (ETLayoutItemBuilder *) builder

            Returns the AppKit to EtoileUI builder that converts AppKit windows, views etc. to items when a nib is loaded.

            By default, returns an ETEtoileUIBuilder instance.

              - (void) startObserveObject: (id)anObject forNotificationName: (NSString *)aName selector: (SEL)aSelector

              Adds the receiver as an observer on the given object and notification name combination to the default notification center. The method bound to the given selector on the receiver will be called back, when the observed object posts a notification whose name matches aName.

              Pass nil as the notification name, if you want to receive all notifications posted by the observed object.

              The observed object must not be nil.

                - (void) stopObserveObject: (id)anObject forNotificationName: (NSString *)aName

                Removes the receiver as an observer on the given object and notification name combination from the default notification center. Pass nil as the notification name, if you want to stop to receive any notification posted by the observed object.

                The observed object must not be nil.

                  - (id) copyWithZone: (NSZone *)aZone content: (ETLayoutItemGroup *)newContent

                  Returns a receiver copy which uses the given content.

                  This method is ETController designated copier. Subclasses that want to extend the copying support must invoke it instead of -copyWithZone: . The persistent object context is retained in the copy.

                    - (void) finishDeepCopy: (ETController *)newController withZone: (NSZone *)aZone content: (ETLayoutItemGroup *)newContent
                    Description forthcoming.
                      - (ETItemTemplate *) templateForType: (ETUTI *)aUTI

                      Returns the template to create the right UI and model to view or edit the given element type.

                      If no template matches the UTI exactly, looks up a template based on the UTI supertypes. The supertypes are -[EUTI allSupertypes] and are tested in the same order. When a template was previously bound to a supertype by calling -setTemplate:forType: , this method returns it.

                      In case all supertypes have been tried without success, nil is returned.

                      This lookup mechanism is named supercasting.

                      See -newItemWithURL:ofType:options and ETItemTemplate.

                        - (void) setTemplate: (ETItemTemplate *)aTemplate forType: (ETUTI *)aUTI

                        Sets the template to create the right UI and model to view or edit the given element type.

                        See -newItemWithURL:ofType:options and ETItemTemplate.

                          - (ETUTI *) currentObjectType

                          Returns the type of the template to be instantiated on -add: and -insert:. By default, returns kETTemplateObjectType.

                          Can be overriden to return a custom type based on a use case or a user setting.

                          See also -setCurrentObjectType: and -setTemplate:forType: .

                            - (void) setCurrentObjectType: (ETUTI *)aUTI

                            Sets the type of the template to be instantiated on -add: and -insert:. See also -setTemplate:forType: .

                              - (ETUTI *) currentGroupType

                              Returns the type of the template to be instantiated on -addNewGroup: and -insertNewGroup: . By default, returns kETTemplateGroupType.

                              Can be overriden to return a custom type based on a use case or a user setting.

                              See also -setTemplate:forType: .

                                Persistent Object Context

                                - (id ) persistentObjectContext

                                Returns the object that manages persistency.

                                The returned context is usually either a COPersistentRoot for inserting new inner objects inside a given persistent root, or a COEditingContext for inserting new root objects along their persistent roots. Can be overriden to return the right context based on the circumstances. For example, based on the current selection (a persistent object vs no selection), a persistent root or the editing context could be returned.

                                Returns the object that manages persistency.

                                The returned context is usually either a COPersistentRoot for inserting new inner objects inside a given persistent root, or a COEditingContext for inserting new root objects along their persistent roots. Can be overriden to return the right context based on the circumstances. For example, based on the current selection (a persistent object vs no selection), a persistent root or the editing context could be returned.

                                  - (void) setPersistentObjectContext: (id )aContext

                                  Sets the object that manages persistency. If the given object doesn't conform to the protocol, raises an NSInvalidArgumentException.

                                  See -persistentObjectContext for more details.

                                    - (COUndoTrack *) undoTrack

                                    Returns the undo track to record user actions in -[ETUIObject commitWithIdentifier:metadata:] .

                                    By default, returns the parent controller undo track.

                                    The parent controller is the controller attached to [[self content] controllerItem]. Can be overriden to return the right undo track based on the circumstances. For example, based on the current selection or active item, the main undo track or some more specialized track (e.g. an Undo view) could be returned.

                                      - (void) add: (id)sender

                                      Creates a new object by calling -newItemWithURL:ofType:options: and adds it to the content.

                                        - (void) addNewGroup: (id)sender

                                        Creates a new object group by calling -newItemWithURL:ofType:options: and adds it to the content.

                                          - (void) insert: (id)sender

                                          Creates a new object by calling -newItemWithURL:ofType:options: and inserts it into the content at -insertionIndex .

                                            - (void) insertNewGroup: (id)sender

                                            Creates a new object group by calling -newItemWithURL:ofType:options: and inserts it into the content at -insertionIndex .

                                              - (void) remove: (id)sender

                                              Removes all selected objects in the content. Selected objects are retrieved by calling -selectedItemsInLayout on the content.

                                                - (id) nextResponder

                                                Returns the next responder in the responder chain.

                                                The next responder is the enclosing item of the item to which the controller is bound to.

                                                You can override this method in a subclass, although it should rarely be needed.

                                                  - (ETLayoutItem *) initialFocusedItem

                                                  Returns the initial focused item.

                                                  If the controller content is a first responder sharing area without a focused item, and on the way to become visible, the content decorator item that conforms to ETFirstResponderSharingArea protocol gives the focus to the initial focused item.

                                                  If the decorator item that represents the first responder sharing area is higher in the item tree (not set on the controller content), the receiver initial focused item is ignored.

                                                  If the item is not a controller content descendant item, an NSInternalInconsistencyException is raised.

                                                  By default, returns nil.

                                                    - (void) setInitialFocusedItem: (ETLayoutItem *)anItem

                                                    Sets the initial focused item.

                                                    See -initialFocusedItem .

                                                      - (ETLayoutItem *) newItemWithURL: (NSURL *)aURL ofType: (ETUTI *)aUTI options: (NSDictionary *)options

                                                      Returns a new retained ETLayoutItem or ETLayoutItemGroup object for the given URL and options.

                                                      The template to which the item instantiation is delegated is looked up based on the UTI argument and the registered item templates.

                                                      See -setTemplate:forType: to register new templates.

                                                      If the given URL is nil, the user action is a 'New' and not 'Open'.

                                                      This method is used by -add: and -insert: actions to generate the object to be inserted into the content of the controller.

                                                      You must use this method in any 'add' or 'insert' action methods to mutate the controller content.

                                                      Raises an NSInvalidArgumentException if the given type is nil.

                                                      The returned object is retained.

                                                      See also ETItemTemplate.

                                                        - (NSDictionary *) defaultOptions

                                                        Returns the current options that controls the content mutation.

                                                        A common option is kETTemplateOptionPersistentObjectContext (bound to -persistentObjectContext). Another one is kETTemplateOptionModelDescriptionRepository that is retrieved against the persistent object context. When calling methods that includes a options argument such as -newItemWithURL:ofType:options: , you must use -defaultOptions for the argument.

                                                        Extra options can be added to the returned dictionary.

                                                          - (BOOL) canMutate

                                                          Returns whether remove, add and insert actions are possible.

                                                          By default, returns -isContentMutable value.

                                                          This method is invoked by -add: , -addNewGroup: , -insertGroup: , -insert: and -remove: . You must call it in new mutation action methods you implement. For example, if you implement - (IBAction) addNewMailbox: (id )sender, the code should look like:

                                                          if ([self canMutate])
                                                          {
                                                          	id mailboxItem = [[self templateForType: mailboxUTI] newItemWithURL: nil options: [self defaultOptions]];
                                                          	[self insertItem: mailboxItem atIndex: ETUndeterminedIndex];
                                                          }
                                                          

                                                          If you want to change the return value of -canMutate based on the type of object you insert, then you can implement a related method. For the example above:

                                                          - (BOOL) canMutateForMailbox
                                                          {
                                                          	return [self canMutate] && otherCondition;
                                                          }
                                                          
                                                          - (IBAction) addNewMailbox
                                                          {
                                                          	if ([self canMutateForMailbox])
                                                          	{
                                                          		id mailboxItem = [[self templateForType: mailboxUTI] newItemWithURL: nil options: [self defaultOptions]];
                                                          ...
                                                          
                                                          More specialized methods per operation could be implemented. For example:
                                                          • -insertNewMailbox: would use -canInsertMailbox
                                                          • -addNewMailbox: woul use -canAddMailbox
                                                          • -removeCurrentMailbox: would use -canRemoveMailbox
                                                          Alternatively -insertMailbox: and -addNewMailbox could both use -canInsertMailbox .
                                                            - (BOOL) isContentMutable

                                                            Returns whether the content item group can be mutated.

                                                            When the content represented object is not a mutable collection, returns NO, otherwise returns YES.

                                                            You shouldn't use this method but rather -canMutate .

                                                              - (NSInteger) insertionIndex

                                                              Returns the position in the content, at which -insert: and -insertGroup: will insert the object they create.

                                                              The returned value is the last selection index + 1 in the content, or the content count (no selection).

                                                              No selection means that -selectionIndexes on the content returns an empty set.

                                                              This method can be overriden to return a custom index.

                                                                - (NSIndexPath *) insertionIndexPath

                                                                Returns the index path into the content, at which insert: and insertGroup: will insert the object they create.

                                                                The returned value is the next sibling of the selected index in the content, or the content count (no selection).

                                                                The selected index path for this method is the last selected item in the content.

                                                                  - (NSIndexPath *) additionIndexPath

                                                                  Returns the index path into the content, at which add: and addNewGroup: will insert the object they create.

                                                                  The returned value is the last child of the selected index in the content, the next sibling (when the selected index path is a leaf node), or the content count (no selection).

                                                                  The selected index path for this method is the last selected item in the content.

                                                                    - (NSString *) insertionKey

                                                                    Returns the insertion key used for a keyed collection content.

                                                                    If the representedObject bound to the controller content returns YES to -isKeyed , the insertion key is included in -defaultOptions , and passed to -[ETItemTemplate newItemWithURL:options:] . The new item then usually uses a ETKeyValuePair object as its represented object.

                                                                    By default, returns Unknown.

                                                                    Can be overriden to return a custom value but must not return nil. See kETTemplateOptionKeyValuePairKey.

                                                                      - (void) insertItem: (ETLayoutItem *)anItem atIndex: (NSUInteger)anIndex

                                                                      Removes the filtering if -clearsFilterPredicateOnInsertion is YES, inserts the item at the given index and selects the inserted item if -selectsInsertedObjects is YES.

                                                                      You must use this method to mutate the content in new or overriden insertion action methods (e.g. -add: or -insertNewGroup:).

                                                                      Although the inserted object is usually an ETLayoutItem or ETLayoutItemGroup instance, you can pass an arbitrary object, it will get automatically boxed into a layout item based on the receiver template.

                                                                      You can pass ETUndeterminedIndex to trigger -[ETLayoutItemGroup addObject:] on the content rather than -[ETLayoutItemGroup insertObject:atIndex:] .

                                                                        - (void) insertItem: (ETLayoutItem *)anItem atIndexPath: (NSIndexPath *)anIndexPath

                                                                        Removes the filtering if -clearsFilterPredicateOnInsertion is YES, inserts the item at the given index path and selects the inserted item if -selectsInsertedObjects is YES.

                                                                        You must use this method to mutate the content in new or overriden insertion action methods (e.g. -add: or -insertNewGroup:).

                                                                        Although the inserted object is usually an ETLayoutItem or ETLayoutItemGroup instance, you can pass an arbitrary object, it will get automatically boxed into a layout item based on the receiver template.

                                                                        You can pass nil to trigger -[ETLayoutItemGroup addObject:] on the content rather than -[ETLayoutItemGroup insertObject:atIndex:] .

                                                                          - (BOOL) clearsFilterPredicateOnInsertion

                                                                          Returns whether the filter predicate should be discarded when a new item is inserted.

                                                                          By default, return YES.

                                                                            - (void) setClearsFilterPredicateOnInsertion: (BOOL)clear

                                                                            Sets whether the filter predicate should be discarded when a new item is inserted.

                                                                              - (BOOL) selectsInsertedObjects

                                                                              Returns whether new items should be selected on insertion.

                                                                              By default, returns YES.

                                                                                - (void) setSelectsInsertedObjects: (BOOL)select

                                                                                Sets whether new items should be selected on insertion.

                                                                                  - (NSArray *) sortDescriptors

                                                                                  Returns the sort descriptors used to sort the content associated with the receiver.

                                                                                  By default, returns an empty array.

                                                                                    - (void) setSortDescriptors: (NSArray *)sortDescriptors

                                                                                    Set the sort descriptors used to sort the content associated with the receiver.

                                                                                      - (NSPredicate *) filterPredicate

                                                                                      Returns the search predicate to filter the controller content.

                                                                                        - (void) setFilterPredicate: (NSPredicate *)searchPredicate

                                                                                        Sets the search predicate to filter the controller content.

                                                                                          - (void) rearrangeObjects

                                                                                          Arranges the objects in the content by sorting them, then filtering them with -filterPredicate if the returned predicate is not nil.

                                                                                          If the content is a tree structure, the entire tree is rearranged recursively by sorting and filtering each item group that get traversed.

                                                                                          You can override this method to implement another sort and filter strategy than the default one based on -[ETLayoutItemGroup sortWithSortDescriptors:recursively:] , -sortDescriptors , -[ETLayoutItemGroup filterWithPredicate:recursively:] and -filterPredicate .

                                                                                            - (BOOL) automaticallyRearrangesObjects

                                                                                            Returns whether -rearrangeObjects should be automatically called when -setFilterPredicate: is called.

                                                                                            Returns YES by default.

                                                                                              - (void) setAutomaticallyRearrangesObjects: (BOOL)flag

                                                                                              Sets whether -rearrangeObjects should be automatically called when -setFilterPredicate: is called.

                                                                                                - (NSArray *) allowedPickTypes
                                                                                                Description forthcoming.
                                                                                                  - (void) setAllowedPickTypes: (NSArray *)UTIs
                                                                                                  Description forthcoming.
                                                                                                    - (NSArray *) allowedDropTypesForTargetType: (ETUTI *)aUTI
                                                                                                    Description forthcoming.
                                                                                                      - (void) setAllowedDropTypes: (NSArray *)UTIs forTargetType: (ETUTI *)targetUTI
                                                                                                      Description forthcoming.
                                                                                                        - (BOOL) isEditing

                                                                                                        Returns YES when one or several edited items are registered, otherwise NO.

                                                                                                        See -allEditedItems .

                                                                                                          - (BOOL) commitEditing

                                                                                                          Tries to commit all the pending changes existing for the current edited items that were previously registered with -subjectDidBeginEditingForItem:property:.

                                                                                                          When all pending changes have been committed and all editors have been unregistered returns YES, otherwise returns NO.

                                                                                                            - (void) discardEditing

                                                                                                            Discards all the pending changes existing the current edited items that were previously registered with -subjectDidBeginEditingForItem:property:.

                                                                                                            All the edited items get unregistered.

                                                                                                              - (void) subjectDidBeginEditingForItem: (ETLayoutItem *)anItem property: (NSString *)aKey

                                                                                                              Notifies the controller the given item has begun to be edited. For text editing, -subjectDidBeginEditingForItem:property is called only if the user types something. If the focus changes before the user has typed anything, -subjectDidEndEditingForItem:property: is sent (and no -subjectDidBeginEditingForItem:property: is ever sent).

                                                                                                              You should never need to invoke this method.

                                                                                                              See instead -[ETLayoutItem subjectDidBeginEditingForProperty:fieldEditorItem:] .

                                                                                                                - (void) subjectDidChangeValueForItem: (ETLayoutItem *)anItem property: (NSString *)aKey
                                                                                                                Description forthcoming.
                                                                                                                  - (void) subjectDidEndEditingForItem: (ETLayoutItem *)anItem property: (NSString *)aKey

                                                                                                                  Notifies the controller the editing which was underway in the given item has ended.

                                                                                                                  You should never need to invoke this method.

                                                                                                                  See instead -[ETLayoutItem subjectDidEndEditingForProperty:] .

                                                                                                                    - (id) editedItem

                                                                                                                    Returns the current edited layout item.

                                                                                                                    If the edited item doesn't belong to this controller, returns nil. This happens if the focus is in a first responder sharing area that is managed by another controller (either a controller bound to a descendant item or a controller in located elsewhere in the item tree). You can use this method in action or delegate methods implemented in your controller subclass. The edited item is not the same as the editor item. If the editing is not managed entirely by the widget backend (in other words was initiated by using -[ETActionHandler beginEditingItem:property:inRect:] ), the editor item can be retrieved through [[self editedItem] firstResponderSharingArea] activeFieldEditorItem] .

                                                                                                                      - (NSString *) editedProperty

                                                                                                                      Returns the current edited property for -editedItem . If the edited item is nil, returns nil too.

                                                                                                                      You can use this method in action or delegate methods implemented in your controller subclass.

                                                                                                                        - (NSArray *) allEditedItems

                                                                                                                        Returns the edited items for all the first responder sharing areas managed by this controller. In most cases, returns a single item, because the controller is located inside a first responder sharing area or is at the same level (which means there is a single focused item that translates into a single edited item). First responder sharing areas cannot be nested. A controller bound to -[ETLayoutItemGroup windowGroup] is going to manage multiple edited items for all windows (aka first responder sharing areas) that don't have a controller bound to them. Edited items are not the same editor items, see the explanations in -editedItem .

                                                                                                                          - (NSArray *) allEditedProperties

                                                                                                                          Returns the edited properties for all the first responder sharing areas managed by this controller. The explanations concerning the number of returned objects for -allEditedItems apply to -allEditedProperties in the same way.

                                                                                                                            - (NSSet *) validatableItems
                                                                                                                            Returns the items to be validated by -validateItems .

                                                                                                                            The returned items must be look using -[ETLayoutItemGroup itemForIdentifier:] or a property set on your ETController subclass, but never using an index e.g. -[ETLayoutItemGroup itemAtIndex:] .

                                                                                                                            The returned items must be limited to descendant items. For example, returning an item such as [[[controller content] parentItem] itemForIdentifier: someId] in another item tree part or an ancestor item is not supported.

                                                                                                                              - (void) validateItems

                                                                                                                              Tells the receiver to enable and disable -validatableItems by using -validateItem: .

                                                                                                                              This method is automatically called by EtoileUI each time an event is processed, so you should rarely to call it. See -[ETEventProcessor runUpdatePhases] .

                                                                                                                                - (BOOL) validateItem: (ETLayoutItem *)anItem

                                                                                                                                Returns whether the item should be enabled or disabled now.

                                                                                                                                Can be overriden to indicate enabled items among -validatableItems . Items must be matched based on their identifier or their action. For unknow item, the superclass implementation must be called. For example:

                                                                                                                                if ([[anItem identifier] isEqual: @"add"])
                                                                                                                                {
                                                                                                                                	return [self canAdd];
                                                                                                                                }
                                                                                                                                else if (sel_isEqual([anItem action], @selector(print:))
                                                                                                                                {
                                                                                                                                	return [self isContentPrintable];
                                                                                                                                }
                                                                                                                                else
                                                                                                                                {
                                                                                                                                	return [super validateItem: anItem];
                                                                                                                                }
                                                                                                                                
                                                                                                                                  + (id <ETTemplateProvider>) basicTemplateProvider

                                                                                                                                  This method is only exposed to be used internally by EtoileUI.

                                                                                                                                  Returns a shared and immutable template provider in which basic templates are registered for -currentObjectType and -currentGroupType .