1 package net.sf.openrocket.unit;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Iterator;
7 import javax.swing.event.ChangeEvent;
8 import javax.swing.event.ChangeListener;
10 import net.sf.openrocket.rocketcomponent.Configuration;
11 import net.sf.openrocket.rocketcomponent.Rocket;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
14 import net.sf.openrocket.util.MathUtil;
17 public class CaliberUnit extends GeneralUnit {
19 public static final double DEFAULT_CALIBER = 0.01;
21 private final Configuration configuration;
22 private final Rocket rocket;
24 private double caliber = -1;
27 /* Listener for rocket and configuration, resets the caliber to -1. */
28 private final ChangeListener listener = new ChangeListener() {
30 public void stateChanged(ChangeEvent e) {
37 public CaliberUnit(Configuration configuration) {
39 this.configuration = configuration;
41 if (configuration == null) {
44 this.rocket = configuration.getRocket();
45 configuration.addChangeListener(listener);
49 public CaliberUnit(Rocket rocket) {
51 this.configuration = null;
54 rocket.addChangeListener(listener);
60 public double fromUnit(double value) {
64 return value * caliber;
68 public double toUnit(double value) {
72 return value / caliber;
76 // TODO: HIGH: Check caliber calculation method...
77 private void calculateCaliber() {
80 Iterator<RocketComponent> iterator;
81 if (configuration != null) {
82 iterator = configuration.iterator();
83 } else if (rocket != null) {
84 iterator = rocket.iterator(false);
86 Collection<RocketComponent> set = Collections.emptyList();
87 iterator = set.iterator();
90 while (iterator.hasNext()) {
91 RocketComponent c = iterator.next();
92 if (c instanceof SymmetricComponent) {
93 double r1 = ((SymmetricComponent) c).getForeRadius() * 2;
94 double r2 = ((SymmetricComponent) c).getAftRadius() * 2;
95 caliber = MathUtil.max(caliber, r1, r2);
100 caliber = DEFAULT_CALIBER;