Improve default app.
[gpl/argeo-suite.git] / environment / org.argeo.geo.ui / src / org / argeo / support / openlayers / OverviewMap.java
1 package org.argeo.support.openlayers;
2
3 import java.util.List;
4
5 import javax.jcr.Node;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.observation.Event;
8 import javax.jcr.observation.EventIterator;
9 import javax.jcr.observation.EventListener;
10 import javax.jcr.query.Query;
11
12 import org.argeo.cms.ui.CmsUiProvider;
13 import org.argeo.cms.ui.util.CmsUiUtils;
14 import org.argeo.entity.EntityType;
15 import org.argeo.jcr.JcrException;
16 import org.argeo.jcr.JcrUtils;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21
22 /** Displays an overview map. */
23 public class OverviewMap implements CmsUiProvider {
24
25         @Override
26         public Control createUi(Composite parent, Node context) throws RepositoryException {
27                 parent.setLayout(new GridLayout());
28                 refreshUi(parent, context);
29
30                 try {
31                         String[] nodeTypes = { EntityType.geopoint.get() };
32                         context.getSession().getWorkspace().getObservationManager().addEventListener(new EventListener() {
33
34                                 @Override
35                                 public void onEvent(EventIterator events) {
36                                         if (!parent.isDisposed())
37                                                 parent.getDisplay().asyncExec(() -> {
38                                                         try {
39                                                                 refreshUi(parent, context);
40                                                         } catch (RepositoryException e) {
41                                                                 throw new JcrException(e);
42                                                         }
43                                                 });
44                                 }
45                         }, Event.PROPERTY_CHANGED | Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED, "/", true, null,
46                                         nodeTypes, false);
47                 } catch (RepositoryException e) {
48                         throw new IllegalStateException("Cannot add JCR observer", e);
49                 }
50
51                 return parent;
52         }
53
54         protected void refreshUi(Composite parent, Node context) throws RepositoryException {
55                 CmsUiUtils.clear(parent);
56                 Query query = context.getSession().getWorkspace().getQueryManager()
57                                 .createQuery("SELECT * FROM [" + EntityType.geopoint.get() + "]", Query.JCR_SQL2);
58                 List<Node> geoPoints = JcrUtils.nodeIteratorToList(query.execute().getNodes());
59                 OpenLayersMap apafMap = new OpenLayersMap(parent, SWT.NONE, getClass().getResource("map-osm.html"));
60                 apafMap.setLayoutData(CmsUiUtils.fillAll());
61
62                 // apafMap.setZoom(7);
63                 // apafMap.setCenter(-2.472, 8.010);
64                 apafMap.addPoints(geoPoints);
65         }
66 }