]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/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.
7 * @author Charles du Jeu
9 qx
.Class
.define("org.argeo.ria.event.CommandsManager",
12 extend
: qx
.core
.Object
,
14 construct : function(){
16 this.setInitialDefinitions(qx
.lang
.Object
.copy(this.getDefinitions()));
17 this.addListener("changedCommands", this.createCommands
, this);
23 * Commands definitions
24 * @see org.argeo.ria.event.Command for the definition Map details.
30 icon
: "resource/slc/process-stop.png",
31 shortcut
: "Control+s",
35 callback : function(e
){},
41 icon : "resource/slc/system-shutdown.png",
42 shortcut : "Control+q",
46 callback : function(e){},
51 label
: "Show Console",
52 icon
: "resource/slc/help-contents.png",
58 callback : function(e
){
59 org
.argeo
.ria
.components
.Logger
.getInstance().toggle();
65 icon
: "resource/slc/help-about.png",
66 shortcut
: "Control+h",
70 callback : function(e
){
71 var win
= new org
.argeo
.ria
.components
.Modal("About SLC", null, "SLC is a product from Argeo.");
81 initialDefinitions
: {
88 * Triggered when the whole commands list is changed.
90 "changedCommands" : "qx.event.type.Event"
94 *****************************************************************************
96 *****************************************************************************
102 * Creates all the objects (if they are not already existing) from the definitions maps.
104 createCommands : function(){
107 var defs
= this.getDefinitions();
108 for(var key
in defs
){
109 var definition
= defs
[key
];
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));
120 command
.setEnabled(definition
.enabled
);
121 if(definition
.toggle
){
122 command
.setToggle(true);
124 command
.addListener("execute", definition
.callback
, (definition
.callbackContext
?definition
.callbackContext
:this));
126 var binded
= qx
.lang
.Function
.bind(definition
.init
, command
);
129 definition
.command
= command
;
131 command
= definition
.command
;
134 if(!this.menus
[definition
.menu
]) this.menus
[definition
.menu
] = [];
135 this.menus
[definition
.menu
].push(definition
);
137 if(definition
.toolbar
){
138 if(!this.toolbars
[definition
.toolbar
]) this.toolbars
[definition
.toolbar
] = [];
139 this.toolbars
[definition
.toolbar
].push(command
);
142 this.setDefinitions(defs
);
146 * Refresh the current commands status depending on the viewSelection.
147 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
149 refreshCommands : function(viewSelection
){
150 var defs
= this.getDefinitions();
152 if(viewSelection
.getCount() > 0){
153 var xmlNodes
= viewSelection
.getNodes();
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
);
164 * Record a menubar for the application
165 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
167 registerMenuBar : function(menuBar
){
168 this.addListener("changedCommands", function(){
169 this.createMenuButtons(menuBar
);
171 this.createMenuButtons(menuBar
);
175 * Record a toolbar for the application
176 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
178 registerToolBar : function(toolBar
){
179 this.addListener("changedCommands", function(){
180 this.createToolbarParts(toolBar
);
182 this.createToolbarParts(toolBar
);
186 * Creates the real buttons and add them to the passed menuBar.
187 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
189 createMenuButtons : function(menuBar
){
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
;
208 // Add specific anchored buttons
209 if(anchors
.first
) menuBar
.addAt(anchors
.first
, 0);
210 else if(anchors
.last
){
211 menuBar
.add(anchors
.last
);
216 * Creates the real buttons and add them to the passed toolbar.
217 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
219 createToolbarParts : function(toolbar
){
221 for(var key
in this.toolbars
){
222 var tPart
= new qx
.ui
.toolbar
.Part();
224 this.toolbars
[key
].map(function(command
){
225 tPart
.add(command
.getToolbarButton());
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}
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
]];
240 var command
= definition
.command
;
241 contextMenu
.add(command
.getMenuButton());
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.
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
];
257 this.setDefinitions(crtDefs
);
258 this.fireEvent("changedCommands");
261 * Removes a whole set of commands by their definitions maps.
262 * @param definitions {Map} a set of commands definitions
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;
270 crtDefs
[key
] = initDefs
[key
];
275 this.setDefinitions(crtDefs
);
276 this.fireEvent("changedCommands");
279 * Executes a command by its id.
280 * @param commandId {String} The command id.
282 executeCommand : function(commandId
){
283 var defs
= this.getDefinitions();
284 if(defs
[commandId
] && defs
[commandId
].command
.getEnabled()){
285 defs
[commandId
].command
.execute();
289 * Retrieves a command by its id.
290 * @param commandId {String} The command id.
292 getCommandById : function(commandId
){
293 var defs
= this.getDefinitions();
294 if(defs
[commandId
] && defs
[commandId
].command
){
295 return defs
[commandId
].command
;
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
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
);
314 mgr
.setSelected(both
);
315 toolbar
.setContextMenu(menu
);
316 mgr
.addListener("changeValue", function(e
){
317 this.setShow(e
.getData());