]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/Command.js
Create Argeo SLC RIA project
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / main / webapp / argeo-ria-src / class / org / argeo / ria / event / Command.js
1 /**
2 * The standard command for all actions. It registers keyboard shortcuts, centralizes
3 * command state, callback, etc. It is defined by command definitions that can be found
4 * in the CommandsManager.
5 */
6 qx.Class.define("org.argeo.ria.event.Command",
7 {
8 extend : qx.event.Command,
9 implement : [org.argeo.ria.components.ILoadStatusable],
10
11 properties : {
12 /**
13 * Unique id of the command
14 */
15 id : {init:""},
16 /**
17 * Label of the command
18 */
19 label : {init:""},
20 /**
21 * Icon of the command
22 */
23 icon : {init:""},
24 /**
25 * Weather this command is a true/false state
26 */
27 toggle : {init:false},
28 /**
29 * It toggle button, initial state
30 */
31 toggleInitialState : {init : false},
32 /**
33 * Sub menu if needed
34 */
35 menu : {
36 nullable: true,
37 event : "changeMenu"
38 },
39 /**
40 * Callback associated to the submenu of the command
41 */
42 menuCallback : {nullable:true},
43 /**
44 * Context used when triggering menuCallback
45 */
46 menuContext : {nullable:true}
47 },
48
49 /**
50 * @param id {String} Id of the command
51 * @param label {String} Label of the command
52 * @param icon {String} Icon of the command
53 * @param shortcut {String} Keyboard Shortcut (like alt+o, ctrl+z, etc..)
54 */
55 construct : function(id, label, icon, shortcut){
56 this.base(arguments, shortcut);
57 this.setId(id);
58 this.setLabel(label);
59 this.setIcon(icon);
60 this.menuClones = [];
61 this.callbacks = {};
62 },
63
64 members :
65 {
66 /**
67 * Create a Button that suits a qx.ui.menu.MenuBar linked to this command
68 * @return {qx.ui.menu.Button}
69 */
70 getMenuButton : function(){
71 if(this.getToggle()){
72 button = new qx.ui.menu.CheckBox(this.getLabel());
73 this._registerToggleButtonListeners(button);
74 }else{
75 var button = new qx.ui.menu.Button(
76 this.getLabel(),
77 this.getIcon(),
78 this,
79 this.getMenuClone()
80 );
81 if(this.getMenu()){
82 this.addListener("changeMenu", function(event){
83 button.setMenu(this.getMenuClone());
84 }, this);
85 }
86 }
87 this.addTooltip(button);
88 return button;
89 },
90
91 /**
92 * Create a Button that suits a qx.ui.toolbar.Toolbar part linked to this command.
93 * @return {qx.ui.toolbar.MenuButton}
94 */
95 getToolbarButton : function(){
96 var button;
97 if(this.getMenu()){
98 button = new qx.ui.toolbar.MenuButton(
99 this.getLabel(),
100 this.getIcon(),
101 this.getMenuClone()
102 );
103 this.addListener("changeMenu", function(event){
104 button.setMenu(this.getMenuClone());
105 }, this);
106 this.addListener("changeEnabled", function(e){
107 this.setEnabled(e.getData());
108 }, button);
109 button.setEnabled(this.getEnabled());
110 }else if(this.getToggle()){
111 button = new qx.ui.toolbar.CheckBox(this.getLabel(), this.getIcon());
112 if(this.getToggleInitialState()){
113 button.setChecked(true);
114 }
115 this._registerToggleButtonListeners(button);
116 }else{
117 button = new qx.ui.toolbar.Button(
118 this.getLabel(),
119 this.getIcon(),
120 this
121 );
122 }
123 this.addTooltip(button);
124 return button;
125 },
126
127 /**
128 * Register a given callback to be shared by one or more focusable part.
129 * @param callback {Function} A callback function
130 * @param focusablePartId {String} A string identifiing a focusable part. At the moment, it can only be "view:viewId"
131 */
132 registerCallback : function(callback, focusablePartId){
133 this.callbacks[focusablePartId] = callback;
134 },
135 /**
136 * Return all the registered callbacks for this command.
137 * @return {Map} A map of callback, viewId => callBack.
138 */
139 getCallbacks : function(){
140 return this.callbacks;
141 },
142 /**
143 * Remove a callback for a given focusable part.
144 * @param focusablePartId {String} A id like "view:viewId".
145 */
146 removeCallback : function(focusablePartId){
147 if(this.callbacks[focusablePartId]){
148 delete this.callbacks[focusablePartId];
149 }
150 },
151
152 /**
153 * Special tricks using UserData to enable/disable listeners to avoid loops...
154 * @param button {qx.ui.core.Widget} toolbar Checkbox or menu Checkbox button.
155 */
156 _registerToggleButtonListeners : function(button){
157 button.addListener("changeChecked", function(event){
158 if(button.getUserData("disableListener")) return;
159 this.setUserData("slc.command.toggleState", event.getData());
160 this.setUserData("slc.command.toggleStateSource", button);
161 this.fireEvent("execute");
162 }, this);
163 this.addListener("execute", function(event){
164 if(this.getUserData("slc.command.toggleStateSource") == button) return;
165 button.setUserData("disableListener", true);
166 button.setChecked(this.getUserData("slc.command.toggleState"));
167 button.setUserData("disableListener", false);
168 }, this);
169 },
170
171 /**
172 * Clones the command menu
173 * @return {qx.ui.menu.Menu}
174 */
175 getMenuClone : function(){
176 var menuClone = new qx.ui.menu.Menu();
177 menuClone.setMinWidth(100);
178 var submenus = this.getMenu();
179 if(!submenus) return;
180 for(var i=0;i<submenus.length;i++){
181 if(submenus[i].separator){
182 menuClone.add(new qx.ui.menu.Separator());
183 }else{
184 var button = new qx.ui.menu.Button(submenus[i].label, submenus[i].icon);
185 if(submenus[i].disabled){
186 button.setEnabled(false);
187 }
188 button.setUserData("commandId", submenus[i].commandId);
189 button.addListener("execute", this.executeSubMenuCallback, this);
190 menuClone.add(button);
191 }
192 }
193 this.menuClones.push(menuClone);
194 return menuClone;
195 },
196
197 /**
198 * Remove all existing menus and their clones.
199 */
200 clearMenus : function(){
201 if(!this.getMenu()) return;
202 for(var i=0;i<this.menuClones.length;i++){
203 this.menuClones[i].destroy();
204 }
205 this.menuClones = [];
206 },
207
208 /**
209 * Triggers the menuCallback property in the right context.
210 * @param event {qx.event.type.Event} The firing event.
211 */
212 executeSubMenuCallback : function(event){
213 var button = event.getTarget();
214 var callback = this.getMenuCallback();
215 var context;
216 if(this.getMenuContext()){
217 if(typeof(this.getMenuContext()) == "object") context = this.getMenuContext();
218 else if(typeof(this.getMenuContext()) == "string"){
219 if(this.getMenuContext().split(":")[0] == "view"){
220 var viewId = this.getMenuContext().split(":")[1];
221 context = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(viewId).getContent();
222 }
223 }
224 }
225 callback = qx.lang.Function.bind(callback, context || this);
226 callback(button.getUserData("commandId"));
227 },
228 /**
229 * Adds a tooltip to a button.
230 * @param element {qx.ui.core.Widget} The element to which the command tooltip is added.
231 */
232 addTooltip : function(element){
233 if(this.getShortcut() != null){
234 element.setToolTip(new qx.ui.tooltip.ToolTip(this.getShortcut()));
235 }
236 },
237
238 /**
239 * Implementation of the ILoadStatusable interface.
240 * Sets the whole command enabled if not loading and disabled if loading.
241 * @param status {Boolean} The loading status of the button.
242 */
243 setOnLoad : function(status){
244 this.setEnabled(!status);
245 }
246
247 }
248 });