Add option to ignore window information exporting
This commit is contained in:
		
							parent
							
								
									e7824c5553
								
							
						
					
					
						commit
						42714e73e7
					
				| @ -384,6 +384,8 @@ PreferencesImporter.chooser.title = Import a Preferences File | ||||
| PreferencesOptionPanel.title = Export settings | ||||
| PreferencesOptionPanel.checkbox.userDirectories = Export user directories | ||||
| PreferencesOptionPanel.checkbox.userDirectories.ttip = If checked, user directories (possible sensitive information) will be exported. | ||||
| PreferencesOptionPanel.checkbox.windowInfo = Export window information (position, size\u2026) | ||||
| PreferencesOptionPanel.checkbox.windowInfo.ttip = If checked, window information (position, size\u2026) will be exported. | ||||
| 
 | ||||
| ! Welcome dialog | ||||
| welcome.dlg.title = Welcome to OpenRocket | ||||
|  | ||||
| @ -82,6 +82,7 @@ public abstract class Preferences implements ChangeSource { | ||||
| 	private static final String SHOW_MARKERS = "ShowMarkers"; | ||||
| 	private static final String SHOW_ROCKSIM_FORMAT_WARNING = "ShowRocksimFormatWarning"; | ||||
| 	private static final String EXPORT_USER_DIRECTORIES = "ExportUserDirectories"; | ||||
| 	private static final String EXPORT_WINDOW_INFORMATION = "ExportWindowInformation"; | ||||
| 	 | ||||
| 	//Preferences related to 3D graphics | ||||
| 	public static final String OPENGL_ENABLED = "OpenGLIsEnabled"; | ||||
| @ -242,6 +243,14 @@ public abstract class Preferences implements ChangeSource { | ||||
| 		this.putBoolean(EXPORT_USER_DIRECTORIES, check); | ||||
| 	} | ||||
| 
 | ||||
| 	public final boolean getExportWindowInformation() { | ||||
| 		return this.getBoolean(EXPORT_WINDOW_INFORMATION, false); | ||||
| 	} | ||||
| 
 | ||||
| 	public final void setExportWindowInformation(boolean check) { | ||||
| 		this.putBoolean(EXPORT_WINDOW_INFORMATION, check); | ||||
| 	} | ||||
| 
 | ||||
| 	public final double getDefaultMach() { | ||||
| 		return Application.getPreferences().getChoice(Preferences.DEFAULT_MACH_NUMBER, 0.9, 0.3); | ||||
| 	} | ||||
|  | ||||
| @ -24,6 +24,7 @@ public class PreferencesOptionPanel extends JPanel { | ||||
|         JPanel panel = new JPanel(new MigLayout("fill, ins 4lp")); | ||||
|         panel.setBorder(BorderFactory.createTitledBorder(trans.get("PreferencesOptionPanel.title"))); | ||||
| 
 | ||||
|         // Export user directories | ||||
|         JCheckBox exportUserDirectories = new JCheckBox(trans.get("PreferencesOptionPanel.checkbox.userDirectories")); | ||||
|         exportUserDirectories.setToolTipText(trans.get("PreferencesOptionPanel.checkbox.userDirectories.ttip")); | ||||
|         exportUserDirectories.setSelected(prefs.getExportUserDirectories()); | ||||
| @ -33,7 +34,20 @@ public class PreferencesOptionPanel extends JPanel { | ||||
|                 prefs.setExportUserDirectories(e.getStateChange() == ItemEvent.SELECTED); | ||||
|             } | ||||
|         }); | ||||
|         panel.add(exportUserDirectories, "wrap 10lp"); | ||||
|         panel.add(exportUserDirectories, "wrap"); | ||||
| 
 | ||||
|         // Export window information (position, size...) | ||||
|         JCheckBox exportWindowInfo = new JCheckBox(trans.get("PreferencesOptionPanel.checkbox.windowInfo")); | ||||
|         exportWindowInfo.setToolTipText(trans.get("PreferencesOptionPanel.checkbox.windowInfo.ttip")); | ||||
|         exportWindowInfo.setSelected(prefs.getExportWindowInformation()); | ||||
|         exportWindowInfo.addItemListener(new ItemListener() { | ||||
|             @Override | ||||
|             public void itemStateChanged(ItemEvent e) { | ||||
|                 prefs.setExportWindowInformation(e.getStateChange() == ItemEvent.SELECTED); | ||||
|             } | ||||
|         }); | ||||
|         panel.add(exportWindowInfo, "wrap 10lp"); | ||||
| 
 | ||||
| 
 | ||||
|         this.add(panel, "growx, north"); | ||||
|     } | ||||
|  | ||||
| @ -30,6 +30,9 @@ import java.io.FileOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Path; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.function.BiPredicate; | ||||
| import java.util.prefs.BackingStoreException; | ||||
| import java.util.prefs.Preferences; | ||||
| 
 | ||||
