]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/argeo-ria-lib/slc/class/org/argeo/slc/ria/execution/CellEditorFactory.js
Reopening Realized Flows (using a window caching for the moment)
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / argeo-ria-lib / slc / class / org / argeo / slc / ria / execution / CellEditorFactory.js
1 /*******************************************************************************
2 *
3 * Argeo
4 *
5 ******************************************************************************/
6
7 /**
8 * A cell editor factory creating text fields or disabled text fields.
9 *
10 */
11 qx.Class.define("org.argeo.slc.ria.execution.CellEditorFactory",
12 {
13 extend : qx.ui.table.cellrenderer.Default,
14 implement : qx.ui.table.ICellEditorFactory,
15
16 /*
17 * ****************************************************************************
18 * CONSTRUCTOR
19 * ****************************************************************************
20 */
21
22 construct : function() {
23 this.base(arguments);
24 },
25
26 /*
27 * ****************************************************************************
28 * PROPERTIES
29 * ****************************************************************************
30 */
31
32 properties : {
33
34 /**
35 * function that validates the result the function will be called with
36 * the new value and the old value and is supposed to return the value
37 * that is set as the table value.
38 */
39 validationFunction : {
40 check : "Function",
41 nullable : true,
42 init : null
43 }
44
45 },
46
47 /*
48 * ****************************************************************************
49 * MEMBERS
50 * ****************************************************************************
51 */
52 members : {
53
54 // overridden
55 _getContentHtml : function(cellInfo) {
56 var table = cellInfo.table;
57 var tableModel = table.getTableModel();
58 var rowData = tableModel.getRowData(cellInfo.row);
59 var metaData = rowData[2];
60 if (metaData.disabled) {
61 return '<span style="color:#999;">' + qx.bom.String.escape(this._formatValue(cellInfo)) + '</span>';
62 }else{
63 return qx.bom.String.escape(this._formatValue(cellInfo));
64 }
65 },
66
67
68 // interface implementation
69 createCellEditor : function(cellInfo) {
70 var table = cellInfo.table;
71 var tableModel = table.getTableModel();
72 var rowData = tableModel.getRowData(cellInfo.row);
73 var metaData = rowData[2];
74 if (metaData.disabled) {
75 return null; // var cellEditor = new
76 // qx.ui.table.celleditor.TextField();
77 }
78 if(metaData.type == "primitive"){
79 var cellEditor = new qx.ui.form.TextField;
80 cellEditor.setAppearance("table-editor-textfield");
81 cellEditor.originalValue = cellInfo.value;
82 if (cellInfo.value === null) {
83 cellInfo.value = "";
84 }
85 cellEditor.setValue("" + cellInfo.value);
86
87 cellEditor.addListener("appear", function() {
88 cellEditor.selectAll();
89 });
90
91 var validationFunc;
92 if (metaData.subType == "integer") {
93 validationFunc = function(newValue, oldValue){
94 var isNum = !isNaN(newValue * 1);
95 if (!isNum) {
96 alert("Warning, this field only accepts Integers!");
97 return oldValue;
98 }
99 return newValue;
100 };
101 }
102 cellEditor.setUserData("validationFunc", validationFunc);
103 }else if(metaData.type == "ref"){
104 var cellEditor = new qx.ui.form.SelectBox().set({
105 appearance: "table-editor-selectbox"
106 });
107 cellEditor.setUserData("validationFunc", function(value, oldValue){
108 if(value == "__empty__"){
109 alert("Warning, value cannot be empty! You must select at least one option below.");
110 return oldValue;
111 }
112 return value;
113 });
114
115 var value = cellInfo.value;
116 cellEditor.originalValue = value;
117 // replace null values
118 if ( value === null ) {
119 value = "";
120 }
121 if(value == ""){
122 cellEditor.add(new qx.ui.form.ListItem("", null, "__empty__"));
123 }
124 var list = metaData.refList;
125 if (list)
126 {
127 var item;
128
129 for (var i=0,l=list.length; i<l; i++)
130 {
131 var row = list[i];
132 if ( row instanceof Array ) {
133 // Array [key, description] where description can be null
134 item = new qx.ui.form.ListItem(row[0], null, row[0]);
135 if(row[1]){
136 item.setToolTip(new qx.ui.tooltip.ToolTip(row[1]));
137 }
138 } else {
139 item = new qx.ui.form.ListItem(row, null, row)
140 }
141 if(value == item.getValue()){
142 cellEditor.setSelected(item);
143 }
144 cellEditor.add(item);
145 };
146 }
147
148 cellEditor.setValue("" + value);
149 cellEditor.addListener("appear", function() {
150 cellEditor.open();
151 });
152 }
153
154 return cellEditor;
155 },
156
157 // interface implementation
158 getCellEditorValue : function(cellEditor) {
159 var value = cellEditor.getValue();
160 var validationFunc = cellEditor.getUserData("validationFunc");
161
162 // validation function will be called with new and old value
163 // var validationFunc = this.getValidationFunction();
164 if (validationFunc) {
165 value = validationFunc(value, cellEditor.originalValue);
166 }
167
168 if (typeof cellEditor.originalValue == "number") {
169 value = parseFloat(value);
170 }
171
172 return value;
173 }
174 }
175 });