OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
73 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.ui.light;
15
 
16
import org.openconcerto.sql.PropsConfiguration;
132 ilm 17
import org.openconcerto.sql.element.SQLElement;
73 ilm 18
import org.openconcerto.sql.model.FieldMapper;
19
import org.openconcerto.sql.model.SQLField;
142 ilm 20
import org.openconcerto.sql.model.SQLRowAccessor;
132 ilm 21
import org.openconcerto.sql.request.RowItemDesc;
22
import org.openconcerto.sql.view.EditPanel.EditMode;
73 ilm 23
import org.openconcerto.ui.group.Group;
24
import org.openconcerto.ui.group.Item;
25
import org.openconcerto.ui.group.LayoutHints;
156 ilm 26
import org.openconcerto.ui.light.LightUIAutoCompleteComboBox;
94 ilm 27
import org.openconcerto.ui.light.LightUICheckBox;
28
import org.openconcerto.ui.light.LightUIDate;
73 ilm 29
import org.openconcerto.ui.light.LightUIElement;
94 ilm 30
import org.openconcerto.ui.light.LightUIFrame;
31
import org.openconcerto.ui.light.LightUILabel;
73 ilm 32
import org.openconcerto.ui.light.LightUILine;
94 ilm 33
import org.openconcerto.ui.light.LightUIPanel;
156 ilm 34
import org.openconcerto.ui.light.LightUITabbed;
94 ilm 35
import org.openconcerto.ui.light.LightUITextField;
156 ilm 36
import org.openconcerto.utils.Log;
73 ilm 37
import org.openconcerto.utils.i18n.TranslationManager;
38
 
132 ilm 39
import java.awt.Color;
142 ilm 40
import java.math.BigDecimal;
41
import java.math.BigInteger;
132 ilm 42
import java.sql.Timestamp;
43
import java.util.Date;
44
import java.util.HashMap;
174 ilm 45
import java.util.List;
132 ilm 46
import java.util.Map;
47
 
