Merge remote-tracking branch 'origin/issue-244' into issue-244

This commit is contained in:
SiboVG 2022-02-14 23:09:31 +01:00
commit 08eeac5236

View File

@ -3,6 +3,10 @@ package net.sf.openrocket.gui.main.flightconfigpanel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.EventObject; import java.util.EventObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -27,6 +31,7 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketvisitors.ListComponents; import net.sf.openrocket.rocketvisitors.ListComponents;
import net.sf.openrocket.rocketvisitors.ListMotorMounts; import net.sf.openrocket.rocketvisitors.ListMotorMounts;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.StateChangeListener; import net.sf.openrocket.util.StateChangeListener;
import net.sf.openrocket.gui.widgets.SelectColorButton; import net.sf.openrocket.gui.widgets.SelectColorButton;
@ -165,51 +170,71 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
* create simulation for new configuration * create simulation for new configuration
*/ */
private void addOrCopyConfiguration(boolean copy) { private void addOrCopyConfiguration(boolean copy) {
FlightConfiguration newConfig; Map<FlightConfigurationId, FlightConfiguration> newConfigs = new LinkedHashMap<>();
FlightConfigurationId newId;
// create or copy configuration // create or copy configuration
if (copy) { if (copy) {
FlightConfigurationId oldId = this.motorConfigurationPanel.getSelectedConfigurationId(); List<FlightConfigurationId> oldIds = getSelectedConfigurationIds();
FlightConfiguration oldConfig = rocket.getFlightConfiguration(oldId); if (oldIds == null || oldIds.size() == 0) return;
newConfig = oldConfig.copy(null); for (FlightConfigurationId oldId : oldIds) {
newId = newConfig.getId(); final FlightConfiguration oldConfig = rocket.getFlightConfiguration(oldId);
for (RocketComponent c : rocket) { final FlightConfiguration newConfig = oldConfig.copy(null);
if (c instanceof FlightConfigurableComponent) { final FlightConfigurationId newId = newConfig.getId();
((FlightConfigurableComponent) c).copyFlightConfiguration(oldId, newId);
for (RocketComponent c : rocket) {
if (c instanceof FlightConfigurableComponent) {
((FlightConfigurableComponent) c).copyFlightConfiguration(oldId, newId);
}
} }
newConfigs.put(newId, newConfig);
} }
} else { } else {
newConfig = new FlightConfiguration(rocket, null); final FlightConfiguration newConfig = new FlightConfiguration(rocket, null);
newId = newConfig.getId(); final FlightConfigurationId newId = newConfig.getId();
}
// associate configuration with Id and select it
rocket.setFlightConfiguration(newId, newConfig);
rocket.setSelectedConfiguration(newId);
// create simulation for configuration newConfigs.put(newId, newConfig);
Simulation newSim = new Simulation(rocket); }
OpenRocketDocument doc = BasicFrame.findDocument(rocket); for (FlightConfigurationId newId : newConfigs.keySet()) {
if (doc != null) { // associate configuration with Id and select it
newSim.setName(doc.getNextSimulationName()); rocket.setFlightConfiguration(newId, newConfigs.get(newId));
doc.addSimulation(newSim);
} // create simulation for configuration
Simulation newSim = new Simulation(rocket);
OpenRocketDocument doc = BasicFrame.findDocument(rocket);
if (doc != null) {
newSim.setName(doc.getNextSimulationName());
doc.addSimulation(newSim);
}
}
rocket.setSelectedConfiguration((FlightConfigurationId) newConfigs.keySet().toArray()[0]);
} }
private void renameConfiguration() { private void renameConfiguration() {
FlightConfigurationId currentId = this.motorConfigurationPanel.getSelectedConfigurationId(); List<FlightConfigurationId> fcIds = getSelectedConfigurationIds();
new RenameConfigDialog(SwingUtilities.getWindowAncestor(this), rocket, currentId).setVisible(true); if (fcIds == null) return;
FlightConfigurationId initFcId = fcIds.get(0);
new RenameConfigDialog(SwingUtilities.getWindowAncestor(this), rocket, initFcId).setVisible(true);
String newName = rocket.getFlightConfiguration(initFcId).getName();
for (int i = 1; i < fcIds.size(); i++) {
rocket.getFlightConfiguration(fcIds.get(i)).setName(newName);
}
} }
private void removeConfiguration() { private void removeConfiguration() {
FlightConfigurationId currentId = this.motorConfigurationPanel.getSelectedConfigurationId(); List<FlightConfigurationId> fcIds = getSelectedConfigurationIds();
if (currentId == null) if (fcIds == null || fcIds.size() == 0)
return; return;
document.removeFlightConfigurationAndSimulations(currentId);
for (FlightConfigurationId fcId : fcIds) {
document.removeFlightConfigurationAndSimulations(fcId);
}
configurationChanged(ComponentChangeEvent.NONFUNCTIONAL_CHANGE); configurationChanged(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
} }
@ -255,6 +280,19 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
} }
private List<FlightConfigurationId> getSelectedConfigurationIds() {
switch (tabs.getSelectedIndex()) {
case MOTOR_TAB_INDEX:
return this.motorConfigurationPanel.getSelectedConfigurationIds();
case RECOVERY_TAB_INDEX:
return this.recoveryConfigurationPanel.getSelectedConfigurationIds();
case SEPARATION_TAB_INDEX:
return this.separationConfigurationPanel.getSelectedConfigurationIds();
default:
return null;
}
}
public void setSelectedComponent(RocketComponent component) { public void setSelectedComponent(RocketComponent component) {
this.basicFrame.setSelectedComponent(component); this.basicFrame.setSelectedComponent(component);
} }