OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | 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;
26
import org.openconcerto.ui.light.CustomEditorProvider;
156 ilm 27
import org.openconcerto.ui.light.LightUIAutoCompleteComboBox;
94 ilm 28
import org.openconcerto.ui.light.LightUICheckBox;
29
import org.openconcerto.ui.light.LightUIDate;
73 ilm 30
import org.openconcerto.ui.light.LightUIElement;
94 ilm 31
import org.openconcerto.ui.light.LightUIFrame;
32
import org.openconcerto.ui.light.LightUILabel;
73 ilm 33
import org.openconcerto.ui.light.LightUILine;
94 ilm 34
import org.openconcerto.ui.light.LightUIPanel;
156 ilm 35
import org.openconcerto.ui.light.LightUITabbed;
94 ilm 36
import org.openconcerto.ui.light.LightUITextField;
156 ilm 37
import org.openconcerto.utils.Log;
73 ilm 38
import org.openconcerto.utils.i18n.TranslationManager;
39
 
132 ilm 40
import java.awt.Color;
142 ilm 41
import java.math.BigDecimal;
42
import java.math.BigInteger;
132 ilm 43
import java.sql.Timestamp;
44
import java.util.Date;
45
import java.util.HashMap;
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;
73 ilm 52
    private Map<String, CustomEditorProvider> customEditorProviders = new HashMap<String, CustomEditorProvider>();
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());
76
        if (!sqlElement.getGroupForCreation().equals(group) && sqlElement.getGroupForModification().equals(group)) {
77
            throw new IllegalArgumentException("This group isn't attached to this SQLElement, group ID: " + group.getId() + " element code: " + sqlElement.getCode());
78
        }
79
 
142 ilm 80
        final LightEditFrame editFrame = new LightEditFrame(this.configuration, group, defaultRow.asRowValues(), parentFrame, editMode);
149 ilm 81
        final LightUIPanel framePanel = editFrame.getContentPanel();
132 ilm 82
 
156 ilm 83
        Map<String, LightUITabbed> tabbedMap = new HashMap<>();
84
        append(sqlElement, framePanel, group, tabbedMap);
85
 
132 ilm 86
        String frameTitle = TranslationManager.getInstance().getTranslationForItem(group.getId());
87
        if (frameTitle == null) {
88
            frameTitle = group.getId();
89
        }
90
 
142 ilm 91
        editFrame.createTitlePanel(frameTitle);
92
 
132 ilm 93
        Log.get().warning("No translation for " + group.getId());
94
        return editFrame;
73 ilm 95
    }
96
 