73 ilm 48
public class GroupToLightUIConvertor {
49
    private final int maxColumnCount;
50
    private PropsConfiguration configuration;
132 ilm 51
    private FieldMapper mapper;
174 ilm 52
    private Map<String, CustomRowEditor> customRowEditors = new HashMap<>();
73 ilm 53
 
54
    public GroupToLightUIConvertor(PropsConfiguration conf) {
55
        this(conf, 4);
56
    }
57
 
58
    public GroupToLightUIConvertor(PropsConfiguration conf, int columns) {
59
        this.maxColumnCount = columns;
60
        this.configuration = conf;
132 ilm 61
        this.mapper = this.configuration.getFieldMapper();
62
        if (this.mapper == null) {
63
            throw new IllegalArgumentException("null mapper");
64
        }
73 ilm 65
    }
66
 
142 ilm 67
    public LightEditFrame convert(final Group group, final SQLRowAccessor defaultRow, final LightUIFrame parentFrame, final EditMode editMode) {
132 ilm 68
        if (group == null) {
69
            throw new IllegalArgumentException("Null Group");
70
        }
71
        if (defaultRow == null) {
72
            throw new IllegalArgumentException("Null default SQLRowValues");
73
        }
74
 
75
        final SQLElement sqlElement = this.configuration.getDirectory().getElement(defaultRow.getTable());
174 ilm 76
        if (sqlElement == null) {
77
            throw new IllegalArgumentException("not SQLElement found for default row table : " + defaultRow.getTable().getName());
78
        }
79
        if (sqlElement.getGroupForCreation() == null) {
80
            throw new IllegalArgumentException("no group for creation defined in " + sqlElement.getClass().getName());
81
        }
82
        if (sqlElement.getGroupForModification() == null) {
83
            throw new IllegalArgumentException("no group for creation defined in " + sqlElement.getClass().getName());
84
        }
132 ilm 85
        if (!sqlElement.getGroupForCreation().equals(group) && sqlElement.getGroupForModification().equals(group)) {
86
            throw new IllegalArgumentException("This group isn't attached to this SQLElement, group ID: " + group.getId() + " element code: " + sqlElement.getCode());
87
        }
88
 
142 ilm 89
        final LightEditFrame editFrame = new LightEditFrame(this.configuration, group, defaultRow.asRowValues(), parentFrame, editMode);
149 ilm 90
        final LightUIPanel framePanel = editFrame.getContentPanel();
132 ilm 91
 
156 ilm 92
        Map<String, LightUITabbed> tabbedMap = new HashMap<>();
93
        append(sqlElement, framePanel, group, tabbedMap);
94
 
132 ilm 95
        String frameTitle = TranslationManager.getInstance().getTranslationForItem(group.getId());
96
        if (frameTitle == null) {
97
            frameTitle = group.getId();
98
        }
99
 
142 ilm 100
        editFrame.createTitlePanel(frameTitle);
101
 
132 ilm 102
        Log.get().warning("No translation for " + group.getId());
103
        return editFrame;
73 ilm 104
    }
105
 
156 ilm 106
    private void append(final SQLElement sqlElement, final LightUIPanel panel, final Item item, Map<String, LightUITabbed> tabbedMap) {
73 ilm 107
        if (item instanceof Group) {
94 ilm 108
            final Group gr = (Group) item;
73 ilm 109
            int size = gr.getSize();
132 ilm 110
 
156 ilm 111
            if (gr.getTabId() == null) {
112
                final String groupTitle = TranslationManager.getInstance().getTranslationForItem(gr.getId());
113
                final LightUIPanel childPanel = new LightUIPanel(gr.getId());
114
                childPanel.setFillWidth(true);
115
                childPanel.setGridWidth(4);
132 ilm 116
 
156 ilm 117
                if (gr.getLocalHint().isFoldable()) {
118
                    childPanel.setTitle(groupTitle);
119
                    childPanel.setFoldable(true);
120
                } else {
121
                    if (groupTitle != null) {
122
                        final LightUILine titleLine = new LightUILine();
123
                        final LightUILabel titleLabel = new LightUILabel(gr.getId() + ".title.label");
124
                        titleLabel.setGridWidth(4);
125
                        titleLabel.setFontBold(true);
126
                        titleLabel.setLabel(groupTitle);
127
                        titleLabel.setFillWidth(true);
128
                        titleLine.addChild(titleLabel);
129
                        childPanel.addChild(titleLine);
130
                        final LightUILine line = new LightUILine();
131
                        childPanel.addChild(line);
132
                    }
133
                }
134
 
135
                for (int i = 0; i < size; i++) {
136
                    this.append(sqlElement, childPanel, gr.getItem(i), tabbedMap);
137
                }
138
 
139
                final LightUILine line = new LightUILine();
140
                line.addChild(childPanel);
141
                panel.addChild(line);
132 ilm 142
            } else {
156 ilm 143
                String tabId = gr.getTabId();
144
                LightUITabbed tabbed = tabbedMap.get(tabId);
145
                if (tabbed == null) {
146
                    tabbed = new LightUITabbed(tabId) {
147
 
148
                        @Override
149
                        public void loadTab(String tabId) {
150
                            // TODO Auto-generated method stub
151
 
152
                        }
153
                    };
132 ilm 154
                    final LightUILine line = new LightUILine();
156 ilm 155
                    line.addChild(tabbed);
156
                    panel.addChild(line);
157
                    tabbedMap.put(tabId, tabbed);
132 ilm 158
                }
156 ilm 159
                // add the group in the tabbed
160
                final LightUIPanel childPanel = new LightUIPanel(gr.getId());
161
                childPanel.setFillWidth(true);
162
                childPanel.setGridWidth(4);
163
                String title = TranslationManager.getInstance().getTranslationForItem(gr.getId());
164
                childPanel.setTitle(title);
165
                for (int i = 0; i < size; i++) {
166
                    this.append(sqlElement, childPanel, gr.getItem(i), tabbedMap);
167
                }
142 ilm 168
 
156 ilm 169
                tabbed.addChild(childPanel);
170
 
142 ilm 171
            }
73 ilm 172
        } else {
94 ilm 173
            final LayoutHints localHint = item.getLocalHint();
174
            LightUILine currentLine = panel.getLastLine();
142 ilm 175
 
176
            if (currentLine.getTotalGridWidth() >= 4) {
177
                currentLine = new LightUILine();
178
                panel.addChild(currentLine);
179
            }
180
 
132 ilm 181
            currentLine.setMarginTop(1);
182
            currentLine.setMarginBottom(1);
73 ilm 183
            if (localHint.isSeparated()) {
142 ilm 184
                if (currentLine.getChildrenCount() > 0) {
73 ilm 185
                    currentLine = new LightUILine();
132 ilm 186
                    panel.addChild(currentLine);
73 ilm 187
                }
188
            }
189
            if (localHint.fillHeight()) {
190
                currentLine.setFillHeight(true);
191
            }
192
 
193
            if (localHint.largeHeight()) {
194
                currentLine.setWeightY(1);
195
            }
196
 
142 ilm 197
            if (currentLine.getChildrenCount() >= this.maxColumnCount) {
73 ilm 198
                currentLine = new LightUILine();
132 ilm 199
                panel.addChild(currentLine);
73 ilm 200
            }
132 ilm 201
 
156 ilm 202
            SQLField field = this.mapper.getSQLFieldForItem(item.getId());
203
            if (field == null) {
204
                field = sqlElement.getTable().getFieldRaw(item.getId());
205
            }
94 ilm 206
            LightUILabel elementLabel = null;
142 ilm 207
 
208
            String label = this.getLabelForItem(field, item);
209
            if (label == null) {
210
                label = item.getId();
211
                Log.get().warning("No translation for " + item.getId());
212
            }
213
 
73 ilm 214
            if (localHint.showLabel()) {
132 ilm 215
                currentLine.setElementPadding(5);
216
 
217
                elementLabel = new LightUILabel(item.getId() + ".label");
218
                elementLabel.setHorizontalAlignement(LightUIElement.HALIGN_RIGHT);
219
 
73 ilm 220
                elementLabel.setLabel(label);
142 ilm 221
                elementLabel.setWeightX(0);
73 ilm 222
                if (localHint.isSplit()) {
132 ilm 223
                    elementLabel.setHorizontalAlignement(LightUIElement.HALIGN_LEFT);
224
                    if (currentLine.getChildrenCount() != 0) {
225
                        currentLine = new LightUILine();
226
                        panel.addChild(currentLine);
227
                    }
73 ilm 228
                    elementLabel.setGridWidth(4);
229
                } else {
230
                    elementLabel.setGridWidth(1);
231
                }
232
 
132 ilm 233
                currentLine.addChild(elementLabel);
73 ilm 234
            }
235
            LightUIElement elementEditor = this.getCustomEditor(item.getId());
236
            if (elementEditor == null) {
237
                if (field != null) {
238
                    Class<?> javaType = field.getType().getJavaType();
239
                    if (field.isKey()) {
156 ilm 240
                        elementEditor = new LightUIAutoCompleteComboBox(item.getId());
73 ilm 241
                        elementEditor.setMinInputSize(20);
142 ilm 242
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_REF);
73 ilm 243
                    } else if (javaType.equals(String.class)) {
156 ilm 244
                        elementEditor = new LightUITextField(item.getId());
245
                        elementEditor.setValue("");
246
                        elementEditor.setMinInputSize(10);
142 ilm 247
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_STRING);
248
                    } else if (javaType.equals(Boolean.class)) {
249
                        elementEditor = new LightUICheckBox(item.getId(), "");
250
                        elementEditor.setLabel(label);
251
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_BOOLEAN);
144 ilm 252
                        if (elementLabel != null) {
253
                            elementLabel.setLabel("");
254
                        }
255
                    } else if (javaType.equals(Date.class) || javaType.equals(Timestamp.class)) {
94 ilm 256
                        elementEditor = new LightUIDate(item.getId());
142 ilm 257
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_DATE);
258
                    } else if (javaType.equals(Integer.class) || javaType.equals(Long.class) || javaType.equals(Short.class) || javaType.equals(BigInteger.class)) {
94 ilm 259
                        elementEditor = new LightUITextField(item.getId());
260
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_INTEGER);
142 ilm 261
                    } else if (javaType.equals(BigDecimal.class) || javaType.equals(Float.class) || javaType.equals(Double.class)) {
262
                        elementEditor = new LightUITextField(item.getId());
263
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_DECIMAL);
73 ilm 264
                    } else {
94 ilm 265
                        elementEditor = new LightUITextField(item.getId());
73 ilm 266
                        Log.get().warning("unsupported type " + javaType.getName());
267
                        elementEditor.setValue("unsupported type " + javaType.getName());
268
                    }
