bug fixes
[debian/openrocket] / src / net / sf / openrocket / gui / plot / SimulationPlotPanel.java
index cac004554d2881bad24f91710e33b5c4155a6545..898d7f67ae6d293439d5864b97577a9cac4ad872 100644 (file)
@@ -24,26 +24,38 @@ import net.miginfocom.swing.MigLayout;
 import net.sf.openrocket.document.Simulation;
 import net.sf.openrocket.gui.components.DescriptionArea;
 import net.sf.openrocket.gui.components.UnitSelector;
+import net.sf.openrocket.l10n.Translator;
 import net.sf.openrocket.simulation.FlightDataBranch;
+import net.sf.openrocket.simulation.FlightDataType;
 import net.sf.openrocket.simulation.FlightEvent;
-import net.sf.openrocket.simulation.FlightDataBranch.Type;
+import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.unit.Unit;
 import net.sf.openrocket.util.GUIUtil;
 import net.sf.openrocket.util.Icons;
-import net.sf.openrocket.util.Pair;
+import net.sf.openrocket.util.Utils;
 
+/**
+ * Panel that displays the simulation plot options to the user.
+ * 
+ * @author Sampo Niskanen <sampo.niskanen@iki.fi>
+ */
 public class SimulationPlotPanel extends JPanel {
+       private static final Translator trans = Application.getTranslator();
        
        // TODO: LOW: Should these be somewhere else?
        public static final int AUTO = -1;
        public static final int LEFT = 0;
        public static final int RIGHT = 1;
        
-       public static final String AUTO_NAME = "Auto";
-       public static final String LEFT_NAME = "Left";
-       public static final String RIGHT_NAME = "Right";
+       //// Auto
+       public static final String AUTO_NAME = trans.get("simplotpanel.AUTO_NAME");
+       //// Left
+       public static final String LEFT_NAME = trans.get("simplotpanel.LEFT_NAME");
+       //// Right
+       public static final String RIGHT_NAME = trans.get("simplotpanel.RIGHT_NAME");
        
-       private static final String CUSTOM = "Custom";
+       //// Custom
+       private static final String CUSTOM = trans.get("simplotpanel.CUSTOM");
        
        /** The "Custom" configuration - not to be used for anything other than the title. */
        private static final PlotConfiguration CUSTOM_CONFIGURATION;
@@ -54,20 +66,20 @@ public class SimulationPlotPanel extends JPanel {
        /** The array of presets for the combo box. */
        private static final PlotConfiguration[] PRESET_ARRAY;
        static {
-               PRESET_ARRAY = Arrays.copyOf(PlotConfiguration.DEFAULT_CONFIGURATIONS, 
+               PRESET_ARRAY = Arrays.copyOf(PlotConfiguration.DEFAULT_CONFIGURATIONS,
                                PlotConfiguration.DEFAULT_CONFIGURATIONS.length + 1);
-               PRESET_ARRAY[PRESET_ARRAY.length-1] = CUSTOM_CONFIGURATION;
+               PRESET_ARRAY[PRESET_ARRAY.length - 1] = CUSTOM_CONFIGURATION;
        }
        
-       
-       
+
+
        /** The current default configuration, set each time a plot is made. */
        private static PlotConfiguration defaultConfiguration =
-               PlotConfiguration.DEFAULT_CONFIGURATIONS[0].resetUnits();
-       
+                       PlotConfiguration.DEFAULT_CONFIGURATIONS[0].resetUnits();
        
+
        private final Simulation simulation;
-       private final FlightDataBranch.Type[] types;
+       private final FlightDataType[] types;
        private PlotConfiguration configuration;
        
 
@@ -79,57 +91,59 @@ public class SimulationPlotPanel extends JPanel {
        private JPanel typeSelectorPanel;
        private FlightEventTableModel eventTableModel;
        
-       
+
        private int modifying = 0;
        
-
+       
        public SimulationPlotPanel(final Simulation simulation) {
                super(new MigLayout("fill"));
                
                this.simulation = simulation;
-               if (simulation.getSimulatedData() == null  ||
-                               simulation.getSimulatedData().getBranchCount()==0) {
+               if (simulation.getSimulatedData() == null ||
+                               simulation.getSimulatedData().getBranchCount() == 0) {
                        throw new IllegalArgumentException("Simulation contains no data.");
                }
                FlightDataBranch branch = simulation.getSimulatedData().getBranch(0);
                types = branch.getTypes();
                
-               // TODO: LOW: Revert to custom if data type is not available.
-               configuration = defaultConfiguration.clone();
-               
+               setConfiguration(defaultConfiguration);
                
                ////  Configuration selector
                
                // Setup the combo box
                configurationSelector = new JComboBox(PRESET_ARRAY);
-               for (PlotConfiguration config: PRESET_ARRAY) {
+               for (PlotConfiguration config : PRESET_ARRAY) {
                        if (config.getName().equals(configuration.getName())) {
                                configurationSelector.setSelectedItem(config);
                        }
                }
+               
+               // FIXME:  Bugs when expected branch is not present
+               
                configurationSelector.addItemListener(new ItemListener() {
                        @Override
                        public void itemStateChanged(ItemEvent e) {
                                if (modifying > 0)
                                        return;
-                               PlotConfiguration conf = (PlotConfiguration)configurationSelector.getSelectedItem();
+                               PlotConfiguration conf = (PlotConfiguration) configurationSelector.getSelectedItem();
                                if (conf == CUSTOM_CONFIGURATION)
                                        return;
                                modifying++;
-                               configuration = conf.clone().resetUnits();
+                               setConfiguration(conf.clone().resetUnits());
                                updatePlots();
                                modifying--;
                        }
                });
-               this.add(new JLabel("Preset plot configurations: "), "spanx, split");
-               this.add(configurationSelector,"growx, wrap 20lp");
-
-               
+               //// Preset plot configurations:
+               this.add(new JLabel(trans.get("simplotpanel.lbl.Presetplotconf")), "spanx, split");
+               this.add(configurationSelector, "growx, wrap 20lp");
                
+
+
                //// X axis
                
-               
-               this.add(new JLabel("X axis type:"), "spanx, split");
+               //// X axis type:
+               this.add(new JLabel(trans.get("simplotpanel.lbl.Xaxistype")), "spanx, split");
                domainTypeSelector = new JComboBox(types);
                domainTypeSelector.setSelectedItem(configuration.getDomainAxisType());
                domainTypeSelector.addItemListener(new ItemListener() {
@@ -137,7 +151,7 @@ public class SimulationPlotPanel extends JPanel {
                        public void itemStateChanged(ItemEvent e) {
                                if (modifying > 0)
                                        return;
-                               FlightDataBranch.Type type = (Type) domainTypeSelector.getSelectedItem();
+                               FlightDataType type = (FlightDataType) domainTypeSelector.getSelectedItem();
                                configuration.setDomainAxisType(type);
                                domainUnitSelector.setUnitGroup(type.getUnitGroup());
                                domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit());
@@ -145,9 +159,9 @@ public class SimulationPlotPanel extends JPanel {
                        }
                });
                this.add(domainTypeSelector, "gapright para");
-
                
-               this.add(new JLabel("Unit:"));
+               //// Unit:
+               this.add(new JLabel(trans.get("simplotpanel.lbl.Unit")));
                domainUnitSelector = new UnitSelector(configuration.getDomainAxisType().getUnitGroup());
                domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit());
                domainUnitSelector.addItemListener(new ItemListener() {
@@ -160,25 +174,24 @@ public class SimulationPlotPanel extends JPanel {
                });
                this.add(domainUnitSelector, "width 40lp, gapright para");
                
-               
-               DescriptionArea desc = new DescriptionArea("The data will be plotted in time order " +
-                               "even if the X axis type is not time.", 2, -2f);
+               //// The data will be plotted in time order even if the X axis type is not time.
+               DescriptionArea desc = new DescriptionArea(trans.get("simplotpanel.Desc"), 2, -2f);
                desc.setViewportBorder(BorderFactory.createEmptyBorder());
                this.add(desc, "width 1px, growx 1, wrap unrel");
                
-               
-               
+
+
                //// Y axis selector panel
-               
-               this.add(new JLabel("Y axis types:"));
-               
-               this.add(new JLabel("Flight events:"), "wrap rel");
+               //// Y axis types:
+               this.add(new JLabel(trans.get("simplotpanel.lbl.Yaxistypes")));
+               //// Flight events:
+               this.add(new JLabel(trans.get("simplotpanel.lbl.Flightevents")), "wrap rel");
                
                typeSelectorPanel = new JPanel(new MigLayout("gapy rel"));
                JScrollPane scroll = new JScrollPane(typeSelectorPanel);
                this.add(scroll, "spany 2, height 10px, grow 100, gapright para");
                
-               
+
                //// Flight events
                eventTableModel = new FlightEventTableModel();
                JTable table = new JTable(eventTableModel);
@@ -196,97 +209,134 @@ public class SimulationPlotPanel extends JPanel {
                table.addMouseListener(new GUIUtil.BooleanTableClickListener(table));
                this.add(new JScrollPane(table), "height 10px, width 200lp, grow 1, wrap rel");
                
-               
+
                ////  All + None buttons
-               JButton button = new JButton("All");
+               JButton button = new JButton(trans.get("simplotpanel.but.All"));
                button.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               for (FlightEvent.Type t: FlightEvent.Type.values())
+                               for (FlightEvent.Type t : FlightEvent.Type.values())
                                        configuration.setEvent(t, true);
                                eventTableModel.fireTableDataChanged();
                        }
                });
                this.add(button, "split 2, gapleft para, gapright para, growx, sizegroup buttons");
                
-               button = new JButton("None");
+               //// None
+               button = new JButton(trans.get("simplotpanel.but.None"));
                button.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               for (FlightEvent.Type t: FlightEvent.Type.values())
+                               for (FlightEvent.Type t : FlightEvent.Type.values())
                                        configuration.setEvent(t, false);
                                eventTableModel.fireTableDataChanged();
                        }
                });
                this.add(button, "gapleft para, gapright para, growx, sizegroup buttons, wrap para");
                
-               
-               
-               
-               button = new JButton("New Y axis plot type");
+
+
+               //// New Y axis plot type
+               button = new JButton(trans.get("simplotpanel.but.NewYaxisplottype"));
                button.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                if (configuration.getTypeCount() >= 15) {
-                                       JOptionPane.showMessageDialog(SimulationPlotPanel.this, 
-                                                       "A maximum of 15 plots is allowed.", "Cannot add plot", 
+                                       JOptionPane.showMessageDialog(SimulationPlotPanel.this,
+                                                       //// A maximum of 15 plots is allowed.
+                                                       //// Cannot add plot
+                                                       trans.get("simplotpanel.OptionPane.lbl1"),
+                                                       trans.get("simplotpanel.OptionPane.lbl2"),
                                                        JOptionPane.ERROR_MESSAGE);
                                        return;
                                }
-
+                               
                                // Select new type smartly
-                               FlightDataBranch.Type type = null;
-                               for (FlightDataBranch.Type t: 
+                               FlightDataType type = null;
+                               for (FlightDataType t :
                                        simulation.getSimulatedData().getBranch(0).getTypes()) {
-                                       
-                                       boolean used = false;
-                                       if (configuration.getDomainAxisType().equals(t)) {
-                                               used = true;
-                                       } else {
-                                               for (int i=0; i < configuration.getTypeCount(); i++) {
-                                                       if (configuration.getType(i).equals(t)) {
-                                                               used = true;
-                                                               break;
+                                               
+                                               boolean used = false;
+                                               if (configuration.getDomainAxisType().equals(t)) {
+                                                       used = true;
+                                               } else {
+                                                       for (int i = 0; i < configuration.getTypeCount(); i++) {
+                                                               if (configuration.getType(i).equals(t)) {
+                                                                       used = true;
+                                                                       break;
+                                                               }
                                                        }
                                                }
+                                               
+                                               if (!used) {
+                                                       type = t;
+                                                       break;
+                                               }
                                        }
-                                       
-                                       if (!used) {
-                                               type = t;
-                                               break;
+                                       if (type == null) {
+                                               type = simulation.getSimulatedData().getBranch(0).getTypes()[0];
                                        }
+                                       
+                                       // Add new type
+                                       configuration.addPlotDataType(type);
+                                       setToCustom();
+                                       updatePlots();
                                }
-                               if (type == null) {
-                                       type = simulation.getSimulatedData().getBranch(0).getTypes()[0];
-                               }
-                               
-                               // Add new type
-                               configuration.addPlotDataType(type);
-                               setToCustom();
-                               updatePlots();
-                       }
                });
                this.add(button, "spanx, split");
                
-               
+
                this.add(new JPanel(), "growx");
                
-               button = new JButton("Plot flight");
+               //// Plot flight
+               button = new JButton(trans.get("simplotpanel.but.Plotflight"));
                button.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
+                               if (configuration.getTypeCount() == 0) {
+                                       JOptionPane.showMessageDialog(SimulationPlotPanel.this,
+                                                       trans.get("error.noPlotSelected"),
+                                                       trans.get("error.noPlotSelected.title"),
+                                                       JOptionPane.ERROR_MESSAGE);
+                                       return;
+                               }
                                defaultConfiguration = configuration.clone();
-                               PlotDialog.showPlot(SwingUtilities.getWindowAncestor(SimulationPlotPanel.this), 
+                               SimulationPlotDialog.showPlot(SwingUtilities.getWindowAncestor(SimulationPlotPanel.this),
                                                simulation, configuration);
                        }
                });
                this.add(button, "right");
-
                
+
                updatePlots();
        }
        
        
+       private void setConfiguration(PlotConfiguration conf) {
+               
+               boolean modified = false;
+               
+               configuration = conf.clone();
+               if (!Utils.contains(types, configuration.getDomainAxisType())) {
+                       configuration.setDomainAxisType(types[0]);
+                       modified = true;
+               }
+               
+               for (int i = 0; i < configuration.getTypeCount(); i++) {
+                       if (!Utils.contains(types, configuration.getType(i))) {
+                               configuration.removePlotDataType(i);
+                               i--;
+                               modified = true;
+                       }
+               }
+               
+               if (modified) {
+                       configuration.setName(CUSTOM);
+               }
+               
+       }
+       
+       
        private void setToCustom() {
                modifying++;
                configuration.setName(CUSTOM);
@@ -301,8 +351,8 @@ public class SimulationPlotPanel extends JPanel {
                domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit());
                
                typeSelectorPanel.removeAll();
-               for (int i=0; i < configuration.getTypeCount(); i++) {
-                       FlightDataBranch.Type type = configuration.getType(i);
+               for (int i = 0; i < configuration.getTypeCount(); i++) {
+                       FlightDataType type = configuration.getType(i);
                        Unit unit = configuration.getUnit(i);
                        int axis = configuration.getAxis(i);
                        
@@ -315,8 +365,8 @@ public class SimulationPlotPanel extends JPanel {
        }
        
        
-       
-       
+
+
        /**
         * A JPanel which configures a single plot of a PlotConfiguration.
         */
@@ -329,11 +379,11 @@ public class SimulationPlotPanel extends JPanel {
                private JComboBox axisSelector;
                
                
-               public PlotTypeSelector(int index, FlightDataBranch.Type type) {
-                       this (index, type, null, -1);
+               public PlotTypeSelector(int index, FlightDataType type) {
+                       this(index, type, null, -1);
                }
                
-               public PlotTypeSelector(int plotIndex, FlightDataBranch.Type type, Unit unit, int position) {
+               public PlotTypeSelector(int plotIndex, FlightDataType type, Unit unit, int position) {
                        super(new MigLayout("ins 0"));
                        
                        this.index = plotIndex;
@@ -345,7 +395,7 @@ public class SimulationPlotPanel extends JPanel {
                                public void itemStateChanged(ItemEvent e) {
                                        if (modifying > 0)
                                                return;
-                                       FlightDataBranch.Type type = (Type) typeSelector.getSelectedItem();
+                                       FlightDataType type = (FlightDataType) typeSelector.getSelectedItem();
                                        configuration.setPlotDataType(index, type);
                                        unitSelector.setUnitGroup(type.getUnitGroup());
                                        unitSelector.setSelectedUnit(configuration.getUnit(index));
@@ -354,7 +404,8 @@ public class SimulationPlotPanel extends JPanel {
                        });
                        this.add(typeSelector, "gapright para");
                        
-                       this.add(new JLabel("Unit:"));
+                       //// Unit:
+                       this.add(new JLabel(trans.get("simplotpanel.lbl.Unit")));
                        unitSelector = new UnitSelector(type.getUnitGroup());
                        if (unit != null)
                                unitSelector.setSelectedUnit(unit);
@@ -363,13 +414,14 @@ public class SimulationPlotPanel extends JPanel {
                                public void itemStateChanged(ItemEvent e) {
                                        if (modifying > 0)
                                                return;
-                                       Unit unit = (Unit) unitSelector.getSelectedUnit();
+                                       Unit unit = unitSelector.getSelectedUnit();
                                        configuration.setPlotDataUnit(index, unit);
                                }
                        });
                        this.add(unitSelector, "width 40lp, gapright para");
                        
-                       this.add(new JLabel("Axis:"));
+                       //// Axis:
+                       this.add(new JLabel(trans.get("simplotpanel.lbl.Axis")));
                        axisSelector = new JComboBox(POSITIONS);
                        if (position == LEFT)
                                axisSelector.setSelectedIndex(1);
@@ -388,9 +440,10 @@ public class SimulationPlotPanel extends JPanel {
                        });
                        this.add(axisSelector);
                        
-                       
+
                        JButton button = new JButton(Icons.DELETE);
-                       button.setToolTipText("Remove this plot");
+                       //// Remove this plot
+                       button.setToolTipText(trans.get("simplotpanel.but.ttip.Removethisplot"));
                        button.setBorderPainted(false);
                        button.addActionListener(new ActionListener() {
                                @Override
@@ -405,16 +458,15 @@ public class SimulationPlotPanel extends JPanel {
        }
        
        
-       
+
        private class FlightEventTableModel extends AbstractTableModel {
                private final FlightEvent.Type[] eventTypes;
                
                public FlightEventTableModel() {
                        EnumSet<FlightEvent.Type> set = EnumSet.noneOf(FlightEvent.Type.class);
-                       for (int i=0; i < simulation.getSimulatedData().getBranchCount(); i++) {
-                               for (Pair<Double,FlightEvent> e:
-                                       simulation.getSimulatedData().getBranch(i).getEvents()) {
-                                       set.add(e.getV().getType());
+                       for (int i = 0; i < simulation.getSimulatedData().getBranchCount(); i++) {
+                               for (FlightEvent e : simulation.getSimulatedData().getBranch(i).getEvents()) {
+                                       set.add(e.getType());
                                }
                        }
                        set.remove(FlightEvent.Type.ALTITUDE);
@@ -422,7 +474,7 @@ public class SimulationPlotPanel extends JPanel {
                        
                        eventTypes = new FlightEvent.Type[count];
                        int pos = 0;
-                       for (FlightEvent.Type t: FlightEvent.Type.values()) {
+                       for (FlightEvent.Type t : FlightEvent.Type.values()) {
                                if (set.contains(t)) {
                                        eventTypes[pos] = t;
                                        pos++;
@@ -434,7 +486,7 @@ public class SimulationPlotPanel extends JPanel {
                public int getColumnCount() {
                        return 2;
                }
-
+               
                @Override
                public int getRowCount() {
                        return eventTypes.length;
@@ -450,10 +502,10 @@ public class SimulationPlotPanel extends JPanel {
                                return String.class;
                                
                        default:
-                               throw new IndexOutOfBoundsException("column="+column);
+                               throw new IndexOutOfBoundsException("column=" + column);
                        }
                }
-
+               
                @Override
                public Object getValueAt(int row, int column) {
                        switch (column) {
@@ -464,7 +516,7 @@ public class SimulationPlotPanel extends JPanel {
                                return eventTypes[row].toString();
                                
                        default:
-                               throw new IndexOutOfBoundsException("column="+column);
+                               throw new IndexOutOfBoundsException("column=" + column);
                        }
                }
                
@@ -476,10 +528,10 @@ public class SimulationPlotPanel extends JPanel {
                @Override
                public void setValueAt(Object value, int row, int column) {
                        if (column != 0 || !(value instanceof Boolean)) {
-                               throw new IllegalArgumentException("column="+column+", value="+value);
+                               throw new IllegalArgumentException("column=" + column + ", value=" + value);
                        }
                        
-                       configuration.setEvent(eventTypes[row], (Boolean)value);
+                       configuration.setEvent(eventTypes[row], (Boolean) value);
                        this.fireTableCellUpdated(row, column);
                }
        }