create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / utils / GraphicalMotorSelector.java
1 package net.sf.openrocket.utils;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import net.sf.openrocket.file.motor.GeneralMotorLoader;
12 import net.sf.openrocket.motor.Motor;
13 import net.sf.openrocket.motor.ThrustCurveMotor;
14 import net.sf.openrocket.util.Pair;
15
16 public class GraphicalMotorSelector {
17         
18         public static void main(String[] args) throws IOException {
19                 
20                 if (args.length == 0) {
21                         System.err.println("MotorPlot <files>");
22                         System.exit(1);
23                 }
24                 
25                 // Load files
26                 Map<String, List<Pair<String, ThrustCurveMotor>>> map =
27                                 new LinkedHashMap<String, List<Pair<String, ThrustCurveMotor>>>();
28                 
29                 GeneralMotorLoader loader = new GeneralMotorLoader();
30                 for (String file : args) {
31                         
32                         for (Motor motor : loader.load(new FileInputStream(file), file)) {
33                                 ThrustCurveMotor m = (ThrustCurveMotor) motor;
34                                 System.out.println("Loaded " + m + " from file " + file);
35                                 
36                                 Pair<String, ThrustCurveMotor> pair = new Pair<String, ThrustCurveMotor>(file, m);
37                                 String key = m.getManufacturer() + ":" + m.getDesignation();
38                                 
39                                 List<Pair<String, ThrustCurveMotor>> list = map.get(key);
40                                 if (list == null) {
41                                         list = new ArrayList<Pair<String, ThrustCurveMotor>>();
42                                         map.put(key, list);
43                                 }
44                                 
45                                 list.add(pair);
46                         }
47                 }
48                 
49
50                 // Go through different motors
51                 int count = 0;
52                 for (String key : map.keySet()) {
53                         count++;
54                         List<Pair<String, ThrustCurveMotor>> list = map.get(key);
55                         
56
57                         // Select best one of identical motors
58                         List<String> filenames = new ArrayList<String>();
59                         List<ThrustCurveMotor> motors = new ArrayList<ThrustCurveMotor>();
60                         for (Pair<String, ThrustCurveMotor> pair : list) {
61                                 String file = pair.getU();
62                                 ThrustCurveMotor m = pair.getV();
63                                 
64                                 int index = indexOf(motors, m);
65                                 if (index >= 0) {
66                                         // Replace previous if this has more delays, a known type or longer comment
67                                         ThrustCurveMotor m2 = motors.get(index);
68                                         if (m.getStandardDelays().length > m2.getStandardDelays().length ||
69                                                         (m2.getMotorType() == Motor.Type.UNKNOWN &&
70                                                         m.getMotorType() != Motor.Type.UNKNOWN) ||
71                                                         (m.getDescription().trim().length() >
72                                                         m2.getDescription().trim().length())) {
73                                                 
74                                                 filenames.set(index, file);
75                                                 motors.set(index, m);
76                                                 
77                                         }
78                                 } else {
79                                         filenames.add(file);
80                                         motors.add(m);
81                                 }
82                         }
83                         
84                         if (filenames.size() == 0) {
85                                 
86                                 System.out.println("ERROR selecting from " + list);
87                                 System.exit(1);
88                                 
89                         } else if (filenames.size() == 1) {
90                                 
91                                 select(filenames.get(0), list, false);
92                                 
93                         } else {
94                                 
95                                 System.out.println("Choosing from " + filenames +
96                                                 " (" + count + "/" + map.size() + ")");
97                                 MotorPlot plot = new MotorPlot(filenames, motors);
98                                 plot.setVisible(true);
99                                 plot.dispose();
100                                 int n = plot.getSelected();
101                                 if (n < 0) {
102                                         System.out.println("NONE SELECTED from " + filenames);
103                                 } else {
104                                         select(filenames.get(n), list, true);
105                                 }
106                                 
107                         }
108                         
109                 }
110                 
111         }
112         
113         private static void select(String selected, List<Pair<String, ThrustCurveMotor>> list, boolean manual) {
114                 System.out.print("SELECT " + selected + " ");
115                 if (manual) {
116                         System.out.println("(manual)");
117                 } else if (list.size() == 1) {
118                         System.out.println("(only)");
119                 } else {
120                         System.out.println("(identical)");
121                 }
122                 
123                 for (Pair<String, ThrustCurveMotor> pair : list) {
124                         String file = pair.getU();
125                         if (!file.equals(selected)) {
126                                 System.out.println("IGNORE " + file);
127                         }
128                 }
129         }
130         
131         
132         private static int indexOf(List<ThrustCurveMotor> motors, ThrustCurveMotor motor) {
133                 for (int i = 0; i < motors.size(); i++) {
134                         ThrustCurveMotor m = motors.get(i);
135                         // TODO: Similar?
136                         if (m.equals(motor)) {
137                                 if (m.getStandardDelays().length == 0 || motor.getStandardDelays().length == 0 ||
138                                                 Arrays.equals(m.getStandardDelays(), motor.getStandardDelays())) {
139                                         return i;
140                                 }
141                         }
142                 }
143                 return -1;
144         }
145 }