156 ilm 97
    private void append(final SQLElement sqlElement, final LightUIPanel panel, final Item item, Map<String, LightUITabbed> tabbedMap) {
73 ilm 98
        if (item instanceof Group) {
94 ilm 99
            final Group gr = (Group) item;
73 ilm 100
            int size = gr.getSize();
132 ilm 101
 
156 ilm 102
            if (gr.getTabId() == null) {
103
                final String groupTitle = TranslationManager.getInstance().getTranslationForItem(gr.getId());
104
                final LightUIPanel childPanel = new LightUIPanel(gr.getId());
105
                childPanel.setFillWidth(true);
106
                childPanel.setGridWidth(4);
132 ilm 107
 
156 ilm 108
                if (gr.getLocalHint().isFoldable()) {
109
                    childPanel.setTitle(groupTitle);
110
                    childPanel.setFoldable(true);
111
                } else {
112
                    if (groupTitle != null) {
113
                        final LightUILine titleLine = new LightUILine();
114
                        final LightUILabel titleLabel = new LightUILabel(gr.getId() + ".title.label");
115
                        titleLabel.setGridWidth(4);
116
                        titleLabel.setFontBold(true);
117
                        titleLabel.setLabel(groupTitle);
118
                        titleLabel.setFillWidth(true);
119
                        titleLine.addChild(titleLabel);
120
                        childPanel.addChild(titleLine);
121
                        final LightUILine line = new LightUILine();
122
                        childPanel.addChild(line);
123
                    }
124
                }
125
 
126
                for (int i = 0; i < size; i++) {
127
                    this.append(sqlElement, childPanel, gr.getItem(i), tabbedMap);
128
                }
129
 
130
                final LightUILine line = new LightUILine();
131
                line.addChild(childPanel);
132
                panel.addChild(line);
132 ilm 133
            } else {
156 ilm 134
                String tabId = gr.getTabId();
135
                LightUITabbed tabbed = tabbedMap.get(tabId);
136
                if (tabbed == null) {
137
                    tabbed = new LightUITabbed(tabId) {
138
 
139
                        @Override
140
                        public void loadTab(String tabId) {
141
                            // TODO Auto-generated method stub
142
 
143
                        }
144
                    };
132 ilm 145
                    final LightUILine line = new LightUILine();
156 ilm 146
                    line.addChild(tabbed);
147
                    panel.addChild(line);
148
                    tabbedMap.put(tabId, tabbed);
132 ilm 149
                }
156 ilm 150
                // add the group in the tabbed
151
                final LightUIPanel childPanel = new LightUIPanel(gr.getId());
152
                childPanel.setFillWidth(true);
153
                childPanel.setGridWidth(4);
154
                String title = TranslationManager.getInstance().getTranslationForItem(gr.getId());
155
                childPanel.setTitle(title);
156
                for (int i = 0; i < size; i++) {
157
                    this.append(sqlElement, childPanel, gr.getItem(i), tabbedMap);
158
                }
142 ilm 159
 
156 ilm 160
                tabbed.addChild(childPanel);
161
 
142 ilm 162
            }
73 ilm 163
        } else {
94 ilm 164
            final LayoutHints localHint = item.getLocalHint();
165
            LightUILine currentLine = panel.getLastLine();
142 ilm 166
 
167
            if (currentLine.getTotalGridWidth() >= 4) {
168
                currentLine = new LightUILine();
169
                panel.addChild(currentLine);
170
            }
171
 
132 ilm 172
            currentLine.setMarginTop(1);
173
            currentLine.setMarginBottom(1);
73 ilm 174
            if (localHint.isSeparated()) {
142 ilm 175
                if (currentLine.getChildrenCount() > 0) {
73 ilm 176
                    currentLine = new LightUILine();
132 ilm 177
                    panel.addChild(currentLine);
73 ilm 178
                }
179
            }
180
            if (localHint.fillHeight()) {
181
                currentLine.setFillHeight(true);
182
            }
183
 
184
            if (localHint.largeHeight()) {
185
                currentLine.setWeightY(1);
186
            }
187
 
142 ilm 188
            if (currentLine.getChildrenCount() >= this.maxColumnCount) {
73 ilm 189
                currentLine = new LightUILine();
132 ilm 190
                panel.addChild(currentLine);
73 ilm 191
            }
132 ilm 192
 
156 ilm 193
            SQLField field = this.mapper.getSQLFieldForItem(item.getId());
194
            if (field == null) {
195
                field = sqlElement.getTable().getFieldRaw(item.getId());
196
            }
94 ilm 197
            LightUILabel elementLabel = null;
142 ilm 198
 
199
            String label = this.getLabelForItem(field, item);
200
            if (label == null) {
201
                label = item.getId();
202
                Log.get().warning("No translation for " + item.getId());
203
            }
204
 
73 ilm 205
            if (localHint.showLabel()) {
132 ilm 206
                currentLine.setElementPadding(5);
207
 
208
                elementLabel = new LightUILabel(item.getId() + ".label");
209
                elementLabel.setHorizontalAlignement(LightUIElement.HALIGN_RIGHT);
210
 
73 ilm 211
                elementLabel.setLabel(label);
142 ilm 212
                elementLabel.setWeightX(0);
73 ilm 213
                if (localHint.isSplit()) {
132 ilm 214
                    elementLabel.setHorizontalAlignement(LightUIElement.HALIGN_LEFT);
215
                    if (currentLine.getChildrenCount() != 0) {
216
                        currentLine = new LightUILine();
217
                        panel.addChild(currentLine);
218
                    }
73 ilm 219
                    elementLabel.setGridWidth(4);
220
                } else {
221
                    elementLabel.setGridWidth(1);
222
                }
223
 
132 ilm 224
                currentLine.addChild(elementLabel);
73 ilm 225
            }
226
            LightUIElement elementEditor = this.getCustomEditor(item.getId());
227
            if (elementEditor == null) {
228
                if (field != null) {
229
                    Class<?> javaType = field.getType().getJavaType();
230
                    if (field.isKey()) {
156 ilm 231
                        elementEditor = new LightUIAutoCompleteComboBox(item.getId());
73 ilm 232
                        elementEditor.setMinInputSize(20);
142 ilm 233
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_REF);
73 ilm 234
                    } else if (javaType.equals(String.class)) {
156 ilm 235
                        elementEditor = new LightUITextField(item.getId());
236
                        elementEditor.setValue("");
237
                        elementEditor.setMinInputSize(10);
142 ilm 238
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_STRING);
239
                    } else if (javaType.equals(Boolean.class)) {
240
                        elementEditor = new LightUICheckBox(item.getId(), "");
241
                        elementEditor.setLabel(label);
242
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_BOOLEAN);
144 ilm 243
                        if (elementLabel != null) {
244
                            elementLabel.setLabel("");
245
                        }
246
                    } else if (javaType.equals(Date.class) || javaType.equals(Timestamp.class)) {
94 ilm 247
                        elementEditor = new LightUIDate(item.getId());
142 ilm 248
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_DATE);
249
                    } else if (javaType.equals(Integer.class) || javaType.equals(Long.class) || javaType.equals(Short.class) || javaType.equals(BigInteger.class)) {
94 ilm 250
                        elementEditor = new LightUITextField(item.getId());
251
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_INTEGER);
142 ilm 252
                    } else if (javaType.equals(BigDecimal.class) || javaType.equals(Float.class) || javaType.equals(Double.class)) {
253
                        elementEditor = new LightUITextField(item.getId());
254
                        elementEditor.setValueType(LightUIElement.VALUE_TYPE_DECIMAL);
73 ilm 255
                    } else {
94 ilm 256
                        elementEditor = new LightUITextField(item.getId());
73 ilm 257
                        Log.get().warning("unsupported type " + javaType.getName());
258
                        elementEditor.setValue("unsupported type " + javaType.getName());
259
                    }
