]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/Command.js
Documentation
[gpl/argeo-slc.git] / org.argeo.slc.webapp / 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 * Sub menu if needed
30 */
31 menu : {
32 nullable: true,
33 event : "changeMenu"
34 },
35 /**
36 * Callback associated to the submenu of the command
37 */
38 menuCallback : {nullable:true},
39 /**
40 * Context used when triggering menuCallback
41 */
42 menuContext : {nullable:true}
43 },
44
45 /**
46 * @param id {String} Id of the command
47 * @param label {String} Label of the command
48 * @param icon {String} Icon of the command
49 * @param shortcut {String} Keyboard Shortcut (like alt+o, ctrl+z, etc..)
50 */
51 construct : function(id, label, icon, shortcut){
52 this.base(arguments, shortcut);
53 this.setId(id);
54 this.setLabel(label);
55 this.setIcon(icon);
56 this.menuClones = [];
57 },
58
59 members :
60 {
61 /**
62 * Create a Button that suits a qx.ui.menu.MenuBar linked to this command
63 * @return {qx.ui.menu.Button}
64 */
65 getMenuButton : function(){
66 if(this.getToggle()){
67 button = new qx.ui.menu.CheckBox(this.getLabel());
68 this._registerToggleButtonListeners(button);
69 }else{
70 var button = new qx.ui.menu.Button(
71 this.getLabel(),
72 this.getIcon(),
73 this,
74 this.getMenuClone()
75 );
76 if(this.getMenu()){
77 this.addListener("changeMenu", function(event){
78 button.setMenu(this.getMenuClone());
79 }, this);
80 }
81 }
82 this.addTooltip(button);
83 return button;
84 },
85
86 /**
87 * Create a Button that suits a qx.ui.toolbar.Toolbar part linked to this command.
88 * @return {qx.ui.toolbar.MenuButton}
89 */
90 getToolbarButton : function(){
91 var button;
92 if(this.getMenu()){
93 button = new qx.ui.toolbar.MenuButton(
94 this.getLabel(),
95 this.getIcon(),
96 this.getMenuClone()
97 );
98 this.addListener("changeMenu", function(event){
99 button.setMenu(this.getMenuClone());
100 }, this);
101 this.addListener("changeEnabled", function(e){
102 this.setEnabled(e.getData());
103 }, button);
104 button.setEnabled(this.getEnabled());
105 }else if(this.getToggle()){
106 button = new qx.ui.toolbar.CheckBox(this.getLabel(), this.getIcon());
107 this._registerToggleButtonListeners(button);
108 }else{
109 button = new qx.ui.toolbar.Button(
110 this.getLabel(),
111 this.getIcon(),
112 this
113 );
114 }
115 this.addTooltip(button);
116 return button;
117 },
118
119 /**
120 * Special tricks using UserData to enable/disable listeners to avoid loops...
121 * @param button {qx.ui.core.Widget} toolbar Checkbox or menu Checkbox button.
122 */
123 _registerToggleButtonListeners : function(button){
124 button.addListener("changeChecked", function(event){
125 if(button.getUserData("disableListener")) return;
126 this.setUserData("slc.command.toggleState", event.getData());
127 this.setUserData("slc.command.toggleStateSource", button);
128 this.fireEvent("execute");
129 }, this);
130 this.addListener("execute", function(event){
131 if(this.getUserData("slc;command.toggleStateSource") == button) return;
132 button.setUserData("disableListener", true);
133 button.setChecked(this.getUserData("slc.command.toggleState"));
134 button.setUserData("disableListener", false);
135 }, this);
136 },
137
138 /**
139 * Clones the command menu
140 * @return {qx.ui.menu.Menu}
141 */
142 getMenuClone : function(){
143 var menuClone = new qx.ui.menu.Menu();
144 menuClone.setMinWidth(100);
145 var submenus = this.getMenu();
146 if(!submenus) return;
147 for(var i=0;i<submenus.length;i++){
148 if(submenus[i].separator){
149 menuClone.add(new qx.ui.menu.Separator());
150 }else{
151 var button = new qx.ui.menu.Button(submenus[i].label, submenus[i].icon);
152 button.setUserData("commandId", submenus[i].commandId);
153 button.addListener("execute", this.executeSubMenuCallback, this);
154 menuClone.add(button);
155 }
156 }
157 this.menuClones.push(menuClone);
158 return menuClone;
159 },
160
161 /**
162 * Remove all existing menus and their clones.
163 */
164 clearMenus : function(){
165 if(!this.getMenu()) return;
166 for(var i=0;i<this.menuClones.length;i++){
167 this.menuClones[i].destroy();
168 }
169 this.menuClones = [];
170 },
171
172 /**
173 * Triggers the menuCallback property in the right context.
174 * @param event {qx.event.type.Event} The firing event.
175 */
176 executeSubMenuCallback : function(event){
177 var button = event.getTarget();
178 var callback = this.getMenuCallback();
179 callback = qx.lang.Function.bind(callback, this.getMenuContext() || this);
180 callback(button.getUserData("commandId"));
181 },
182 /**
183 * Adds a tooltip to a button.
184 * @param element {qx.ui.core.Widget} The element to which the command tooltip is added.
185 */
186 addTooltip : function(element){
187 if(this.getShortcut() != null){
188 element.setToolTip(new qx.ui.tooltip.ToolTip(this.getShortcut()));
189 }
190 },
191
192 /**
193 * Implementation of the ILoadStatusable interface.
194 * Sets the whole command enabled if not loading and disabled if loading.
195 * @param status {Boolean} The loading status of the button.
196 */
197 setOnLoad : function(status){
198 this.setEnabled(!status);
199 }
200
201 }
202 });