]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/source/class/org/argeo/ria/event/CommandsManager.js
7bd0957dd7d430605674f4ad9c1cc7456b497f87
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
){},
40 icon
: "resource/slc/system-shutdown.png",
41 shortcut
: "Control+q",
45 callback : function(e
){},
49 label
: "Show Console",
50 icon
: "resource/slc/help-contents.png",
56 callback : function(e
){
57 org
.argeo
.ria
.components
.Logger
.getInstance().toggle();
63 icon
: "resource/slc/help-about.png",
64 shortcut
: "Control+h",
68 callback : function(e
){
69 var win
= new org
.argeo
.ria
.components
.Modal("About SLC", null, "SLC is a product from Argeo.");
79 initialDefinitions
: {
86 * Triggered when the whole commands list is changed.
88 "changedCommands" : "qx.event.type.Event"
92 *****************************************************************************
94 *****************************************************************************
100 * Creates all the objects (if they are not already existing) from the definitions maps.
102 createCommands : function(){
105 var defs
= this.getDefinitions();
106 for(var key
in defs
){
107 var definition
= defs
[key
];
109 if(!definition
.command
){
110 command
= new org
.argeo
.ria
.event
.Command(key
, definition
.label
, definition
.icon
, definition
.shortcut
);
111 if(definition
.submenu
){
112 var menu
= new qx
.ui
.menu
.Menu();
113 command
.setMenu(menu
);
114 if(definition
.submenuCallback
){
115 command
.setMenuCallback(definition
.submenuCallback
);
116 command
.setMenuContext((definition
.callbackContext
?definition
.callbackContext
:null));
119 command
.setEnabled(definition
.enabled
);
120 command
.addListener("execute", definition
.callback
, (definition
.callbackContext
?definition
.callbackContext
:this));
121 definition
.command
= command
;
123 command
= definition
.command
;
126 if(!this.menus
[definition
.menu
]) this.menus
[definition
.menu
] = [];
127 this.menus
[definition
.menu
].push(definition
);
129 if(definition
.toolbar
){
130 if(!this.toolbars
[definition
.toolbar
]) this.toolbars
[definition
.toolbar
] = [];
131 this.toolbars
[definition
.toolbar
].push(command
);
134 this.setDefinitions(defs
);
138 * Refresh the current commands status depending on the viewSelection.
139 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
141 refreshCommands : function(viewSelection
){
142 var defs
= this.getDefinitions();
144 if(viewSelection
.getCount() > 0){
145 var xmlNodes
= viewSelection
.getNodes();
147 for(var key
in defs
){
148 var definition
= defs
[key
];
149 if(!definition
.selectionChange
) continue;
150 var binded
= qx
.lang
.Function
.bind(definition
.selectionChange
, definition
.command
);
151 binded(viewSelection
.getViewId(), xmlNodes
);
156 * Record a menubar for the application
157 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
159 registerMenuBar : function(menuBar
){
160 this.addListener("changedCommands", function(){
161 this.createMenuButtons(menuBar
);
163 this.createMenuButtons(menuBar
);
167 * Record a toolbar for the application
168 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
170 registerToolBar : function(toolBar
){
171 this.addListener("changedCommands", function(){
172 this.createToolbarParts(toolBar
);
174 this.createToolbarParts(toolBar
);
178 * Creates the real buttons and add them to the passed menuBar.
179 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
181 createMenuButtons : function(menuBar
){
184 for(var key
in this.menus
){
185 var menu
= new qx
.ui
.menu
.Menu();
186 var button
= new qx
.ui
.menubar
.Button(key
, null, menu
);
187 var anchorDetected
= false;
188 for(var i
=0; i
<this.menus
[key
].length
;i
++){
189 var def
= this.menus
[key
][i
];
190 menu
.add(def
.command
.getMenuButton());
191 if(!anchorDetected
&& def
.menuPosition
){
192 anchorDetected
= true;
193 anchors
[def
.menuPosition
] = button
;
200 // Add specific anchored buttons
201 if(anchors
.first
) menuBar
.addAt(anchors
.first
, 0);
202 else if(anchors
.last
){
203 menuBar
.add(anchors
.last
);
208 * Creates the real buttons and add them to the passed toolbar.
209 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
211 createToolbarParts : function(toolbar
){
213 for(var key
in this.toolbars
){
214 var tPart
= new qx
.ui
.toolbar
.Part();
216 this.toolbars
[key
].map(function(command
){
217 tPart
.add(command
.getToolbarButton());
222 * Creates a context menu from an array of commands ids.
223 * @param commandIdsArray {Array} An array of string
224 * @return {qx.ui.menu.Menu}
226 createMenuFromIds : function(commandIdsArray
){
227 var defs
= this.getDefinitions();
228 var contextMenu
= new qx
.ui
.menu
.Menu();
229 for(var i
=0;i
<commandIdsArray
.length
;i
++){
230 var definition
= defs
[commandIdsArray
[i
]];
232 var command
= definition
.command
;
233 contextMenu
.add(command
.getMenuButton());
239 * Add a new set of commands definitions
240 * @param definitions {Map} a set of commands definitions.
241 * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks.
243 addCommands : function(definitions
, callbackContext
){
244 var crtDefs
= this.getDefinitions();
245 for(var key
in definitions
){
246 if(callbackContext
) definitions
[key
]['callbackContext'] = callbackContext
;
247 crtDefs
[key
] = definitions
[key
];
249 this.setDefinitions(crtDefs
);
250 this.fireEvent("changedCommands");
253 * Removes a whole set of commands by their definitions maps.
254 * @param definitions {Map} a set of commands definitions
256 removeCommands : function(definitions
){
257 var crtDefs
= this.getDefinitions();
258 var initDefs
= this.getInitialDefinitions();
259 for(var key
in definitions
){
260 if(!crtDefs
[key
]) continue;
262 crtDefs
[key
] = initDefs
[key
];
267 this.setDefinitions(crtDefs
);
268 this.fireEvent("changedCommands");
271 * Executes a command by its id.
272 * @param commandId {String} The command id.
274 executeCommand : function(commandId
){
275 var defs
= this.getDefinitions();
276 if(defs
[commandId
] && defs
[commandId
].command
.getEnabled()){
277 defs
[commandId
].command
.execute();
281 * Retrieves a command by its id.
282 * @param commandId {String} The command id.
284 getCommandById : function(commandId
){
285 var defs
= this.getDefinitions();
286 if(defs
[commandId
] && defs
[commandId
].command
){
287 return defs
[commandId
].command
;
291 * Add a standard context menu to a toolbar for button look and feel (show icon, text, both).
292 * @param toolbar {qx.ui.toolbar.ToolBar} The toolbar
294 addToolbarContextMenu : function(toolbar
){
295 var menu
= new qx
.ui
.menu
.Menu();
296 var icon
= new qx
.ui
.menu
.RadioButton("Show Icons");
297 icon
.setValue("icon");
298 var text
= new qx
.ui
.menu
.RadioButton("Show Text");
299 text
.setValue("label");
300 var both
= new qx
.ui
.menu
.RadioButton("Show Both");
301 both
.setValue("both");
302 var mgr
= new qx
.ui
.form
.RadioGroup(icon
, text
, both
);
306 toolbar
.setContextMenu(menu
);
307 mgr
.addListener("changeValue", function(e
){
308 this.setShow(e
.getData());