OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
83 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.erp.action;
15
 
156 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
94 ilm 17
import org.openconcerto.sql.Configuration;
83 ilm 18
import org.openconcerto.sql.PropsConfiguration;
19
import org.openconcerto.sql.element.SQLElement;
94 ilm 20
import org.openconcerto.sql.ui.light.LightUIFrameProvider;
21
import org.openconcerto.sql.users.UserManager;
83 ilm 22
import org.openconcerto.sql.view.list.IListeAction;
23
import org.openconcerto.sql.view.list.RowAction;
24
import org.openconcerto.sql.view.list.SQLTableModelColumn;
25
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
26
import org.openconcerto.ui.light.ActivationOnSelectionControler;
27
import org.openconcerto.ui.light.ColumnSpec;
28
import org.openconcerto.ui.light.ColumnsSpec;
29
import org.openconcerto.ui.light.CustomEditorProvider;
149 ilm 30
import org.openconcerto.ui.light.LightUIButtonWithContext;
83 ilm 31
import org.openconcerto.ui.light.LightUIElement;
94 ilm 32
import org.openconcerto.ui.light.LightUIFrame;
83 ilm 33
import org.openconcerto.ui.light.LightUILine;
94 ilm 34
import org.openconcerto.ui.light.LightUIPanel;
35
import org.openconcerto.ui.light.LightUITable;
83 ilm 36
import org.openconcerto.ui.light.ListToolbarLine;
94 ilm 37
import org.openconcerto.ui.light.RowSelectionSpec;
83 ilm 38
import org.openconcerto.ui.light.TableSpec;
156 ilm 39
import org.openconcerto.utils.StringUtils;
40
import org.openconcerto.utils.i18n.MessageArgs;
41
import org.openconcerto.utils.i18n.NounClass;
42
import org.openconcerto.utils.i18n.TM.MissingMode;
83 ilm 43
import org.openconcerto.utils.i18n.TranslationManager;
44
 
132 ilm 45
import java.util.ArrayList;
46
import java.util.Collection;
47
import java.util.Iterator;
48
import java.util.List;
49
 
156 ilm 50
import javax.swing.JFrame;
51
 
132 ilm 52
import org.jdom2.Document;
53
import org.jdom2.input.DOMBuilder;
54
 
