]>
git.argeo.org Git - gpl/argeo-slc.git/blob - event/CommandsManager.js
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 * See the "definitions" property documentation below for more info on how to define new commands.
8 * @author Charles du Jeu
10 qx
.Class
.define("org.argeo.ria.event.CommandsManager",
13 extend
: qx
.core
.Object
,
15 construct : function(){
17 this.setInitialDefinitions(qx
.lang
.Object
.copy(this.getDefinitions()));
18 this.addListener("changedCommands", this.createCommands
, this);
24 * The commands definitions is a Map described as below
28 * | The label of the action
33 * <b>shortcut : "",</b>
34 * | The keyboard shortcut, as defined in qooxdoo (Control+s, Alt+k, etc.). Warning, the letter must be lowercase.
36 * <b>enabled : true,</b>
37 * | Whether it is enabled or disabled at creation
39 * <b>menu : ""|null,</b>
40 * | The menu group to which the command will be added. If null, will not appear in the menus.
42 * <b>menuPosition : "first"|"last"</b>
43 * | Optional : force the menu group to be first or last in the menubar.
45 * <b>toolbar : ""|null,</b>
46 * | The toolbar group to which the command will be added. If null, will not appear in the toolbars.
48 * <b>init : function(){},</b>
49 * | Optional function called at command creation.
50 * | Function context : the command itself
52 * <b>callback : function(e){},</b>
53 * | The main callback to be triggered when command is executed.
54 * | Function context : the current class (not the command!)
56 * <b>selectionChange : function(viewPaneId, xmlNodes){},</b>
57 * | Optional function called each time a selectionChange is detected in one of the active viewPane.
58 * | The origin viewPaneId and the new selection as a map of nodes are passed as arguments.
59 * | Function context : the command itself.
61 * <b>submenu : [{label:"", icon:"", commandId:""}, ...],</b>
62 * | If set, the command will create a submenu, being in a menu or in the toolbar.
63 * | The submenu is created with the various array entries, and the submenuCallback function
64 * | will be called with the 'commandId' parameter when a submenu entry is selected.
66 * <b>submenuCallback : function(commandId){},</b>
67 * | Callback if command is a submenu (cf. above).
68 * | Function context : the current class/
70 * <b>command : null</b>
71 * | For internal use only, caching the actual org.argeo.ria.event.Command object.
74 * @see org.argeo.ria.event.Command for the definition Map details.
80 icon
: "resource/slc/process-stop.png",
81 shortcut
: "Control+s",
85 callback : function(e
){},
91 icon : "resource/slc/system-shutdown.png",
92 shortcut : "Control+q",
96 callback : function(e){},
101 label
: "Show Console",
102 icon
: "resource/slc/help-contents.png",
106 menuPosition
: "last",
108 callback : function(e
){
109 org
.argeo
.ria
.components
.Logger
.getInstance().toggle();
115 icon
: "resource/slc/help-about.png",
116 shortcut
: "Control+h",
120 callback : function(e
){
121 var win
= new org
.argeo
.ria
.components
.Modal("About SLC", null, "SLC is a product from Argeo.");
131 initialDefinitions
: {
138 * Triggered when the whole commands list is changed. Mainly used internally by the manager.
140 "changedCommands" : "qx.event.type.Event"
144 *****************************************************************************
146 *****************************************************************************
152 * Creates all the objects (if they are not already existing) from the definitions maps.
154 createCommands : function(){
157 var defs
= this.getDefinitions();
158 for(var key
in defs
){
159 var definition
= defs
[key
];
161 if(!definition
.command
){
162 command
= new org
.argeo
.ria
.event
.Command(key
, definition
.label
, definition
.icon
, definition
.shortcut
);
163 if(definition
.submenu
){
164 command
.setMenu(definition
.submenu
);
165 if(definition
.submenuCallback
){
166 command
.setMenuCallback(definition
.submenuCallback
);
167 command
.setMenuContext((definition
.callbackContext
?definition
.callbackContext
:null));
170 command
.setEnabled(definition
.enabled
);
171 if(definition
.toggle
){
172 command
.setToggle(true);
174 command
.addListener("execute", definition
.callback
, (definition
.callbackContext
?definition
.callbackContext
:this));
176 var binded
= qx
.lang
.Function
.bind(definition
.init
, command
);
179 definition
.command
= command
;
181 command
= definition
.command
;
184 if(!this.menus
[definition
.menu
]) this.menus
[definition
.menu
] = [];
185 this.menus
[definition
.menu
].push(definition
);
187 if(definition
.toolbar
){
188 if(!this.toolbars
[definition
.toolbar
]) this.toolbars
[definition
.toolbar
] = [];
189 this.toolbars
[definition
.toolbar
].push(command
);
192 this.setDefinitions(defs
);
196 * Refresh the current commands status depending on the viewSelection.
197 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
199 refreshCommands : function(viewSelection
){
200 var defs
= this.getDefinitions();
202 if(viewSelection
.getCount() > 0){
203 var xmlNodes
= viewSelection
.getNodes();
205 for(var key
in defs
){
206 var definition
= defs
[key
];
207 if(!definition
.selectionChange
) continue;
208 var binded
= qx
.lang
.Function
.bind(definition
.selectionChange
, definition
.command
);
209 binded(viewSelection
.getViewId(), xmlNodes
);
214 * Record a menubar for the application
215 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
217 registerMenuBar : function(menuBar
){
218 this.addListener("changedCommands", function(){
219 this.createMenuButtons(menuBar
);
221 this.createMenuButtons(menuBar
);
225 * Record a toolbar for the application
226 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
228 registerToolBar : function(toolBar
){
229 this.addListener("changedCommands", function(){
230 this.createToolbarParts(toolBar
);
232 this.createToolbarParts(toolBar
);
236 * Creates the real buttons and add them to the passed menuBar.
237 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
239 createMenuButtons : function(menuBar
){
242 for(var key
in this.menus
){
243 var menu
= new qx
.ui
.menu
.Menu();
244 var button
= new qx
.ui
.menubar
.Button(key
, null, menu
);
245 var anchorDetected
= false;
246 for(var i
=0; i
<this.menus
[key
].length
;i
++){
247 var def
= this.menus
[key
][i
];
248 menu
.add(def
.command
.getMenuButton());
249 if(!anchorDetected
&& def
.menuPosition
){
250 anchorDetected
= true;
251 anchors
[def
.menuPosition
] = button
;
258 // Add specific anchored buttons
259 if(anchors
.first
) menuBar
.addAt(anchors
.first
, 0);
260 else if(anchors
.last
){
261 menuBar
.add(anchors
.last
);
266 * Creates the real buttons and add them to the passed toolbar.
267 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
269 createToolbarParts : function(toolbar
){
271 for(var key
in this.toolbars
){
272 var tPart
= new qx
.ui
.toolbar
.Part();
274 this.toolbars
[key
].map(function(command
){
275 tPart
.add(command
.getToolbarButton());
280 * Creates a context menu from an array of commands ids.
281 * @param commandIdsArray {Array} An array of string
282 * @return {qx.ui.menu.Menu}
284 createMenuFromIds : function(commandIdsArray
){
285 var defs
= this.getDefinitions();
286 var contextMenu
= new qx
.ui
.menu
.Menu();
287 for(var i
=0;i
<commandIdsArray
.length
;i
++){
288 var definition
= defs
[commandIdsArray
[i
]];
290 var command
= definition
.command
;
291 contextMenu
.add(command
.getMenuButton());
297 * Add a new set of commands definitions. See the definitions property of this class.
298 * @param definitions {Map} a set of commands definitions.
299 * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks.
301 addCommands : function(definitions
, callbackContext
){
302 var crtDefs
= this.getDefinitions();
303 for(var key
in definitions
){
304 if(callbackContext
) definitions
[key
]['callbackContext'] = callbackContext
;
305 crtDefs
[key
] = definitions
[key
];
307 this.setDefinitions(crtDefs
);
308 this.fireEvent("changedCommands");
311 * Removes a whole set of commands by their definitions maps.
312 * @param definitions {Map} a set of commands definitions
314 removeCommands : function(definitions
){
315 var crtDefs
= this.getDefinitions();
316 var initDefs
= this.getInitialDefinitions();
317 for(var key
in definitions
){
318 if(!crtDefs
[key
]) continue;
320 crtDefs
[key
] = initDefs
[key
];
325 this.setDefinitions(crtDefs
);
326 this.fireEvent("changedCommands");
329 * Executes a command by its id.
330 * @param commandId {String} The command id.
332 executeCommand : function(commandId
){
333 var defs
= this.getDefinitions();
334 if(defs
[commandId
] && defs
[commandId
].command
.getEnabled()){
335 defs
[commandId
].command
.execute();
339 * Retrieves a command by its id.
340 * @param commandId {String} The command id.
342 getCommandById : function(commandId
){
343 var defs
= this.getDefinitions();
344 if(defs
[commandId
] && defs
[commandId
].command
){
345 return defs
[commandId
].command
;
349 * Add a standard context menu to a toolbar for button look and feel (show icon, text, both).
350 * @param toolbar {qx.ui.toolbar.ToolBar} The toolbar
352 addToolbarContextMenu : function(toolbar
){
353 var menu
= new qx
.ui
.menu
.Menu();
354 var icon
= new qx
.ui
.menu
.RadioButton("Show Icons");
355 icon
.setValue("icon");
356 var text
= new qx
.ui
.menu
.RadioButton("Show Text");
357 text
.setValue("label");
358 var both
= new qx
.ui
.menu
.RadioButton("Show Both");
359 both
.setValue("both");
360 var mgr
= new qx
.ui
.form
.RadioGroup(icon
, text
, both
);
364 mgr
.setSelected(both
);
365 toolbar
.setContextMenu(menu
);
366 mgr
.addListener("changeValue", function(e
){
367 this.setShow(e
.getData());