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