| @ -38,17 +41,20 @@ public abstract class PreferencesExporter { | ||||
|     private static final Logger log = LoggerFactory.getLogger(PreferencesExporter.class); | ||||
|     private static final net.sf.openrocket.startup.Preferences prefs = Application.getPreferences(); | ||||
| 
 | ||||
|     private static final String[] userDirectoriesKeysToIgnore;      // Preference keys to ignore when exporting user directories (= keys that export user directories) | ||||
|     private static final String[] prefixKeysToIgnore;              // Preference keys to ignore when exporting user directories (= keys that start with these prefixes) | ||||
|     private static final List<String> keysToIgnore = new ArrayList<>();         // Preference keys to ignore when exporting user directories (= keys that export user directories) | ||||
|     private static final List<String> prefixKeysToIgnore = new ArrayList<>();   // Preference keys to ignore when exporting user directories (= keys that start with these prefixes), e.g. | ||||
|     private static final List<String> nodesToIgnore = new ArrayList<>();        // Preferences nodes that should not be exported | ||||
| 
 | ||||
|     static { | ||||
|         userDirectoriesKeysToIgnore = new String[] { | ||||
|                 net.sf.openrocket.startup.Preferences.USER_THRUST_CURVES_KEY, | ||||
|                 net.sf.openrocket.startup.Preferences.DEFAULT_DIRECTORY | ||||
|         }; | ||||
|         prefixKeysToIgnore = new String[] { | ||||
|                 MRUDesignFile.MRU_FILE_LIST_PROPERTY | ||||
|         }; | ||||
|         if (!prefs.getExportUserDirectories()) { | ||||
|             keysToIgnore.add(net.sf.openrocket.startup.Preferences.USER_THRUST_CURVES_KEY); | ||||
|             keysToIgnore.add(net.sf.openrocket.startup.Preferences.DEFAULT_DIRECTORY); | ||||
|             prefixKeysToIgnore.add(MRUDesignFile.MRU_FILE_LIST_PROPERTY); | ||||
|         } | ||||
| 
 | ||||
|         if (!prefs.getExportWindowInformation()) { | ||||
|             nodesToIgnore.add(SwingPreferences.NODE_WINDOWS); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| @ -64,7 +70,7 @@ public abstract class PreferencesExporter { | ||||
|         // The macOS file chooser has an issue where it does not update its size when the accessory is added. | ||||
|         if (SystemInfo.getPlatform() == SystemInfo.Platform.MAC_OS) { | ||||
|             Dimension currentSize = chooser.getPreferredSize(); | ||||
|             Dimension newSize = new Dimension((int) (1.25 * currentSize.width), (int) (1.2 * currentSize.height)); | ||||
|             Dimension newSize = new Dimension((int) (1.35 * currentSize.width), (int) (1.2 * currentSize.height)); | ||||
|             chooser.setPreferredSize(newSize); | ||||
|         } | ||||
| 
 | ||||
| @ -89,7 +95,7 @@ public abstract class PreferencesExporter { | ||||
|         } | ||||
| 
 | ||||
|         try (FileOutputStream fos = new FileOutputStream(newFile)) { | ||||
|             if (prefs.getExportUserDirectories()) { | ||||
|             if (keysToIgnore.isEmpty() && nodesToIgnore.isEmpty() && prefixKeysToIgnore.isEmpty()) { | ||||
|                 // Export all preferences | ||||
|                 preferences.exportSubtree(fos); | ||||
|             } else { | ||||
| @ -115,7 +121,7 @@ public abstract class PreferencesExporter { | ||||
|         Preferences tempPrefs = root.node(nodeName); | ||||
| 
 | ||||
|         // Fill in all parameters to the temporary preferences, except for user directories | ||||
|         copyFilteredPreferences(preferences, tempPrefs, userDirectoriesKeysToIgnore, prefixKeysToIgnore); | ||||
|         copyFilteredPreferences(preferences, tempPrefs, nodesToIgnore, keysToIgnore, prefixKeysToIgnore); | ||||
| 
 | ||||
|         // Export the filtered preferences | ||||
|         try { | ||||
| @ -163,32 +169,23 @@ public abstract class PreferencesExporter { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private static void copyFilteredPreferences(Preferences src, Preferences dest, String[] keysToIgnore, String[] prefixKeysToIgnore) throws BackingStoreException { | ||||
|     private static void copyFilteredPreferences(Preferences src, Preferences dest, | ||||
|                 List<String> nodesToIgnore, List<String> keysToIgnore, List<String> prefixKeysToIgnore) throws BackingStoreException { | ||||
|         for (String key : src.keys()) { | ||||
|             boolean ignoreKey = false; | ||||
|             for (String keyToIgnore : keysToIgnore) { | ||||
|                 if (key.equals(keyToIgnore)) { | ||||
|                     ignoreKey = true; | ||||
|                     break; | ||||
|             if (keysToIgnore.contains(key) | ||||
|                     || prefixKeysToIgnore.stream().anyMatch(key::startsWith)) { | ||||
|                 continue; | ||||
|             } | ||||
|             } | ||||
|             if (!ignoreKey) { | ||||
|                 for (String prefixKeyToIgnore : prefixKeysToIgnore) { | ||||
|                     if (key.startsWith(prefixKeyToIgnore)) { | ||||
|                         ignoreKey = true; | ||||
|                         break; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|             if (!ignoreKey) { | ||||
|             dest.put(key, src.get(key, null)); | ||||
|         } | ||||
|         } | ||||
| 
 | ||||
|         for (String childNodeName : src.childrenNames()) { | ||||
|             if (nodesToIgnore.contains(childNodeName)) { | ||||
|                 continue; | ||||
|             } | ||||
|             Preferences srcChild = src.node(childNodeName); | ||||
|             Preferences destChild = dest.node(childNodeName); | ||||
|             copyFilteredPreferences(srcChild, destChild, keysToIgnore, prefixKeysToIgnore); | ||||
|             copyFilteredPreferences(srcChild, destChild, nodesToIgnore, keysToIgnore, prefixKeysToIgnore); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -40,6 +40,8 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences { | ||||
| 	private static final String SPLIT_CHARACTER = "|"; | ||||
| 
 | ||||
| 
 | ||||
| 	public static final String NODE_WINDOWS = "windows"; | ||||
| 	 | ||||
| 	private static final List<Locale> SUPPORTED_LOCALES; | ||||
| 	static { | ||||
| 		List<Locale> list = new ArrayList<Locale>(); | ||||
| @ -378,7 +380,7 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences { | ||||
| 	 | ||||
| 	public Point getWindowPosition(Class<?> c) { | ||||
| 		int x, y; | ||||
| 		String pref = PREFNODE.node("windows").get("position." + c.getCanonicalName(), null); | ||||
| 		String pref = PREFNODE.node(NODE_WINDOWS).get("position." + c.getCanonicalName(), null); | ||||
| 		 | ||||
| 		if (pref == null) | ||||
| 			return null; | ||||
| @ -396,7 +398,7 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences { | ||||
| 	} | ||||
| 	 | ||||
| 	public void setWindowPosition(Class<?> c, Point p) { | ||||
| 		PREFNODE.node("windows").put("position." + c.getCanonicalName(), "" + p.x + "," + p.y); | ||||
| 		PREFNODE.node(NODE_WINDOWS).put("position." + c.getCanonicalName(), "" + p.x + "," + p.y); | ||||
| 		storeVersion(); | ||||
| 	} | ||||
| 	 | ||||
| @ -405,7 +407,7 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences { | ||||
| 	 | ||||
| 	public Dimension getWindowSize(Class<?> c) { | ||||
| 		int x, y; | ||||
| 		String pref = PREFNODE.node("windows").get("size." + c.getCanonicalName(), null); | ||||
| 		String pref = PREFNODE.node(NODE_WINDOWS).get("size." + c.getCanonicalName(), null); | ||||
| 		 | ||||
| 		if (pref == null) | ||||
| 			return null; | ||||
| @ -424,17 +426,17 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences { | ||||
| 	 | ||||
| 	 | ||||
| 	public boolean isWindowMaximized(Class<?> c) { | ||||
| 		String pref = PREFNODE.node("windows").get("size." + c.getCanonicalName(), null); | ||||
| 		String pref = PREFNODE.node(NODE_WINDOWS).get("size." + c.getCanonicalName(), null); | ||||
| 		return "max".equals(pref); | ||||
| 	} | ||||
| 	 | ||||
| 	public void setWindowSize(Class<?> c, Dimension d) { | ||||
| 		PREFNODE.node("windows").put("size." + c.getCanonicalName(), "" + d.width + "," + d.height); | ||||
| 		PREFNODE.node(NODE_WINDOWS).put("size." + c.getCanonicalName(), "" + d.width + "," + d.height); | ||||
| 		storeVersion(); | ||||
| 	} | ||||
| 	 | ||||
| 	public void setWindowMaximized(Class<?> c) { | ||||
| 		PREFNODE.node("windows").put("size." + c.getCanonicalName(), "max"); | ||||
| 		PREFNODE.node(NODE_WINDOWS).put("size." + c.getCanonicalName(), "max"); | ||||
| 		storeVersion(); | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user