-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathWCSCoveragePanel.java
More file actions
354 lines (302 loc) · 11.5 KB
/
WCSCoveragePanel.java
File metadata and controls
354 lines (302 loc) · 11.5 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (C) 2014 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwindx.examples.util;
import gov.nasa.worldwind.*;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.globes.ElevationModel;
import gov.nasa.worldwind.ogc.wcs.wcs100.*;
import gov.nasa.worldwind.terrain.CompoundElevationModel;
import javax.swing.*;
import javax.swing.border.*;
import javax.xml.stream.XMLStreamException;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.net.*;
import java.util.*;
/**
* @author tag
* @version $Id$
*/
public class WCSCoveragePanel extends JPanel
{
protected static class CoverageInfo
{
protected WCS100Capabilities caps;
protected AVListImpl params = new AVListImpl();
protected String getTitle()
{
return params.getStringValue(AVKey.DISPLAY_NAME);
}
protected String getName()
{
return params.getStringValue(AVKey.COVERAGE_IDENTIFIERS);
}
}
protected WorldWindow wwd;
protected URI serverURI;
protected Dimension size;
protected Thread loadingThread;
protected final TreeSet<CoverageInfo> coverageInfos = new TreeSet<CoverageInfo>(new Comparator<CoverageInfo>()
{
public int compare(CoverageInfo infoA, CoverageInfo infoB)
{
String nameA = infoA.getTitle();
String nameB = infoB.getTitle();
return nameA.compareTo(nameB);
}
});
public WCSCoveragePanel(WorldWindow wwd, String server, Dimension size) throws URISyntaxException
{
super(new BorderLayout());
// See if the server name is a valid URI. Throw an exception if not.
this.serverURI = new URI(server.trim()); // throws an exception if server name is not a valid uri.
this.wwd = wwd;
this.size = size;
this.setPreferredSize(this.size);
this.makeProgressPanel();
// Thread off a retrieval of the server's capabilities document and update of this panel.
this.loadingThread = new Thread(new Runnable()
{
public void run()
{
load();
}
});
this.loadingThread.setPriority(Thread.MIN_PRIORITY);
this.loadingThread.start();
}
public String getServerDisplayString()
{
return this.serverURI.getHost();
}
protected void load()
{
WCS100Capabilities caps;
try
{
caps = WCS100Capabilities.retrieve(this.serverURI);
caps.parse();
}
catch (Exception e)
{
e.printStackTrace();
Container c = WCSCoveragePanel.this.getParent();
if (c != null)
c.remove(WCSCoveragePanel.this);
JOptionPane.showMessageDialog((Component) wwd, "Unable to connect to server " + serverURI.toString(),
"Server Error", JOptionPane.ERROR_MESSAGE);
return;
}
final java.util.List<WCS100CoverageOfferingBrief> coverages = caps.getContentMetadata().getCoverageOfferings();
if (coverages == null)
return;
try
{
for (WCS100CoverageOfferingBrief coverage : coverages)
{
CoverageInfo coverageInfo = this.createCoverageInfo(caps, coverage);
WCSCoveragePanel.this.coverageInfos.add(coverageInfo);
}
}
catch (Exception e)
{
e.printStackTrace();
return;
}
// Fill the panel with the coverage titles.
EventQueue.invokeLater(new Runnable()
{
public void run()
{
WCSCoveragePanel.this.removeAll();
makeCoverageInfosPanel(coverageInfos);
}
});
}
protected void makeCoverageInfosPanel(Collection<CoverageInfo> coverageInfos)
{
// Create the panel holding the coverage names.
JPanel layersPanel = new JPanel(new GridLayout(0, 1, 0, 4));
layersPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add the server's coverages to the panel.
for (CoverageInfo coverageInfo : coverageInfos)
{
addCoverageInfoPanel(layersPanel, WCSCoveragePanel.this.wwd, coverageInfo);
}
// Put the name panel in a scroll bar.
JScrollPane scrollPane = new JScrollPane(layersPanel);
scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
scrollPane.setPreferredSize(size);
// Add the scroll bar and name panel to a titled panel that will resize with the main window.
JPanel westPanel = new JPanel(new GridLayout(0, 1, 0, 10));
westPanel.setBorder(
new CompoundBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9), new TitledBorder("Coverages")));
westPanel.add(scrollPane);
this.add(westPanel, BorderLayout.CENTER);
this.revalidate();
}
protected void addCoverageInfoPanel(JPanel coveragesPanel, WorldWindow wwd, CoverageInfo coverageInfo)
{
// Give a coverage a button and label and add it to the names panel.
CoverageInfoAction action = new CoverageInfoAction(coverageInfo, wwd);
JCheckBox jcb = new JCheckBox(action);
jcb.setSelected(false);
coveragesPanel.add(jcb);
}
protected class CoverageInfoAction extends AbstractAction
{
protected WorldWindow wwd;
protected CoverageInfo coverageInfo;
protected Object component;
public CoverageInfoAction(CoverageInfo info, WorldWindow wwd)
{
super(info.getTitle());
// Capture info we'll need later to control the coverage.
this.wwd = wwd;
this.coverageInfo = info;
}
public void actionPerformed(ActionEvent actionEvent)
{
// If the coverage is selected, add it to the WorldWindow's current model, else remove it from the model.
if (((JCheckBox) actionEvent.getSource()).isSelected())
{
if (this.component == null) {
this.component = createComponent(coverageInfo.caps, coverageInfo);
if (this.component == null) return;
}
updateComponent(this.component, true);
}
else
{
if (this.component != null)
updateComponent(this.component, false);
}
// Tell the WorldWindow to update.
wwd.redraw();
}
}
protected CoverageInfo createCoverageInfo(WCS100Capabilities caps, WCS100CoverageOfferingBrief coverage)
{
// Create the layer info specified by the coverage capabilities.
CoverageInfo info = new CoverageInfo();
info.caps = caps;
info.params = new AVListImpl();
info.params.setValue(AVKey.COVERAGE_IDENTIFIERS, coverage.getName());
info.params.setValue(AVKey.DISPLAY_NAME, coverage.getLabel());
return info;
}
protected void updateComponent(Object component, boolean enable)
{
ElevationModel model = (ElevationModel) component;
CompoundElevationModel compoundModel;
// Guarantee that we have a compound elevation model, so additional elevation models can be added.
ElevationModel em = this.wwd.getModel().getGlobe().getElevationModel();
if (!(em instanceof CompoundElevationModel)) {
compoundModel = new CompoundElevationModel();
compoundModel.addElevationModel(em);
this.wwd.getModel().getGlobe().setElevationModel(compoundModel);
} else {
compoundModel = (CompoundElevationModel) em;
}
if (enable)
{
if (!compoundModel.getElevationModels().contains(model))
compoundModel.addElevationModel(model);
}
else
{
compoundModel.removeElevationModel(model);
}
wwd.firePropertyChange(new PropertyChangeEvent(wwd, AVKey.ELEVATION_MODEL, null, compoundModel));
}
protected static Object createComponent(WCS100Capabilities caps, CoverageInfo coverageInfo)
{
AVList configParams = coverageInfo.params.copy(); // Copy to insulate changes from the caller.
// Some wcs servers are slow, so increase the timeouts and limits used by WorldWind's retrievers.
configParams.setValue(AVKey.URL_CONNECT_TIMEOUT, 30000);
configParams.setValue(AVKey.URL_READ_TIMEOUT, 30000);
configParams.setValue(AVKey.RETRIEVAL_QUEUE_STALE_REQUEST_LIMIT, 60000);
try
{
String describeCoverageUrlString = caps.getCapability().getGetOperationAddress("DescribeCoverage");
URI uri = new URI(describeCoverageUrlString);
WCS100DescribeCoverage coverageDescription = WCS100DescribeCoverage.retrieve(uri, coverageInfo.getName());
coverageDescription.parse();
configParams.setValue(AVKey.DOCUMENT, coverageDescription);
}
catch (URISyntaxException e)
{
e.printStackTrace();
return null;
}
catch (XMLStreamException e)
{
e.printStackTrace();
return null;
}
try
{
Factory factory = (Factory) WorldWind.createConfigurationComponent(AVKey.ELEVATION_MODEL_FACTORY);
return factory.createFromConfigSource(caps, configParams);
}
catch (Exception e)
{
// Ignore the exception, and just return null.
}
return null;
}
protected void makeProgressPanel()
{
// Create the panel holding the progress bar during loading.
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
outerPanel.setPreferredSize(this.size);
JPanel innerPanel = new JPanel(new BorderLayout());
innerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
innerPanel.add(progressBar, BorderLayout.CENTER);
JButton cancelButton = new JButton("Cancel");
innerPanel.add(cancelButton, BorderLayout.EAST);
cancelButton.addActionListener(new AbstractAction()
{
public void actionPerformed(ActionEvent actionEvent)
{
if (loadingThread.isAlive())
loadingThread.interrupt();
Container c = WCSCoveragePanel.this.getParent();
c.remove(WCSCoveragePanel.this);
}
});
outerPanel.add(innerPanel, BorderLayout.NORTH);
this.add(outerPanel, BorderLayout.CENTER);
this.revalidate();
}
public static void main(String[] args)
{
try
{
final JFrame controlFrame = new JFrame();
controlFrame.getContentPane().add(new WCSCoveragePanel(null, "https://worldwind26.arc.nasa.gov/wcs?",
new Dimension(400, 600)));
controlFrame.pack();
controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlFrame.setVisible(true);
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
controlFrame.setVisible(true);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}