OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Rev 156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 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
 
142 ilm 16
import org.openconcerto.utils.CollectionUtils;
132 ilm 17
import org.openconcerto.utils.io.JSONConverter;
18
 
144 ilm 19
import java.util.Collections;
132 ilm 20
import java.util.HashMap;
21
import java.util.Map;
22
 
94 ilm 23
import net.minidev.json.JSONObject;
24
 
25
public class LightUIButtonLink extends LightUIElement {
144 ilm 26
    private static final String OPEN_NEW_FRAME_KEY = "open-new-frame";
27
    private static final String URL_PARAMETER_KEY = "url-parameter";
132 ilm 28
 
29
    private Boolean openNewFrame = false;
30
    private Map<String, String> urlParameters = new HashMap<String, String>();
31
 
32
    public LightUIButtonLink(final String id, final String filePath, final boolean openNewFrame) {
33
        super(id);
34
        this.setType(LightUIElement.TYPE_BUTTON_LINK);
35
        this.setValue(filePath);
36
        this.setOpenNewFrame(openNewFrame);
37
    }
38
 
94 ilm 39
    public LightUIButtonLink(final JSONObject json) {
132 ilm 40
        super(json);
94 ilm 41
    }
42
 
132 ilm 43
    public LightUIButtonLink(final LightUIButtonLink button) {
44
        super(button);
94 ilm 45
    }
132 ilm 46
 
47
    public Boolean isOpenNewFrame() {
48
        return this.openNewFrame;
49
    }
50
 
51
    public void setOpenNewFrame(final Boolean openNewFrame) {
52
        this.openNewFrame = openNewFrame;
53
    }
54
 
55
    public void addUrlParameter(final String key, final String value) {
56
        this.urlParameters.put(key, value);
57
    }
142 ilm 58
 
132 ilm 59
    public void removeUrlParameter(final String key) {
60
        this.urlParameters.remove(key);
61
    }
142 ilm 62
 
132 ilm 63
    public void clearUrlParameter() {
64
        this.urlParameters.clear();
65
    }
66
 
144 ilm 67
    public Map<String, String> getUrlParameters() {
68
        return Collections.unmodifiableMap(urlParameters);
132 ilm 69
    }
70
 
71
    @Override
72
    protected void copy(LightUIElement element) {
73
        super.copy(element);
74
 
75
        if (!(element instanceof LightUIButtonLink)) {
76
            throw new InvalidClassException(LightUIButtonLink.class.getName(), element.getClassName(), element.getId());
77
        }
78
        final LightUIButtonLink button = (LightUIButtonLink) element;
79
        this.openNewFrame = button.openNewFrame;
80
        this.urlParameters = button.urlParameters;
81
    }
82
 
83
    @Override
84
    public JSONToLightUIConvertor getConvertor() {
85
        return new JSONToLightUIConvertor() {
86
            @Override
87
            public LightUIElement convert(final JSONObject json) {
88
                return new LightUIButtonLink(json);
89
            }
90
        };
91
    }
92
 
93
    @Override
94
    public JSONObject toJSON() {
95
        final JSONObject json = super.toJSON();
96
 
97
        if (this.openNewFrame) {
98
            json.put(OPEN_NEW_FRAME_KEY, true);
99
        }
100
        if (!this.urlParameters.isEmpty()) {
101
            json.put(URL_PARAMETER_KEY, this.urlParameters);
102
        }
103
 
104
        return json;
105
    }
106
 
107
    @Override
108
    public void fromJSON(JSONObject json) {
109
        super.fromJSON(json);
110
        this.openNewFrame = JSONConverter.getParameterFromJSON(json, OPEN_NEW_FRAME_KEY, Boolean.class, false);
142 ilm 111
        this.urlParameters = CollectionUtils.castMap(JSONConverter.getParameterFromJSON(json, URL_PARAMETER_KEY, Map.class), String.class, String.class);
132 ilm 112
    }
94 ilm 113
}