]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server.ui/src/org/simantics/maps/server/ui/prefs/MapsServerPreferencePage.java
Adding pkg-precompiled tileserver-mapnik to avoid npm install
[simantics/district.git] / org.simantics.maps.server.ui / src / org / simantics / maps / server / ui / prefs / MapsServerPreferencePage.java
1 package org.simantics.maps.server.ui.prefs;
2
3 import java.io.IOException;
4 import java.net.URISyntaxException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.util.List;
9
10 import org.eclipse.core.runtime.preferences.InstanceScope;
11 import org.eclipse.jface.layout.GridDataFactory;
12 import org.eclipse.jface.layout.GridLayoutFactory;
13 import org.eclipse.jface.preference.BooleanFieldEditor;
14 import org.eclipse.jface.preference.ComboFieldEditor;
15 import org.eclipse.jface.preference.FieldEditorPreferencePage;
16 import org.eclipse.jface.preference.FileFieldEditor;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.preference.IntegerFieldEditor;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Group;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.IWorkbenchPreferencePage;
30 import org.eclipse.ui.preferences.ScopedPreferenceStore;
31 import org.simantics.district.maps.server.TileserverMapnik;
32 import org.simantics.district.maps.server.TileserverMapnikInstance;
33 import org.simantics.district.maps.server.prefs.MapsServerPreferences;
34 import org.simantics.maps.server.ui.TileserverMapnikStartJob;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class MapsServerPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(MapsServerPreferencePage.class);
41     
42     private TileserverMapnik server;
43
44     private FileFieldEditor addNew;
45
46     public MapsServerPreferencePage() {
47         super(GRID);
48         setDescription("Maps server preferences");
49     }
50     
51     @Override
52     public void init(IWorkbench workbench) {
53         try {
54             server = TileserverMapnikInstance.get();
55         } catch (IOException | URISyntaxException e) {
56             LOGGER.error("Could not initialize " + getClass(), e);
57         }
58     }
59
60     @Override
61     protected IPreferenceStore doGetPreferenceStore() {
62         return new ScopedPreferenceStore(InstanceScope.INSTANCE, MapsServerPreferences.P_NODE);
63     }
64
65     private void createServerStatusField(Composite parent) {
66         Label label = new Label(parent, SWT.NONE);
67
68         Button b = new Button(parent, SWT.NONE);
69         updateWidgets(label, b);
70         b.addSelectionListener(new SelectionAdapter() {
71             
72             @Override
73             public void widgetSelected(SelectionEvent e) {
74                 try {
75                     if (server.isRunning()) {
76                         server.stop();
77                         updateWidgets(label, b);
78                     } else {
79                         new TileserverMapnikStartJob().schedule();
80                     }
81                 } catch (Exception ex) {
82                     LOGGER.error("Could not start/stop server", ex);
83                 }
84             }
85         });
86     }
87     
88     private void updateWidgets(Label label, Button b) {
89         String labelText = "";
90         String buttonText = "";
91         int labelColor = -1;
92         boolean running = false;
93         try {
94             if (server.isRunning()) {
95                 running = true;
96                 buttonText = "Stop";
97                 labelText = "running";
98                 labelColor = SWT.COLOR_DARK_GREEN;
99             } else {
100                 labelText = "stopped";
101                 buttonText = "Start";
102             }
103         } catch (IOException | InterruptedException e) {
104             LOGGER.error("Could not create server status field!", e);
105             labelText = "unknown";
106             labelColor = SWT.COLOR_DARK_RED;
107         }
108         label.setText("Tileserver is currently " + labelText);
109         label.setForeground(getShell().getDisplay().getSystemColor(labelColor));
110         
111         b.setText(buttonText + " server");
112     }
113     
114     private void createGeneralGroup() {
115         Group serverGroup = new Group(getFieldEditorParent(), SWT.NONE);
116         serverGroup.setText("General");
117         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(serverGroup);
118         
119         createServerStatusField(serverGroup);
120
121         IntegerFieldEditor port = new IntegerFieldEditor(MapsServerPreferences.P_DEFAULT_PORT, "Tileserver port", serverGroup);
122         addField(port);
123
124         BooleanFieldEditor automatically = new BooleanFieldEditor(MapsServerPreferences.P_START_AUTOMATICALLY, "Start tileserver automatically", serverGroup);
125         addField(automatically);
126         
127         Button openInExplorer = new Button(serverGroup, SWT.NONE);
128         openInExplorer.setText("Open server folder");
129         openInExplorer.addSelectionListener(new SelectionAdapter() {
130             
131             @Override
132             public void widgetSelected(SelectionEvent e) {
133 //                E4WorkbenchUtils.showInSystemExplorer("");
134             }
135         });
136         
137         GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(serverGroup);
138     }
139     
140     @Override
141     protected void createFieldEditors() {
142         createGeneralGroup();
143         
144         createStylesGroup();
145         
146         createTilesGroup();
147     }
148
149     private void createStylesGroup() {
150         Group stylesGroup = new Group(getFieldEditorParent(), SWT.NONE);
151         stylesGroup.setText("Styles");
152         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(stylesGroup);
153         
154         createTM2StylesField(stylesGroup);
155         
156         GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(stylesGroup);
157     }
158     
159     private void createTM2StylesField(Composite parent) {
160         try {
161             List<String> styles = server.listStyles();
162             
163             String[][] namesAndValues = new String[styles.size()][];
164             for (int i = 0; i < styles.size(); i++) {
165                 String style = styles.get(i);
166                 String[] nameAndValue = new String[2];
167                 nameAndValue[0] = style;
168                 nameAndValue[1] = style;
169                 namesAndValues[i] = nameAndValue;
170             }
171             
172             ComboFieldEditor selector = new ComboFieldEditor(MapsServerPreferences.P_CURRENT_TM2STYLE, "Tile Styles", namesAndValues, parent);
173             addField(selector);
174         } catch (IOException e) {
175             e.printStackTrace();
176         }
177     }
178     
179     private void createTilesGroup() {
180         Group tilesGroup = new Group(getFieldEditorParent(), SWT.NONE);
181         tilesGroup.setText("MBTiles");
182         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(tilesGroup);
183         
184         createTilesField(tilesGroup);
185         
186         GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(tilesGroup);
187     }
188
189     private void createTilesField(Composite parent) {
190         try {
191             List<String> tiles = server.availableMBTiles();
192             
193             String[][] namesAndValues = new String[tiles.size()][];
194             for (int i = 0; i < tiles.size(); i++) {
195                 String style = tiles.get(i);
196                 String[] nameAndValue = new String[2];
197                 nameAndValue[0] = style;
198                 nameAndValue[1] = style;
199                 namesAndValues[i] = nameAndValue;
200             }
201             
202             ComboFieldEditor selector = new ComboFieldEditor(MapsServerPreferences.P_CURRENT_MBTILES, "MBTiles", namesAndValues, parent);
203             addField(selector);
204             
205             addNew = new FileFieldEditor("Add new tiles", "Add new tiles", parent);
206             addNew.setPropertyChangeListener(new IPropertyChangeListener() {
207                 
208                 @Override
209                 public void propertyChange(PropertyChangeEvent event) {
210                     System.out.println(event);
211                 }
212             });
213             
214         } catch (IOException e) {
215             LOGGER.error("Could not create tiles field", e);
216         }
217     }
218     
219     @Override
220     protected void performApply() {
221         // Upload new mbtiles file
222         String fileLocation = addNew.getStringValue();
223         if (fileLocation != null && !fileLocation.isEmpty()) {
224             Path p = Paths.get(fileLocation);
225             Path target = server.getDataDirectory().resolve(p.getFileName());
226             try {
227                 Files.copy(p, target);
228             } catch (IOException e) {
229                 String message = "Could not upload " + fileLocation + " to " + target.toAbsolutePath(); 
230                 LOGGER.error(message, e);
231                 setErrorMessage(message);
232             }
233         }
234         super.performApply();
235     }
236     
237     @Override
238     public boolean performOk() {
239         boolean success = super.performOk();
240         
241         try {
242             server.stop();
243             new TileserverMapnikStartJob().schedule();
244         } catch (Exception e) {
245             LOGGER.error("Could not restart tileserver-mapnik", e);
246         }
247         
248         return success;
249     }
250
251 }