Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_editor_base_controller.h
blob310c2717011d337f33882aafe6b9dfd42aa63a4a
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;
18 namespace bookmarks {
19 class BookmarkModel;
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
29 : NSWindowController<NSOutlineViewDelegate> {
30 @private
31 IBOutlet NSButton* newFolderButton_;
32 IBOutlet NSButton* okButton_; // Used for unit testing only.
33 IBOutlet NSTreeController* folderTreeController_;
34 IBOutlet NSOutlineView* folderTreeView_;
36 NSWindow* parentWindow_; // weak
37 Profile* profile_; // weak
38 const bookmarks::BookmarkNode* parentNode_; // weak; owned by the model
39 GURL url_; // This and title_ are only used for new urls.
40 base::string16 title_;
41 BookmarkEditor::Configuration configuration_;
42 NSString* initialName_;
43 NSString* displayName_; // Bound to a text field in the dialog.
44 BOOL creatingNewFolders_; // True while in createNewFolders.
45 // An array of BookmarkFolderInfo where each item describes a folder in the
46 // BookmarkNode structure.
47 base::scoped_nsobject<NSArray> folderTreeArray_;
48 // Bound to the table view giving a path to the current selections, of which
49 // there should only ever be one.
50 base::scoped_nsobject<NSArray> tableSelectionPaths_;
51 // C++ bridge object that observes the BookmarkModel for me.
52 scoped_ptr<BookmarkEditorBaseControllerBridge> observer_;
55 @property(nonatomic, copy) NSString* initialName;
56 @property(nonatomic, copy) NSString* displayName;
57 @property(nonatomic, retain, readonly) NSArray* folderTreeArray;
58 @property(nonatomic, copy) NSArray* tableSelectionPaths;
60 // Designated initializer. Derived classes should call through to this init.
61 // |url| and |title| are only used for BookmarkNode::Type::NEW_URL.
62 - (id)initWithParentWindow:(NSWindow*)parentWindow
63 nibName:(NSString*)nibName
64 profile:(Profile*)profile
65 parent:(const bookmarks::BookmarkNode*)parent
66 url:(const GURL&)url
67 title:(const base::string16&)title
68 configuration:(BookmarkEditor::Configuration)configuration;
70 // Run the bookmark editor as a modal sheet. Does not block.
71 - (void)runAsModalSheet;
73 // Create a new folder at the end of the selected parent folder, give it
74 // an untitled name, and put it into editing mode.
75 - (IBAction)newFolder:(id)sender;
77 // The cancel action will dismiss the dialog. Derived classes which
78 // override cancel:, must call this after accessing any dialog-related
79 // data.
80 - (IBAction)cancel:(id)sender;
82 // The OK action will dismiss the dialog. This action is bound
83 // to the OK button of a dialog which presents a tree view of a profile's
84 // folder hierarchy and allows the creation of new folders within that tree.
85 // When the OK button is pressed, this function will: 1) call the derived
86 // class's -[willCommit] function, 2) create any new folders created by
87 // the user while the dialog is presented, 3) call the derived class's
88 // -[didCommit] function, and then 4) dismiss the dialog. At least one
89 // of -[willCommit] and -[didCommit] must be provided by the derived class
90 // and should return a NSNumber containing a BOOL or nil ('nil' means YES)
91 // indicating if the operation should be allowed to continue.
92 // Note: A derived class should not override the ok: action.
93 - (IBAction)ok:(id)sender;
95 // Methods for use by derived classes only.
97 // Determine and returns the rightmost selected/highlighted element (node)
98 // in the bookmark tree view if the tree view is showing, otherwise returns
99 // the original |parentNode_|. If the tree view is showing but nothing is
100 // selected then the root node is returned.
101 - (const bookmarks::BookmarkNode*)selectedNode;
103 // Expands the set of BookmarkNodes in |nodes|.
104 - (void)expandNodes:(
105 const bookmarks::BookmarkExpandedStateTracker::Nodes&)nodes;
107 // Returns the set of expanded BookmarkNodes.
108 - (bookmarks::BookmarkExpandedStateTracker::Nodes)getExpandedNodes;
110 // Select/highlight the given node within the browser tree view. If the
111 // node is nil then select the bookmark bar node. Exposed for unit test.
112 - (void)selectNodeInBrowser:(const bookmarks::BookmarkNode*)node;
114 // Notifications called when the BookmarkModel changes out from under me.
115 - (void)nodeRemoved:(const bookmarks::BookmarkNode*)node
116 fromParent:(const bookmarks::BookmarkNode*)parent;
117 - (void)modelChangedPreserveSelection:(BOOL)preserve;
119 // Determines if the ok button should be enabled, can be overridden.
120 - (BOOL)okEnabled;
122 // Accessors
123 - (bookmarks::BookmarkModel*)bookmarkModel;
124 - (Profile*)profile;
125 - (const bookmarks::BookmarkNode*)parentNode;
126 - (const GURL&)url;
127 - (const base::string16&)title;
129 @end
131 // Describes the profile's bookmark folder structure: the folder name, the
132 // original BookmarkNode pointer (if the folder already exists), a BOOL
133 // indicating if the folder is new (meaning: created during this session
134 // but not yet committed to the bookmark structure), and an NSArray of
135 // child folder BookmarkFolderInfo's following this same structure.
136 @interface BookmarkFolderInfo : NSObject {
137 @private
138 NSString* folderName_;
139 const bookmarks::BookmarkNode* folderNode_; // weak
140 NSMutableArray* children_;
141 BOOL newFolder_;
144 @property(nonatomic, copy) NSString* folderName;
145 @property(nonatomic, assign) const bookmarks::BookmarkNode* folderNode;
146 @property(nonatomic, retain) NSMutableArray* children;
147 @property(nonatomic, assign) BOOL newFolder;
149 // Convenience creator for adding a new folder to the editor's bookmark
150 // structure. This folder will be added to the bookmark model when the
151 // user accepts the dialog. |folderName| must be provided.
152 + (id)bookmarkFolderInfoWithFolderName:(NSString*)folderName;
154 // Designated initializer. |folderName| must be provided. For folders which
155 // already exist in the bookmark model, |folderNode| and |children| (if any
156 // children are already attached to this folder) must be provided and
157 // |newFolder| should be NO. For folders which the user has added during
158 // this session and which have not been committed yet, |newFolder| should be
159 // YES and |folderNode| and |children| should be NULL/nil.
160 - (id)initWithFolderName:(NSString*)folderName
161 folderNode:(const bookmarks::BookmarkNode*)folderNode
162 children:(NSMutableArray*)children
163 newFolder:(BOOL)newFolder;
165 // Convenience creator used during construction of the editor's bookmark
166 // structure. |folderName| and |folderNode| must be provided. |children|
167 // is optional. Private: exposed here for unit testing purposes.
168 + (id)bookmarkFolderInfoWithFolderName:(NSString*)folderName
169 folderNode:
170 (const bookmarks::BookmarkNode*)folderNode
171 children:(NSMutableArray*)children;
173 @end
175 @interface BookmarkEditorBaseController(TestingAPI)
177 @property(nonatomic, readonly) BOOL okButtonEnabled;
179 // Create any newly added folders. New folders are nodes in folderTreeArray
180 // which are marked as being new (i.e. their kFolderTreeNewFolderKey
181 // dictionary item is YES). This is called by -[ok:].
182 - (void)createNewFolders;
184 // Select the given bookmark node within the tree view.
185 - (void)selectTestNodeInBrowser:(const bookmarks::BookmarkNode*)node;
187 // Return the dictionary for the folder selected in the tree.
188 - (BookmarkFolderInfo*)selectedFolder;
190 @end
192 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_