269
                } else {
94 ilm 270
                    elementEditor = new LightUITextField(item.getId());
73 ilm 271
                    elementEditor.setMinInputSize(10);
272
                    elementEditor.setToolTip("No field attached to " + item.getId());
142 ilm 273
                    elementEditor.setValueType(LightUIElement.VALUE_TYPE_STRING);
73 ilm 274
                    Log.get().warning("No field attached to " + item.getId());
275
                    if (elementLabel != null) {
94 ilm 276
                        elementLabel.setBackgroundColor(Color.ORANGE);
73 ilm 277
                        elementLabel.setToolTip("No field attached to " + item.getId());
278
                    }
279
                }
280
            }
132 ilm 281
 
282
            if (elementEditor != null) {
283
                elementEditor.setWeightX(1);
94 ilm 284
            }
142 ilm 285
 
73 ilm 286
            if (localHint.isSplit()) {
142 ilm 287
                if (currentLine.getTotalGridWidth() > 0) {
73 ilm 288
                    currentLine = new LightUILine();
132 ilm 289
                    panel.addChild(currentLine);
73 ilm 290
                }
291
            }
292
 
293
            if (localHint.isSplit()) {
294
                elementEditor.setGridWidth(4);
295
            } else if (localHint.largeWidth()) {
296
 
297
                if (localHint.showLabel()) {
298
                    elementEditor.setGridWidth(3);
299
                } else {
300
                    elementEditor.setGridWidth(4);
301
                }
302
            } else {
303
                elementEditor.setGridWidth(1);
304
            }
