Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / DummyNamesTranslator.java @ 2192

History | View | Annotate | Download (3.55 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.tools.namestranslator;
26

    
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.List;
30
import javax.json.Json;
31
import javax.json.JsonArrayBuilder;
32
import javax.json.JsonObject;
33
import javax.json.JsonObjectBuilder;
34

    
35
/**
36
 *
37
 * @author jjdelcerro
38
 */
39
public class DummyNamesTranslator 
40
        extends AbstractNamesTranslator
41
        implements NamesTranslator 
42
  {
43

    
44
  protected ArrayList<String> translatedNames;
45

    
46
  protected DummyNamesTranslator() {
47
    
48
  }
49
  
50
  @Override
51
  protected void build() {
52
    this.translatedNames = new ArrayList<>(sourceNames);
53
    this.sourceNames = Collections.unmodifiableList(this.sourceNames);
54
  }
55

    
56
  @Override
57
  public List<String> getSourceNames() {
58
    return this.sourceNames;
59
  }
60

    
61
  @Override
62
  public List<String> getTranslatedNames() {
63
    return this.translatedNames;
64
  }
65

    
66
  @Override
67
  public String getTranslation(String sourceName) {
68
    int n = this.sourceNames.indexOf(sourceName);
69
    if( n<0 ) {
70
      return null;
71
    }
72
    return this.translatedNames.get(n);
73
  }
74

    
75
  @Override
76
  public String getSource(String name) {
77
    return name;
78
  }
79

    
80
  @Override
81
  public String getSuggestion(String name) {
82
    return name;
83
  }
84

    
85
  @Override
86
  public String getTranslation(int index) {
87
    return this.translatedNames.get(index);
88
  }
89

    
90
  @Override
91
  public int setTranslation(String sourceName, String translatedName) {
92
    int n = this.sourceNames.indexOf(sourceName);
93
    if( n>=0 ) {
94
      this.translatedNames.set(n, translatedName);
95
    }
96
    return -1;
97
  }
98

    
99
  @Override
100
  public JsonObject toJson() {
101
    JsonObjectBuilder builder = Json.createObjectBuilder();
102
    JsonArrayBuilder translatedNamesBuilder = Json.createArrayBuilder();
103
    for (String translatedName : translatedNames) {
104
      translatedNamesBuilder.add(translatedName);
105
    }
106
    JsonArrayBuilder sourceNamesBuilder = Json.createArrayBuilder();
107
    for (String sourceName : sourceNames) {
108
      sourceNamesBuilder.add(sourceName);
109
    }
110
    builder.add("sourceNames", sourceNamesBuilder);
111
    builder.add("translatedNames", translatedNamesBuilder);
112
    return builder.build();
113
  }
114

    
115
  @Override
116
  public void fromJson(JsonObject json) {
117
    this.sourceNames = new ArrayList<>();
118
    this.translatedNames = new ArrayList<>();
119
    for (int i = 0; i < json.getJsonArray("sourceNames").size(); i++) {
120
      String sourceName = json.getJsonArray("sourceNames").get(i).toString();
121
      String translatedName = json.getJsonArray("translatedNames").get(i).toString();
122
      this.sourceNames.add(sourceName);
123
      this.translatedNames.add(translatedName);
124
    }
125
    this.sourceNames = Collections.unmodifiableList(this.sourceNames);
126
  }
127
  
128
}