diff --git a/tscore/src/main/java/com/timestored/tscore/persistance/KeyInterface.java b/tscore/src/main/java/com/timestored/tscore/persistance/KeyInterface.java new file mode 100644 index 0000000..4b5880b --- /dev/null +++ b/tscore/src/main/java/com/timestored/tscore/persistance/KeyInterface.java @@ -0,0 +1,21 @@ +/* + * qStudio - Free SQL Analysis Tool + * Copyright C 2013-2024 TimeStored + * + * Licensed under the Apache License, Version 2.0 the "License"; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.timestored.tscore.persistance; + +public interface KeyInterface { + String name(); +} diff --git a/tscore/src/main/java/com/timestored/tscore/persistance/PersistanceInterface.java b/tscore/src/main/java/com/timestored/tscore/persistance/PersistanceInterface.java new file mode 100644 index 0000000..f44ec17 --- /dev/null +++ b/tscore/src/main/java/com/timestored/tscore/persistance/PersistanceInterface.java @@ -0,0 +1,84 @@ +/* + * qStudio - Free SQL Analysis Tool + * Copyright C 2013-2024 TimeStored + * + * Licensed under the Apache License, Version 2.0 the "License"; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.timestored.tscore.persistance; + +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; + + +public interface PersistanceInterface { + + //@TODO ideally this would use KeyedPrefs + public static final String PATH_SPLIT = ";"; + + /** + * @param key key with which the specified value is to be associated. + * @param value value to be associated with the specified key. + */ + void put(KeyInterface key, String value); + + /** + * Return the value associate with a key or default value otherwise. + * @param key key whose associated value is to be returned. + * @param def the value to be returned in the event that this preference node has no value associated with key. + */ + String get(KeyInterface key, String def); + + /** + * @param key key with which the specified value is to be associated. + * @param value value to be associated with the specified key. + */ + void putBoolean(KeyInterface key, boolean value); + + /** + * Return the value associate with a key or default value otherwise. + * @param key key whose associated value is to be returned. + * @param def the value to be returned in the event that this preference node has no value associated with key. + */ + boolean getBoolean(KeyInterface key, boolean def); + + /** + * @param key key with which the specified value is to be associated. + * @param value value to be associated with the specified key. + */ + void putInt(KeyInterface key, int value); + + /** + * Return the value associate with a key or default value otherwise. + * @param key key whose associated value is to be returned. + * @param def the value to be returned in the event that this preference node has no value associated with key. + */ + int getInt(KeyInterface key, int def); + + void putLong(KeyInterface key, long val); + + long getLong(KeyInterface key, long def); + + /** + * Removes all persisted values. (except FERD) + * @param wipeLicense wipes everything, license and first ever run date. + * meaning the software would revert to a free trial. + */ + void clear(boolean wipeLicense) throws BackingStoreException; + + /** + * @return underlying preference store. Not recommended for common use as + * it gives much rawer access without key checking etc. + */ + Preferences getPref(); + +} \ No newline at end of file diff --git a/tscore/src/main/java/com/timestored/tscore/persistance/RecentDocumentPersister.java b/tscore/src/main/java/com/timestored/tscore/persistance/RecentDocumentPersister.java new file mode 100644 index 0000000..cd4ec26 --- /dev/null +++ b/tscore/src/main/java/com/timestored/tscore/persistance/RecentDocumentPersister.java @@ -0,0 +1,123 @@ +/* + * qStudio - Free SQL Analysis Tool + * Copyright C 2013-2024 TimeStored + * + * Licensed under the Apache License, Version 2.0 the "License"; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.timestored.tscore.persistance; + +import java.io.File; +import java.util.List; + +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.timestored.docs.Document; +import com.timestored.docs.OpenDocumentsModel; +import com.timestored.misc.FifoBuffer; + +/** + * Save a list of open and recent documents each time one is opened/closed. + * Also provides a method to save all currently open documents to a scratch area for later restoration. + * If you want to read the recent/opened docs previously saved be careful to read before + * adding this as a listener. As any events would overwrite previous values. + */ +public class RecentDocumentPersister implements OpenDocumentsModel.Listener { + + /* + * Note some tricky issues: + * Documents may be saved to disk or may not yet be saved to disk + * File names may not be unique + * In particular in-memory "new files" may not be unique + * We also only receive add/close/save notifications, some files may have changed outside of that individual file. + */ + + private final FifoBuffer recentFilePaths = new FifoBuffer(9); + private final PersistanceInterface persistance; + private final KeyInterface recentDocsKey; + private final KeyInterface lastOpenedFolderKey; + + public RecentDocumentPersister(PersistanceInterface persistance, + KeyInterface recentDocsKey, + KeyInterface lastOpenedFolderKey) { + + this.persistance = Preconditions.checkNotNull(persistance); + this.recentDocsKey = Preconditions.checkNotNull(recentDocsKey); + this.lastOpenedFolderKey = Preconditions.checkNotNull(lastOpenedFolderKey); + + recentFilePaths.addAll(getFilePaths(persistance, recentDocsKey)); + + } + + private static List getFilePaths(PersistanceInterface persistance, KeyInterface filekey) { + List filepaths = Lists.newArrayList(); + String recent = persistance.get(filekey, ""); + String[] recDocs = recent.split(PersistanceInterface.PATH_SPLIT); + for(String filepath : recDocs) { + if(!filepath.trim().isEmpty()) { + filepaths.add(filepath); + } + } + return filepaths; + } + + private void persistRecentDocuments() { + String recent = Joiner.on(PersistanceInterface.PATH_SPLIT).join(recentFilePaths.getAll()); + persistance.put(recentDocsKey, recent); + } + + public List getRecentFilePaths() { + return recentFilePaths.getAll(); + } + + /** @return most recently opened folder or null if none was set **/ + public File getOpenFolder(PersistanceInterface persistance) { + + String path = persistance.get(lastOpenedFolderKey, ""); + if(!path.equals("")) { + File f = new File(path); + if(f.isDirectory()) { + return f; + } + } + return null; + } + + /* + * Any time there is an add/close or save persist list of open docs + */ + @Override public void docClosed(Document document) { + if(document.getFilePath()!=null) { + recentFilePaths.add(document.getFilePath()); + } + persistRecentDocuments(); + } + + @Override public void docAdded(Document document) { + if(document.getFilePath()!=null) { + recentFilePaths.add(document.getFilePath()); + } + persistRecentDocuments(); + } + + @Override public void docSaved() { } + @Override public void docSelected(Document document) {} + @Override public void docContentModified() {} + @Override public void docCaratModified() {} + + @Override public void folderSelected(File selectedFolder) { + String path = selectedFolder == null ? "" : selectedFolder.getAbsolutePath(); + persistance.put(lastOpenedFolderKey, path); + } +} +