OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | Rev 139 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 74 Rev 86
Line 12... Line 12...
12
import java.util.Collections;
12
import java.util.Collections;
13
import java.util.Comparator;
13
import java.util.Comparator;
14
import java.util.HashSet;
14
import java.util.HashSet;
15
import java.util.LinkedHashSet;
15
import java.util.LinkedHashSet;
16
import java.util.List;
16
import java.util.List;
-
 
17
import java.util.Locale;
17
import java.util.Set;
18
import java.util.Set;
18
import java.util.logging.Level;
19
import java.util.logging.Level;
19
 
20
 
20
import javax.swing.JFrame;
21
import javax.swing.JFrame;
21
import javax.swing.JOptionPane;
22
import javax.swing.JOptionPane;
Line 55... Line 56...
55
import org.openconcerto.sql.model.SQLField;
56
import org.openconcerto.sql.model.SQLField;
56
import org.openconcerto.sql.model.SQLName;
57
import org.openconcerto.sql.model.SQLName;
57
import org.openconcerto.sql.model.SQLRowAccessor;
58
import org.openconcerto.sql.model.SQLRowAccessor;
58
import org.openconcerto.sql.model.SQLRowValues;
59
import org.openconcerto.sql.model.SQLRowValues;
59
import org.openconcerto.sql.model.SQLTable;
60
import org.openconcerto.sql.model.SQLTable;
60
import org.openconcerto.sql.model.graph.Path;
61
import org.openconcerto.sql.model.graph.PathBuilder;
61
import org.openconcerto.sql.request.ListSQLRequest;
62
import org.openconcerto.sql.request.ListSQLRequest;
62
import org.openconcerto.sql.utils.AlterTable;
63
import org.openconcerto.sql.utils.AlterTable;
63
import org.openconcerto.sql.utils.ChangeTable;
64
import org.openconcerto.sql.utils.ChangeTable;
64
import org.openconcerto.sql.utils.SQLCreateTable;
65
import org.openconcerto.sql.utils.SQLCreateTable;
65
import org.openconcerto.sql.view.EditFrame;
66
import org.openconcerto.sql.view.EditFrame;
Line 78... Line 79...
78
import org.openconcerto.ui.group.LayoutHints;
79
import org.openconcerto.ui.group.LayoutHints;
79
import org.openconcerto.ui.group.modifier.AddGroupModifier;
80
import org.openconcerto.ui.group.modifier.AddGroupModifier;
80
import org.openconcerto.ui.group.modifier.AddItemModifier;
81
import org.openconcerto.ui.group.modifier.AddItemModifier;
81
import org.openconcerto.ui.group.modifier.MoveToGroupModifier;
82
import org.openconcerto.ui.group.modifier.MoveToGroupModifier;
82
import org.openconcerto.utils.CollectionUtils;
83
import org.openconcerto.utils.CollectionUtils;
-
 
84
import org.openconcerto.utils.i18n.TranslationManager;
83
 
85
 
