OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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