305
            elementEditor.setFillWidth(localHint.fillWidth());
142 ilm 306
            elementEditor.setMarginBottom(4);
132 ilm 307
            currentLine.addChild(elementEditor);
73 ilm 308
 
309
        }
310
    }
311
 
142 ilm 312
    private String getLabelForItem(final SQLField field, final Item item) {
313
        String label = TranslationManager.getInstance().getTranslationForItem(item.getId());
314
 
315
        if (label == null && field != null) {
316
            final RowItemDesc desc = this.configuration.getTranslator().getDescFor(field.getTable(), field.getName());
317
            if (desc != null) {
318
                label = desc.getLabel();
319
            }
320
        }
321
        return label;
322
    }
323
 
132 ilm 324
    private LightUIElement getCustomEditor(final String id) {
174 ilm 325
        if (id == null) {
326
            throw new IllegalArgumentException("null id");
327
        }
328
        final CustomRowEditor customEditorProvider = this.customRowEditors.get(id);
73 ilm 329
        if (customEditorProvider != null) {
174 ilm 330
            final LightUIElement element = customEditorProvider.createUIElement();
73 ilm 331
            if (element.getId() == null) {
174 ilm 332
                throw new IllegalStateException("Null id for custom editor for id: " + id + " element : " + element);
73 ilm 333
            }
334
            return element;
335
        }
336
        return null;
337
    }
338
 
174 ilm 339
    public void putCustomEditorProvider(final CustomRowEditor e) {
340
        this.customRowEditors.put(e.getItemId(), e);
73 ilm 341
    }
132 ilm 342
 
174 ilm 343
    public void putAllCustomEditorProvider(final List<CustomRowEditor> editors) {
344
        for (CustomRowEditor e : editors)
345
            this.customRowEditors.put(e.getItemId(), e);
132 ilm 346
    }
73 ilm 347
}