OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 
156 ilm 16
import org.openconcerto.utils.io.JSONConverter;
17
import org.openconcerto.utils.io.Transferable;
18
 
80 ilm 19
import java.util.ArrayList;
20
import java.util.List;
21
 
94 ilm 22
import net.minidev.json.JSONArray;
23
import net.minidev.json.JSONObject;
24
 
174 ilm 25
public class TableContent implements Transferable {
80 ilm 26
    private static final long serialVersionUID = 3648381615123520834L;
94 ilm 27
    private String tableId;
80 ilm 28
    private List<Row> rows;
29
 
30
    public TableContent() {
31
        // Serialization
32
    }
33
 
94 ilm 34
    public TableContent(final String tableId) {
142 ilm 35
        this.init(tableId, new ArrayList<Row>());
94 ilm 36
    }
37
 
38
    public TableContent(final String tableId, final List<Row> rows) {
39
        this.init(tableId, rows);
40
    }
41
 
42
    public TableContent(final JSONObject json) {
43
        this.fromJSON(json);
44
    }
45
 
142 ilm 46
    private final void init(final String tableId, final List<Row> rows) {
94 ilm 47
        this.tableId = tableId;
142 ilm 48
        this.rows = rows;
94 ilm 49
    }
50
 
142 ilm 51
    public final String getTableId() {
94 ilm 52
        return this.tableId;
53
    }
54
 
142 ilm 55
    public final void setTableId(final String tableId) {
94 ilm 56
        this.tableId = tableId;
57
    }
58
 
142 ilm 59
    public final synchronized Row getRow(final int index) {
60
        return this.rows.get(index);
80 ilm 61
    }
62
 
142 ilm 63
    public final synchronized int getRowsCount() {
64
        return this.rows.size();
65
    }
66
 
67
    public final synchronized boolean addRow(final Row row) {
68
        return this.rows.add(row);
69
    }
70
 
71
    public final synchronized Row setRow(final int index, final Row row) {
72
        return this.rows.set(index, row);
73
    }
74
 
156 ilm 75
    public final synchronized void insertRow(final int index, final Row row) {
76
        this.rows.add(index, row);
77
    }
78
 
142 ilm 79
    public final synchronized Row removeRow(final int index) {
80
        return this.rows.remove(index);
81
    }
82
 
83
    public final synchronized boolean removeRow(final Row row) {
84
        return this.rows.remove(row);
85
    }
86
 
87
    public final synchronized void setRows(List<Row> rows) {
80 ilm 88
        this.rows = rows;
89
    }
90
 
142 ilm 91
    public final synchronized void clearRows() {
92
        this.rows.clear();
93
    }
156 ilm 94
 
142 ilm 95
    /**
96
     * @return a copy of the list
97
     */
156 ilm 98
    public final synchronized List<Row> getRows() {
174 ilm 99
        return new ArrayList<>(this.rows);
142 ilm 100
    }
101
 
94 ilm 102
    @Override
142 ilm 103
    public synchronized String toString() {
104
        return "TableContent of " + this.tableId + " lines count : " + this.getRowsCount();
80 ilm 105
    }
106
 
107
    @Override
142 ilm 108
    public synchronized JSONObject toJSON() {
94 ilm 109
        final JSONObject result = new JSONObject();
110
        result.put("class", "TableContent");
111
        result.put("table-id", this.tableId);
112
        result.put("rows", JSONConverter.getJSON(this.rows));
113
        return result;
80 ilm 114
    }
93 ilm 115
 
116
    @Override
142 ilm 117
    public synchronized void fromJSON(final JSONObject json) {
118
        this.tableId = JSONConverter.getParameterFromJSON(json, "table-id", String.class);
119
        final JSONArray jsonRows = JSONConverter.getParameterFromJSON(json, "rows", JSONArray.class);
94 ilm 120
        if (jsonRows != null) {
174 ilm 121
            final List<Row> listRows = new ArrayList<>();
94 ilm 122
            for (final Object o : jsonRows) {
142 ilm 123
                listRows.add(new Row(JSONConverter.getObjectFromJSON(o, JSONObject.class)));
94 ilm 124
            }
142 ilm 125
            this.setRows(listRows);
94 ilm 126
        }
93 ilm 127
    }
156 ilm 128
 
80 ilm 129
}