3 * (C) 2006 by Simo Sorce
13 qx.OO.defineClass("swat.module.ldbbrowse.LdbModify", qx.ui.layout.VerticalBoxLayout,
16 qx.ui.layout.VerticalBoxLayout.call(this);
18 this._mainArea = new qx.ui.layout.VerticalBoxLayout();
25 // Add an horizonatl layout for the "New" and "Modify" buttons
26 // We need a vertical box layout for the tree and the buttons
27 this._hlayout = new qx.ui.layout.HorizontalBoxLayout();
33 // add a spacer to align buttons to the right
34 this._leftSpacer = new qx.ui.basic.HorizontalSpacer();
36 // Add the "Cancel" button
37 this._cancelbtn = new qx.ui.form.Button("Cancel");
38 this._cancelbtn.addEventListener("execute", this._cancelOp, this);
40 // Add the "OK" button
41 this._okbtn = new qx.ui.form.Button("OK");
42 this._okbtn.addEventListener("execute", fsm.eventListener, fsm);
44 // We'll be receiving events on the object, so save its friendly name
45 fsm.addObject("commit", this._okbtn, "swat.main.fsmUtils.disable_during_rpc");
47 // Add the buttons to the hlayout
48 this._hlayout.add(this._leftSpacer, this._cancelbtn, this._okbtn);
50 // Add the hlayout to the vlayout.
51 this.add(this._mainArea, this._hlayout);
53 // By default this is a new record creator
56 // By default this is inactive
64 qx.OO.addProperty({ name : "basedn", type : "string" });
67 * Set the type of operation
69 * @param type {String}
70 * A string containing "new" or "modify"
72 * @param data {Object}
73 * An LDB object with the current object parameters
74 * Used only if type = "modify"
78 qx.Proto.isActive = function() {
79 if (this._active == true) {
85 * Set the base of the object to add
87 * @param type {String}
88 * A string containing the base DN
91 qx.Proto.setBase = function(base) {
96 if (this._type == "add") {
98 this._basedn.setValue(this.basedn);
99 this._basedn.setWidth(8 * this.basedn.length);
104 qx.Proto.initNew = function(callback, obj) {
106 this._setExitCallback(callback, obj);
111 var hlayout = new qx.ui.layout.HorizontalBoxLayout();
112 hlayout.set({ height: "auto", spacing: 10 });
114 var dnlabel = new qx.ui.basic.Label("DN: ");
116 // The name of the new/modified object
117 // TODO: add validator
118 this._rdn = new qx.ui.form.TextField("");
119 this._rdn.setWidth(128);
121 var dnsep = new qx.ui.basic.Label(",");
123 // The basedn of the object
124 // TODO: add validator
125 this._basedn = new qx.ui.form.TextField(this.basedn);
126 this._basedn.setWidth(8 * this.basedn.length);
128 hlayout.add(dnlabel, this._rdn, dnsep, this._basedn);
130 this._mainArea.add(hlayout);
132 this._createAttributesArea();
137 qx.Proto.initMod = function(tablemodel, callback, obj) {
139 this._setExitCallback(callback, obj);
141 if (this.basedn == "") {
142 this._callExitCallback();
147 this._type = "modify";
149 this._dn = new qx.ui.basic.Label("DN: " + this.basedn);
151 this._mainArea.add(this._dn);
153 this._createAttributesArea();
155 // for each entry in the table, add new entries in the object
156 var count = tablemodel.getRowCount();
157 for (var i = 0; i < count; i++) {
158 var row = tablemodel.getRowData(i);
159 this._addNewAttribute(row[0], row[1]);
162 this._modBaseTableModel = tablemodel;
165 qx.Proto._setExitCallback = function(vFunction, vObject) {
167 if(typeof vFunction !== "function") {
168 throw new Error("swat.module.ldbbrowse.LdbModify: setExitCallback(" + vFunction + "' is not a function!");
171 this._exitCallback = {
177 qx.Proto._callExitCallback = function() {
179 // Shortcuts for handler and object
180 vFunction = this._exitCallback.handler;
181 vObject = this._exitCallback.object;
183 // Call object function
186 if(typeof vFunction === "function") {
187 vFunction.call(qx.util.Validation.isValid(vObject) ? vObject : this);
192 this.error("swat.module.ldbbrowse.LdbModify: Could not call exit callback: ", ex);
196 qx.Proto._reset = function() {
198 // Remove existing attributes
199 this._mainArea.removeAll();
200 this._active = false;
205 qx.Proto._cancelOp = function() {
208 this._callExitCallback();
211 qx.Proto._okOp = function() {
213 //TODO: disable ok/cancel buttons and call fsm instead
215 this._callExitCallback();
218 qx.Proto._addNewAttribute = function(name, value, before) {
220 var hlayout = new qx.ui.layout.HorizontalBoxLayout();
221 hlayout.set({ width: "auto", height: "auto", spacing: 10 });
223 var aButton = new qx.ui.form.Button("+");
224 aButton.set({ width: 15, height: 15});
225 aButton.addEventListener("execute", function() {
226 this._addNewAttribute(name, null, hlayout);
229 var aNameTextField = new qx.ui.form.TextField(name);
230 aNameTextField.setWidth(150);
232 var aValTextField = new qx.ui.form.TextField(value);
233 aValTextField.setWidth(250);
235 var rButton = new qx.ui.form.Button("-");
236 rButton.set({ left: 5, width: 15, height: 15});
237 rButton.addEventListener("execute", function() {
238 hlayout.setParent(null);
241 hlayout.add(aButton, aNameTextField, aValTextField, rButton);
242 hlayout.setUserData("attrName", aNameTextField);
243 hlayout.setUserData("attrVal", aValTextField);
246 this._attrArea.addAfter(hlayout, before);
248 //TODO: check the same attribute is not already present, if so just add a new value instead
249 this._attrArea.addBefore(hlayout, this._attrAddButton);
253 qx.Proto._createAttributesArea = function() {
255 this._attrArea = new qx.ui.layout.VerticalBoxLayout();
257 this._attrAddButton = new qx.ui.form.Button("+");
258 this._attrAddButton.set({ width: 15, height: 15});
259 this._attrAddButton.addEventListener("execute", this._addNewAttribute, this);
261 this._attrArea.add(this._attrAddButton);
263 this._mainArea.add(this._attrArea);
266 qx.Proto.getOpType = function() {
270 qx.Proto.getLdif = function() {
272 if (this._type != "add") {
276 var ldif = "# Add operation\n";
277 ldif = ldif + "dn: " + this._rdn.getValue() + "," + this._basedn.getValue() + "\n";
279 c = this._attrArea.getChildren();
281 for (var i = 0; i < c.length; i++) {
282 if (c[i] instanceof qx.ui.layout.HorizontalBoxLayout) {
283 ldif = ldif + c[i].getUserData("attrName").getComputedValue() + ": " + c[i].getUserData("attrVal").getComputedValue() + "\n";
286 // terminate ldif record