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