OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
80 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.ui.light;
15
 
132 ilm 16
import java.awt.event.ActionEvent;
17
import java.awt.event.ActionListener;
18
import java.util.ArrayList;
144 ilm 19
import java.util.Collections;
132 ilm 20
import java.util.List;
21
 
22
import net.minidev.json.JSONObject;
23
 
80 ilm 24
public class LightUIButton extends LightUIElement {
156 ilm 25
    protected transient List<ActionListener> clickListeners = new ArrayList<>();
132 ilm 26
 
156 ilm 27
    public LightUIButton() {
28
        // Serialization
29
    }
30
 
132 ilm 31
    public LightUIButton(final JSONObject json) {
32
        super(json);
80 ilm 33
    }
132 ilm 34
 
35
    public LightUIButton(final String id) {
36
        super(id);
37
        this.init(TYPE_BUTTON);
38
    }
39
 
144 ilm 40
    public LightUIButton(final String id, String text) {
41
        super(id);
42
        this.init(TYPE_BUTTON);
43
        this.setLabel(text);
44
    }
45
 
132 ilm 46
    protected LightUIButton(final String id, final int buttonType) {
47
        super(id);
48
        this.init(buttonType);
49
    }
50
 
144 ilm 51
    protected LightUIButton(final String id, final int buttonType, String text) {
52
        super(id);
53
        this.init(buttonType);
54
        this.setLabel(text);
55
    }
56
 
57
    public void setText(String text) {
58
        this.setLabel(text);
59
    }
60
 
132 ilm 61
    private void init(final int buttonType) {
62
        this.setType(buttonType);
63
    }
64
 
65
    public LightUIButton(final LightUIButton button) {
66
        super(button);
67
    }
68
 
69
    public void addClickListener(final ActionListener listener) {
156 ilm 70
        if (!clickListeners.contains(listener)) {
71
            this.clickListeners.add(listener);
72
        }
132 ilm 73
    }
74
 
156 ilm 75
    public void removeClickListener(final ActionListener listener) {
76
        this.clickListeners.remove(listener);
132 ilm 77
    }
78
 
144 ilm 79
    public List<ActionListener> getClickListeners() {
80
        return Collections.unmodifiableList(this.clickListeners);
81
    }
82
 
132 ilm 83
    public void fireClick() {
156 ilm 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
88
        for (final ActionListener listener : listeners) {
132 ilm 89
            listener.actionPerformed(new ActionEvent(this, 1, "click"));
90
        }
91
    }
142 ilm 92
 
93
    @Override
94
    public void destroy() {
95
        super.destroy();
156 ilm 96
        this.clickListeners = null;
142 ilm 97
    }
149 ilm 98
 
80 ilm 99
}