OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.ui.light;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.minidev.json.JSONObject;

public class LightUIButton extends LightUIElement {
    protected transient List<ActionListener> clickListeners = new ArrayList<>();

    public LightUIButton() {
        // Serialization
    }

    public LightUIButton(final JSONObject json) {
        super(json);
    }

    public LightUIButton(final String id) {
        super(id);
        this.init(TYPE_BUTTON);
    }

    public LightUIButton(final String id, String text) {
        super(id);
        this.init(TYPE_BUTTON);
        this.setLabel(text);
    }

    protected LightUIButton(final String id, final int buttonType) {
        super(id);
        this.init(buttonType);
    }

    protected LightUIButton(final String id, final int buttonType, String text) {
        super(id);
        this.init(buttonType);
        this.setLabel(text);
    }

    public void setText(String text) {
        this.setLabel(text);
    }

    private void init(final int buttonType) {
        this.setType(buttonType);
    }

    public LightUIButton(final LightUIButton button) {
        super(button);
    }

    public void addClickListener(final ActionListener listener) {
        if (!clickListeners.contains(listener)) {
            this.clickListeners.add(listener);
        }
    }

    public void removeClickListener(final ActionListener listener) {
        this.clickListeners.remove(listener);
    }

    public List<ActionListener> getClickListeners() {
        return Collections.unmodifiableList(this.clickListeners);
    }

    public void fireClick() {
        final List<ActionListener> listeners = new ArrayList<>(this.clickListeners.size());
        listeners.addAll(this.clickListeners);
        // The list is duplicated to avoid ConcurrentModificationException when a listener destroys
        // this button
        for (final ActionListener listener : listeners) {
            listener.actionPerformed(new ActionEvent(this, 1, "click"));
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        this.clickListeners = null;
    }

}