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