@@ -122,9 +122,12 @@ private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset
122122 }
123123 }
124124
125- private boolean doSave () {
125+ public boolean save (Path targetPath , SaveDatabaseMode mode ) {
126+ if (mode == SaveDatabaseMode .NORMAL ) {
127+ panel .frame ().getDialogService ().notify (Localization .lang ("Saving library" ) + "..." );
128+ }
129+
126130 panel .setSaving (true );
127- Path targetPath = panel .getBibDatabaseContext ().getDatabasePath ().get ();
128131 try {
129132 Charset encoding = panel .getBibDatabaseContext ()
130133 .getMetaData ()
@@ -144,9 +147,8 @@ private boolean doSave() {
144147 panel .setNonUndoableChange (false );
145148 panel .setBaseChanged (false );
146149
147- // Reset title of tab
148150 frame .setTabTitle (panel , panel .getTabTitle (),
149- panel . getBibDatabaseContext (). getDatabasePath (). get () .toAbsolutePath ().toString ());
151+ targetPath .toAbsolutePath ().toString ());
150152 frame .setWindowTitle ();
151153 frame .updateAllTabTitles ();
152154 }
@@ -161,32 +163,36 @@ private boolean doSave() {
161163 }
162164 }
163165
164- public boolean save () {
165- return save (SaveDatabaseMode .NORMAL );
166+ public boolean save (BibDatabaseContext bibDatabaseContext ) {
167+ return save (bibDatabaseContext , SaveDatabaseMode .NORMAL );
166168 }
167169
168- public boolean save (SaveDatabaseMode mode ) {
169- if (panel .getBibDatabaseContext ().getDatabasePath ().isPresent ()) {
170- if (mode == SaveDatabaseMode .NORMAL ) {
171- panel .frame ().getDialogService ().notify (Localization .lang ("Saving library" ) + "..." );
172- }
173- return doSave ();
174- } else {
175- Optional <Path > savePath = getSavePath ();
176- if (savePath .isPresent ()) {
177- saveAs (savePath .get ());
178- return true ;
170+ public boolean save (BibDatabaseContext bibDatabaseContext , SaveDatabaseMode mode ) {
171+ Optional <Path > databasePath = bibDatabaseContext .getDatabasePath ();
172+ if (!databasePath .isPresent ()) {
173+ Optional <Path > savePath = askForSavePath ();
174+ if (!savePath .isPresent ()) {
175+ return false ;
179176 }
177+ return saveAs (savePath .get (), mode );
180178 }
181179
182- return false ;
180+ return save ( databasePath . get (), mode ) ;
183181 }
184182
183+ /**
184+ * Asks the user for the path and saves afterwards
185+ */
185186 public void saveAs () {
186- getSavePath ().ifPresent (this ::saveAs );
187+ askForSavePath ().ifPresent (this ::saveAs );
187188 }
188189
189- private Optional <Path > getSavePath () {
190+ /**
191+ * Asks the user for the path to save to. Stores the directory to the preferences, which is used next time when opening the dialog.
192+ *
193+ * @return the path set by the user
194+ */
195+ public Optional <Path > askForSavePath () {
190196 FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration .Builder ()
191197 .addExtensionFilter (StandardFileType .BIBTEX_DB )
192198 .withDefaultExtension (StandardFileType .BIBTEX_DB )
@@ -197,14 +203,22 @@ private Optional<Path> getSavePath() {
197203 return selectedPath ;
198204 }
199205
200- public void saveAs (Path file ) {
206+ public boolean saveAs (Path file ) {
207+ return this .saveAs (file , SaveDatabaseMode .NORMAL );
208+ }
209+
210+ /**
211+ * @param file the new file name to save the data base to. This is stored in the database context of the panel upon successful save.
212+ * @return true on successful save
213+ */
214+ public boolean saveAs (Path file , SaveDatabaseMode mode ) {
201215 BibDatabaseContext context = panel .getBibDatabaseContext ();
202216
203217 // Close AutosaveManager and BackupManager for original library
204218 Optional <Path > databasePath = context .getDatabasePath ();
205219 if (databasePath .isPresent ()) {
206220 final Path oldFile = databasePath .get ();
207- context .setDatabaseFile (oldFile . toFile () );
221+ context .setDatabasePath (oldFile );
208222 AutosaveManager .shutdown (context );
209223 BackupManager .shutdown (context );
210224 }
@@ -215,22 +229,28 @@ public void saveAs(Path file) {
215229 new SharedDatabasePreferences (context .getDatabase ().generateSharedDatabaseID ())
216230 .putAllDBMSConnectionProperties (context .getDBMSSynchronizer ().getConnectionProperties ());
217231 }
218- context .setDatabaseFile (file );
219232
220- // Save
221- save ();
233+ boolean saveResult = save (file , mode );
222234
223- // Reinstall AutosaveManager and BackupManager
224- panel .resetChangeMonitorAndChangePane ();
225- if (readyForAutosave (context )) {
226- AutosaveManager autosaver = AutosaveManager .start (context );
227- autosaver .registerListener (new AutosaveUIManager (panel ));
228- }
229- if (readyForBackup (context )) {
230- BackupManager .start (context , entryTypesManager , prefs );
235+ if (saveResult ) {
236+ // we managed to successfully save the file
237+ // thus, we can store the store the path into the context
238+ context .setDatabasePath (file );
239+
240+ // Reinstall AutosaveManager and BackupManager for the new file name
241+ panel .resetChangeMonitorAndChangePane ();
242+ if (readyForAutosave (context )) {
243+ AutosaveManager autosaver = AutosaveManager .start (context );
244+ autosaver .registerListener (new AutosaveUIManager (panel ));
245+ }
246+ if (readyForBackup (context )) {
247+ BackupManager .start (context , entryTypesManager , prefs );
248+ }
249+
250+ frame .getFileHistory ().newFile (file );
231251 }
232252
233- context . getDatabasePath (). ifPresent ( presentFile -> frame . getFileHistory (). newFile ( presentFile )) ;
253+ return saveResult ;
234254 }
235255
236256 private boolean readyForAutosave (BibDatabaseContext context ) {
@@ -246,7 +266,7 @@ private boolean readyForBackup(BibDatabaseContext context) {
246266 }
247267
248268 public void saveSelectedAsPlain () {
249- getSavePath ().ifPresent (path -> {
269+ askForSavePath ().ifPresent (path -> {
250270 try {
251271 saveDatabase (path , true , prefs .getDefaultEncoding (), SavePreferences .DatabaseSaveType .PLAIN_BIBTEX );
252272 frame .getFileHistory ().newFile (path );
0 commit comments