84
public class Extension {
86
public class Extension {
85
 
87
 
86
    // Descriptors of the extension
88
    // Descriptors of the extension
87
    private List<ElementDescriptor> elementList = new ArrayList<ElementDescriptor>();
89
    private List<ElementDescriptor> elementList = new ArrayList<ElementDescriptor>();
Line 139... Line 141...
139
 
141
 
140
    public boolean isAutoStart() {
142
    public boolean isAutoStart() {
141
        return autoStart;
143
        return autoStart;
142
    }
144
    }
143
 
145
 
144
    public void start(DBRoot root) throws SQLException {
146
    public void start(DBRoot root, boolean inModuleStart) throws SQLException {
145
 
147
 
146
        // Ensure that database is configured
148
        // Ensure that database is configured
147
        boolean databaseOk = setupDatabase(root);
149
        boolean databaseOk = setupDatabase(root);
148
        if (!databaseOk) {
150
        if (!databaseOk) {
149
            Log.get().severe("Extension " + this.getName() + " not started due to database error");
151
            Log.get().severe("Extension " + this.getName() + " not started due to database error");
150
            return;
152
            return;
151
        }
153
        }
152
        // Register translations
154
        // Register translations
153
 
-
 
-
 
155
        registerTranslations();
154
        // Create menus
156
        // Create menus
-
 
157
        if (!inModuleStart) {
-
 
158
            final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
155
        setupMenu();
159
            registerMenuActions(copy);
-
 
160
            MenuManager.getInstance().setMenuAndActions(copy);
-
 
161
        }
156
        Log.get().info("Extension " + this.getName() + " started");
162
        Log.get().info("Extension " + this.getName() + " started");
157
        this.isStarted = true;
163
        this.isStarted = true;
158
        this.autoStart = true;
164
        this.autoStart = true;
159
        fireChanged();
165
        fireChanged();
160
    }
166
    }
161
 
167
 
162
    private void setupMenu() {
168
    private void registerTranslations() {
-
 
169
        final String locale = Locale.getDefault().toString();
-
 
170
        for (MenuTranslation mTranslation : this.menuTranslations) {
-
 
171
            if (locale.equals(mTranslation.getLocale())) {
-
 
172
                TranslationManager.getInstance().setTranslationForMenu(mTranslation.getId(), mTranslation.getLabel());
-
 
173
            }
-
 
174
        }
-
 
175
        for (ActionTranslation mTranslation : this.actionTranslations) {
-
 
176
            if (locale.equals(mTranslation.getLocale())) {
-
 
177
                TranslationManager.getInstance().setTranslationForAction(mTranslation.getId(), mTranslation.getLabel());
-
 
178
            }
-
 
179
        }
-
 
180
        for (FieldTranslation mTranslation : this.fieldTranslations) {
-
 
181
            if (locale.equals(mTranslation.getLocale())) {
-
 
182
                TranslationManager.getInstance().setTranslationForItem(mTranslation.getTableName() + "." + mTranslation.getFieldName(), mTranslation.getLabel());
-
 
183
            }
-
 
184
        }
-
 
185
        for (TableTranslation mTranslation : this.tableTranslations) {
-
 
186
            if (locale.equals(mTranslation.getLocale())) {
-
 
187
                // FIXME voir avec Sylvain
-
 
188
            }
-
 
189
        }
-
 
190
    }
-
 
191
 
-
 
192
    private void registerMenuActions(MenuAndActions menuAndActions) {
163
        // register actions
193
        // register actions
164
        for (final MenuDescriptor element : getCreateMenuList()) {
194
        for (final MenuDescriptor element : getCreateMenuList()) {
165
            if (element.getType().equals(MenuDescriptor.CREATE)) {
195
            if (element.getType().equals(MenuDescriptor.CREATE)) {
-
 
196
                Log.get().info("Registering action for menu creation id:'" + element.getId() + "'");
-
 
197
 
166
                MenuManager.getInstance().registerAction(element.getId(), new CreateFrameAbstractAction() {
198
                menuAndActions.putAction(new CreateFrameAbstractAction() {
167
 
199
 
168
                    @Override
200
                    @Override
169
                    public JFrame createFrame() {
201
                    public JFrame createFrame() {
170
 
202
 
171
                        JFrame editFrame = new JFrame();
203
                        JFrame editFrame = new JFrame();
172
                        String componentId = element.getComponentId();
204
                        String componentId = element.getComponentId();
-
 
205
                        if (componentId == null) {
-
 
206
                            throw new IllegalStateException("No ComponentId for menu " + element.getId());
-
 
207
                        }
173
                        ComponentDescritor n = getCreateComponentFromId(componentId);
208
                        ComponentDescritor n = getCreateComponentFromId(componentId);
-
 
209
                        if (n == null) {
-
 
210
                            throw new IllegalStateException("No ComponentDescritor for " + componentId);
-
 
211
                        }
174
                        final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
212
                        final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
175
                        if (t == null) {
213
                        if (t == null) {
176
                            throw new IllegalStateException("No table  " + n.getTable());
214
                            throw new IllegalStateException("No table  " + n.getTable());
177
                        }
215
                        }
178
                        final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
216
                        final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
Line 183... Line 221...
183
                        editFrame.pack();
221
                        editFrame.pack();
184
 
222
 
185
                        return editFrame;
223
                        return editFrame;
186
 
224
 
187
                    }
225
                    }
188
                });
226
                }, element.getId(), true);
189
 
227
 
190
            } else if (element.getType().equals(MenuDescriptor.LIST)) {
228
            } else if (element.getType().equals(MenuDescriptor.LIST)) {
191
                MenuManager.getInstance().registerAction(element.getId(), new CreateFrameAbstractAction() {
229
                menuAndActions.putAction(new CreateFrameAbstractAction() {
192
 
230
 
193
                    @Override
231
                    @Override
194
                    public JFrame createFrame() {
232
                    public JFrame createFrame() {
195
                        final String componentId = element.getListId();
233
                        final String componentId = element.getListId();
196
                        final ListDescriptor listDesc = getCreateListFromId(componentId);
234
                        final ListDescriptor listDesc = getCreateListFromId(componentId);
Line 210... Line 248...
210
                            final String[] paths = fieldspath.split(",");
248
                            final String[] paths = fieldspath.split(",");
211
                            final Set<FieldPath> fps = new LinkedHashSet<FieldPath>();
249
                            final Set<FieldPath> fps = new LinkedHashSet<FieldPath>();
212
                            for (int i = 0; i < paths.length; i++) {
250
                            for (int i = 0; i < paths.length; i++) {
213
                                // LOCAL, id_batiment.id_site.nom
251
                                // LOCAL, id_batiment.id_site.nom
214
                                final SQLName name = SQLName.parse(paths[i].trim());
252
                                final SQLName name = SQLName.parse(paths[i].trim());
-
 
253
 
215
                                final Path p = new Path(element.getTable());
254
                                final PathBuilder p = new PathBuilder(element.getTable());
216
                                final int stop = name.getItemCount() - 1;
255
                                final int stop = name.getItemCount() - 1;
217
                                for (int j = 0; j < stop; j++) {
256
                                for (int j = 0; j < stop; j++) {
218
                                    String it = name.getItem(j);
257
                                    String it = name.getItem(j);
219
                                    p.addForeignField(it);
258
                                    p.addForeignField(it);
220
                                }
259
                                }
221
                                final FieldPath fp = new FieldPath(p, name.getName());
260
                                final FieldPath fp = new FieldPath(p.build(), name.getName());
222
                                fps.add(fp);
261
                                fps.add(fp);
223
 
262
 
224
                            }
263
                            }
225
                            cols.add(new BaseSQLTableModelColumn(cDesc.getId(), String.class) {
264
                            cols.add(new BaseSQLTableModelColumn(cDesc.getId(), String.class) {
226
 
265
 
Line 249... Line 288...
249
                        final IListFrame editFrame = new IListFrame(new ListeAddPanel(element, list));
288
                        final IListFrame editFrame = new IListFrame(new ListeAddPanel(element, list));
250
                        editFrame.pack();
289
                        editFrame.pack();
251
                        return editFrame;
290
                        return editFrame;
252
 
291
 
253
                    }
292
                    }
254
                });
293
                }, element.getId(), true);
255
            } else if (element.getType().equals(MenuDescriptor.LIST)) {
294
            } else if (element.getType().equals(MenuDescriptor.LIST)) {
256
                // No action to register
295
                // No action to register
257
            } else {
296
            } else {
258
                Log.get().warning("unknown type " + element.getType());
297
                Log.get().warning("unknown type " + element.getType());
259
            }
298
            }
260
        }
299
        }
261
 
300
 
-
 
301
        // System.err.println("****" + MenuManager.getInstance().getActionForId("test1"));
-
 
302
        //
262
        final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
303
        // final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
263
        // create group
304
        // // create group
264
        final Group group = copy.getGroup();
305
        // final Group group = copy.getGroup();
265
        initMenuGroup(group);
306
        initMenuGroup(menuAndActions.getGroup());
266
        MenuManager.getInstance().setMenuAndActions(copy);
307
        // MenuManager.getInstance().setMenuAndActions(copy);
267
 
-
 
-
 
308
        // System.err.println("*******" + MenuManager.getInstance().getActionForId("test1"));
268
    }
309
    }
269
 
310
 
270
    public void initMenuGroup(final Group group) {
311
    public void initMenuGroup(final Group group) {
271
        for (MenuDescriptor element : getCreateMenuList()) {
312
        for (MenuDescriptor element : getCreateMenuList()) {
272
            if (element.getType().equals(MenuDescriptor.GROUP)) {
313
            if (element.getType().equals(MenuDescriptor.GROUP)) {
Line 458... Line 499...
458
 
499
 
459
    public void stop() {
500
    public void stop() {
460
        this.isStarted = false;
501
        this.isStarted = false;
461
        this.autoStart = false;
502
        this.autoStart = false;
462
        Log.get().info("Extension " + this.getName() + " stopped");
503
        Log.get().info("Extension " + this.getName() + " stopped");
-
 
504
        // TODO : remove menu changes
463
        fireChanged();
505
        fireChanged();
464
    }
506
    }
465
 
507
 
466
    public String getName() {
508
    public String getName() {
467
        return name;
509
        return name;
Line 1329... Line 1371...
1329
    }
1371
    }
1330
 
1372
 
1331
    public void setupMenu(MenuContext ctxt) {
1373
    public void setupMenu(MenuContext ctxt) {
1332
        final Group group = ctxt.getMenuAndActions().getGroup();
1374
        final Group group = ctxt.getMenuAndActions().getGroup();
1333
        initMenuGroup(group);
1375
        initMenuGroup(group);
1334
 
-
 
-
 
1376
        registerMenuActions(ctxt.getMenuAndActions());
1335
    }
1377
    }
1336
 
1378
 
1337
    public void removeRemoveMenuForId(String id) {
1379
    public void removeRemoveMenuForId(String id) {
1338
        for (int i = removeMenuList.size() - 1; i >= 0; i--) {
1380
        for (int i = removeMenuList.size() - 1; i >= 0; i--) {
1339
            final MenuDescriptor m = removeMenuList.get(i);
1381
            final MenuDescriptor m = removeMenuList.get(i);
Line 1384... Line 1426...
1384
                m.setInsertInMenu(parentId);
1426
                m.setInsertInMenu(parentId);
1385
            }
1427
            }
1386
        }
1428
        }
1387
 
1429
 
1388
    }
1430
    }
-
 
1431
 
-
 
1432
    public void setMenuTranslation(String id, String text, Locale locale) {
-
 
1433
        MenuTranslation mTranslation = null;
-
 
1434
        for (MenuTranslation mTr : this.menuTranslations) {
-
 
1435
            if (mTr.getId().equals(id) && mTr.getLocale().equals(locale.toString())) {
-
 
1436
                mTranslation = mTr;
-
 
1437
                break;
-
 
1438
            }
-
 
1439
 
-
 
1440
        }
-
 
1441
        if (mTranslation == null) {
-
 
1442
            mTranslation = new MenuTranslation(locale.toString(), id);
-
 
1443
            this.menuTranslations.add(mTranslation);
-
 
1444
        }
-
 
1445
        mTranslation.setLabel(text);
-
 
1446
 
-
 
1447
    }
1389
}
1448
}