OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
67 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
67 ilm 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.config;
15
 
16
import org.openconcerto.ui.group.Group;
17
import org.openconcerto.utils.i18n.TranslationManager;
18
 
73 ilm 19
import java.beans.PropertyChangeListener;
20
import java.beans.PropertyChangeSupport;
67 ilm 21
 
22
import javax.swing.Action;
23
 
24
public class MenuManager {
80 ilm 25
    private static MenuManager instance = null;
67 ilm 26
 
182 ilm 27
    public static final void setInstance(final MenuAndActions baseMA) {
28
        setInstance(new MenuManager(baseMA));
80 ilm 29
    }
30
 
182 ilm 31
    public static synchronized final void setInstance(final MenuManager mm) {
32
        instance = mm;
33
    }
34
 
80 ilm 35
    public static synchronized final MenuManager getInstance() {
36
        if (instance == null)
37
            throw new IllegalStateException("Not inited");
67 ilm 38
        return instance;
39
    }
40
 
80 ilm 41
    private MenuAndActions baseMA;
73 ilm 42
    private MenuAndActions menuAndActions;
43
    private Group group;
44
    private final PropertyChangeSupport supp = new PropertyChangeSupport(this);
67 ilm 45
 
80 ilm 46
    public MenuManager(final MenuAndActions baseMA) {
47
        this.baseMA = baseMA;
73 ilm 48
        this.setMenuAndActions(this.createBaseMenuAndActions());
49
        assert this.group != null;
50
    }
67 ilm 51
 
73 ilm 52
    public final Group getGroup() {
53
        return this.group;
67 ilm 54
    }
55
 
73 ilm 56
    // MAYBE remove : only use setMenuAndActions()
67 ilm 57
    public void registerAction(String id, Action a) {
73 ilm 58
        this.menuAndActions.putAction(a, id, true);
59
        this.supp.firePropertyChange("actions", null, null);
67 ilm 60
    }
61
 
62
    public Action getActionForId(String id) {
73 ilm 63
        return this.menuAndActions.getAction(id);
67 ilm 64
    }
65
 
66
    public String getLabelForId(String id) {
67
        return TranslationManager.getInstance().getTranslationForMenu(id);
68
    }
69
 
73 ilm 70
    public final MenuAndActions createBaseMenuAndActions() {
80 ilm 71
        return this.baseMA.copy();
73 ilm 72
    }
67 ilm 73
 
73 ilm 74
    public final MenuAndActions copyMenuAndActions() {
75
        return this.menuAndActions.copy();
76
    }
77
 
78
    public synchronized void setMenuAndActions(MenuAndActions menuAndActions) {
79
        this.menuAndActions = menuAndActions.copy();
80
        this.supp.firePropertyChange("menuAndActions", null, null);
81
        this.supp.firePropertyChange("actions", null, null);
82
 
83
        if (!this.menuAndActions.getGroup().equalsDesc(this.group)) {
84
            final Group oldGroup = this.group;
85
            this.group = this.menuAndActions.getGroup();
86
            this.group.freeze();
87
            this.supp.firePropertyChange("group", oldGroup, this.getGroup());
88
        }
89
    }
90
 
91
    public final void addPropertyChangeListener(final PropertyChangeListener listener) {
92
        this.supp.addPropertyChangeListener(listener);
93
    }
94
 
95
    public final void removePropertyChangeListener(final PropertyChangeListener listener) {
96
        this.supp.removePropertyChangeListener(listener);
97
    }
67 ilm 98
}