260
                } else {
94 ilm 261
                    elementEditor = new LightUITextField(item.getId());
73 ilm 262
                    elementEditor.setMinInputSize(10);
263
                    elementEditor.setToolTip("No field attached to " + item.getId());
142 ilm 264
                    elementEditor.setValueType(LightUIElement.VALUE_TYPE_STRING);
73 ilm 265
                    Log.get().warning("No field attached to " + item.getId());
266
                    if (elementLabel != null) {
94 ilm 267
                        elementLabel.setBackgroundColor(Color.ORANGE);
73 ilm 268
                        elementLabel.setToolTip("No field attached to " + item.getId());
269
                    }
270
                }
271
            }
132 ilm 272
 
273
            if (elementEditor != null) {
274
                elementEditor.setWeightX(1);
94 ilm 275
            }
142 ilm 276
 
73 ilm 277
            if (localHint.isSplit()) {
142 ilm 278
                if (currentLine.getTotalGridWidth() > 0) {
73 ilm 279
                    currentLine = new LightUILine();
132 ilm 280
                    panel.addChild(currentLine);
73 ilm 281
                }
282
            }
283
 
284
            if (localHint.isSplit()) {
285
                elementEditor.setGridWidth(4);
286
            } else if (localHint.largeWidth()) {
287
 
288
                if (localHint.showLabel()) {
289
                    elementEditor.setGridWidth(3);
290
                } else {
291
                    elementEditor.setGridWidth(4);
292
                }
293
            } else {
294
                elementEditor.setGridWidth(1);
295
            }
296
            elementEditor.setFillWidth(localHint.fillWidth());
142 ilm 297
            elementEditor.setMarginBottom(4);
132 ilm 298
            currentLine.addChild(elementEditor);
73 ilm 299
 
300
        }
301
    }
302
 
142 ilm 303
    private String getLabelForItem(final SQLField field, final Item item) {
304
        String label = TranslationManager.getInstance().getTranslationForItem(item.getId());
305
 
306
        if (label == null && field != null) {
307
            final RowItemDesc desc = this.configuration.getTranslator().getDescFor(field.getTable(), field.getName());
308
            if (desc != null) {
309
                label = desc.getLabel();
310
            }
311
        }
312
        return label;
313
    }
314
 
132 ilm 315
    private LightUIElement getCustomEditor(final String id) {
73 ilm 316
        final CustomEditorProvider customEditorProvider = this.customEditorProviders.get(id);
317
        if (customEditorProvider != null) {
132 ilm 318
            final LightUIElement element = customEditorProvider.createUIElement(id);
73 ilm 319
            if (element.getId() == null) {
320
                throw new IllegalStateException("Null id for custom editor for id: " + id);
321
            }
322
            return element;
323
        }
324
        return null;
325
    }
326
 
132 ilm 327
    public void putCustomEditorProvider(final String id, final CustomEditorProvider provider) {
73 ilm 328
        this.customEditorProviders.put(id, provider);
329
    }
132 ilm 330
 
331
    public void putAllCustomEditorProvider(final Map<String, CustomEditorProvider> map) {
332
        this.customEditorProviders.putAll(map);
333
    }
73 ilm 334
}