1 package org
.argeo
.gis
.ui
.commands
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
5 import java
.util
.Iterator
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
13 import org
.argeo
.ArgeoException
;
14 import org
.argeo
.eclipse
.ui
.AbstractTreeContentProvider
;
15 import org
.argeo
.eclipse
.ui
.TreeParent
;
16 import org
.argeo
.eclipse
.ui
.dialogs
.Error
;
17 import org
.argeo
.geotools
.jcr
.GeoJcrMapper
;
18 import org
.eclipse
.core
.commands
.AbstractHandler
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.jface
.dialogs
.Dialog
;
22 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.jface
.viewers
.LabelProvider
;
25 import org
.eclipse
.jface
.viewers
.TreeViewer
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Shell
;
33 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
34 import org
.geotools
.data
.FeatureSource
;
35 import org
.opengis
.feature
.simple
.SimpleFeature
;
36 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
38 public class AddFeatureSources
extends AbstractHandler
{
39 private GeoJcrMapper geoJcrMapper
;
40 private Session session
;
42 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
44 FeatureSourceChooserDialog dialog
= new FeatureSourceChooserDialog(
45 HandlerUtil
.getActiveShell(event
));
46 if (dialog
.open() == Dialog
.OK
) {
47 Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>> featureSources
= dialog
49 for (String alias
: featureSources
.keySet()) {
50 for (FeatureSource
<SimpleFeatureType
, SimpleFeature
> fs
: featureSources
52 Node fsNode
= geoJcrMapper
.getFeatureSourceNode(
55 fsNode
.getSession().save();
56 } catch (RepositoryException e
) {
57 throw new ArgeoException("Cannot save " + fsNode
, e
);
62 } catch (Exception e
) {
63 Error
.show("Cannot add new feature source", e
);
68 public void setGeoJcrMapper(GeoJcrMapper geoJcrMapper
) {
69 this.geoJcrMapper
= geoJcrMapper
;
72 public void setSession(Session session
) {
73 this.session
= session
;
76 class FeatureSourceChooserDialog
extends TitleAreaDialog
{
77 private TreeViewer viewer
;
78 private Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>> featureSources
= new HashMap
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>>();
80 public FeatureSourceChooserDialog(Shell parentShell
) {
84 protected Point
getInitialSize() {
85 return new Point(300, 400);
88 protected Control
createDialogArea(Composite parent
) {
89 setTitle("Feature Source");
90 setMessage("Select or or many feature sources to register");
91 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
92 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
93 composite
.setLayout(new GridLayout(1, false));
95 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
97 viewer
= new TreeViewer(composite
);
98 viewer
.getTree().setLayoutData(
99 new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
100 viewer
.setContentProvider(new DataStoreContentProvider());
101 viewer
.setLabelProvider(new DataStoreLabelProvider());
102 viewer
.setInput(geoJcrMapper
.getPossibleFeatureSources());
107 @SuppressWarnings("unchecked")
109 protected void okPressed() {
110 Iterator
<Object
> it
= ((IStructuredSelection
) viewer
.getSelection())
112 while (it
.hasNext()) {
113 Object obj
= it
.next();
114 if (obj
instanceof FeatureSourceNode
) {
115 FeatureSourceNode fsn
= (FeatureSourceNode
) obj
;
116 String alias
= fsn
.getDataStoreAlias();
117 if (!featureSources
.containsKey(alias
))
120 new ArrayList
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>());
121 featureSources
.get(alias
).add(fsn
.getFeatureSource());
124 String alias
= obj
.toString();
125 featureSources
.put(alias
, geoJcrMapper
126 .getPossibleFeatureSources().get(alias
));
132 public Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>> getFeatureSources() {
133 return featureSources
;
137 private class DataStoreContentProvider
extends AbstractTreeContentProvider
{
139 @SuppressWarnings("unchecked")
140 public Object
[] getElements(Object inputElement
) {
141 List
<TreeParent
> dataStoreNodes
= new ArrayList
<TreeParent
>();
142 Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>> featureSources
= (Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>>) inputElement
;
143 for (String alias
: featureSources
.keySet()) {
144 TreeParent dataStoreNode
= new TreeParent(alias
);
145 for (FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
: featureSources
147 dataStoreNode
.addChild(new FeatureSourceNode(alias
,
150 dataStoreNodes
.add(dataStoreNode
);
152 return dataStoreNodes
.toArray();
157 private class DataStoreLabelProvider
extends LabelProvider
{
160 public String
getText(Object element
) {
161 return super.getText(element
);
166 private class FeatureSourceNode
extends TreeParent
{
167 private final String dataStoreAlias
;
168 private final FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
;
170 public FeatureSourceNode(String dataStoreAlias
,
171 FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
) {
172 super(featureSource
.getName().toString());
173 this.dataStoreAlias
= dataStoreAlias
;
174 this.featureSource
= featureSource
;
177 public String
getDataStoreAlias() {
178 return dataStoreAlias
;
181 public FeatureSource
<SimpleFeatureType
, SimpleFeature
> getFeatureSource() {
182 return featureSource
;