/
CoverageViewEditor.java
211 lines (177 loc) · 8.07 KB
/
CoverageViewEditor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved
* (c) 2014 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.web.data.layer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.geoserver.catalog.CoverageView;
import org.geoserver.catalog.CoverageView.CompositionType;
import org.geoserver.catalog.CoverageView.InputCoverageBand;
import org.geoserver.catalog.CoverageView.CoverageBand;
/**
*
*/
@SuppressWarnings("serial")
public class CoverageViewEditor extends FormComponentPanel<List<String>> {
IModel<List<String>> coverages;
IModel<List<CoverageBand>> outputBands;
List<String> availableCoverages;
List<CoverageBand> currentOutputBands;
ListMultipleChoice<String> coveragesChoice;
CompositionType compositionType;
ListMultipleChoice<CoverageBand> outputBandsChoice;
TextField<String> definition;
DropDownChoice<CompositionType> compositionChoice;
/**
* Creates a new editor.
*
* @param id
* @param The module should return a non null collection of strings.
*/
public CoverageViewEditor(String id, final IModel<List<String>> inputCoverages, final IModel<List<CoverageBand>> bands,
List<String> availableCoverages) {
super(id, inputCoverages);
this.coverages = inputCoverages;
this.outputBands = bands;
this.availableCoverages = availableCoverages;
coveragesChoice = new ListMultipleChoice<String>("coveragesChoice", new Model<>(),
new ArrayList<String>((List<String>) coverages.getObject()),
new ChoiceRenderer<String>() {
@Override
public Object getDisplayValue(String coverage) {
return coverage;
}
});
coveragesChoice.setOutputMarkupId(true);
add(coveragesChoice);
new ArrayList<CoverageBand>();
outputBandsChoice = new ListMultipleChoice<CoverageBand>("outputBandsChoice", new Model<>(),
new ArrayList<CoverageBand>(outputBands.getObject()),
new ChoiceRenderer<CoverageBand>() {
@Override
public Object getDisplayValue(CoverageBand vcb) {
return vcb.getDefinition();
}
});
outputBandsChoice.setOutputMarkupId(true);
add(outputBandsChoice);
currentOutputBands = new ArrayList<CoverageBand>(outputBandsChoice.getChoices());
add(addBandButton());
definition = new TextField<>("definition", new Model<>());
definition.setOutputMarkupId(true);
// TODO: make this parametric on the CompositionType choice
definition.setEnabled(false);
// TODO Uncomment this row when it can be used
//add(definition);
compositionType = CompositionType.getDefault();
compositionChoice = new DropDownChoice<>("compositionType", new PropertyModel<>(this,
"compositionType"), Arrays.asList(CompositionType.BAND_SELECT), new CompositionTypeRenderer());
compositionChoice.setOutputMarkupId(true);
compositionChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
compositionType = compositionChoice.getModelObject();
// TODO Uncomment these rows when they can be used
//definition.setEnabled(compositionType != CompositionType.BAND_SELECT);
//target.add(definition);
}
});
// TODO Uncomment this row when it can be used
//add(compositionChoice);
add(addRemoveAllButton());
add(addRemoveButton());
}
private AjaxButton addBandButton() {
AjaxButton button = new AjaxButton("addBand") {
@Override
public void onSubmit(AjaxRequestTarget target, Form<?> form) {
List<String> selection = (List<String>) coveragesChoice.getModelObject();
compositionType = compositionChoice.getModelObject();
List<CoverageBand> bandsList = new ArrayList<CoverageBand>();
int i = currentOutputBands != null && !currentOutputBands.isEmpty() ? currentOutputBands.size() : 0;
for (Iterator<String> it = selection.iterator(); it.hasNext();) {
String coverage = it.next();
final int bandIndexChar = coverage.indexOf(CoverageView.BAND_SEPARATOR);
String coverageName = coverage;
String bandIndex = "-1";
if (bandIndexChar != -1) {
coverageName = coverage.substring(0, bandIndexChar);
bandIndex = coverage.substring(bandIndexChar + 1, coverage.length());
}
CoverageBand band = new CoverageBand(
Collections.singletonList(new InputCoverageBand(coverageName, bandIndex)),
coverageName, i++, compositionType);
bandsList.add(band);
}
currentOutputBands.addAll(bandsList);
outputBandsChoice.setChoices(currentOutputBands);
outputBandsChoice.modelChanged();
coveragesChoice.setChoices(availableCoverages);
coveragesChoice.modelChanged();
// TODO: Reset choice
target.add(coveragesChoice);
target.add(outputBandsChoice);
}
};
return button;
}
private AjaxButton addRemoveAllButton() {
AjaxButton button = new AjaxButton("removeAllBands") {
@Override
public void onSubmit(AjaxRequestTarget target, Form<?> form) {
List<CoverageBand> outputBands = (List<CoverageBand>) outputBandsChoice
.getModelObject();
outputBands.clear();
currentOutputBands.clear();
outputBandsChoice.setChoices(currentOutputBands);
outputBandsChoice.modelChanged();
// TODO: Reset choice
target.add(outputBandsChoice);
}
};
return button;
}
private AjaxButton addRemoveButton() {
AjaxButton button = new AjaxButton("removeBands") {
@Override
public void onSubmit(AjaxRequestTarget target, Form<?> form) {
List<CoverageBand> removedBands = (List<CoverageBand>) outputBandsChoice
.getModel().getObject();
for (Object band : removedBands) {
currentOutputBands.remove(band);
}
outputBandsChoice.setChoices(currentOutputBands);
outputBandsChoice.modelChanged();
// TODO: Reset choice
target.add(outputBandsChoice);
}
};
return button;
}
private class CompositionTypeRenderer extends ChoiceRenderer<CompositionType> {
public Object getDisplayValue(CompositionType object) {
return object.displayValue();
}
public String getIdValue(CompositionType object, int index) {
return object.toValue();
}
}
}