/
TrackOrganiser.java
498 lines (429 loc) · 20.9 KB
/
TrackOrganiser.java
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jcan2;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author Micha
*/
public class TrackOrganiser extends javax.swing.JFrame {
private static final boolean TRACK_DEBUG = true;
public static final int GLOBAL = 0;
public static final int PUBLIC = 1;
private final ArrayList<Data> trackList = new ArrayList<>(); //contains all the tracks
private Data currentTrack;
private int idCounter = 0;
/**
* Creates new form TrackOrganiser
*/
public TrackOrganiser() {
initComponents();
//getContentPane().setBackground( Color.blue );
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(trackInfoPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(trackInfoPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(trackInfoPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(trackInfoPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
public void createNewTrack(String name){
currentTrack = new Data(idCounter,name); //create first data layer remove
idCounter++;
trackList.add(currentTrack);
trackChooser.addItem(getDisplayName(currentTrack));
updateTrackChooser();
}
public void createNewTrack(String name, JSONArray gpsData) throws JSONException{
currentTrack = new Data(idCounter,name); //create first data layer remove
idCounter++;
trackList.add(currentTrack);
trackChooser.addItem(getDisplayName(currentTrack));
currentTrack.importJSONList(gpsData);
updateTrackChooser();
}
public void deleteOnServerFlag(){
trackList.get(PUBLIC).removeSendToServerFlag();
}
public void addPublicPoints(JSONArray gpsData) throws JSONException{
trackList.get(PUBLIC).importJSONList(gpsData);
}
public void replaceGlobalData(JSONArray gpsData) throws JSONException{
trackList.get(GLOBAL).deleteAllPoints();
trackList.get(GLOBAL).importJSONListServer(gpsData);
}
private String getDisplayName(Data d){
if(d.getID()>1){
return ((d.getID()-1) + " - " + d.getLayerName());
}
//no numbers for global tracks
else return "-- " + d.getLayerName() + " --";
}
public void setCurrentTrack(Data currentTrack){
this.currentTrack = currentTrack;
BeMapEditor.mainWindow.setTrackField(currentTrack.getLayerName());
updateTrackChooser();
}
public void setGlobal(){
setCurrentTrack(trackList.get(GLOBAL));
}
/**
* Returns the current Data layer (sometimes called track) currently treated
* by MainWindow.
* @return Data layer
*/
public Data getData(){
return currentTrack;
//int track = trackChooser.getSelectedIndex();
//return trackList.get(track);
}
/**
* Updates the track chooser combobox, has to be called every time a data layer
* (sometimes called track) is added or removed.
*/
private void updateTrackChooser(){
int index = trackChooser.getSelectedIndex();
trackChooser.removeAllItems();
mergeComboBox.removeAllItems();
//add them again with the correct names.
Iterator<Data> it = trackList.iterator();
while(it.hasNext())
{
Data d = it.next();
trackChooser.addItem(getDisplayName(d));
mergeComboBox.addItem(getDisplayName(d));
}
//set the correct track layer on the chooser
trackChooser.setSelectedIndex(index);
trackNameField.setText(currentTrack.getLayerName());
}
public void clearTrackList(){
trackList.clear();
}
public void deleteCurrentTrack(){
if(currentTrack.getID()==0||currentTrack.getID()==1){
BeMapEditor.mainWindow.append("\nError: Public tracks cannot be deleted!");
}
else{
trackList.remove(trackChooser.getSelectedIndex());
updateTrackChooser();
BeMapEditor.mainWindow.updateMap();
}
}
public void renameTrack(){
if(currentTrack.getID()==0||currentTrack.getID()==1){
BeMapEditor.mainWindow.append("\nError: The public tracks cannot be deleted!");
}
else{
String newName = trackNameField.getText();
currentTrack.setLayerName(newName);
trackNameField.setText(newName);
updateTrackChooser();
}
}
/**
* Prepares a String containing all the data.
* @return JSONString
* @throws JSONException
*/
public JSONObject prepareJSONtoExport() throws JSONException{
//this will be the object that contains all the data layers.
JSONObject trackArr = new JSONObject();
//loop through all data layers and add them into a JSONArray
Iterator<Data> it = trackList.iterator();
while(it.hasNext())
{
if(TRACK_DEBUG) BeMapEditor.mainWindow.append("\nNew Layer");
Data d = it.next();
//don't export the global layer
if(d.getID()!=0){
trackArr.put(d.getLayerName(),d.exportJSONList());
}
}
JSONObject usrObj = new JSONObject();
usrObj.put(BeMapEditor.mainWindow.getUsrString(), trackArr);
if(TRACK_DEBUG) BeMapEditor.mainWindow.append(usrObj.toString());
return usrObj;
}
/**
*
* @return String containing all the data to be written in the csv file
*/
public String prepareCSVtoExport() throws JSONException {
String csvExport = "Point ID, Device ID, Track ID, Date, Latitude, Longitude, "
+ "Temperature, Humidity, CO sensor RAW, NOx sensor RAW, Road quality, "
+ "CO ppm, NOx ppm\n";
//loop through all data layers and add them into a JSONArray
Iterator<Data> it = trackList.iterator();
while(it.hasNext())
{
if(TRACK_DEBUG) BeMapEditor.mainWindow.append("\nNew Layer");
Data d = it.next();
//don't export the global layer
if(d.getID()!=0){
csvExport += d.exportCSV();
}
}
return csvExport;
}
/**
* Prepares a String containing all the data, for export to server (only
* data from the public layer)
* @return JSONString
* @throws JSONException
*/
public JSONObject JSONExportPublic() throws JSONException{
//this will be the object that contains all the data layers.
JSONObject trackArr = new JSONObject();
Data layer = trackList.get(PUBLIC);
trackArr.put(layer.getLayerName(),layer.exportJSONListServer());
JSONObject usrObj = new JSONObject();
usrObj.put(BeMapEditor.mainWindow.getUsrString(), trackArr);
if(TRACK_DEBUG) BeMapEditor.mainWindow.append(usrObj.toString());
return usrObj;
}
private void duplicateTrack() throws JSONException{
JSONArray currentArray = currentTrack.exportJSONList();
createNewTrack(currentTrack.getLayerName()+" Copy",currentArray);
}
private void mergeTracks() throws JSONException{
Data track1 = trackList.get(mergeComboBox.getSelectedIndex());
if(track1 == currentTrack){
BeMapEditor.mainWindow.append("\nError: Same tracks cannot be merged");
}
else if(track1.getID()==0||currentTrack.getID()==0){
BeMapEditor.mainWindow.append("\nError: You cannot merge the public tracks");
}
else if(track1.getID()==1){
//just copy the points to public points
track1.importJSONList(currentTrack.exportJSONList());
updateTrackChooser();
BeMapEditor.mainWindow.updateMap();
}
else if(currentTrack.getID()==1){
currentTrack.importJSONList(track1.exportJSONList());
updateTrackChooser();
BeMapEditor.mainWindow.updateMap();
}
else{
//merge this track to the current track
currentTrack.importJSONList(track1.exportJSONList());
currentTrack.setLayerName(currentTrack.getLayerName()+" / "+track1.getLayerName());
trackList.remove(mergeComboBox.getSelectedIndex());
updateTrackChooser();
BeMapEditor.mainWindow.updateMap();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
trackChooser = new javax.swing.JComboBox();
newTrackButton = new javax.swing.JButton();
trackNameField = new javax.swing.JTextField();
renameTrackButton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
mergeComboBox = new javax.swing.JComboBox();
mergeButton = new javax.swing.JButton();
closeButton = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("BeMap Track Organiser");
jLabel1.setText("Organise your cycling tracks");
trackChooser.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
trackChooserMouseReleased(evt);
}
});
trackChooser.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
trackChooserPopupMenuWillBecomeInvisible(evt);
}
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
}
});
trackChooser.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
trackChooserComponentShown(evt);
}
});
newTrackButton.setText("new");
newTrackButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newTrackButtonActionPerformed(evt);
}
});
trackNameField.setText("Track");
renameTrackButton.setText("rename");
renameTrackButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
renameTrackButtonActionPerformed(evt);
}
});
deleteButton.setText("delete");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
mergeButton.setText("merge");
mergeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mergeButtonActionPerformed(evt);
}
});
closeButton.setText("done");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});
jButton1.setText("copy");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Lucida Grande", 2, 10)); // NOI18N
jLabel2.setText("Hint: merge to \"my public points\" to share with the community!");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(closeButton)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(mergeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(mergeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(trackNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(trackChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(newTrackButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(renameTrackButton)))))
.addGap(0, 22, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(trackChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addComponent(deleteButton, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(newTrackButton)
.addComponent(trackNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(renameTrackButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mergeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mergeButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
.addComponent(closeButton)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
deleteCurrentTrack();
}//GEN-LAST:event_deleteButtonActionPerformed
private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
this.dispose();
}//GEN-LAST:event_closeButtonActionPerformed
private void trackChooserMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trackChooserMouseReleased
}//GEN-LAST:event_trackChooserMouseReleased
private void trackChooserComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_trackChooserComponentShown
}//GEN-LAST:event_trackChooserComponentShown
private void trackChooserPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_trackChooserPopupMenuWillBecomeInvisible
setCurrentTrack(trackList.get(trackChooser.getSelectedIndex()));
BeMapEditor.mainWindow.updateMap();
}//GEN-LAST:event_trackChooserPopupMenuWillBecomeInvisible
private void renameTrackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameTrackButtonActionPerformed
renameTrack();
}//GEN-LAST:event_renameTrackButtonActionPerformed
private void newTrackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTrackButtonActionPerformed
createNewTrack(trackNameField.getText());
}//GEN-LAST:event_newTrackButtonActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
duplicateTrack();
} catch (JSONException ex) {
Logger.getLogger(TrackOrganiser.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton1ActionPerformed
private void mergeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mergeButtonActionPerformed
try {
mergeTracks();
} catch (JSONException ex) {
Logger.getLogger(TrackOrganiser.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_mergeButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton closeButton;
private javax.swing.JButton deleteButton;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton mergeButton;
private javax.swing.JComboBox mergeComboBox;
private javax.swing.JButton newTrackButton;
private javax.swing.JButton renameTrackButton;
private javax.swing.JComboBox trackChooser;
private javax.swing.JTextField trackNameField;
// End of variables declaration//GEN-END:variables
}