156 ilm 55
public abstract class CreateListFrameAbstractAction<E extends SQLElement, F extends JFrame> extends GenericElementFrameAction<E, F> implements LightUIFrameProvider {
83 ilm 56
 
156 ilm 57
    private static final String TRANSLATION_KEY = "listMenuItem.name";
58
    private static final String[] TRANSLATION_KEY_ARRAY = new String[] { TRANSLATION_KEY };
59
 
60
    private final ComptaPropsConfiguration conf;
61
 
62
    protected CreateListFrameAbstractAction(final ComptaPropsConfiguration conf, final Class<? extends E> clazz) {
174 ilm 63
        this(conf, conf.getDirectory().getElementOfClass(clazz, true));
64
    }
65
 
66
    protected CreateListFrameAbstractAction(final ComptaPropsConfiguration conf, final E elem) {
67
        super(elem);
156 ilm 68
        this.conf = conf;
69
        final NounClass nounClass = this.getElem().getName().getNounClass();
70
        final String[] translationKeys = nounClass == null ? TRANSLATION_KEY_ARRAY : new String[] { TRANSLATION_KEY + '.' + nounClass.getName(), TRANSLATION_KEY };
177 ilm 71
        this.putValue(NAME, StringUtils.firstUp(conf.getERP_TM().translateFirst(MissingMode.STRING, MessageArgs.create("elem", this.getElem().getName()), translationKeys)));
156 ilm 72
    }
73
 
74
    public final ComptaPropsConfiguration getConf() {
75
        return this.conf;
76
    }
77
 
83 ilm 78
    @Override
156 ilm 79
    protected void initFrame(F frame) {
80
        super.initFrame(frame);
81
        CreateFrameAbstractAction.initFrame(frame, this, getConf(), true);
82
    }
83
 
84
    @Override
94 ilm 85
    public LightUIFrame getUIFrame(PropsConfiguration configuration) {
86
        // Get SQLElement
156 ilm 87
        SQLElement element = getElem();
132 ilm 88
        final String elementCode = element.getCode();
89
 
94 ilm 90
        // Title of frame should be the element code with .title
91
        final String frameTitle = TranslationManager.getInstance().getTranslationForItem(elementCode + ".title");
132 ilm 92
 
94 ilm 93
        // Create frame
94
        final LightUIFrame frame = new LightUIFrame(elementCode);
142 ilm 95
        frame.createTitlePanel(frameTitle);
83 ilm 96
 
94 ilm 97
        // Create table
132 ilm 98
        final String tableId = element.getCode() + ".table";
99
        final LightUIElement table = getTableCustomEditorProvider(element).createUIElement(tableId);
94 ilm 100
        table.setFillWidth(true);
132 ilm 101
 
94 ilm 102
        // Get actions associate to the SQLElement and create buttons for them
132 ilm 103
        final Collection<IListeAction> actions = element.getRowActions();
104
 
149 ilm 105
        final LightUIPanel panel = frame.getContentPanel();
132 ilm 106
        final LightUILine l0 = new LightUILine();
107
 
83 ilm 108
        l0.setGridAlignment(LightUILine.ALIGN_LEFT);
132 ilm 109
 
110
        for (final Iterator<IListeAction> iterator = actions.iterator(); iterator.hasNext();) {
83 ilm 111
            RowAction iListeAction = (RowAction) iterator.next();
112
            if (iListeAction.inHeader()) {
149 ilm 113
                LightUIButtonWithContext element2 = new LightUIButtonWithContext(iListeAction.getID());
83 ilm 114
                element2.setValue(iListeAction.getID());
115
 
116
                String label = TranslationManager.getInstance().getTranslationForAction(iListeAction.getID());
117
 
118
                element2.setLabel(label);
119
 
132 ilm 120
                l0.addChild(element2);
83 ilm 121
 
94 ilm 122
                panel.addControler(new ActivationOnSelectionControler(tableId, element2.getId()));
83 ilm 123
 
124
            }
125
        }
132 ilm 126
        panel.addChild(l0);
83 ilm 127
 
132 ilm 128
        final LightUILine l1 = new LightUILine();
83 ilm 129
        l1.setFillHeight(true);
130
        l1.setWeightY(1);
132 ilm 131
        l1.addChild(table);
132
        panel.addChild(l1);
83 ilm 133
 
132 ilm 134
        panel.addChild(new ListToolbarLine());
135
 
136
        frame.dump(System.out, 0);
94 ilm 137
        return frame;
83 ilm 138
    }
139
 
94 ilm 140
    public static CustomEditorProvider getTableCustomEditorProvider(final SQLElement element) {
83 ilm 141
        // generic list of elements
142
        return new CustomEditorProvider() {
143
 
144
            @Override
132 ilm 145
            public LightUIElement createUIElement(final String id) {
94 ilm 146
                final List<String> possibleColumnIds = new ArrayList<String>();
147
                final List<String> sortedIds = new ArrayList<String>();
83 ilm 148
 
94 ilm 149
                final SQLTableModelSourceOnline source = element.getTableSource();
150
                final List<SQLTableModelColumn> columns = source.getColumns();
151
                final List<ColumnSpec> columnsSpec = new ArrayList<ColumnSpec>(columns.size());
83 ilm 152
 
132 ilm 153
                // Get user preferences for this table
94 ilm 154
                final long userId = UserManager.getUserID();
155
                Document columnsPrefs = null;
156
                try {
157
                    final DOMBuilder in = new DOMBuilder();
158
                    final org.w3c.dom.Document w3cDoc = Configuration.getInstance().getXMLConf(userId, id);
159
                    if (w3cDoc != null) {
160
                        columnsPrefs = in.build(w3cDoc);
161
                    }
162
                } catch (Exception ex) {
163
                    throw new IllegalArgumentException("Failed to get ColumnPrefs for table " + id + " and for user " + userId + "\n" + ex.getMessage());
164
                }
83 ilm 165
 
94 ilm 166
                // Create ColumnSpec from the SQLTableModelColumn
167
                final int sqlColumnsCount = columns.size();
132 ilm 168
                for (int i = 0; i < sqlColumnsCount; i++) {
94 ilm 169
                    final SQLTableModelColumn sqlColumn = columns.get(i);
170
                    // TODO : creer la notion d'ID un peu plus dans le l'esprit sales.invoice.amount
171
                    final String columnId = sqlColumn.getIdentifier();
172
                    possibleColumnIds.add(columnId);
173
                    columnsSpec.add(new ColumnSpec(columnId, sqlColumn.getValueClass(), sqlColumn.getName(), null, false, null));
83 ilm 174
                }
132 ilm 175
 
83 ilm 176
                // FIXME : recuperer l'info sauvegardée sur le serveur par user (à coder)
94 ilm 177
                sortedIds.add(columnsSpec.get(0).getId());
83 ilm 178
 
94 ilm 179
                // Create TableSpec
180
                final ColumnsSpec cSpec = new ColumnsSpec(element.getCode(), columnsSpec, possibleColumnIds, sortedIds);
181
                cSpec.setAllowMove(true);
182
                cSpec.setAllowResize(true);
183
                cSpec.setUserPrefs(columnsPrefs);
132 ilm 184
 
94 ilm 185
                final RowSelectionSpec selectionSpec = new RowSelectionSpec(id);
186
                final TableSpec tSpec = new TableSpec(id, selectionSpec, cSpec);
132 ilm 187
 
94 ilm 188
                // Create table
189
                final LightUITable e = new LightUITable(id);
190
                e.setTableSpec(tSpec);
83 ilm 191
 
192
                return e;
193
            }
194
        };
195
    }
196
}