-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPamSettingManager.java
More file actions
2439 lines (2187 loc) · 76.7 KB
/
PamSettingManager.java
File metadata and controls
2439 lines (2187 loc) · 76.7 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* PAMGUARD - Passive Acoustic Monitoring GUARDianship.
* To assist in the Detection Classification and Localisation
* of marine mammals (cetaceans).
*
* Copyright (C) 2006
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package PamController;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import PamController.settings.SettingsNameChange;
import PamController.settings.SettingsNameChanger;
import PamUtils.PamCalendar;
import PamUtils.PamFileChooser;
import PamUtils.PamFileFilter;
import PamView.dialog.warn.WarnOnce;
import generalDatabase.DBControl;
import generalDatabase.DBControlSettings;
import javafx.scene.control.Alert.AlertType;
import pamViewFX.fxNodes.utilsFX.PamUtilsFX;
import pamViewFX.fxSettingsPanes.SettingsFileDialogFX;
import pamguard.GlobalArguments;
import pamguard.LogFileUtils;
import pamguard.Pamguard;
//XMLSettings
//import org.jdom.Document;
//import org.jdom.Element;
//import org.jdom.JDOMException;
//import org.jdom.input.SAXBuilder;
//import org.jdom.output.XMLOutputter;
//import org.w3c.dom.Node;
//import com.thoughtworks.xstream.XStream;
//import javax.xml.transform.OutputKeys;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerException;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
//import sun.jdbc.odbc.OdbcDef;
import tipOfTheDay.TipOfTheDayManager;
//import javax.swing.filechooser.FileFilter;
//import javax.swing.filechooser.FileNameExtensionFilter;
//import PamUtils.PamFileFilter;
/**
* @author Doug Gillespie
*
* Singleton class for managing Pam settings - where and how they are stored in
* a persistent way between runs.
*
* Any class that wants is settings saved should register with the
* PamSettingsManager.
* <p>
* When the GUI closes, SaveSettings is called, SaveSettings goes through the
* list of registered objects and asks each one to give it a reference to an
* Object containing the settings (this MUST implement serialisable). This can
* be the object itself, but will more likely be a reference to another object
* just containing settings parameters. The class implementing PamSettings must
* also provide functions getUnitType, getUnitName and getSettingsVersion. These
* four pieces of information are then bundled into a PamControlledUnitSettings
* which is added to an array list which is then stored in a serialised file.
* <p>
* When PAMGUARD starts, after all detectors have been created, the serialised
* file is reopened. Each PamControlledUnitSettings is taken in turn and
* compared with the list of registered objects to find one with the same name,
* type and settings version. Once one is found, it is given the reference to
* the settings data which t is responsible for casting into whatever class it
* requires.
*
*
*/
public class PamSettingManager {
static public final int LOAD_SETTINGS_OK = 0;
static public final int LOAD_SETTINGS_CANCEL = 1;
static public final int LOAD_SETTINGS_NEW = 2; // new settings
private static PamSettingManager pamSettingManager;
/**
* List of modules that have / want PAMGUARD Settings
* which get stored in the psf file and / or the database store.
*/
// private ArrayList<PamSettings> owners;
/**
* List of modules that specifically use settings from the database
* storage.
*/
private ArrayList<PamSettings> databaseOwners;
/**
* List of modules that are stored globally on the PC
* with a single common psf type file.
*/
private ArrayList<PamSettings> globalOwners;
private ArrayList<PamControlledUnitSettings> globalSettings;
/**
* List of settings used by 'normal' modules.
*/
private ArrayList<PamControlledUnitSettings> initialSettingsList;
/**
* List of settings used specifically by databases.
* This list never get's stored anywhere, but is just held
* in memory so that the database identified at startup in
* viewer and mixed modes gets reloaded later on .
*/
private ArrayList<PamControlledUnitSettings> databaseSettingsList;
// static public final String oldFileEnd = "PamSettingsFiles.ser";
static public final String fileEnd = "psf";
static public final String fileEndx = "psfx";
static public final String fileEndXML = "psfxml";
private static boolean saveAsPSFX = true;
/**
* A secondary configuration to use when loading configs into
* batch mode for viewing and extracting offline tasks. This is a
* real bodge and bad style, but can't do much about it at this stage.
* USe very sparingly and make sure it's set null once the external batch
* configuration is loaded.
*/
private PamConfiguration secondaryConfiguration;
static public String getCurrentSettingsFileEnd() {
if (saveAsPSFX) {
return fileEndx;
}
else {
return fileEnd;
}
}
/**
* Name of the file that contains a list of recent psf files.
*/
transient private final String settingsListFileName = "PamSettingsFilesUID";
/**
* End of the name - will be joine to the name, but may be changed a bit for funny versions
*/
transient private final String settingsListFileEnd = ".psg";
transient private final String gloablListfileName = "PamguardGlobals";
/**
* Name of a list of recent database informations (probably just the last one)
*/
transient private final String databaseListFile = "recentDatabasesUID.psg";
/**
* Identifier for modules that go in the 'normal' list
* (everything apart from database modules)
*/
public static final int LIST_UNITS = 0x1;
/**
* Identifier for modules which are part of the database system.
*/
public static final int LIST_DATABASESTUFF = 0x2;
/**
* Stuff which is global to the computer system (at the user level).
* Invented for colour settings, might extend to other things too.
*/
public static final int LIST_SYSTEMGLOBAL = 0x4;
/**
* Save settings to a psf file
*/
static public final int SAVE_PSF = 0x1;
/**
* Save settings to database tables (if available).
*/
static public final int SAVE_DATABASE = 0x2;
/**
* running in remote mode, default normal
*/
static public boolean RUN_REMOTE = false;
static public String remote_psf = null;
static public String external_wav = null;
private boolean loadingLocalSettings;
// File currentFile; // always use firstfile from the settingsFileData
private boolean[] settingsUsed;
// private boolean userNotifiedAbsentSettingsFile = false;
// private boolean userNotifiedAbsentDefaultSettingsFile = false;
private boolean programStart = true;
private SettingsFileData settingsFileData;
private PamSettingManager() {
// owners = new ArrayList<PamSettings>();
databaseOwners = new ArrayList<PamSettings>();
globalOwners = new ArrayList<PamSettings>();
// setCurrentFile(new File(defaultFile));
}
public static PamSettingManager getInstance() {
if (pamSettingManager == null) {
pamSettingManager = new PamSettingManager();
}
return pamSettingManager;
}
/**
* Clear all settings from the manager
*/
public void reset() {
initialSettingsList = null;
databaseSettingsList = null;
// owners = new ArrayList<PamSettings>();
getOwners().clear();
databaseOwners = new ArrayList<PamSettings>();
}
/*
* Flag to indicate that initialisation of PAMGUARD has completed.
*/
private boolean initializationComplete = false;
/**
* Called everytime anything in the model changes.
* @param changeType type of change
*/
public void notifyModelChanged(int changeType) {
if (changeType == PamControllerInterface.INITIALIZATION_COMPLETE) {
initializationComplete = true;
}
}
/**
* Register a PAMGAURD module that wants to store settings in a
* serialised file (.psf file) and / or have those settings stored
* in the database settings table.
* <p>Normally, all modules will
* call this for at least one set of settings. Often the PamSettings
* is implemented by the class that extends PamControlledunit, but
* it's also possible to have multiple sub modules, processes or displays
* implement PamSettings so that different settings for different bits of
* a PamControlledUnit are stored separately.
* @see PamSettings
* @see PamControlledUnit
* @param pamUnit Reference to a PamSettings module
* @return True if settings correctly restored. This is either the return of the restoreSettings() function
* in the calling pamUnit, or will be false if there was a ClassCastException in the call to restoreSettings()
*/
public boolean registerSettings(PamSettings pamUnit) {
return registerSettings(pamUnit, LIST_UNITS);
}
/**
* Deregister a settings.
* @param pamUnit
* @return
*/
public boolean unRegisterSettings(PamSettings pamUnit) {
boolean found = getOwners().remove(pamUnit);
found |= databaseOwners.remove(pamUnit);
found |= globalOwners.remove(pamUnit);
return found;
}
/**
* Register modules that have settings information that
* should be stored in serialised form in
* psf files and database Pamguard_Settings tables.
* @param pamUnit Unit containing the settings
* @param whichLists which lists to store the settings in. <p>
* N.B. These are internal lists and not the external storage. Basically
* any database modules connected with settings should to in LIST_DATABASESTUFF
* everything else (including the normal database) should go to LISTS_UNITS
* @return true if settings registered sucessfully.
*/
public boolean registerSettings(PamSettings pamUnit, int whichLists) {
if ((whichLists & LIST_UNITS) != 0) {
getOwners().add(pamUnit);
}
if ((whichLists & LIST_DATABASESTUFF) != 0) {
databaseOwners.add(pamUnit);
}
if ((whichLists & LIST_SYSTEMGLOBAL) != 0) {
globalOwners.add(pamUnit);
}
PamControlledUnitSettings settings = findSettings(pamUnit, whichLists);
if (settings != null && settings.getSettings() != null) {
try {
return pamUnit.restoreSettings(settings);
}
catch (ClassCastException e) {
System.out.printf("Error restoring settings to module %s,%s: %s\n", pamUnit.getUnitType(),
pamUnit.getUnitName(), e.getMessage());
}
}
return false;
}
/**
* Find settings for a particular user in one or more lists.
* @param user PamSettings user.
* @param whichLists lists to search
* @return settings object.
*/
private PamControlledUnitSettings findSettings(PamSettings user, int whichLists) {
PamControlledUnitSettings settings = null;
if ((whichLists & LIST_SYSTEMGLOBAL) != 0) {
if (globalSettings != null) {
settings = findSettings(globalSettings, null, user);
if (settings != null) {
return settings;
}
}
}
if ((whichLists & LIST_UNITS) != 0) {
if (initialSettingsList == null) return null;
if (settingsUsed == null || settingsUsed.length != initialSettingsList.size()) {
settingsUsed = new boolean[initialSettingsList.size()];
}
settings = findSettings(initialSettingsList, settingsUsed, user);
}
if (settings == null && (whichLists & LIST_DATABASESTUFF) != 0) {
settings = findGeneralSettings(user.getUnitType());
}
return settings;
}
/**
* Find settings in a list of settings, ignoring settings which have
* already been used by a module.
* @param settingsList settings list
* @param usedSettings list of settings that have already been used.
* @param user module that uses the settings.
* @return Settings object.
*/
private PamControlledUnitSettings findSettings(ArrayList<PamControlledUnitSettings> settingsList,
boolean[] usedSettings, PamSettings user) {
if (settingsList == null) return null;
// go through the list and see if any match this module. Avoid repeats.
// String unitName = user.getUnitName();
// String unitType = user.getUnitType();
for (int i = 0; i < settingsList.size(); i++) {
if (usedSettings != null && usedSettings[i]) continue;
if (isSettingsUnit(user, settingsList.get(i))) {
if (usedSettings != null) {
usedSettings[i] = true;
}
return settingsList.get(i);
}
}
/*
* To improve complex module loading where settings may be saved by multiple sub-modules, in
* July 2015 many modules which had fixed settings had their settings names and types changed !
* Therefore these modules won't have found their settings on the first go, so need to also check
* against the alternate names defined for each class.
* It should be possible to work out from the settingsUser.Class what changes may have been made !
*/
SettingsNameChange otherName = SettingsNameChanger.getInstance().findNameChange(user);
if (otherName == null) {
return null;
}
for (int i = 0; i < settingsList.size(); i++) {
if (usedSettings != null && usedSettings[i]) continue;
if (isSettingsUnit(otherName, settingsList.get(i))) {
if (usedSettings != null) {
usedSettings[i] = true;
}
return settingsList.get(i);
}
}
return null;
}
/**
* Searches a list of settings for settings with a
* specific type.
* @param unitType
* @return PamControlledUnitSettings or null if none found
* @see PamControlledUnitSettings
*/
public PamControlledUnitSettings findGeneralSettings(String unitType) {
if (databaseSettingsList == null) {
return null;
}
for (int i = 0; i < databaseSettingsList.size(); i++) {
if (databaseSettingsList.get(i).getUnitType().equalsIgnoreCase(unitType)) {
return databaseSettingsList.get(i);
}
}
return null;
}
/**
* Find settings in a list of settings by name and by type.
* @param settingsList settings list to search
* @param unitType unit name
* @param unitName unit type
* @return settings object
*/
public PamControlledUnitSettings findSettings(ArrayList<PamControlledUnitSettings> settingsList,
String unitType, String unitName) {
if (settingsList == null) {
return null;
}
PamControlledUnitSettings aSet;
try {
for (int i = 0; i < settingsList.size(); i++) {
aSet = settingsList.get(i);
if (aSet.getUnitType().equals(unitType) & (unitName == null | aSet.getUnitName().equals(unitName))) {
return aSet;
}
}
}
catch (NullPointerException e) {
System.out.printf("Error finding settings for %s : %s\n", unitType, unitName);
}
return null;
}
/**
* Find a settings owner for a type, name and class.
* @param unitType unit Type
* @param unitName unit Name
* @param unitClass unit Class
* @return Settings owner or null.
*/
public PamSettings findSettingsOwner(String unitType, String unitName, String unitClassName) {
ArrayList<PamSettings> owners = getOwners();
for (PamSettings owner:owners) {
if (owner.getClass() != null && unitClassName != null) {
if (!owner.getClass().getName().equals(unitClassName)) {
continue;
}
}
if (owner.getUnitName().equals(unitName) &&
owner.getUnitType().equals(unitType)) {
return owner;
}
}
return null;
}
/**
* Call just before PAMGUARD exits to save the settings
* either to psf and / or database tables.
* @return true if settings saved successfully.
*/
public boolean saveFinalSettings() {
int runMode = PamController.getInstance().getRunMode();
switch (runMode) {
case PamController.RUN_NORMAL:
case PamController.RUN_NETWORKRECEIVER:
return saveSettings(SAVE_PSF | SAVE_DATABASE);
case PamController.RUN_PAMVIEW:
if (GlobalArguments.getParam(GlobalArguments.BATCHVIEW) != null) {
return saveSettings(SAVE_PSF | SAVE_DATABASE);
}
else {
return saveSettings(SAVE_DATABASE);
}
case PamController.RUN_MIXEDMODE:
return saveSettings(SAVE_DATABASE);
case PamController.RUN_NOTHING:
return saveSettings(SAVE_PSF);
}
return false;
}
/**
* Save settings to a psf file and / or the database tables.
* @param saveWhere
* @return true if sucessful
*/
public boolean saveSettings(int saveWhere) {
if (!initializationComplete) {
// if PAMGAURD hasn't finished loading, then don't save the settings
// or the file will get wrecked (bug tracker 2269579)
String msg = "There was an error loading settings from this configuration, so the configuration"
+ " may be incomplete. <p>Do you want to save anyway ? <p>"
+ " If you have added new modules, the answer is probably \"Yes\"";
int ans = WarnOnce.showWarning("Confuguration file warning", msg, WarnOnce.YES_NO_OPTION);
if (ans == WarnOnce.CANCEL_OPTION) {
System.out.println("Settings have not yet loaded. Don't save file");
return false;
}
}
saveGlobalSettings();
// saveSettingToDatabase();
if ((saveWhere & SAVE_PSF) != 0) {
boolean success = saveSettingsToFile();
if (!success) {
String title = "Error saving settings to psf file";
String msg = "There was an error while trying to save the current settings to the psf file <p>" +
getSettingsFileName() + "<p>" +
"This could occur if the psf file location is in a read-only folder, or the filename is " +
"invalid. Please check and try again.";
String help = null;
int ans = WarnOnce.showWarning(PamController.getMainFrame(), title, msg, WarnOnce.WARNING_MESSAGE, help);
}
}
/**
* Always save the settings file data (list of recent files) since it includes
* static information such as whether to show day tips.
*/
saveSettingsFileData();
if ((saveWhere & SAVE_DATABASE) != 0) {
saveSettingsToDatabase();
saveDatabaseFileData();
}
return true;
}
public boolean saveSettingsToFile() {
return saveSettingsToFile(getSettingsFileName());
}
/**
* Save configuration settings to the default (most recently used) psf file.
* @return true if successful.
*/
public boolean saveSettingsToFile(String fileName) {
if (saveAsPSFX) {
// check the file end is psfx.
if (fileName.endsWith("psf")) {
fileName += "x";
settingsFileData.setFirstFile(new File(fileName));
String warnTxt = "<html>To avoid backwards compatibility issues, settings are now saved in psfx files." +
"<p>These are not backwards compatible with earlier versions of PAMGuard." +
"<p><p>If an existing psf file was loaded into this version of PAMGuard it will not have been changed" +
"and you will find both psf and psfx files in your configurations folder";
WarnOnce.showWarning(PamController.getMainFrame(), "PAMGuard configuration settings", warnTxt, WarnOnce.OK_OPTION);
}
return PSFXReadWriter.getInstance().writePSFX(fileName);
}
else {
return saveSettingsToPSFFile(fileName);
}
}
private boolean saveGlobalSettings() {
File setFile = getGlobalSettingsFile();
ObjectOutputStream outStream = openOutputFile(setFile.getAbsolutePath());
for (PamSettings gs:globalOwners) {
PamControlledUnitSettings pus = new PamControlledUnitSettings(gs.getUnitType(),
gs.getUnitName(), gs.getClass().getName(), gs.getSettingsVersion(), gs.getSettingsReference());
try {
outStream.writeObject(pus);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
private boolean loadGlobalSettings() {
globalSettings = new ArrayList<>();
File setFile = getGlobalSettingsFile();
ObjectInputStream ois = null;
boolean ok= true;
try {
ois = new ObjectInputStream(new FileInputStream(setFile));
}
catch (IOException e) {
ok = false;
}
while (ok) {
try {
Object o = ois.readObject();
PamControlledUnitSettings pus = (PamControlledUnitSettings) o;
globalSettings.add(pus);
}
catch (EOFException eof){
break;
}
catch (IOException e) {
ok = false;
} catch (ClassNotFoundException e) {
System.out.println("Global settings error " + e.getMessage());
ok = false;
}
}
try {
if (ois != null) {
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return ok;
}
/**
* Save configuration settings to the default (most recently used) psf file.
* @return true if successful.
*/
public boolean saveSettingsToPSFFile(String fileName) {
/*
* Create a new list of settings in case they have changed
*/
ArrayList<PamControlledUnitSettings> pamSettingsList;
pamSettingsList = new ArrayList<PamControlledUnitSettings>();
ArrayList<PamSettings> owners = getOwners();
for (int i = 0; i < getOwners().size(); i++) {
pamSettingsList
.add(new PamControlledUnitSettings(owners.get(i)
.getUnitType(), owners.get(i).getUnitName(),
owners.get(i).getClass().getName(),
owners.get(i).getSettingsVersion(),
owners.get(i).getSettingsReference()));
}
int nUsed = pamSettingsList.size();
/*
* Then go through the initialSettings, that were read in and any that were not used
* add to the current settings output so that they may be used next time around incase
* a module reappears that was temporarily not used.
*/
boolean firstDuplicateFound = true;
boolean purgeDuplicates = false;
if (initialSettingsList != null) {
for (int i = 0; i < initialSettingsList.size(); i++) {
if (settingsUsed != null && settingsUsed.length > i && settingsUsed[i]) continue;
// if this is a duplicate of something already in the list, warn the user and find out if they want to remove it
if (thisIsADuplicate(pamSettingsList, initialSettingsList.get(i))) {
if (firstDuplicateFound) {
firstDuplicateFound = false;
String msg = "<html>Duplicate settings have been found in the psf file. Please select whether to keep them in the psf, or to" +
" delete them. Duplicate settings will not cause Pamguard to crash, however they will enlarge the psf file over time.</html>";
int ans;
if (PamGUIManager.getGUIType()==PamGUIManager.FX) ans = WarnOnce.showWarningFX(PamController.getInstance().getGuiManagerFX().getMainScene().getOwner(),
"Duplicate settings encountered", PamUtilsFX.htmlToNormal(msg), AlertType.WARNING, null, null, "Keep Duplicates", "Delete Duplicates");
else ans = WarnOnce.showWarning(null, "Duplicate settings encountered", msg, WarnOnce.OK_CANCEL_OPTION, null, null,"Keep Duplicates", "Delete Duplicates");
if (ans == WarnOnce.CANCEL_OPTION) {
purgeDuplicates = true;
}
}
if (purgeDuplicates) continue;
pamSettingsList.add(initialSettingsList.get(i));
}
// if this is not a duplicate, go ahead and add it to the list
else {
pamSettingsList.add(initialSettingsList.get(i));
}
}
}
/*
* then save it to a single serialized file
*/
ObjectOutputStream file = openOutputFile(fileName);
PamControlledUnitSettings unitSettings = null;
if (file == null) {
System.err.println("Error opening " + fileName + " for write access. Cannot save settings information.");
return false;
}
try {
for (int i = 0; i < pamSettingsList.size(); i++){
PamControlledUnitSettings ps = pamSettingsList.get(i);
// System.out.println(String.format("Write %s %s", ps.getUnitType(), ps.getUnitName()));
file.writeObject(unitSettings = pamSettingsList.get(i));
}
file.close();
} catch (Exception Ex) {
System.err.println("Error writing settings to file object " + unitSettings.getUnitName() + " object " + unitSettings.getSettings());
Ex.printStackTrace();
return false;
}
// try { // experimenting with xml output.
// FileOutputStream fos = new FileOutputStream("pamguard.xml");
// XMLEncoder xe = new XMLEncoder(fos);
// for (int i = 0; i < nUsed; i++) {
// xe.writeObject(pamSettingsList.get(i).getUnitName());
// }
// xe.flush();
// xe.close();
// fos.close();
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// and save the settings file list to that's file
return true;
}
/**
* Checks if the PamControlledSettings object is already in the settings ArrayList. Comparison is done by
* checking the unit type and unit name.
* @param pamSettingsList the ArrayList containing the PamControlledUnitSettings
* @param settingToCheck the PamControlledUnitSettings to check
* @return true if it is in the list, false if not
*/
public boolean thisIsADuplicate(ArrayList<PamControlledUnitSettings> pamSettingsList, PamControlledUnitSettings settingToCheck) {
int listSize = pamSettingsList.size();
for (int i=0; i<listSize; i++) {
if (settingToCheck.getUnitType().equals(pamSettingsList.get(i).getUnitType()) &&
settingToCheck.getUnitName() != null &&
settingToCheck.getUnitName().equals(pamSettingsList.get(i).getUnitName())) {
return true;
}
}
return false;
}
/**
* Save configuration settings to a PSFX file (XML).
* @return true if successful.
*/
public boolean saveSettingsToXMLFile(File file) {
/*
* Create a new list of settings in case they have changed
*/
//XMLSettings
ArrayList<PamControlledUnitSettings> pamSettingsList;
pamSettingsList = new ArrayList<PamControlledUnitSettings>();
ArrayList<PamSettings> owners = getOwners();
for (int i = 0; i < owners.size(); i++) {
pamSettingsList
.add(new PamControlledUnitSettings(owners.get(i)
.getUnitType(), owners.get(i).getUnitName(),
owners.get(i).getClass().getName(),
owners.get(i).getSettingsVersion(),
owners.get(i).getSettingsReference()));
}
int nUsed = pamSettingsList.size();
/*
* Then go through the initialSettings, that were read in and any that were not used
* add to the current settings output so that they may be used next time around incase
* a module reappears that was temporarily not used.
*/
if (initialSettingsList != null) {
for (int i = 0; i < initialSettingsList.size(); i++) {
if (settingsUsed != null && settingsUsed.length > i && settingsUsed[i]) continue;
pamSettingsList.add(initialSettingsList.get(i));
}
}
/*
* then save it to a single XML file
*/
//XML file test
objectToXMLFile(pamSettingsList,file);
return true;
}
/**
* An object is serializable iff .... TBC
*/
public void objectToXMLFile(Object serialisableObject, File file){
// XStream xStream = new XStream();
// OutputStream outputStream = null;
// Writer writer = null;
//
// try {
// outputStream = new FileOutputStream(file);
// writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
// xStream.toXML(serialisableObject, writer);
// } catch (Exception exp) {
// exp.printStackTrace();
//// log.error(null, exp);
//// return false;
// } finally {
// try {
// writer.close();
// outputStream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// System.out.println("done!");
//
// }
System.out.println("The code for objectToXMLFile(Object serialisableObject, File file) has been commented out!!");
}
/**
* Load the PAMGAURD settings either from psf file or from
* a database, depending on the run mode and type of settings required.
* @param runMode
* @return OK if load was successful.
*/
public int loadPAMSettings(int runMode) {
int ans = LOAD_SETTINGS_OK;
loadGlobalSettings();
switch(runMode) {
case PamController.RUN_NORMAL:
case PamController.RUN_NETWORKRECEIVER:
ans = loadNormalSettings();
break;
case PamController.RUN_PAMVIEW:
if (GlobalArguments.getParam(GlobalArguments.BATCHVIEW) != null) {
ans = loadNormalSettings();
}
else {
ans = loadViewerSettings();
}
break;
case PamController.RUN_MIXEDMODE:
ans = loadMixedModeSettings();
break;
case PamController.RUN_REMOTE:
PamSettingManager.RUN_REMOTE = true;
ans = loadNormalSettings();
break;
case PamController.RUN_NOTHING:
ans = loadNormalSettings();
break;
default:
return LOAD_SETTINGS_CANCEL;
}
if (ans == LOAD_SETTINGS_OK) {
initialiseRegisteredModules();
}
return ans;
}
/**
* Load settings perfectly 'normally' from a psf file.
* @return OK whether or not any settings were loaded.
*/
private int loadNormalSettings() {
return loadPSFSettings();
}
/**
* Load settings for viewer mode. These must come from
* an old PAMGUARD database containing settings information.
* @return true if settings loaded sucessfully.
*/
private int loadViewerSettings() {
return loadDBSettings();
}
/**
* Load settings for mixed mode. These must come from
* an old PAMGUARD database containing settings information.
* @return true if settings loaded sucessfully.
*/
private int loadMixedModeSettings() {
return loadDBSettings();
}
/**
* Some modules may have already registered before the
* settings were loaded, so this function is called
* as soon as they are loaded which sends settings to
* all modules in the list.
*/
private void initialiseRegisteredModules() {
ArrayList<PamSettings> owners = getOwners();
if (owners == null) {
return;
}
PamControlledUnitSettings settings = null;
if (settingsUsed == null || settingsUsed.length != initialSettingsList.size()) {
settingsUsed = new boolean[initialSettingsList.size()];
}
for (int i = 0; i < owners.size(); i++) {
settings = findSettings(initialSettingsList, settingsUsed, owners.get(i));
if (settings != null) {
try {
owners.get(i).restoreSettings(settings);
}
catch (ClassCastException e) {
e.printStackTrace();
}
}
}
}
/**
* Open the file that contains a list of files and optionally open a dialog
* giving the list of recent files.
* <p>
* Unfortunately, as soon as this gets called the first time, it tries to
* open a database to get more settings information and different database
* plug ins all start trying to get more settings and it goes round and round and
* round. Need to ensure that these loop around only get given the general settings
* information.
* @return
*/
private int loadPSFSettings() {
if (PamSettingManager.remote_psf == null) {
if (settingsFileData == null) {
loadLocalSettings();
}
if (loadingLocalSettings) return LOAD_SETTINGS_OK;
if (
// settingsFileData.showFileList &&
programStart) {
SettingsFileData newData = showSettingsDailog(settingsFileData);
if (newData != null) {
settingsFileData = newData.clone();
/*
* Save the settings file data immediately so that if we crash, this file
* is still at the top of the list next time we run.
*/
saveSettingsFileData();
}
else {
return LOAD_SETTINGS_CANCEL;
}
programStart = false;
}
File ff = settingsFileData.getFirstFile();
}
// if we are running a psf remotely, add it to the SettingsFileData list
else {
setDefaultFile(PamSettingManager.remote_psf);
}
initialSettingsList = loadSettingsFromFile();
//XMLSettings
// initialSettingsList = loadSettingsFromXMLFile();
/*TODO FIXME -implement this properly (see also PamGui-line 478) to enable saving menu item
* so far it works for some settings- one it doesn't work for is File Folder Acquisition
*
* output from loading XML
* ------------------------------------
PAMGUARD Version 1.11.02j branch SMRU
Revision 1028
java.version 1.7.0_07
java.vendor Oracle Corporation
java.vm.version 23.3-b01