1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_
8 #import <Cocoa/Cocoa.h>
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/ui/bookmarks/bookmark_editor.h"
13 #include "components/bookmarks/browser/bookmark_expanded_state_tracker.h"
15 class BookmarkEditorBaseControllerBridge
;
16 @
class BookmarkTreeBrowserCell
;
22 // A base controller class for bookmark creation and editing dialogs which
23 // present the current bookmark folder structure in a tree view. Do not
24 // instantiate this controller directly -- use one of its derived classes.
25 // NOTE: If a derived class is intended to be dispatched via the
26 // BookmarkEditor::Show static function found in the accompanying
27 // implementation, that function will need to be update.
28 @interface BookmarkEditorBaseController
: NSWindowController
{
30 IBOutlet NSButton
* newFolderButton_
;
31 IBOutlet NSButton
* okButton_
; // Used for unit testing only.
32 IBOutlet NSTreeController
* folderTreeController_
;
33 IBOutlet NSOutlineView
* folderTreeView_
;
35 NSWindow
* parentWindow_
; // weak
36 Profile
* profile_
; // weak
37 const bookmarks::BookmarkNode
* parentNode_
; // weak; owned by the model
38 GURL url_
; // This and title_ are only used for new urls.
39 base::string16 title_
;
40 BookmarkEditor::Configuration configuration_
;
41 NSString
* initialName_
;
42 NSString
* displayName_
; // Bound to a text field in the dialog.
43 BOOL creatingNewFolders_
; // True while in createNewFolders.
44 // An array of BookmarkFolderInfo where each item describes a folder in the
45 // BookmarkNode structure.
46 base::scoped_nsobject
<NSArray
> folderTreeArray_
;
47 // Bound to the table view giving a path to the current selections, of which
48 // there should only ever be one.
49 base::scoped_nsobject
<NSArray
> tableSelectionPaths_
;
50 // C++ bridge object that observes the BookmarkModel for me.
51 scoped_ptr
<BookmarkEditorBaseControllerBridge
> observer_
;
54 @
property(nonatomic
, copy
) NSString
* initialName
;
55 @
property(nonatomic
, copy
) NSString
* displayName
;
56 @
property(nonatomic
, retain
, readonly
) NSArray
* folderTreeArray
;
57 @
property(nonatomic
, copy
) NSArray
* tableSelectionPaths
;
59 // Designated initializer. Derived classes should call through to this init.
60 // |url| and |title| are only used for BookmarkNode::Type::NEW_URL.
61 - (id
)initWithParentWindow
:(NSWindow
*)parentWindow
62 nibName
:(NSString
*)nibName
63 profile
:(Profile
*)profile
64 parent
:(const bookmarks::BookmarkNode
*)parent
66 title
:(const base::string16
&)title
67 configuration
:(BookmarkEditor::Configuration
)configuration
;
69 // Run the bookmark editor as a modal sheet. Does not block.
70 - (void)runAsModalSheet
;
72 // Create a new folder at the end of the selected parent folder, give it
73 // an untitled name, and put it into editing mode.
74 - (IBAction
)newFolder
:(id
)sender
;
76 // The cancel action will dismiss the dialog. Derived classes which
77 // override cancel:, must call this after accessing any dialog-related
79 - (IBAction
)cancel
:(id
)sender
;
81 // The OK action will dismiss the dialog. This action is bound
82 // to the OK button of a dialog which presents a tree view of a profile's
83 // folder hierarchy and allows the creation of new folders within that tree.
84 // When the OK button is pressed, this function will: 1) call the derived
85 // class's -[willCommit] function, 2) create any new folders created by
86 // the user while the dialog is presented, 3) call the derived class's
87 // -[didCommit] function, and then 4) dismiss the dialog. At least one
88 // of -[willCommit] and -[didCommit] must be provided by the derived class
89 // and should return a NSNumber containing a BOOL or nil ('nil' means YES)
90 // indicating if the operation should be allowed to continue.
91 // Note: A derived class should not override the ok: action.
92 - (IBAction
)ok
:(id
)sender
;
94 // Methods for use by derived classes only.
96 // Determine and returns the rightmost selected/highlighted element (node)
97 // in the bookmark tree view if the tree view is showing, otherwise returns
98 // the original |parentNode_|. If the tree view is showing but nothing is
99 // selected then the root node is returned.
100 - (const bookmarks::BookmarkNode
*)selectedNode
;
102 // Expands the set of BookmarkNodes in |nodes|.
103 - (void)expandNodes
:(
104 const bookmarks::BookmarkExpandedStateTracker::Nodes
&)nodes
;
106 // Returns the set of expanded BookmarkNodes.
107 - (bookmarks::BookmarkExpandedStateTracker::Nodes
)getExpandedNodes
;
109 // Select/highlight the given node within the browser tree view. If the
110 // node is nil then select the bookmark bar node. Exposed for unit test.
111 - (void)selectNodeInBrowser
:(const bookmarks::BookmarkNode
*)node
;
113 // Notifications called when the BookmarkModel changes out from under me.
114 - (void)nodeRemoved
:(const bookmarks::BookmarkNode
*)node
115 fromParent
:(const bookmarks::BookmarkNode
*)parent
;
116 - (void)modelChangedPreserveSelection
:(BOOL
)preserve
;
118 // Determines if the ok button should be enabled, can be overridden.
122 - (bookmarks::BookmarkModel
*)bookmarkModel
;
124 - (const bookmarks::BookmarkNode
*)parentNode
;
126 - (const base::string16
&)title
;
130 // Describes the profile's bookmark folder structure: the folder name, the
131 // original BookmarkNode pointer (if the folder already exists), a BOOL
132 // indicating if the folder is new (meaning: created during this session
133 // but not yet committed to the bookmark structure), and an NSArray of
134 // child folder BookmarkFolderInfo's following this same structure.
135 @interface BookmarkFolderInfo
: NSObject
{
137 NSString
* folderName_
;
138 const bookmarks::BookmarkNode
* folderNode_
; // weak
139 NSMutableArray
* children_
;
143 @
property(nonatomic
, copy
) NSString
* folderName
;
144 @
property(nonatomic
, assign
) const bookmarks::BookmarkNode
* folderNode
;
145 @
property(nonatomic
, retain
) NSMutableArray
* children
;
146 @
property(nonatomic
, assign
) BOOL newFolder
;
148 // Convenience creator for adding a new folder to the editor's bookmark
149 // structure. This folder will be added to the bookmark model when the
150 // user accepts the dialog. |folderName| must be provided.
151 + (id
)bookmarkFolderInfoWithFolderName
:(NSString
*)folderName
;
153 // Designated initializer. |folderName| must be provided. For folders which
154 // already exist in the bookmark model, |folderNode| and |children| (if any
155 // children are already attached to this folder) must be provided and
156 // |newFolder| should be NO. For folders which the user has added during
157 // this session and which have not been committed yet, |newFolder| should be
158 // YES and |folderNode| and |children| should be NULL/nil.
159 - (id
)initWithFolderName
:(NSString
*)folderName
160 folderNode
:(const bookmarks::BookmarkNode
*)folderNode
161 children
:(NSMutableArray
*)children
162 newFolder
:(BOOL
)newFolder
;
164 // Convenience creator used during construction of the editor's bookmark
165 // structure. |folderName| and |folderNode| must be provided. |children|
166 // is optional. Private: exposed here for unit testing purposes.
167 + (id
)bookmarkFolderInfoWithFolderName
:(NSString
*)folderName
169 (const bookmarks::BookmarkNode
*)folderNode
170 children
:(NSMutableArray
*)children
;
174 @interface
BookmarkEditorBaseController(TestingAPI
)
176 @
property(nonatomic
, readonly
) BOOL okButtonEnabled
;
178 // Create any newly added folders. New folders are nodes in folderTreeArray
179 // which are marked as being new (i.e. their kFolderTreeNewFolderKey
180 // dictionary item is YES). This is called by -[ok:].
181 - (void)createNewFolders
;
183 // Select the given bookmark node within the tree view.
184 - (void)selectTestNodeInBrowser
:(const bookmarks::BookmarkNode
*)node
;
186 // Return the dictionary for the folder selected in the tree.
187 - (BookmarkFolderInfo
*)selectedFolder
;
191 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_