33e888f7e5d740d4e253ce2d423de2006c8f04a1
[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.api.NodeConstants;
13 import org.argeo.cms.auth.CurrentUser;
14 import org.argeo.cms.ui.CmsUiProvider;
15 import org.argeo.cms.ui.CmsView;
16 import org.argeo.cms.ui.util.CmsUiUtils;
17 import org.argeo.entity.EntityType;
18 import org.argeo.jcr.JcrException;
19 import org.argeo.jcr.JcrUtils;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24
25 /** Displays an overview map. */
26 public class OverviewMap implements CmsUiProvider {
27
28         @Override
29         public Control createUi(Composite parent, Node context) throws RepositoryException {
30                 parent.setLayout(new GridLayout());
31                 refreshUi(parent, context);
32
33                 try {
34                         String[] nodeTypes = { EntityType.geopoint.get() };
35                         context.getSession().getWorkspace().getObservationManager().addEventListener(new EventListener() {
36
37                                 @Override
38                                 public void onEvent(EventIterator events) {
39                                         if (!parent.isDisposed())
40                                                 parent.getDisplay().asyncExec(() -> {
41                                                         try {
42                                                                 refreshUi(parent, context);
43                                                         } catch (RepositoryException e) {
44                                                                 throw new JcrException(e);
45                                                         }
46                                                 });
47                                 }
48                         }, Event.PROPERTY_CHANGED | Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED,
49                                         "/", true, null, nodeTypes, false);
50                 } catch (RepositoryException e) {
51                         throw new IllegalStateException("Cannot add JCR observer", e);
52                 }
53
54                 return parent;
55         }
56
57         protected void refreshUi(Composite parent, Node context) throws RepositoryException {
58                 CmsUiUtils.clear(parent);
59                 boolean coworker = CmsView.getCmsView(parent).doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
60                 Query query = context.getSession().getWorkspace().getQueryManager()
61                                 .createQuery("SELECT * FROM [" + EntityType.local.get() + "] WHERE [entity:type]='"
62                                                 + EntityType.geopoint.get() + "'",
63                                                 Query.JCR_SQL2);
64                 List<Node> geoPoints = JcrUtils.nodeIteratorToList(query.execute().getNodes());
65                 OpenLayersMap apafMap = new OpenLayersMap(parent, SWT.NONE, getClass().getResource("map-osm.html"));
66                 apafMap.setLayoutData(CmsUiUtils.fillAll());
67
68                 // apafMap.setZoom(7);
69                 // apafMap.setCenter(-2.472, 8.010);
70                 apafMap.addPoints(geoPoints);
71         }
72 }