]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/source/class/org/argeo/ria/event/CommandsManager.js
0f6f1182a9817b3df28e04edc9b632222be533f6
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / source / class / org / argeo / ria / event / CommandsManager.js
1 /**
2 * The main controller (in a standard MVC point of view) of the application. It is a singleton
3 * thus can be called by any part of the application.
4 * This will wire all the commands that can be defined dynamically by any IView, and add their
5 * corresponding buttons to the application menubar and toolbars.
6 *
7 * @author Charles du Jeu
8 */
9 qx.Class.define("org.argeo.ria.event.CommandsManager",
10 {
11 type : "singleton",
12 extend : qx.core.Object,
13
14 construct : function(){
15 this.base(arguments);
16 this.setInitialDefinitions(qx.lang.Object.copy(this.getDefinitions()));
17 this.addListener("changedCommands", this.createCommands, this);
18 },
19
20 properties :
21 {
22 /**
23 * Commands definitions
24 * @see org.argeo.ria.event.Command for the definition Map details.
25 */
26 definitions : {
27 init : {
28 "stop" : {
29 label : "Stop",
30 icon : "resource/slc/process-stop.png",
31 shortcut : "Control+s",
32 enabled : false,
33 menu : null,
34 toolbar : "list",
35 callback : function(e){},
36 command : null
37 },
38 /*
39 "quit" : {
40 label : "Quit",
41 icon : "resource/slc/system-shutdown.png",
42 shortcut : "Control+q",
43 enabled : true,
44 menu : "File",
45 toolbar : false,
46 callback : function(e){},
47 command : null
48 },
49 */
50 "log" : {
51 label : "Show Console",
52 icon : "resource/slc/help-contents.png",
53 shortcut : "",
54 enabled : true,
55 menu : "Help",
56 menuPosition: "last",
57 toolbar : false,
58 callback : function(e){
59 org.argeo.ria.components.Logger.getInstance().toggle();
60 },
61 command : null
62 },
63 "help" : {
64 label : "About...",
65 icon : "resource/slc/help-about.png",
66 shortcut : "Control+h",
67 enabled : true,
68 menu : "Help",
69 toolbar : false,
70 callback : function(e){
71 var win = new org.argeo.ria.components.Modal("About SLC", null, "SLC is a product from Argeo.");
72 win.attachAndShow();
73 },
74 command : null
75 }
76 }
77 },
78 /**
79 * For internal use
80 */
81 initialDefinitions : {
82 init : {}
83 }
84 },
85
86 events : {
87 /**
88 * Triggered when the whole commands list is changed.
89 */
90 "changedCommands" : "qx.event.type.Event"
91 },
92
93 /*
94 *****************************************************************************
95 MEMBERS
96 *****************************************************************************
97 */
98
99 members :
100 {
101 /**
102 * Creates all the objects (if they are not already existing) from the definitions maps.
103 */
104 createCommands : function(){
105 this.menus = {};
106 this.toolbars = {};
107 var defs = this.getDefinitions();
108 for(var key in defs){
109 var definition = defs[key];
110 var command;
111 if(!definition.command){
112 command = new org.argeo.ria.event.Command(key, definition.label, definition.icon, definition.shortcut);
113 if(definition.submenu){
114 command.setMenu(definition.submenu);
115 if(definition.submenuCallback){
116 command.setMenuCallback(definition.submenuCallback);
117 command.setMenuContext((definition.callbackContext?definition.callbackContext:null));
118 }
119 }
120 command.setEnabled(definition.enabled);
121 if(definition.toggle){
122 command.setToggle(true);
123 }
124 command.addListener("execute", definition.callback, (definition.callbackContext?definition.callbackContext:this));
125 if(definition.init){
126 var binded = qx.lang.Function.bind(definition.init, command);
127 binded();
128 }
129 definition.command = command;
130 }else{
131 command = definition.command;
132 }
133 if(definition.menu){
134 if(!this.menus[definition.menu]) this.menus[definition.menu] = [];
135 this.menus[definition.menu].push(definition);
136 }
137 if(definition.toolbar){
138 if(!this.toolbars[definition.toolbar]) this.toolbars[definition.toolbar] = [];
139 this.toolbars[definition.toolbar].push(command);
140 }
141 }
142 this.setDefinitions(defs);
143 },
144
145 /**
146 * Refresh the current commands status depending on the viewSelection.
147 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
148 */
149 refreshCommands : function(viewSelection){
150 var defs = this.getDefinitions();
151 var xmlNodes = null;
152 if(viewSelection.getCount() > 0){
153 var xmlNodes = viewSelection.getNodes();
154 }
155 for(var key in defs){
156 var definition = defs[key];
157 if(!definition.selectionChange) continue;
158 var binded = qx.lang.Function.bind(definition.selectionChange, definition.command);
159 binded(viewSelection.getViewId(), xmlNodes);
160 }
161 },
162
163 /**
164 * Record a menubar for the application
165 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
166 */
167 registerMenuBar : function(menuBar){
168 this.addListener("changedCommands", function(){
169 this.createMenuButtons(menuBar);
170 }, this);
171 this.createMenuButtons(menuBar);
172 },
173
174 /**
175 * Record a toolbar for the application
176 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
177 */
178 registerToolBar : function(toolBar){
179 this.addListener("changedCommands", function(){
180 this.createToolbarParts(toolBar);
181 }, this);
182 this.createToolbarParts(toolBar);
183 },
184
185 /**
186 * Creates the real buttons and add them to the passed menuBar.
187 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
188 */
189 createMenuButtons : function(menuBar){
190 menuBar.removeAll();
191 var anchors = {};
192 for(var key in this.menus){
193 var menu = new qx.ui.menu.Menu();
194 var button = new qx.ui.menubar.Button(key, null, menu);
195 var anchorDetected = false;
196 for(var i=0; i<this.menus[key].length;i++){
197 var def = this.menus[key][i];
198 menu.add(def.command.getMenuButton());
199 if(!anchorDetected && def.menuPosition){
200 anchorDetected = true;
201 anchors[def.menuPosition] = button;
202 }
203 }
204 if(!anchorDetected){
205 menuBar.add(button);
206 }
207 }
208 // Add specific anchored buttons
209 if(anchors.first) menuBar.addAt(anchors.first, 0);
210 else if(anchors.last){
211 menuBar.add(anchors.last);
212 }
213 },
214
215 /**
216 * Creates the real buttons and add them to the passed toolbar.
217 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
218 */
219 createToolbarParts : function(toolbar){
220 toolbar.removeAll();
221 for(var key in this.toolbars){
222 var tPart = new qx.ui.toolbar.Part();
223 toolbar.add(tPart);
224 this.toolbars[key].map(function(command){
225 tPart.add(command.getToolbarButton());
226 });
227 }
228 },
229 /**
230 * Creates a context menu from an array of commands ids.
231 * @param commandIdsArray {Array} An array of string
232 * @return {qx.ui.menu.Menu}
233 */
234 createMenuFromIds : function(commandIdsArray){
235 var defs = this.getDefinitions();
236 var contextMenu = new qx.ui.menu.Menu();
237 for(var i=0;i<commandIdsArray.length;i++){
238 var definition = defs[commandIdsArray[i]];
239 if(definition){
240 var command = definition.command;
241 contextMenu.add(command.getMenuButton());
242 }
243 }
244 return contextMenu;
245 },
246 /**
247 * Add a new set of commands definitions
248 * @param definitions {Map} a set of commands definitions.
249 * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks.
250 */
251 addCommands : function(definitions, callbackContext){
252 var crtDefs = this.getDefinitions();
253 for(var key in definitions){
254 if(callbackContext) definitions[key]['callbackContext'] = callbackContext;
255 crtDefs[key] = definitions[key];
256 }
257 this.setDefinitions(crtDefs);
258 this.fireEvent("changedCommands");
259 },
260 /**
261 * Removes a whole set of commands by their definitions maps.
262 * @param definitions {Map} a set of commands definitions
263 */
264 removeCommands : function(definitions){
265 var crtDefs = this.getDefinitions();
266 var initDefs = this.getInitialDefinitions();
267 for(var key in definitions){
268 if(!crtDefs[key]) continue;
269 if(initDefs[key]){
270 crtDefs[key] = initDefs[key];
271 }else{
272 delete crtDefs[key];
273 }
274 }
275 this.setDefinitions(crtDefs);
276 this.fireEvent("changedCommands");
277 },
278 /**
279 * Executes a command by its id.
280 * @param commandId {String} The command id.
281 */
282 executeCommand : function(commandId){
283 var defs = this.getDefinitions();
284 if(defs[commandId] && defs[commandId].command.getEnabled()){
285 defs[commandId].command.execute();
286 }
287 },
288 /**
289 * Retrieves a command by its id.
290 * @param commandId {String} The command id.
291 */
292 getCommandById : function(commandId){
293 var defs = this.getDefinitions();
294 if(defs[commandId] && defs[commandId].command){
295 return defs[commandId].command;
296 }
297 },
298 /**
299 * Add a standard context menu to a toolbar for button look and feel (show icon, text, both).
300 * @param toolbar {qx.ui.toolbar.ToolBar} The toolbar
301 */
302 addToolbarContextMenu : function(toolbar){
303 var menu = new qx.ui.menu.Menu();
304 var icon = new qx.ui.menu.RadioButton("Show Icons");
305 icon.setValue("icon");
306 var text = new qx.ui.menu.RadioButton("Show Text");
307 text.setValue("label");
308 var both = new qx.ui.menu.RadioButton("Show Both");
309 both.setValue("both");
310 var mgr = new qx.ui.form.RadioGroup(icon, text, both);
311 menu.add(icon);
312 menu.add(text);
313 menu.add(both);
314 mgr.setSelected(both);
315 toolbar.setContextMenu(menu);
316 mgr.addListener("changeValue", function(e){
317 this.setShow(e.getData());
318 }, toolbar);
319
320 }
321 }
322 });