From: kruland2607 Date: Tue, 13 Dec 2011 19:40:54 +0000 (+0000) Subject: Move net.sf.openrocket.util.Prefs to net.sf.openrocket.gui.util.SwingPreferences. X-Git-Tag: upstream/12.03~1^2~240 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=7a1b9ea2f71f721b508dd05446811f4dd3ce9349;p=debian%2Fopenrocket Move net.sf.openrocket.util.Prefs to net.sf.openrocket.gui.util.SwingPreferences. git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@230 180e2498-e6e9-4542-8430-84ac67f01cd8 --- diff --git a/src/net/sf/openrocket/database/Databases.java b/src/net/sf/openrocket/database/Databases.java index 2a7a38c1..fbb407e8 100644 --- a/src/net/sf/openrocket/database/Databases.java +++ b/src/net/sf/openrocket/database/Databases.java @@ -1,12 +1,12 @@ package net.sf.openrocket.database; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.material.Material; import net.sf.openrocket.material.MaterialStorage; import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.MathUtil; -import net.sf.openrocket.util.Prefs; /** @@ -95,7 +95,7 @@ public class Databases { // Add user-defined materials - for (Material m : ((Prefs) Application.getPreferences()).getUserMaterials()) { + for (Material m : ((SwingPreferences) Application.getPreferences()).getUserMaterials()) { switch (m.getType()) { case LINE: LINE_MATERIAL.add(m); diff --git a/src/net/sf/openrocket/gui/components/SimulationExportPanel.java b/src/net/sf/openrocket/gui/components/SimulationExportPanel.java index 7261ad54..69bc02f0 100644 --- a/src/net/sf/openrocket/gui/components/SimulationExportPanel.java +++ b/src/net/sf/openrocket/gui/components/SimulationExportPanel.java @@ -23,6 +23,7 @@ import net.miginfocom.swing.MigLayout; import net.sf.openrocket.document.Simulation; import net.sf.openrocket.gui.util.FileHelper; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.SaveCSVWorker; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.simulation.FlightData; @@ -31,7 +32,6 @@ import net.sf.openrocket.simulation.FlightDataType; import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.Unit; import net.sf.openrocket.unit.UnitGroup; -import net.sf.openrocket.util.Prefs; public class SimulationExportPanel extends JPanel { @@ -86,7 +86,7 @@ public class SimulationExportPanel extends JPanel { selected = new boolean[types.length]; units = new Unit[types.length]; for (int i = 0; i < types.length; i++) { - selected[i] = ((Prefs) Application.getPreferences()).isExportSelected(types[i]); + selected[i] = ((SwingPreferences) Application.getPreferences()).isExportSelected(types[i]); units[i] = types[i].getUnitGroup().getDefaultUnit(); } @@ -193,7 +193,7 @@ public class SimulationExportPanel extends JPanel { private void doExport() { JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(FileHelper.CSV_FILE_FILTER); - chooser.setCurrentDirectory(((Prefs) Application.getPreferences()).getDefaultDirectory()); + chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return; @@ -217,9 +217,9 @@ public class SimulationExportPanel extends JPanel { // Store preferences and export int n = 0; - ((Prefs) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory()); + ((SwingPreferences) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory()); for (int i = 0; i < selected.length; i++) { - ((Prefs) Application.getPreferences()).setExportSelected(types[i], selected[i]); + ((SwingPreferences) Application.getPreferences()).setExportSelected(types[i], selected[i]); if (selected[i]) n++; } diff --git a/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java b/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java index c0079208..f4dfa8ba 100644 --- a/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java +++ b/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java @@ -36,6 +36,7 @@ import net.sf.openrocket.gui.components.StyledLabel; import net.sf.openrocket.gui.components.StyledLabel.Style; import net.sf.openrocket.gui.components.UnitSelector; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.material.Material; import net.sf.openrocket.rocketcomponent.ComponentAssembly; @@ -47,7 +48,6 @@ import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.util.Invalidatable; import net.sf.openrocket.util.LineStyle; -import net.sf.openrocket.util.Prefs; public class RocketComponentConfig extends JPanel { @@ -379,7 +379,7 @@ public class RocketComponentConfig extends JPanel { public void actionPerformed(ActionEvent e) { Color c = component.getColor(); if (c == null) { - c = ((Prefs) Application.getPreferences()).getDefaultColor(component.getClass()); + c = ((SwingPreferences) Application.getPreferences()).getDefaultColor(component.getClass()); } //// Choose color @@ -401,7 +401,7 @@ public class RocketComponentConfig extends JPanel { if (colorDefault.isSelected()) component.setColor(null); else - component.setColor(((Prefs) Application.getPreferences()).getDefaultColor(component.getClass())); + component.setColor(((SwingPreferences) Application.getPreferences()).getDefaultColor(component.getClass())); } }); panel.add(colorDefault, "wrap para"); @@ -423,7 +423,7 @@ public class RocketComponentConfig extends JPanel { @Override public void actionPerformed(ActionEvent e) { if (component.getColor() != null) { - ((Prefs) Application.getPreferences()).setDefaultColor(component.getClass(), component.getColor()); + ((SwingPreferences) Application.getPreferences()).setDefaultColor(component.getClass(), component.getColor()); component.setColor(null); } if (component.getLineStyle() != null) { @@ -441,7 +441,7 @@ public class RocketComponentConfig extends JPanel { private Color getColor() { Color c = component.getColor(); if (c == null) { - c = ((Prefs) Application.getPreferences()).getDefaultColor(component.getClass()); + c = ((SwingPreferences) Application.getPreferences()).getDefaultColor(component.getClass()); } return c; } diff --git a/src/net/sf/openrocket/gui/dialogs/PrintDialog.java b/src/net/sf/openrocket/gui/dialogs/PrintDialog.java index 383e5571..729586d3 100644 --- a/src/net/sf/openrocket/gui/dialogs/PrintDialog.java +++ b/src/net/sf/openrocket/gui/dialogs/PrintDialog.java @@ -37,11 +37,11 @@ import net.sf.openrocket.gui.print.components.CheckTreeManager; import net.sf.openrocket.gui.print.components.RocketPrintTree; import net.sf.openrocket.gui.util.FileHelper; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.Prefs; /** * This class isolates the Swing components used to create a panel that is added to a standard Java print dialog. @@ -63,7 +63,7 @@ public class PrintDialog extends JDialog implements TreeSelectionListener { private JButton cancel; - private final static Prefs prefs = (Prefs) Application.getPreferences(); + private final static SwingPreferences prefs = (SwingPreferences) Application.getPreferences(); /** * Constructor. @@ -330,7 +330,7 @@ public class PrintDialog extends JDialog implements TreeSelectionListener { dir = dir.getParentFile(); } if (dir == null) { - dir = ((Prefs) Application.getPreferences()).getDefaultDirectory(); + dir = prefs.getDefaultDirectory(); } chooser.setCurrentDirectory(dir); diff --git a/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java b/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java index 3c35113d..7b978d95 100644 --- a/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java +++ b/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java @@ -49,6 +49,7 @@ import net.sf.openrocket.gui.dialogs.motor.CloseableDialog; import net.sf.openrocket.gui.dialogs.motor.MotorSelector; import net.sf.openrocket.gui.util.GUIUtil; import net.sf.openrocket.gui.util.Icons; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.motor.Motor; @@ -57,7 +58,6 @@ import net.sf.openrocket.motor.ThrustCurveMotor; import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.util.BugException; -import net.sf.openrocket.util.Prefs; import net.sf.openrocket.utils.MotorCorrelation; import org.jfree.chart.ChartColor; @@ -234,11 +234,11 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec //// Hide very similar thrust curves hideSimilarBox = new JCheckBox(trans.get("TCMotorSelPan.checkbox.hideSimilar")); GUIUtil.changeFontSize(hideSimilarBox, -1); - hideSimilarBox.setSelected(Application.getPreferences().getBoolean(Prefs.MOTOR_HIDE_SIMILAR, true)); + hideSimilarBox.setSelected(Application.getPreferences().getBoolean(SwingPreferences.MOTOR_HIDE_SIMILAR, true)); hideSimilarBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Application.getPreferences().putBoolean(Prefs.MOTOR_HIDE_SIMILAR, hideSimilarBox.isSelected()); + Application.getPreferences().putBoolean(SwingPreferences.MOTOR_HIDE_SIMILAR, hideSimilarBox.isSelected()); updateData(); } }); @@ -541,7 +541,7 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec // Sets the filter: - int showMode = Application.getPreferences().getChoice(Prefs.MOTOR_DIAMETER_FILTER, SHOW_MAX, SHOW_EXACT); + int showMode = Application.getPreferences().getChoice(SwingPreferences.MOTOR_DIAMETER_FILTER, SHOW_MAX, SHOW_EXACT); filterComboBox.setSelectedIndex(showMode); @@ -585,7 +585,7 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec } // Store selected motor in preferences node, set all others to false - Preferences prefs = ((Prefs) Application.getPreferences()).getNode(Prefs.PREFERRED_THRUST_CURVE_MOTOR_NODE); + Preferences prefs = ((SwingPreferences) Application.getPreferences()).getNode(SwingPreferences.PREFERRED_THRUST_CURVE_MOTOR_NODE); for (ThrustCurveMotor m : set.getMotors()) { String digest = MotorDigest.digestMotor(m); prefs.putBoolean(digest, m == motor); @@ -815,7 +815,7 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec // Find which motor has been used the most recently List list = set.getMotors(); - Preferences prefs = ((Prefs) Application.getPreferences()).getNode(Prefs.PREFERRED_THRUST_CURVE_MOTOR_NODE); + Preferences prefs = ((SwingPreferences) Application.getPreferences()).getNode(SwingPreferences.PREFERRED_THRUST_CURVE_MOTOR_NODE); for (ThrustCurveMotor m : list) { String digest = MotorDigest.digestMotor(m); if (prefs.getBoolean(digest, false)) { diff --git a/src/net/sf/openrocket/gui/dialogs/optimization/GeneralOptimizationDialog.java b/src/net/sf/openrocket/gui/dialogs/optimization/GeneralOptimizationDialog.java index 753ac2a1..033da286 100644 --- a/src/net/sf/openrocket/gui/dialogs/optimization/GeneralOptimizationDialog.java +++ b/src/net/sf/openrocket/gui/dialogs/optimization/GeneralOptimizationDialog.java @@ -67,6 +67,7 @@ import net.sf.openrocket.gui.scalefigure.RocketFigure; import net.sf.openrocket.gui.scalefigure.ScaleScrollPane; import net.sf.openrocket.gui.util.FileHelper; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.optimization.general.OptimizationException; @@ -91,7 +92,6 @@ import net.sf.openrocket.unit.Value; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.Chars; import net.sf.openrocket.util.Named; -import net.sf.openrocket.util.Prefs; import net.sf.openrocket.util.TextUtil; import com.itextpdf.text.Font; @@ -1236,7 +1236,7 @@ public class GeneralOptimizationDialog extends JDialog { JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(FileHelper.CSV_FILE_FILTER); - chooser.setCurrentDirectory(((Prefs) Application.getPreferences()).getDefaultDirectory()); + chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); chooser.setAccessory(csvOptions); if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) diff --git a/src/net/sf/openrocket/gui/dialogs/preferences/PreferencesDialog.java b/src/net/sf/openrocket/gui/dialogs/preferences/PreferencesDialog.java index 5f664d28..986c4a40 100644 --- a/src/net/sf/openrocket/gui/dialogs/preferences/PreferencesDialog.java +++ b/src/net/sf/openrocket/gui/dialogs/preferences/PreferencesDialog.java @@ -37,6 +37,7 @@ import net.sf.openrocket.gui.components.StyledLabel; import net.sf.openrocket.gui.components.StyledLabel.Style; import net.sf.openrocket.gui.dialogs.UpdateInfoDialog; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.SimpleFileFilter; import net.sf.openrocket.l10n.L10N; import net.sf.openrocket.l10n.Translator; @@ -47,7 +48,6 @@ import net.sf.openrocket.unit.Unit; import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.util.BuildProperties; import net.sf.openrocket.util.Named; -import net.sf.openrocket.util.Prefs; import net.sf.openrocket.util.Utils; @@ -96,7 +96,7 @@ public class PreferencesDialog extends JDialog { this.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { - ((Prefs) Application.getPreferences()).storeDefaultUnits(); + ((SwingPreferences) Application.getPreferences()).storeDefaultUnits(); } }); @@ -115,7 +115,7 @@ public class PreferencesDialog extends JDialog { userLocale = L10N.toLocale(locale); } List> locales = new ArrayList>(); - for (Locale l : Prefs.getSupportedLocales()) { + for (Locale l : SwingPreferences.getSupportedLocales()) { locales.add(new Named(l, l.getDisplayLanguage())); } Collections.sort(locales); @@ -144,7 +144,7 @@ public class PreferencesDialog extends JDialog { //// Position to insert new body components: panel.add(new JLabel(trans.get("pref.dlg.lbl.Positiontoinsert")), "gapright para"); - panel.add(new JComboBox(new PrefChoiseSelector(Prefs.BODY_COMPONENT_INSERT_POSITION_KEY, + panel.add(new JComboBox(new PrefChoiseSelector(Preferences.BODY_COMPONENT_INSERT_POSITION_KEY, //// Always ask //// Insert in middle //// Add to end @@ -154,7 +154,7 @@ public class PreferencesDialog extends JDialog { //// Confirm deletion of simulations: panel.add(new JLabel(trans.get("pref.dlg.lbl.Confirmdeletion"))); - panel.add(new JComboBox(new PrefBooleanSelector(Prefs.CONFIRM_DELETE_SIMULATION, + panel.add(new JComboBox(new PrefBooleanSelector(Preferences.CONFIRM_DELETE_SIMULATION, //// Delete //// Confirm trans.get("pref.dlg.PrefBooleanSelector1"), @@ -163,7 +163,7 @@ public class PreferencesDialog extends JDialog { //// User-defined thrust curves: panel.add(new JLabel(trans.get("pref.dlg.lbl.User-definedthrust")), "spanx, wrap"); final JTextField field = new JTextField(); - List files = ((Prefs) Application.getPreferences()).getUserThrustCurveFiles(); + List files = ((SwingPreferences) Application.getPreferences()).getUserThrustCurveFiles(); String str = ""; for (File file : files) { if (str.length() > 0) { @@ -197,7 +197,7 @@ public class PreferencesDialog extends JDialog { list.add(new File(s)); } } - ((Prefs) Application.getPreferences()).setUserThrustCurveFiles(list); + ((SwingPreferences) Application.getPreferences()).setUserThrustCurveFiles(list); } }); panel.add(field, "w 100px, gapright unrel, spanx, growx, split"); @@ -252,8 +252,8 @@ public class PreferencesDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { // First one sets to the default, but does not un-set the pref - field.setText(((Prefs)Application.getPreferences()).getDefaultUserThrustCurveFile().getAbsolutePath()); - ((Prefs) Application.getPreferences()).setUserThrustCurveFiles(null); + field.setText(((SwingPreferences)Application.getPreferences()).getDefaultUserThrustCurveFile().getAbsolutePath()); + ((SwingPreferences) Application.getPreferences()).setUserThrustCurveFiles(null); } }); panel.add(button, "wrap"); @@ -668,9 +668,9 @@ public class PreferencesDialog extends JDialog { UpdateInfoDialog infoDialog = new UpdateInfoDialog(info); infoDialog.setVisible(true); if (infoDialog.isReminderSelected()) { - Application.getPreferences().putString(Prefs.LAST_UPDATE, ""); + Application.getPreferences().putString(SwingPreferences.LAST_UPDATE, ""); } else { - Application.getPreferences().putString(Prefs.LAST_UPDATE, info.getLatestVersion()); + Application.getPreferences().putString(SwingPreferences.LAST_UPDATE, info.getLatestVersion()); } } diff --git a/src/net/sf/openrocket/gui/main/BasicFrame.java b/src/net/sf/openrocket/gui/main/BasicFrame.java index ac88fe58..cb7bd500 100644 --- a/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -87,6 +87,7 @@ import net.sf.openrocket.gui.util.FileHelper; import net.sf.openrocket.gui.util.GUIUtil; import net.sf.openrocket.gui.util.Icons; import net.sf.openrocket.gui.util.OpenFileWorker; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.SaveFileWorker; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.logging.LogHelper; @@ -99,7 +100,6 @@ import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.MemoryManagement; import net.sf.openrocket.util.MemoryManagement.MemoryData; -import net.sf.openrocket.util.Prefs; import net.sf.openrocket.util.Reflection; import net.sf.openrocket.util.TestRockets; @@ -1005,14 +1005,14 @@ public class BasicFrame extends JFrame { chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setMultiSelectionEnabled(true); - chooser.setCurrentDirectory(((Prefs) Application.getPreferences()).getDefaultDirectory()); + chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); int option = chooser.showOpenDialog(this); if (option != JFileChooser.APPROVE_OPTION) { log.user("Decided not to open files, option=" + option); return; } - ((Prefs) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory()); + ((SwingPreferences) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory()); File[] files = chooser.getSelectedFiles(); log.user("Opening files " + Arrays.toString(files)); @@ -1259,7 +1259,7 @@ public class BasicFrame extends JFrame { new StorageOptionChooser(document, document.getDefaultStorageOptions()); JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER); - chooser.setCurrentDirectory(((Prefs) Application.getPreferences()).getDefaultDirectory()); + chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); chooser.setAccessory(storageChooser); if (document.getFile() != null) chooser.setSelectedFile(document.getFile()); @@ -1276,7 +1276,7 @@ public class BasicFrame extends JFrame { return false; } - ((Prefs) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory()); + ((SwingPreferences) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory()); storageChooser.storeOptions(document.getDefaultStorageOptions()); file = FileHelper.ensureExtension(file, "ork"); diff --git a/src/net/sf/openrocket/gui/main/SimulationRunDialog.java b/src/net/sf/openrocket/gui/main/SimulationRunDialog.java index cc1be696..63248f53 100644 --- a/src/net/sf/openrocket/gui/main/SimulationRunDialog.java +++ b/src/net/sf/openrocket/gui/main/SimulationRunDialog.java @@ -24,6 +24,7 @@ import net.miginfocom.swing.MigLayout; import net.sf.openrocket.document.Simulation; import net.sf.openrocket.gui.dialogs.DetailDialog; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.rocketcomponent.Configuration; @@ -40,7 +41,6 @@ import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.Unit; import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.util.MathUtil; -import net.sf.openrocket.util.Prefs; public class SimulationRunDialog extends JDialog { @@ -63,7 +63,7 @@ public class SimulationRunDialog extends JDialog { * dialogs to run in parallel, ie. they both have their own executor service. */ private final ExecutorService executor = Executors.newFixedThreadPool( - Prefs.getMaxThreadCount()); + SwingPreferences.getMaxThreadCount()); private final JLabel simLabel, timeLabel, altLabel, velLabel; diff --git a/src/net/sf/openrocket/gui/print/DesignReport.java b/src/net/sf/openrocket/gui/print/DesignReport.java index 46810ba7..886e225a 100644 --- a/src/net/sf/openrocket/gui/print/DesignReport.java +++ b/src/net/sf/openrocket/gui/print/DesignReport.java @@ -12,6 +12,7 @@ import net.sf.openrocket.document.Simulation; import net.sf.openrocket.gui.figureelements.FigureElement; import net.sf.openrocket.gui.figureelements.RocketInfo; import net.sf.openrocket.gui.scalefigure.RocketPanel; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.masscalc.BasicMassCalculator; import net.sf.openrocket.masscalc.MassCalculator; @@ -29,7 +30,6 @@ import net.sf.openrocket.unit.Unit; import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.util.Chars; import net.sf.openrocket.util.Coordinate; -import net.sf.openrocket.util.Prefs; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; @@ -427,7 +427,7 @@ public class DesignReport { Rocket duplicate = theRocket.copyWithOriginalID(); FlightData flight = null; try { - Simulation simulation = ((Prefs)Application.getPreferences()).getBackgroundSimulation(duplicate); + Simulation simulation = ((SwingPreferences)Application.getPreferences()).getBackgroundSimulation(duplicate); simulation.getOptions().setMotorConfigurationID(motorId); simulation.simulate(); flight = simulation.getSimulatedData(); diff --git a/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java b/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java index b19a8dd5..62b6ded5 100644 --- a/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java +++ b/src/net/sf/openrocket/gui/scalefigure/RocketFigure.java @@ -21,6 +21,7 @@ import java.util.LinkedHashSet; import net.sf.openrocket.gui.figureelements.FigureElement; import net.sf.openrocket.gui.main.ExceptionHandler; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.motor.Motor; import net.sf.openrocket.rocketcomponent.Configuration; import net.sf.openrocket.rocketcomponent.MotorMount; @@ -30,7 +31,6 @@ import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.Coordinate; import net.sf.openrocket.util.LineStyle; import net.sf.openrocket.util.MathUtil; -import net.sf.openrocket.util.Prefs; import net.sf.openrocket.util.Reflection; import net.sf.openrocket.util.Transformation; @@ -312,7 +312,7 @@ public class RocketFigure extends AbstractScaleFigure { // Set component color and line style Color color = c.getColor(); if (color == null) { - color = ((Prefs) Application.getPreferences()).getDefaultColor(c.getClass()); + color = ((SwingPreferences) Application.getPreferences()).getDefaultColor(c.getClass()); } g2.setColor(color); @@ -348,8 +348,8 @@ public class RocketFigure extends AbstractScaleFigure { // Draw motors String motorID = configuration.getMotorConfigurationID(); - Color fillColor = ((Prefs)Application.getPreferences()).getMotorFillColor(); - Color borderColor = ((Prefs)Application.getPreferences()).getMotorBorderColor(); + Color fillColor = ((SwingPreferences)Application.getPreferences()).getMotorFillColor(); + Color borderColor = ((SwingPreferences)Application.getPreferences()).getMotorBorderColor(); Iterator iterator = configuration.motorIterator(); while (iterator.hasNext()) { MotorMount mount = iterator.next(); diff --git a/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java b/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java index 97ff7f3e..cf458724 100644 --- a/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java +++ b/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java @@ -49,6 +49,7 @@ import net.sf.openrocket.gui.figureelements.Caret; import net.sf.openrocket.gui.figureelements.RocketInfo; import net.sf.openrocket.gui.main.SimulationWorker; import net.sf.openrocket.gui.main.componenttree.ComponentTreeModel; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.masscalc.BasicMassCalculator; import net.sf.openrocket.masscalc.MassCalculator; @@ -67,7 +68,6 @@ import net.sf.openrocket.util.ChangeSource; import net.sf.openrocket.util.Chars; import net.sf.openrocket.util.Coordinate; import net.sf.openrocket.util.MathUtil; -import net.sf.openrocket.util.Prefs; import net.sf.openrocket.util.StateChangeListener; /** @@ -121,7 +121,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change */ private static final Executor backgroundSimulationExecutor; static { - backgroundSimulationExecutor = Executors.newFixedThreadPool(Prefs.getMaxThreadCount(), + backgroundSimulationExecutor = Executors.newFixedThreadPool(SwingPreferences.getMaxThreadCount(), new ThreadFactory() { private ThreadFactory factory = Executors.defaultThreadFactory(); @@ -564,7 +564,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change //////// Flight simulation in background // Check whether to compute or not - if (!((Prefs) Application.getPreferences()).computeFlightInBackground()) { + if (!((SwingPreferences) Application.getPreferences()).computeFlightInBackground()) { extraText.setFlightData(null); extraText.setCalculatingData(false); stopBackgroundSimulation(); @@ -594,7 +594,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change extraText.setCalculatingData(true); Rocket duplicate = (Rocket) configuration.getRocket().copy(); - Simulation simulation = ((Prefs)Application.getPreferences()).getBackgroundSimulation(duplicate); + Simulation simulation = ((SwingPreferences)Application.getPreferences()).getBackgroundSimulation(duplicate); simulation.getOptions().setMotorConfigurationID( configuration.getMotorConfigurationID()); diff --git a/src/net/sf/openrocket/gui/util/GUIUtil.java b/src/net/sf/openrocket/gui/util/GUIUtil.java index c66939b3..c796405c 100644 --- a/src/net/sf/openrocket/gui/util/GUIUtil.java +++ b/src/net/sf/openrocket/gui/util/GUIUtil.java @@ -77,7 +77,6 @@ import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.Invalidatable; import net.sf.openrocket.util.MemoryManagement; -import net.sf.openrocket.util.Prefs; public class GUIUtil { private static final LogHelper log = Application.getLogger(); @@ -305,22 +304,22 @@ public class GUIUtil { @Override public void componentResized(ComponentEvent e) { log.debug("Storing size of " + window.getClass().getName() + ": " + window.getSize()); - ((Prefs) Application.getPreferences()).setWindowSize(window.getClass(), window.getSize()); + ((SwingPreferences) Application.getPreferences()).setWindowSize(window.getClass(), window.getSize()); if (window instanceof JFrame) { if ((((JFrame) window).getExtendedState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) { log.debug("Storing maximized state of " + window.getClass().getName()); - ((Prefs) Application.getPreferences()).setWindowMaximized(window.getClass()); + ((SwingPreferences) Application.getPreferences()).setWindowMaximized(window.getClass()); } } } }); - if (((Prefs) Application.getPreferences()).isWindowMaximized(window.getClass())) { + if (((SwingPreferences) Application.getPreferences()).isWindowMaximized(window.getClass())) { if (window instanceof JFrame) { ((JFrame) window).setExtendedState(JFrame.MAXIMIZED_BOTH); } } else { - Dimension dim = ((Prefs) Application.getPreferences()).getWindowSize(window.getClass()); + Dimension dim = ((SwingPreferences) Application.getPreferences()).getWindowSize(window.getClass()); if (dim != null) { window.setSize(dim); } @@ -336,12 +335,12 @@ public class GUIUtil { window.addComponentListener(new ComponentAdapter() { @Override public void componentMoved(ComponentEvent e) { - ((Prefs) Application.getPreferences()).setWindowPosition(window.getClass(), window.getLocation()); + ((SwingPreferences) Application.getPreferences()).setWindowPosition(window.getClass(), window.getLocation()); } }); // Set window position according to preferences, and set prefs when moving - Point position = ((Prefs) Application.getPreferences()).getWindowPosition(window.getClass()); + Point position = ((SwingPreferences) Application.getPreferences()).getWindowPosition(window.getClass()); if (position != null) { window.setLocationByPlatform(false); window.setLocation(position); diff --git a/src/net/sf/openrocket/gui/util/SwingPreferences.java b/src/net/sf/openrocket/gui/util/SwingPreferences.java new file mode 100644 index 00000000..951ce919 --- /dev/null +++ b/src/net/sf/openrocket/gui/util/SwingPreferences.java @@ -0,0 +1,598 @@ +package net.sf.openrocket.gui.util; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Point; +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; + +import net.sf.openrocket.arch.SystemInfo; +import net.sf.openrocket.document.Simulation; +import net.sf.openrocket.gui.main.ExceptionHandler; +import net.sf.openrocket.logging.LogHelper; +import net.sf.openrocket.material.Material; +import net.sf.openrocket.rocketcomponent.BodyComponent; +import net.sf.openrocket.rocketcomponent.FinSet; +import net.sf.openrocket.rocketcomponent.InternalComponent; +import net.sf.openrocket.rocketcomponent.LaunchLug; +import net.sf.openrocket.rocketcomponent.MassObject; +import net.sf.openrocket.rocketcomponent.RecoveryDevice; +import net.sf.openrocket.rocketcomponent.Rocket; +import net.sf.openrocket.rocketcomponent.RocketComponent; +import net.sf.openrocket.simulation.FlightDataType; +import net.sf.openrocket.simulation.RK4SimulationStepper; +import net.sf.openrocket.simulation.SimulationOptions; +import net.sf.openrocket.startup.Application; +import net.sf.openrocket.unit.UnitGroup; +import net.sf.openrocket.util.BugException; +import net.sf.openrocket.util.BuildProperties; + + +public class SwingPreferences extends net.sf.openrocket.startup.Preferences { + private static final LogHelper log = Application.getLogger(); + + private static final String SPLIT_CHARACTER = "|"; + + + private static final List SUPPORTED_LOCALES; + static { + List list = new ArrayList(); + for (String lang : new String[] { "en", "de", "es", "fr" }) { + list.add(new Locale(lang)); + } + SUPPORTED_LOCALES = Collections.unmodifiableList(list); + } + + + /** + * Whether to use the debug-node instead of the normal node. + */ + private static final boolean DEBUG; + static { + DEBUG = (System.getProperty("openrocket.debug.prefs") != null); + } + + /** + * Whether to clear all preferences at application startup. This has an effect only + * if DEBUG is true. + */ + private static final boolean CLEARPREFS = true; + + /** + * The node name to use in the Java preferences storage. + */ + private static final String NODENAME = (DEBUG ? "OpenRocket-debug" : "OpenRocket"); + + private final Preferences PREFNODE; + + + public SwingPreferences() { + Preferences root = Preferences.userRoot(); + if (DEBUG && CLEARPREFS) { + try { + if (root.nodeExists(NODENAME)) { + root.node(NODENAME).removeNode(); + } + } catch (BackingStoreException e) { + throw new BugException("Unable to clear preference node", e); + } + } + PREFNODE = root.node(NODENAME); + } + + + private static final HashMap, String> DEFAULT_COLORS = + new HashMap, String>(); + static { + DEFAULT_COLORS.put(BodyComponent.class, "0,0,240"); + DEFAULT_COLORS.put(FinSet.class, "0,0,200"); + DEFAULT_COLORS.put(LaunchLug.class, "0,0,180"); + DEFAULT_COLORS.put(InternalComponent.class, "170,0,100"); + DEFAULT_COLORS.put(MassObject.class, "0,0,0"); + DEFAULT_COLORS.put(RecoveryDevice.class, "255,0,0"); + } + + + + ////////////////////// + + + + /** + * Store the current OpenRocket version into the preferences to allow for preferences migration. + */ + private void storeVersion() { + PREFNODE.put("OpenRocketVersion", BuildProperties.getVersion()); + } + + /** + * Return a string preference. + * + * @param key the preference key. + * @param def the default if no preference is stored + * @return the preference value + */ + @Override + public String getString(String key, String def) { + return PREFNODE.get(key, def); + } + + @Override + public String getString( String directory, String key, String defaultValue ) { + Preferences p = PREFNODE.node(directory); + return p.get(key,defaultValue); + } + + /** + * Set a string preference. + * + * @param key the preference key + * @param value the value to set, or null to remove the key + */ + @Override + public void putString(String key, String value) { + if (value == null) { + PREFNODE.remove(key); + } else { + PREFNODE.put(key, value); + } + storeVersion(); + } + + @Override + public void putString(String directory, String key, String value ) { + Preferences p = PREFNODE.node(directory); + if ( value == null ) { + p.remove(key); + } else { + p.put(key,value); + } + storeVersion(); + } + + /** + * Return a boolean preference. + * + * @param key the preference key + * @param def the default if no preference is stored + * @return the preference value + */ + @Override + public boolean getBoolean(String key, boolean def) { + return PREFNODE.getBoolean(key, def); + } + + /** + * Set a boolean preference. + * + * @param key the preference key + * @param value the value to set + */ + @Override + public void putBoolean(String key, boolean value) { + PREFNODE.putBoolean(key, value); + storeVersion(); + } + + @Override + public int getInt( String key, int defaultValue ) { + return PREFNODE.getInt(key, defaultValue); + } + + @Override + public void putInt( String key , int value ) { + PREFNODE.putInt(key, value ); + storeVersion(); + } + + @Override + public double getDouble(String key, double defaultValue) { + return PREFNODE.getDouble(key, defaultValue ); + } + + @Override + public void putDouble(String key, double value) { + PREFNODE.putDouble(key,value); + storeVersion(); + } + + + + /** + * Return a preferences object for the specified node name. + * + * @param nodeName the node name + * @return the preferences object for that node + */ + public Preferences getNode(String nodeName) { + return PREFNODE.node(nodeName); + } + + + ////////////////// + + + public static List getSupportedLocales() { + return SUPPORTED_LOCALES; + } + + public File getDefaultDirectory() { + String file = getString("defaultDirectory", null); + if (file == null) + return null; + return new File(file); + } + + public void setDefaultDirectory(File dir) { + String d; + if (dir == null) { + d = null; + } else { + d = dir.getAbsolutePath(); + } + putString("defaultDirectory", d); + storeVersion(); + } + + + /** + * Return a list of files/directories to be loaded as custom thrust curves. + *

+ * If this property has not been set, the directory "ThrustCurves" in the user + * application directory will be used. The directory will be created if it does not + * exist. + * + * @return a list of files to load as thrust curves. + */ + public List getUserThrustCurveFiles() { + List list = new ArrayList(); + + String files = getString(USER_THRUST_CURVES_KEY, null); + if (files == null) { + // Default to application directory + File tcdir = getDefaultUserThrustCurveFile(); + if (!tcdir.isDirectory()) { + tcdir.mkdirs(); + } + list.add(tcdir); + } else { + for (String file : files.split("\\" + SPLIT_CHARACTER)) { + file = file.trim(); + if (file.length() > 0) { + list.add(new File(file)); + } + } + } + + return list; + } + + public File getDefaultUserThrustCurveFile() { + File appdir = SystemInfo.getUserApplicationDirectory(); + File tcdir = new File(appdir, "ThrustCurves"); + return tcdir; + } + + + /** + * Set the list of files/directories to be loaded as custom thrust curves. + * + * @param files the files to load, or null to reset to default value. + */ + public void setUserThrustCurveFiles(List files) { + if (files == null) { + putString(USER_THRUST_CURVES_KEY, null); + return; + } + + String str = ""; + + for (File file : files) { + if (str.length() > 0) { + str += SPLIT_CHARACTER; + } + str += file.getAbsolutePath(); + } + putString(USER_THRUST_CURVES_KEY, str); + } + + public Color getMotorBorderColor() { + // TODO: MEDIUM: Motor color (settable?) + return new Color(0, 0, 0, 200); + } + + + public Color getMotorFillColor() { + // TODO: MEDIUM: Motor fill color (settable?) + return new Color(0, 0, 0, 100); + } + + public Color getDefaultColor(Class c) { + String color = get("componentColors", c, DEFAULT_COLORS); + if (color == null) + return Color.BLACK; + + net.sf.openrocket.util.Color clr = parseColor(color); + if (clr != null) { + return ColorConversion.toAwtColor(clr); + } else { + return Color.BLACK; + } + } + + public final void setDefaultColor(Class c, Color color) { + if (color == null) + return; + putString("componentColors", c.getSimpleName(), stringifyColor(ColorConversion.fromAwtColor(color))); + } + + + + public static int getMaxThreadCount() { + return Runtime.getRuntime().availableProcessors(); + } + + + + public Point getWindowPosition(Class c) { + int x, y; + String pref = PREFNODE.node("windows").get("position." + c.getCanonicalName(), null); + + if (pref == null) + return null; + + if (pref.indexOf(',') < 0) + return null; + + try { + x = Integer.parseInt(pref.substring(0, pref.indexOf(','))); + y = Integer.parseInt(pref.substring(pref.indexOf(',') + 1)); + } catch (NumberFormatException e) { + return null; + } + return new Point(x, y); + } + + public void setWindowPosition(Class c, Point p) { + PREFNODE.node("windows").put("position." + c.getCanonicalName(), "" + p.x + "," + p.y); + storeVersion(); + } + + + + + public Dimension getWindowSize(Class c) { + int x, y; + String pref = PREFNODE.node("windows").get("size." + c.getCanonicalName(), null); + + if (pref == null) + return null; + + if (pref.indexOf(',') < 0) + return null; + + try { + x = Integer.parseInt(pref.substring(0, pref.indexOf(','))); + y = Integer.parseInt(pref.substring(pref.indexOf(',') + 1)); + } catch (NumberFormatException e) { + return null; + } + return new Dimension(x, y); + } + + + public boolean isWindowMaximized(Class c) { + String pref = PREFNODE.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); + storeVersion(); + } + + public void setWindowMaximized(Class c) { + PREFNODE.node("windows").put("size." + c.getCanonicalName(), "max"); + storeVersion(); + } + + /** + * this class returns a java.awt.Color object for the specified key. + * you can pass (java.awt.Color) null to the second argument to + * disambiguate + */ + public Color getColor( String key, Color defaultValue ) { + net.sf.openrocket.util.Color c = super.getColor(key, (net.sf.openrocket.util.Color) null); + if ( c == null ) { + return defaultValue; + } + return ColorConversion.toAwtColor(c); + } + + /** + * + */ + public void putColor( String key, Color value ) { + net.sf.openrocket.util.Color c = ColorConversion.fromAwtColor(value); + super.putColor(key, c); + } + + //// Printing + + + //// Background flight data computation + + public boolean computeFlightInBackground() { + return PREFNODE.getBoolean("backgroundFlight", true); + } + + public Simulation getBackgroundSimulation(Rocket rocket) { + Simulation s = new Simulation(rocket); + SimulationOptions cond = s.getOptions(); + + cond.setTimeStep(RK4SimulationStepper.RECOMMENDED_TIME_STEP * 2); + cond.setWindSpeedAverage(1.0); + cond.setWindSpeedDeviation(0.1); + cond.setLaunchRodLength(5); + return s; + } + + + + ///////// Export variables + + public boolean isExportSelected(FlightDataType type) { + Preferences prefs = PREFNODE.node("exports"); + return prefs.getBoolean(type.getName(), false); + } + + public void setExportSelected(FlightDataType type, boolean selected) { + Preferences prefs = PREFNODE.node("exports"); + prefs.putBoolean(type.getName(), selected); + } + + + + ///////// Default unit storage + + public void loadDefaultUnits() { + Preferences prefs = PREFNODE.node("units"); + try { + + for (String key : prefs.keys()) { + UnitGroup group = UnitGroup.UNITS.get(key); + if (group == null) + continue; + + try { + group.setDefaultUnit(prefs.get(key, null)); + } catch (IllegalArgumentException ignore) { + } + } + + } catch (BackingStoreException e) { + ExceptionHandler.handleErrorCondition(e); + } + } + + public void storeDefaultUnits() { + Preferences prefs = PREFNODE.node("units"); + + for (String key : UnitGroup.UNITS.keySet()) { + UnitGroup group = UnitGroup.UNITS.get(key); + if (group == null || group.getUnitCount() < 2) + continue; + + prefs.put(key, group.getDefaultUnit().getUnit()); + } + } + + + + //// Material storage + + + /** + * Add a user-defined material to the preferences. The preferences are + * first checked for an existing material matching the provided one using + * {@link Material#equals(Object)}. + * + * @param m the material to add. + */ + public void addUserMaterial(Material m) { + Preferences prefs = PREFNODE.node("userMaterials"); + + + // Check whether material already exists + if (getUserMaterials().contains(m)) { + return; + } + + // Add material using next free key (key is not used when loading) + String mat = m.toStorableString(); + for (int i = 0;; i++) { + String key = "material" + i; + if (prefs.get(key, null) == null) { + prefs.put(key, mat); + return; + } + } + } + + + /** + * Remove a user-defined material from the preferences. The matching is performed + * using {@link Material#equals(Object)}. + * + * @param m the material to remove. + */ + public void removeUserMaterial(Material m) { + Preferences prefs = PREFNODE.node("userMaterials"); + + try { + + // Iterate through materials and remove all keys with a matching material + for (String key : prefs.keys()) { + String value = prefs.get(key, null); + try { + + Material existing = Material.fromStorableString(value, true); + if (existing.equals(m)) { + prefs.remove(key); + } + + } catch (IllegalArgumentException ignore) { + } + + } + + } catch (BackingStoreException e) { + throw new IllegalStateException("Cannot read preferences!", e); + } + } + + + /** + * Return a set of all user-defined materials in the preferences. The materials + * are created marked as user-defined. + * + * @return a set of all user-defined materials. + */ + public Set getUserMaterials() { + Preferences prefs = PREFNODE.node("userMaterials"); + + HashSet materials = new HashSet(); + try { + + for (String key : prefs.keys()) { + String value = prefs.get(key, null); + try { + + Material m = Material.fromStorableString(value, true); + materials.add(m); + + } catch (IllegalArgumentException e) { + log.warn("Illegal material string " + value); + } + + } + + } catch (BackingStoreException e) { + throw new IllegalStateException("Cannot read preferences!", e); + } + + return materials; + } + + + //// Helper methods + +} diff --git a/src/net/sf/openrocket/material/MaterialStorage.java b/src/net/sf/openrocket/material/MaterialStorage.java index 6624283e..5438ece2 100644 --- a/src/net/sf/openrocket/material/MaterialStorage.java +++ b/src/net/sf/openrocket/material/MaterialStorage.java @@ -2,8 +2,8 @@ package net.sf.openrocket.material; import net.sf.openrocket.database.Database; import net.sf.openrocket.database.DatabaseListener; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.Prefs; /** * Class for storing changes to user-added materials. The materials are stored to @@ -16,13 +16,13 @@ public class MaterialStorage implements DatabaseListener { @Override public void elementAdded(Material material, Database source) { if (material.isUserDefined()) { - ((Prefs) Application.getPreferences()).addUserMaterial(material); + ((SwingPreferences) Application.getPreferences()).addUserMaterial(material); } } @Override public void elementRemoved(Material material, Database source) { - ((Prefs) Application.getPreferences()).removeUserMaterial(material); + ((SwingPreferences) Application.getPreferences()).removeUserMaterial(material); } } diff --git a/src/net/sf/openrocket/startup/Startup.java b/src/net/sf/openrocket/startup/Startup.java index be21dc75..d288ea76 100644 --- a/src/net/sf/openrocket/startup/Startup.java +++ b/src/net/sf/openrocket/startup/Startup.java @@ -4,6 +4,7 @@ import java.io.PrintStream; import java.util.Locale; import java.util.prefs.Preferences; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.DebugTranslator; import net.sf.openrocket.l10n.L10N; import net.sf.openrocket.l10n.ResourceBundleTranslator; @@ -13,7 +14,6 @@ import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.logging.LogLevel; import net.sf.openrocket.logging.LogLevelBufferLogger; import net.sf.openrocket.logging.PrintStreamLogger; -import net.sf.openrocket.util.Prefs; /** @@ -50,7 +50,7 @@ public class Startup { // Initialize logging first so we can use it initializeLogging(); - Application.setPreferences( new Prefs() ); + Application.setPreferences( new SwingPreferences() ); // Setup the translations initializeL10n(); diff --git a/src/net/sf/openrocket/startup/Startup2.java b/src/net/sf/openrocket/startup/Startup2.java index e1c2e05d..3c4f1dab 100644 --- a/src/net/sf/openrocket/startup/Startup2.java +++ b/src/net/sf/openrocket/startup/Startup2.java @@ -24,12 +24,12 @@ import net.sf.openrocket.gui.main.BasicFrame; import net.sf.openrocket.gui.main.ExceptionHandler; import net.sf.openrocket.gui.main.Splash; import net.sf.openrocket.gui.util.GUIUtil; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.SimpleFileFilter; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.motor.Motor; import net.sf.openrocket.motor.ThrustCurveMotor; import net.sf.openrocket.util.BuildProperties; -import net.sf.openrocket.util.Prefs; /** * The second class in the OpenRocket startup sequence. This class can assume the @@ -113,7 +113,7 @@ public class Startup2 { ToolTipManager.sharedInstance().setDismissDelay(30000); // Load defaults - ((Prefs) Application.getPreferences()).loadDefaultUnits(); + ((SwingPreferences) Application.getPreferences()).loadDefaultUnits(); // Load motors etc. log.info("Loading databases"); @@ -193,7 +193,7 @@ public class Startup2 { thrustCurveCount = list.size(); // Load the user-defined thrust curves - for (File file : ((Prefs) Application.getPreferences()).getUserThrustCurveFiles()) { + for (File file : ((SwingPreferences) Application.getPreferences()).getUserThrustCurveFiles()) { log.info("Loading motors from " + file); list = MotorLoaderHelper.load(file); for (Motor m : list) { @@ -243,7 +243,7 @@ public class Startup2 { timer.stop(); String current = BuildProperties.getVersion(); - String last = Application.getPreferences().getString(Prefs.LAST_UPDATE, ""); + String last = Application.getPreferences().getString(Preferences.LAST_UPDATE, ""); UpdateInfo info = updateInfo.getUpdateInfo(); if (info != null && info.getLatestVersion() != null && @@ -253,9 +253,9 @@ public class Startup2 { UpdateInfoDialog infoDialog = new UpdateInfoDialog(info); infoDialog.setVisible(true); if (infoDialog.isReminderSelected()) { - Application.getPreferences().putString(Prefs.LAST_UPDATE, ""); + Application.getPreferences().putString(Preferences.LAST_UPDATE, ""); } else { - Application.getPreferences().putString(Prefs.LAST_UPDATE, info.getLatestVersion()); + Application.getPreferences().putString(Preferences.LAST_UPDATE, info.getLatestVersion()); } } } diff --git a/src/net/sf/openrocket/util/Prefs.java b/src/net/sf/openrocket/util/Prefs.java deleted file mode 100644 index d6a42fcc..00000000 --- a/src/net/sf/openrocket/util/Prefs.java +++ /dev/null @@ -1,609 +0,0 @@ -package net.sf.openrocket.util; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Point; -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Set; -import java.util.prefs.BackingStoreException; -import java.util.prefs.Preferences; - -import net.sf.openrocket.arch.SystemInfo; -import net.sf.openrocket.document.Simulation; -import net.sf.openrocket.gui.main.ExceptionHandler; -import net.sf.openrocket.gui.util.ColorConversion; -import net.sf.openrocket.logging.LogHelper; -import net.sf.openrocket.material.Material; -import net.sf.openrocket.rocketcomponent.BodyComponent; -import net.sf.openrocket.rocketcomponent.FinSet; -import net.sf.openrocket.rocketcomponent.InternalComponent; -import net.sf.openrocket.rocketcomponent.LaunchLug; -import net.sf.openrocket.rocketcomponent.MassObject; -import net.sf.openrocket.rocketcomponent.RecoveryDevice; -import net.sf.openrocket.rocketcomponent.Rocket; -import net.sf.openrocket.rocketcomponent.RocketComponent; -import net.sf.openrocket.simulation.FlightDataType; -import net.sf.openrocket.simulation.RK4SimulationStepper; -import net.sf.openrocket.simulation.SimulationOptions; -import net.sf.openrocket.startup.Application; -import net.sf.openrocket.unit.UnitGroup; - - -public class Prefs extends net.sf.openrocket.startup.Preferences { - private static final LogHelper log = Application.getLogger(); - - private static final String SPLIT_CHARACTER = "|"; - - - private static final List SUPPORTED_LOCALES; - static { - List list = new ArrayList(); - for (String lang : new String[] { "en", "de", "es", "fr" }) { - list.add(new Locale(lang)); - } - SUPPORTED_LOCALES = Collections.unmodifiableList(list); - } - - - /** - * Whether to use the debug-node instead of the normal node. - */ - private static final boolean DEBUG; - static { - DEBUG = (System.getProperty("openrocket.debug.prefs") != null); - } - - /** - * Whether to clear all preferences at application startup. This has an effect only - * if DEBUG is true. - */ - private static final boolean CLEARPREFS = true; - - /** - * The node name to use in the Java preferences storage. - */ - private static final String NODENAME = (DEBUG ? "OpenRocket-debug" : "OpenRocket"); - - /** - * Return whether to use additional safety code checks. - */ - public static boolean useSafetyChecks() { - // Currently default to false unless openrocket.debug.safetycheck is defined - String s = System.getProperty("openrocket.debug.safetycheck"); - if (s != null && !(s.equalsIgnoreCase("false") || s.equalsIgnoreCase("off"))) { - return true; - } - return false; - } - - private final Preferences PREFNODE; - - - public Prefs() { - Preferences root = Preferences.userRoot(); - if (DEBUG && CLEARPREFS) { - try { - if (root.nodeExists(NODENAME)) { - root.node(NODENAME).removeNode(); - } - } catch (BackingStoreException e) { - throw new BugException("Unable to clear preference node", e); - } - } - PREFNODE = root.node(NODENAME); - } - - - private static final HashMap, String> DEFAULT_COLORS = - new HashMap, String>(); - static { - DEFAULT_COLORS.put(BodyComponent.class, "0,0,240"); - DEFAULT_COLORS.put(FinSet.class, "0,0,200"); - DEFAULT_COLORS.put(LaunchLug.class, "0,0,180"); - DEFAULT_COLORS.put(InternalComponent.class, "170,0,100"); - DEFAULT_COLORS.put(MassObject.class, "0,0,0"); - DEFAULT_COLORS.put(RecoveryDevice.class, "255,0,0"); - } - - - - ////////////////////// - - - - /** - * Store the current OpenRocket version into the preferences to allow for preferences migration. - */ - private void storeVersion() { - PREFNODE.put("OpenRocketVersion", BuildProperties.getVersion()); - } - - /** - * Return a string preference. - * - * @param key the preference key. - * @param def the default if no preference is stored - * @return the preference value - */ - @Override - public String getString(String key, String def) { - return PREFNODE.get(key, def); - } - - @Override - public String getString( String directory, String key, String defaultValue ) { - Preferences p = PREFNODE.node(directory); - return p.get(key,defaultValue); - } - - /** - * Set a string preference. - * - * @param key the preference key - * @param value the value to set, or null to remove the key - */ - @Override - public void putString(String key, String value) { - if (value == null) { - PREFNODE.remove(key); - } else { - PREFNODE.put(key, value); - } - storeVersion(); - } - - @Override - public void putString(String directory, String key, String value ) { - Preferences p = PREFNODE.node(directory); - if ( value == null ) { - p.remove(key); - } else { - p.put(key,value); - } - storeVersion(); - } - - /** - * Return a boolean preference. - * - * @param key the preference key - * @param def the default if no preference is stored - * @return the preference value - */ - @Override - public boolean getBoolean(String key, boolean def) { - return PREFNODE.getBoolean(key, def); - } - - /** - * Set a boolean preference. - * - * @param key the preference key - * @param value the value to set - */ - @Override - public void putBoolean(String key, boolean value) { - PREFNODE.putBoolean(key, value); - storeVersion(); - } - - @Override - public int getInt( String key, int defaultValue ) { - return PREFNODE.getInt(key, defaultValue); - } - - @Override - public void putInt( String key , int value ) { - PREFNODE.putInt(key, value ); - storeVersion(); - } - - @Override - public double getDouble(String key, double defaultValue) { - return PREFNODE.getDouble(key, defaultValue ); - } - - @Override - public void putDouble(String key, double value) { - PREFNODE.putDouble(key,value); - storeVersion(); - } - - - - /** - * Return a preferences object for the specified node name. - * - * @param nodeName the node name - * @return the preferences object for that node - */ - public Preferences getNode(String nodeName) { - return PREFNODE.node(nodeName); - } - - - ////////////////// - - - public static List getSupportedLocales() { - return SUPPORTED_LOCALES; - } - - public File getDefaultDirectory() { - String file = getString("defaultDirectory", null); - if (file == null) - return null; - return new File(file); - } - - public void setDefaultDirectory(File dir) { - String d; - if (dir == null) { - d = null; - } else { - d = dir.getAbsolutePath(); - } - putString("defaultDirectory", d); - storeVersion(); - } - - - /** - * Return a list of files/directories to be loaded as custom thrust curves. - *

- * If this property has not been set, the directory "ThrustCurves" in the user - * application directory will be used. The directory will be created if it does not - * exist. - * - * @return a list of files to load as thrust curves. - */ - public List getUserThrustCurveFiles() { - List list = new ArrayList(); - - String files = getString(USER_THRUST_CURVES_KEY, null); - if (files == null) { - // Default to application directory - File tcdir = getDefaultUserThrustCurveFile(); - if (!tcdir.isDirectory()) { - tcdir.mkdirs(); - } - list.add(tcdir); - } else { - for (String file : files.split("\\" + SPLIT_CHARACTER)) { - file = file.trim(); - if (file.length() > 0) { - list.add(new File(file)); - } - } - } - - return list; - } - - public File getDefaultUserThrustCurveFile() { - File appdir = SystemInfo.getUserApplicationDirectory(); - File tcdir = new File(appdir, "ThrustCurves"); - return tcdir; - } - - - /** - * Set the list of files/directories to be loaded as custom thrust curves. - * - * @param files the files to load, or null to reset to default value. - */ - public void setUserThrustCurveFiles(List files) { - if (files == null) { - putString(USER_THRUST_CURVES_KEY, null); - return; - } - - String str = ""; - - for (File file : files) { - if (str.length() > 0) { - str += SPLIT_CHARACTER; - } - str += file.getAbsolutePath(); - } - putString(USER_THRUST_CURVES_KEY, str); - } - - public Color getMotorBorderColor() { - // TODO: MEDIUM: Motor color (settable?) - return new Color(0, 0, 0, 200); - } - - - public Color getMotorFillColor() { - // TODO: MEDIUM: Motor fill color (settable?) - return new Color(0, 0, 0, 100); - } - - public Color getDefaultColor(Class c) { - String color = get("componentColors", c, DEFAULT_COLORS); - if (color == null) - return Color.BLACK; - - net.sf.openrocket.util.Color clr = parseColor(color); - if (clr != null) { - return ColorConversion.toAwtColor(clr); - } else { - return Color.BLACK; - } - } - - public final void setDefaultColor(Class c, Color color) { - if (color == null) - return; - putString("componentColors", c.getSimpleName(), stringifyColor(ColorConversion.fromAwtColor(color))); - } - - - - public static int getMaxThreadCount() { - return Runtime.getRuntime().availableProcessors(); - } - - - - public Point getWindowPosition(Class c) { - int x, y; - String pref = PREFNODE.node("windows").get("position." + c.getCanonicalName(), null); - - if (pref == null) - return null; - - if (pref.indexOf(',') < 0) - return null; - - try { - x = Integer.parseInt(pref.substring(0, pref.indexOf(','))); - y = Integer.parseInt(pref.substring(pref.indexOf(',') + 1)); - } catch (NumberFormatException e) { - return null; - } - return new Point(x, y); - } - - public void setWindowPosition(Class c, Point p) { - PREFNODE.node("windows").put("position." + c.getCanonicalName(), "" + p.x + "," + p.y); - storeVersion(); - } - - - - - public Dimension getWindowSize(Class c) { - int x, y; - String pref = PREFNODE.node("windows").get("size." + c.getCanonicalName(), null); - - if (pref == null) - return null; - - if (pref.indexOf(',') < 0) - return null; - - try { - x = Integer.parseInt(pref.substring(0, pref.indexOf(','))); - y = Integer.parseInt(pref.substring(pref.indexOf(',') + 1)); - } catch (NumberFormatException e) { - return null; - } - return new Dimension(x, y); - } - - - public boolean isWindowMaximized(Class c) { - String pref = PREFNODE.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); - storeVersion(); - } - - public void setWindowMaximized(Class c) { - PREFNODE.node("windows").put("size." + c.getCanonicalName(), "max"); - storeVersion(); - } - - /** - * this class returns a java.awt.Color object for the specified key. - * you can pass (java.awt.Color) null to the second argument to - * disambiguate - */ - public Color getColor( String key, Color defaultValue ) { - net.sf.openrocket.util.Color c = super.getColor(key, (net.sf.openrocket.util.Color) null); - if ( c == null ) { - return defaultValue; - } - return ColorConversion.toAwtColor(c); - } - - /** - * - */ - public void putColor( String key, Color value ) { - net.sf.openrocket.util.Color c = ColorConversion.fromAwtColor(value); - super.putColor(key, c); - } - - //// Printing - - - //// Background flight data computation - - public boolean computeFlightInBackground() { - return PREFNODE.getBoolean("backgroundFlight", true); - } - - public Simulation getBackgroundSimulation(Rocket rocket) { - Simulation s = new Simulation(rocket); - SimulationOptions cond = s.getOptions(); - - cond.setTimeStep(RK4SimulationStepper.RECOMMENDED_TIME_STEP * 2); - cond.setWindSpeedAverage(1.0); - cond.setWindSpeedDeviation(0.1); - cond.setLaunchRodLength(5); - return s; - } - - - - ///////// Export variables - - public boolean isExportSelected(FlightDataType type) { - Preferences prefs = PREFNODE.node("exports"); - return prefs.getBoolean(type.getName(), false); - } - - public void setExportSelected(FlightDataType type, boolean selected) { - Preferences prefs = PREFNODE.node("exports"); - prefs.putBoolean(type.getName(), selected); - } - - - - ///////// Default unit storage - - public void loadDefaultUnits() { - Preferences prefs = PREFNODE.node("units"); - try { - - for (String key : prefs.keys()) { - UnitGroup group = UnitGroup.UNITS.get(key); - if (group == null) - continue; - - try { - group.setDefaultUnit(prefs.get(key, null)); - } catch (IllegalArgumentException ignore) { - } - } - - } catch (BackingStoreException e) { - ExceptionHandler.handleErrorCondition(e); - } - } - - public void storeDefaultUnits() { - Preferences prefs = PREFNODE.node("units"); - - for (String key : UnitGroup.UNITS.keySet()) { - UnitGroup group = UnitGroup.UNITS.get(key); - if (group == null || group.getUnitCount() < 2) - continue; - - prefs.put(key, group.getDefaultUnit().getUnit()); - } - } - - - - //// Material storage - - - /** - * Add a user-defined material to the preferences. The preferences are - * first checked for an existing material matching the provided one using - * {@link Material#equals(Object)}. - * - * @param m the material to add. - */ - public void addUserMaterial(Material m) { - Preferences prefs = PREFNODE.node("userMaterials"); - - - // Check whether material already exists - if (getUserMaterials().contains(m)) { - return; - } - - // Add material using next free key (key is not used when loading) - String mat = m.toStorableString(); - for (int i = 0;; i++) { - String key = "material" + i; - if (prefs.get(key, null) == null) { - prefs.put(key, mat); - return; - } - } - } - - - /** - * Remove a user-defined material from the preferences. The matching is performed - * using {@link Material#equals(Object)}. - * - * @param m the material to remove. - */ - public void removeUserMaterial(Material m) { - Preferences prefs = PREFNODE.node("userMaterials"); - - try { - - // Iterate through materials and remove all keys with a matching material - for (String key : prefs.keys()) { - String value = prefs.get(key, null); - try { - - Material existing = Material.fromStorableString(value, true); - if (existing.equals(m)) { - prefs.remove(key); - } - - } catch (IllegalArgumentException ignore) { - } - - } - - } catch (BackingStoreException e) { - throw new IllegalStateException("Cannot read preferences!", e); - } - } - - - /** - * Return a set of all user-defined materials in the preferences. The materials - * are created marked as user-defined. - * - * @return a set of all user-defined materials. - */ - public Set getUserMaterials() { - Preferences prefs = PREFNODE.node("userMaterials"); - - HashSet materials = new HashSet(); - try { - - for (String key : prefs.keys()) { - String value = prefs.get(key, null); - try { - - Material m = Material.fromStorableString(value, true); - materials.add(m); - - } catch (IllegalArgumentException e) { - log.warn("Illegal material string " + value); - } - - } - - } catch (BackingStoreException e) { - throw new IllegalStateException("Cannot read preferences!", e); - } - - return materials; - } - - - //// Helper methods - -} diff --git a/test/net/sf/openrocket/file/rocksim/RocksimTestBase.java b/test/net/sf/openrocket/file/rocksim/RocksimTestBase.java index 372b881e..c3c38825 100644 --- a/test/net/sf/openrocket/file/rocksim/RocksimTestBase.java +++ b/test/net/sf/openrocket/file/rocksim/RocksimTestBase.java @@ -7,9 +7,9 @@ import java.lang.reflect.Field; import java.util.List; import junit.framework.TestCase; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.Prefs; /** * A base class for the Rocksim tests. Includes code from the junitx.addons project. @@ -32,7 +32,7 @@ public abstract class RocksimTestBase extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - Application.setPreferences( new Prefs() ); + Application.setPreferences( new SwingPreferences() ); } diff --git a/test/net/sf/openrocket/util/BaseTestCase/BaseTestCase.java b/test/net/sf/openrocket/util/BaseTestCase/BaseTestCase.java index 5dce9324..c5d1feb1 100644 --- a/test/net/sf/openrocket/util/BaseTestCase/BaseTestCase.java +++ b/test/net/sf/openrocket/util/BaseTestCase/BaseTestCase.java @@ -1,7 +1,7 @@ package net.sf.openrocket.util.BaseTestCase; +import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.Prefs; import org.junit.BeforeClass; @@ -10,7 +10,7 @@ public class BaseTestCase { @BeforeClass public static void setUpApplication () { - Application.setPreferences( new Prefs() ); + Application.setPreferences( new SwingPreferences() ); } }