Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / document / OpenRocketDocument.java
index cafc98f33d174665a88081ad3ca9e34cd1be97a9..12535220b75bcfecc4f983b98b101763d85f6b71 100644 (file)
@@ -1,8 +1,12 @@
 package net.sf.openrocket.document;
 
 import java.io.File;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Set;
 
 import net.sf.openrocket.document.events.DocumentChangeEvent;
 import net.sf.openrocket.document.events.DocumentChangeListener;
@@ -13,6 +17,10 @@ import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
 import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
 import net.sf.openrocket.rocketcomponent.Configuration;
 import net.sf.openrocket.rocketcomponent.Rocket;
+import net.sf.openrocket.simulation.FlightDataType;
+import net.sf.openrocket.simulation.customexpression.CustomExpression;
+import net.sf.openrocket.simulation.exception.SimulationListenerException;
+import net.sf.openrocket.simulation.listeners.SimulationListener;
 import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.util.ArrayList;
 
@@ -50,7 +58,8 @@ public class OpenRocketDocument implements ComponentChangeListener {
        private final Configuration configuration;
        
        private final ArrayList<Simulation> simulations = new ArrayList<Simulation>();
-       
+       private ArrayList<CustomExpression> customExpressions = new ArrayList<CustomExpression>();
+
 
        /*
         * The undo/redo variables and mechanism are documented in doc/undo-redo-flow.*
@@ -103,7 +112,59 @@ public class OpenRocketDocument implements ComponentChangeListener {
        }
        
        
-
+       public void addCustomExpression(CustomExpression expression){
+               if (customExpressions.contains(expression)){
+                       log.user("Could not add custom expression "+expression.getName()+" to document as document alerady has a matching expression.");
+               } else {
+                       customExpressions.add(expression);
+               }
+       }
+       
+       public void removeCustomExpression(CustomExpression expression){
+               customExpressions.remove(expression);
+       }
+       
+       public List<CustomExpression> getCustomExpressions(){
+               return customExpressions;
+       }
+       
+       /*
+        * Returns a set of all the flight data types defined or available in any way in the rocket document
+        */
+       public Set<FlightDataType> getFlightDataTypes(){
+               Set<FlightDataType> allTypes = new LinkedHashSet<FlightDataType>();
+               
+               // built in
+               Collections.addAll(allTypes, FlightDataType.ALL_TYPES);
+               
+               // custom expressions
+               for (CustomExpression exp : customExpressions){
+                       allTypes.add(exp.getType());
+               }
+               
+               // simulation listeners
+               for (Simulation sim : simulations){
+                       for (String className : sim.getSimulationListeners()) {
+                               SimulationListener l = null;
+                               try {
+                                       Class<?> c = Class.forName(className);
+                                       l = (SimulationListener) c.newInstance();
+                                       
+                                       Collections.addAll(allTypes, l.getFlightDataTypes());
+                                       //System.out.println("This document has expression datatype from "+l.getName());
+                               } catch (Exception e) {
+                                       log.error("Could not instantiate listener: " + className);
+                               }
+                       }                       
+               }
+               
+               // imported data
+               /// not implemented yet
+               
+               
+               return allTypes;
+       }
+       
 
        public Rocket getRocket() {
                return rocket;