OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 156
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ui.light;
14
 package org.openconcerto.ui.light;
15
 
15
 
16
import java.awt.event.ActionEvent;
16
import java.awt.event.ActionEvent;
17
import java.awt.event.ActionListener;
17
import java.awt.event.ActionListener;
18
import java.util.ArrayList;
18
import java.util.ArrayList;
19
import java.util.Collections;
19
import java.util.Collections;
20
import java.util.List;
20
import java.util.List;
21
 
21
 
22
import net.minidev.json.JSONObject;
22
import net.minidev.json.JSONObject;
23
 
23
 
24
public class LightUIButton extends LightUIElement {
24
public class LightUIButton extends LightUIElement {
25
    private transient List<ActionListener> clickListeners = new ArrayList<ActionListener>();
25
    protected transient List<ActionListener> clickListeners = new ArrayList<>();
-
 
26
 
-
 
27
    public LightUIButton() {
-
 
28
        // Serialization
-
 
29
    }
26
 
30
 
27
    public LightUIButton(final JSONObject json) {
31
    public LightUIButton(final JSONObject json) {
28
        super(json);
32
        super(json);
29
    }
33
    }
30
 
34
 
31
    public LightUIButton(final String id) {
35
    public LightUIButton(final String id) {
32
        super(id);
36
        super(id);
33
        this.init(TYPE_BUTTON);
37
        this.init(TYPE_BUTTON);
34
    }
38
    }
35
 
39
 
36
    public LightUIButton(final String id, String text) {
40
    public LightUIButton(final String id, String text) {
37
        super(id);
41
        super(id);
38
        this.init(TYPE_BUTTON);
42
        this.init(TYPE_BUTTON);
39
        this.setLabel(text);
43
        this.setLabel(text);
40
    }
44
    }
41
 
45
 
42
    protected LightUIButton(final String id, final int buttonType) {
46
    protected LightUIButton(final String id, final int buttonType) {
43
        super(id);
47
        super(id);
44
        this.init(buttonType);
48
        this.init(buttonType);
45
    }
49
    }
46
 
50
 
47
    protected LightUIButton(final String id, final int buttonType, String text) {
51
    protected LightUIButton(final String id, final int buttonType, String text) {
48
        super(id);
52
        super(id);
49
        this.init(buttonType);
53
        this.init(buttonType);
50
        this.setLabel(text);
54
        this.setLabel(text);
51
    }
55
    }
52
 
56
 
53
    public void setText(String text) {
57
    public void setText(String text) {
54
        this.setLabel(text);
58
        this.setLabel(text);
55
    }
59
    }
56
 
60
 
57
    private void init(final int buttonType) {
61
    private void init(final int buttonType) {
58
        this.setType(buttonType);
62
        this.setType(buttonType);
59
    }
63
    }
60
 
64
 
61
    public LightUIButton(final LightUIButton button) {
65
    public LightUIButton(final LightUIButton button) {
62
        super(button);
66
        super(button);
63
    }
67
    }
64
 
68
 
65
    public void addClickListener(final ActionListener listener) {
69
    public void addClickListener(final ActionListener listener) {
-
 
70
        if (!clickListeners.contains(listener)) {
66
        this.clickListeners.add(listener);
71
            this.clickListeners.add(listener);
67
    }
72
        }
-
 
73
    }
68
 
74
 
69
    public void removeClickListeners() {
75
    public void removeClickListener(final ActionListener listener) {
70
        this.clickListeners.clear();
76
        this.clickListeners.remove(listener);
71
    }
77
    }
72
 
78
 
73
    public List<ActionListener> getClickListeners() {
79
    public List<ActionListener> getClickListeners() {
74
        return Collections.unmodifiableList(this.clickListeners);
80
        return Collections.unmodifiableList(this.clickListeners);
75
    }
81
    }
76
 
82
 
77
    public void fireClick() {
83
    public void fireClick() {
-
 
84
        final List<ActionListener> listeners = new ArrayList<>(this.clickListeners.size());
-
 
85
        listeners.addAll(this.clickListeners);
-
 
86
        // The list is duplicated to avoid ConcurrentModificationException when a listener destroys
-
 
87
        // this button
78
        for (final ActionListener listener : this.clickListeners) {
88
        for (final ActionListener listener : listeners) {
79
            listener.actionPerformed(new ActionEvent(this, 1, "click"));
89
            listener.actionPerformed(new ActionEvent(this, 1, "click"));
80
        }
90
        }
81
    }
91
    }
82
 
92
 
83
    @Override
93
    @Override
84
    public void destroy() {
94
    public void destroy() {
85
        super.destroy();
95
        super.destroy();
86
        this.clickListeners.clear();
96
        this.clickListeners = null;
87
    }
97
    }
88
 
98
 
89
    @Override
-
 
90
    public JSONToLightUIConvertor getConvertor() {
-
 
91
        return new JSONToLightUIConvertor() {
-
 
92
            @Override
-
 
93
            public LightUIElement convert(JSONObject json) {
-
 
94
                return new LightUIButton(json);
-
 
95
            }
-
 
96
        };
-
 
97
    }
-
 
98
}
99
}