gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.spi / src / main / java / org / gvsig / vectorediting / lib / spi / DefaultEditingServiceParameterOptions.java @ 2870
History | View | Annotate | Download (5 KB)
1 | 2723 | fdiaz | /*
|
---|---|---|---|
2 | * To change this license header, choose License Headers in Project Properties.
|
||
3 | * To change this template file, choose Tools | Templates
|
||
4 | * and open the template in the editor.
|
||
5 | */
|
||
6 | package org.gvsig.vectorediting.lib.spi; |
||
7 | |||
8 | import java.util.ArrayList; |
||
9 | import java.util.Iterator; |
||
10 | import java.util.List; |
||
11 | import java.util.Objects; |
||
12 | import org.apache.commons.lang.ObjectUtils; |
||
13 | import org.apache.commons.lang.StringUtils; |
||
14 | import org.gvsig.compat.CompatLocator; |
||
15 | import org.gvsig.vectorediting.lib.api.EditingServiceParameterOptions; |
||
16 | import org.gvsig.vectorediting.lib.api.EditingServiceParameterOptions.ParameterOption; |
||
17 | import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
||
18 | |||
19 | /**
|
||
20 | *
|
||
21 | * @author fdiaz
|
||
22 | * @param <T>
|
||
23 | */
|
||
24 | public class DefaultEditingServiceParameterOptions implements Iterable<ParameterOption>, EditingServiceParameterOptions{ |
||
25 | |||
26 | private final List<ParameterOption> options; |
||
27 | |||
28 | public DefaultEditingServiceParameterOptions() {
|
||
29 | this.options = new ArrayList<>(); |
||
30 | } |
||
31 | |||
32 | @Override
|
||
33 | public DefaultEditingServiceParameterOptions add(ParameterOption option){
|
||
34 | this.options.add(option);
|
||
35 | return this; |
||
36 | } |
||
37 | |||
38 | @Override
|
||
39 | public DefaultEditingServiceParameterOptions add(String label, Object value, String consoleValue){ |
||
40 | this.options.add(new DefaultParameterOption(label, value, consoleValue)); |
||
41 | return this; |
||
42 | } |
||
43 | |||
44 | @Override
|
||
45 | public Iterator<ParameterOption> iterator() { |
||
46 | options.sort((o1, o2) -> { |
||
47 | return ((DefaultParameterOption)o1).compareTo((DefaultParameterOption)o2);
|
||
48 | }); |
||
49 | return options.iterator();
|
||
50 | } |
||
51 | |||
52 | @Override
|
||
53 | public Object getValue(Object key, Object defaultValue) throws InvalidEntryException { |
||
54 | if(key instanceof String){ |
||
55 | String s = (String)key; |
||
56 | for (ParameterOption option : this.options) { |
||
57 | if(StringUtils.equalsIgnoreCase(s, option.getConsoleValue())){
|
||
58 | return option.getValue();
|
||
59 | } |
||
60 | } |
||
61 | for (ParameterOption option : this.options) { |
||
62 | if(StringUtils.equalsIgnoreCase(s, option.getLabel())){
|
||
63 | return option.getValue();
|
||
64 | } |
||
65 | } |
||
66 | for (ParameterOption option : this.options) { |
||
67 | if(StringUtils.equalsIgnoreCase(s, Objects.toString(option.getValue(), null))){ |
||
68 | return option.getValue();
|
||
69 | } |
||
70 | } |
||
71 | 2870 | fdiaz | if(StringUtils.isNotBlank(s)){
|
72 | for (ParameterOption option : this.options) { |
||
73 | if(StringUtils.startsWithIgnoreCase(option.getConsoleValue(), s)){
|
||
74 | return option.getValue();
|
||
75 | } |
||
76 | 2723 | fdiaz | } |
77 | } |
||
78 | if(isValidValue(defaultValue)){
|
||
79 | return defaultValue;
|
||
80 | } |
||
81 | throw new InvalidEntryException(null); |
||
82 | } else {
|
||
83 | if(isValidValue(key)) {
|
||
84 | return key;
|
||
85 | } else if(isValidValue(defaultValue)) { |
||
86 | return defaultValue;
|
||
87 | } else {
|
||
88 | throw new InvalidEntryException(null); |
||
89 | } |
||
90 | } |
||
91 | // return null;
|
||
92 | } |
||
93 | |||
94 | @Override
|
||
95 | public boolean isValidValue(Object value) { |
||
96 | for (ParameterOption option : this.options) { |
||
97 | if(ObjectUtils.equals(option.getValue(), value)) {
|
||
98 | return true; |
||
99 | } |
||
100 | } |
||
101 | return false; |
||
102 | } |
||
103 | |||
104 | public String getLabel(Object value) { |
||
105 | for (ParameterOption option : this.options) { |
||
106 | if(ObjectUtils.equals(option.getValue(), value)) {
|
||
107 | return option.getLabel();
|
||
108 | } |
||
109 | } |
||
110 | return null; |
||
111 | } |
||
112 | 2870 | fdiaz | |
113 | @Override
|
||
114 | public String getConsoleValue(Object value) { |
||
115 | for (ParameterOption option : this.options) { |
||
116 | if(ObjectUtils.equals(option.getValue(), value)) {
|
||
117 | return option.getConsoleValue();
|
||
118 | } |
||
119 | } |
||
120 | return ""; |
||
121 | } |
||
122 | 2723 | fdiaz | |
123 | |||
124 | public static class DefaultParameterOption<T> implements ParameterOption, Comparable{ |
||
125 | |||
126 | String consoleValue;
|
||
127 | String label;
|
||
128 | Object value;
|
||
129 | |||
130 | public DefaultParameterOption(String label, Object value, String consoleValue) { |
||
131 | this.label = label;
|
||
132 | this.value = value;
|
||
133 | this.consoleValue = consoleValue;
|
||
134 | |||
135 | } |
||
136 | |||
137 | @Override
|
||
138 | public String getLabel() { |
||
139 | return this.label; |
||
140 | } |
||
141 | |||
142 | @Override
|
||
143 | public Object getValue() { |
||
144 | return this.value; |
||
145 | } |
||
146 | |||
147 | @Override
|
||
148 | public String getConsoleValue() { |
||
149 | return this.consoleValue; |
||
150 | } |
||
151 | |||
152 | @Override
|
||
153 | public String toString() { |
||
154 | return this.label; |
||
155 | } |
||
156 | |||
157 | @Override
|
||
158 | public int compareTo(Object o) { |
||
159 | return CompatLocator.getStringUtils().compare(this.getLabel(),((DefaultParameterOption)o).getLabel(), true); |
||
160 | // return this.label.compareToIgnoreCase(((DefaultParameterOption)o).getLabel());
|
||
161 | } |
||
162 | |||
163 | } |
||
164 | |||
165 | } |