Save document preferences in design file
This commit is contained in:
parent
144efb5610
commit
500c75aeab
@ -5,6 +5,7 @@ import java.util.*;
|
|||||||
|
|
||||||
import info.openrocket.core.file.wavefrontobj.export.OBJExportOptions;
|
import info.openrocket.core.file.wavefrontobj.export.OBJExportOptions;
|
||||||
import info.openrocket.core.preferences.ApplicationPreferences;
|
import info.openrocket.core.preferences.ApplicationPreferences;
|
||||||
|
import info.openrocket.core.preferences.DocumentPreferences;
|
||||||
import info.openrocket.core.rocketcomponent.*;
|
import info.openrocket.core.rocketcomponent.*;
|
||||||
import info.openrocket.core.util.StateChangeListener;
|
import info.openrocket.core.util.StateChangeListener;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -38,6 +39,7 @@ import info.openrocket.core.util.ArrayList;
|
|||||||
public class OpenRocketDocument implements ComponentChangeListener, StateChangeListener {
|
public class OpenRocketDocument implements ComponentChangeListener, StateChangeListener {
|
||||||
private static final Logger log = LoggerFactory.getLogger(OpenRocketDocument.class);
|
private static final Logger log = LoggerFactory.getLogger(OpenRocketDocument.class);
|
||||||
private static final ApplicationPreferences prefs = Application.getPreferences();
|
private static final ApplicationPreferences prefs = Application.getPreferences();
|
||||||
|
private final DocumentPreferences docPrefs = new DocumentPreferences();
|
||||||
private final List<String> file_extensions = Arrays.asList("ork", "ork.gz", "rkt", "rkt.gz"); // Possible extensions of an OpenRocket document
|
private final List<String> file_extensions = Arrays.asList("ork", "ork.gz", "rkt", "rkt.gz"); // Possible extensions of an OpenRocket document
|
||||||
/**
|
/**
|
||||||
* The minimum number of undo levels that are stored.
|
* The minimum number of undo levels that are stored.
|
||||||
@ -849,6 +851,13 @@ public class OpenRocketDocument implements ComponentChangeListener, StateChangeL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void fireDocumentSavingEvent(DocumentChangeEvent event) {
|
||||||
|
DocumentChangeListener[] array = listeners.toArray(new DocumentChangeListener[0]);
|
||||||
|
for (DocumentChangeListener l : array) {
|
||||||
|
l.documentSaving(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String toSimulationDetail(){
|
public String toSimulationDetail(){
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
str.append(">> Dumping simulation list:\n");
|
str.append(">> Dumping simulation list:\n");
|
||||||
@ -868,4 +877,8 @@ public class OpenRocketDocument implements ComponentChangeListener, StateChangeL
|
|||||||
public void setPhotoSettings(Map<String, String> photoSettings) {
|
public void setPhotoSettings(Map<String, String> photoSettings) {
|
||||||
this.photoSettings = photoSettings;
|
this.photoSettings = photoSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DocumentPreferences getDocumentPreferences() {
|
||||||
|
return docPrefs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,10 @@ package info.openrocket.core.document.events;
|
|||||||
|
|
||||||
public interface DocumentChangeListener {
|
public interface DocumentChangeListener {
|
||||||
|
|
||||||
public void documentChanged(DocumentChangeEvent event);
|
void documentChanged(DocumentChangeEvent event);
|
||||||
|
|
||||||
|
default void documentSaving(DocumentChangeEvent event) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import info.openrocket.core.file.openrocket.savers.PhotoStudioSaver;
|
|||||||
import info.openrocket.core.logging.ErrorSet;
|
import info.openrocket.core.logging.ErrorSet;
|
||||||
import info.openrocket.core.logging.SimulationAbort;
|
import info.openrocket.core.logging.SimulationAbort;
|
||||||
import info.openrocket.core.logging.WarningSet;
|
import info.openrocket.core.logging.WarningSet;
|
||||||
|
import info.openrocket.core.preferences.DocumentPreferences;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -108,6 +109,9 @@ public class OpenRocketSaver extends RocketSaver {
|
|||||||
// Save PhotoSettings
|
// Save PhotoSettings
|
||||||
savePhotoSettings(document.getPhotoSettings());
|
savePhotoSettings(document.getPhotoSettings());
|
||||||
|
|
||||||
|
// Save document preferences
|
||||||
|
saveDocumentPreferences(document.getDocumentPreferences());
|
||||||
|
|
||||||
indent--;
|
indent--;
|
||||||
writeln("</openrocket>");
|
writeln("</openrocket>");
|
||||||
|
|
||||||
@ -428,6 +432,21 @@ public class OpenRocketSaver extends RocketSaver {
|
|||||||
writeln("</photostudio>");
|
writeln("</photostudio>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveDocumentPreferences(DocumentPreferences docPrefs) throws IOException {
|
||||||
|
log.debug("Saving Document Preferences");
|
||||||
|
|
||||||
|
writeln("<docprefs>");
|
||||||
|
indent++;
|
||||||
|
|
||||||
|
Map<String, DocumentPreferences.DocumentPreference> prefs = docPrefs.getPreferencesMap();
|
||||||
|
for (Map.Entry<String, DocumentPreferences.DocumentPreference> entry : prefs.entrySet()) {
|
||||||
|
DocumentPreferences.DocumentPreference pref = entry.getValue();
|
||||||
|
writeEntry("pref", entry.getKey(), pref.getValue(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
indent--;
|
||||||
|
writeln("</docprefs>");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write an entry element, which has a key and type attribute, and a value, to the output.
|
* Write an entry element, which has a key and type attribute, and a value, to the output.
|
||||||
|
@ -0,0 +1,66 @@
|
|||||||
|
package info.openrocket.core.file.openrocket.importt;
|
||||||
|
|
||||||
|
import info.openrocket.core.preferences.DocumentPreferences;
|
||||||
|
import info.openrocket.core.document.OpenRocketDocument;
|
||||||
|
import info.openrocket.core.file.simplesax.ElementHandler;
|
||||||
|
import info.openrocket.core.file.simplesax.PlainTextHandler;
|
||||||
|
import info.openrocket.core.logging.WarningSet;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class DocumentPreferencesHandler extends EntryHandler {
|
||||||
|
private final OpenRocketDocument document;
|
||||||
|
|
||||||
|
public DocumentPreferencesHandler(OpenRocketDocument document) {
|
||||||
|
this.document = document;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
|
||||||
|
if (element.equals("pref") && "list".equals(attributes.get("type"))) {
|
||||||
|
listHandler = new ConfigHandler();
|
||||||
|
return listHandler;
|
||||||
|
} else {
|
||||||
|
return PlainTextHandler.INSTANCE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
|
||||||
|
if (element.equals("pref")) {
|
||||||
|
String key = attributes.get("key");
|
||||||
|
String type = attributes.get("type");
|
||||||
|
Object value = EntryHelper.getValueFromEntry(DocumentPreferencesHandler.this, attributes, content);
|
||||||
|
if (value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key != null) {
|
||||||
|
addValueToDocumentPreferences(key, value, type);
|
||||||
|
} else {
|
||||||
|
list.add(value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
super.closeElement(element, attributes, content, warnings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addValueToDocumentPreferences(String key, Object value, String type) {
|
||||||
|
DocumentPreferences docPrefs = document.getDocumentPreferences();
|
||||||
|
|
||||||
|
if ("boolean".equals(type)) {
|
||||||
|
docPrefs.putBoolean(key, (Boolean) value);
|
||||||
|
} else if ("string".equals(type)) {
|
||||||
|
docPrefs.putString(key, (String) value);
|
||||||
|
} else if ("integer".equals(type)) {
|
||||||
|
docPrefs.putInt(key, (Integer) value);
|
||||||
|
} else if ("double".equals(type)) {
|
||||||
|
docPrefs.putDouble(key, (Double) value);
|
||||||
|
} else if ("number".equals(type)) {
|
||||||
|
throw new RuntimeException("Number preferences are not supported");
|
||||||
|
} else if ("list".equals(type)) {
|
||||||
|
// We don't support nested preferences
|
||||||
|
throw new RuntimeException("Nested preferences are not supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -65,7 +65,11 @@ class OpenRocketContentHandler extends AbstractElementHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (element.equals("photostudio")) {
|
if (element.equals("photostudio")) {
|
||||||
return new PhotoStudioHandler(context.getOpenRocketDocument().getPhotoSettings());
|
return new PhotoStudioHandler(getDocument().getPhotoSettings());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.equals("docprefs")) {
|
||||||
|
return new DocumentPreferencesHandler(getDocument());
|
||||||
}
|
}
|
||||||
|
|
||||||
warnings.add(Warning.fromString("Unknown element " + element + ", ignoring."));
|
warnings.add(Warning.fromString("Unknown element " + element + ", ignoring."));
|
||||||
|
@ -0,0 +1,97 @@
|
|||||||
|
package info.openrocket.core.preferences;
|
||||||
|
|
||||||
|
import info.openrocket.core.util.ChangeSource;
|
||||||
|
import info.openrocket.core.util.StateChangeListener;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ORPreferences specific to an OpenRocket document (= preferences that are saved in the document file, not
|
||||||
|
* implemented application-wise).
|
||||||
|
*
|
||||||
|
* @author Sibo Van Gool <sibo.vangool@hotmail.com>
|
||||||
|
*/
|
||||||
|
public class DocumentPreferences implements ChangeSource, ORPreferences {
|
||||||
|
// Map that stores all the document preferences
|
||||||
|
private final Map<String, DocumentPreference> preferencesMap = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addChangeListener(StateChangeListener listener) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeChangeListener(StateChangeListener listener) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(String key, boolean defaultValue) {
|
||||||
|
DocumentPreference pref = preferencesMap.get(key);
|
||||||
|
return preferencesMap.containsKey(key) ? (Boolean) pref.getValue() : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putBoolean(String key, boolean value) {
|
||||||
|
preferencesMap.put(key, new DocumentPreference(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String key, int defaultValue) {
|
||||||
|
DocumentPreference pref = preferencesMap.get(key);
|
||||||
|
return preferencesMap.containsKey(key) ? (Integer) pref.getValue() : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putInt(String key, int value) {
|
||||||
|
preferencesMap.put(key, new DocumentPreference(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDouble(String key, double defaultValue) {
|
||||||
|
DocumentPreference pref = preferencesMap.get(key);
|
||||||
|
return preferencesMap.containsKey(key) ? (Double) pref.getValue() : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putDouble(String key, double value) {
|
||||||
|
preferencesMap.put(key, new DocumentPreference(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(String key, String defaultValue) {
|
||||||
|
DocumentPreference pref = preferencesMap.get(key);
|
||||||
|
return preferencesMap.containsKey(key) ? (String) pref.getValue() : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putString(String key, String value) {
|
||||||
|
preferencesMap.put(key, new DocumentPreference(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the map that stores all the document preferences key-value pairs.
|
||||||
|
* @return The document preferences map
|
||||||
|
*/
|
||||||
|
public Map<String, DocumentPreference> getPreferencesMap() {
|
||||||
|
return preferencesMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DocumentPreference {
|
||||||
|
private final Object value;
|
||||||
|
private final Class<?> type;
|
||||||
|
|
||||||
|
public DocumentPreference(Object value) {
|
||||||
|
this.value = value;
|
||||||
|
this.type = value.getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -69,3 +69,4 @@ The following file format versions exist:
|
|||||||
|
|
||||||
1.10: Introduced with OpenRocket 24.XX.
|
1.10: Introduced with OpenRocket 24.XX.
|
||||||
Added a priority attribute to simulation warnings.
|
Added a priority attribute to simulation warnings.
|
||||||
|
Added document preferences (<docprefs>).
|
||||||
|
@ -1388,6 +1388,7 @@ public class BasicFrame extends JFrame {
|
|||||||
* @return true if the file was saved, false otherwise
|
* @return true if the file was saved, false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean saveAction() {
|
private boolean saveAction() {
|
||||||
|
document.fireDocumentSavingEvent(new DocumentChangeEvent(this));
|
||||||
File file = document.getFile();
|
File file = document.getFile();
|
||||||
if (file == null || document.getDefaultStorageOptions().getFileType().equals(FileType.ROCKSIM)
|
if (file == null || document.getDefaultStorageOptions().getFileType().equals(FileType.ROCKSIM)
|
||||||
|| document.getDefaultStorageOptions().getFileType().equals(FileType.RASAERO)) {
|
|| document.getDefaultStorageOptions().getFileType().equals(FileType.RASAERO)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user