Beispiel #1
0
 /**
  * Gets a localized track name from the fully qualified track class name.
  *
  * @param trackClass the class name
  * @return the localized name
  */
 protected String getLocalizedTrackName(String trackClass) {
   String trackName = XML.getExtension(trackClass);
   String localized = TrackerRes.getString(trackName + ".Name"); // $NON-NLS-1$
   if (!localized.startsWith("!")) // $NON-NLS-1$
   trackName = localized;
   return trackName;
 }
Beispiel #2
0
 /**
  * Gets a simple class name for the specified class type.
  *
  * @param type the class
  * @return the simple class name
  */
 public static String getSimpleClassName(Class type) {
   String name = type.getName();
   // trim trailing semicolon, if any
   int i = name.indexOf(";"); // $NON-NLS-1$
   if (i > -1) {
     name = name.substring(0, i);
   }
   // add brackets for arrays
   while (name.startsWith("[")) { // $NON-NLS-1$
     name = name.substring(1);
     name = name + "[]"; // $NON-NLS-1$
   }
   // eliminate leading package name, if any
   String ext = XML.getExtension(name);
   if (ext != null) {
     name = ext;
   }
   // substitute int for I and double for D arrays
   i = name.indexOf("["); // $NON-NLS-1$
   if (i > -1) {
     String s = name.substring(0, i);
     if (s.equals("I")) { // $NON-NLS-1$
       s = "int"; // $NON-NLS-1$
     } else if (s.equals("D")) { // $NON-NLS-1$
       s = "double"; // $NON-NLS-1$
     } else if (s.equals("Z")) { // $NON-NLS-1$
       s = "boolean"; // $NON-NLS-1$
     }
     name = s + name.substring(i);
   }
   return name;
 }
Beispiel #3
0
 /**
  * Gets the absolute path.
  *
  * @return the absolute path
  */
 public String getAbsolutePath() {
   if (getFile() != null) {
     try {
       return XML.forwardSlash(getFile().getCanonicalPath());
     } catch (IOException ex) {
       ex.printStackTrace();
     }
     return getFile().getAbsolutePath();
   }
   if (getURL() != null) {
     URL url = getURL();
     String path = url.getPath();
     // remove file protocol, if any
     if (path.startsWith("file:")) { // $NON-NLS-1$
       path = path.substring(5);
     }
     // remove leading slash if drive is specified
     if (path.startsWith("/") && path.indexOf(":") > -1) { // $NON-NLS-1$ //$NON-NLS-2$
       path = path.substring(1);
     }
     // replace "%20" with space
     int i = path.indexOf("%20"); // $NON-NLS-1$
     while (i > -1) {
       String s = path.substring(0, i);
       path = s + " " + path.substring(i + 3); // $NON-NLS-1$
       i = path.indexOf("%20"); // $NON-NLS-1$
     }
     return path;
   }
   return null;
 }
Beispiel #4
0
 /**
  * Saves an object's data to an XMLControl.
  *
  * @param control the control to save to
  * @param obj the object to save
  */
 public void saveObject(XMLControl control, Object obj) {
   ParticleModel p = (ParticleModel) obj;
   // save mass
   control.setValue("mass", p.getMass()); // $NON-NLS-1$
   // save track data
   XML.getLoader(TTrack.class).saveObject(control, obj);
   // save parameters, initial values and functions
   Parameter[] params = p.getParamEditor().getParameters();
   control.setValue("user_parameters", params); // $NON-NLS-1$
   Parameter[] inits = p.getInitEditor().getParameters();
   control.setValue("initial_values", inits); // $NON-NLS-1$
   UserFunction[] functions = p.getFunctionEditor().getMainFunctions();
   control.setValue("main_functions", functions); // $NON-NLS-1$
   functions = p.getFunctionEditor().getSupportFunctions();
   if (functions.length > 0) control.setValue("support_functions", functions); // $NON-NLS-1$
   // save start and end frames (if custom)
   if (p.startFrame > 0) control.setValue("start_frame", p.startFrame); // $NON-NLS-1$
   if (p.endFrame < Integer.MAX_VALUE) control.setValue("end_frame", p.endFrame); // $NON-NLS-1$
   // save inspector size and position
   if (p.inspector != null && p.trackerPanel != null && p.trackerPanel.getTFrame() != null) {
     // save inspector location relative to frame
     TFrame frame = p.trackerPanel.getTFrame();
     int x = p.inspector.getLocation().x - frame.getLocation().x;
     int y = p.inspector.getLocation().y - frame.getLocation().y;
     control.setValue("inspector_x", x); // $NON-NLS-1$
     control.setValue("inspector_y", y); // $NON-NLS-1$  		
     control.setValue("inspector_h", p.inspector.getHeight()); // $NON-NLS-1$
     control.setValue("inspector_visible", p.inspector.isVisible()); // $NON-NLS-1$
   }
 }
Beispiel #5
0
 /**
  * Loads an object with data from an XMLControl.
  *
  * @param control the control
  * @param obj the object
  * @return the loaded object
  */
 public Object loadObject(XMLControl control, Object obj) {
   // load track data
   XML.getLoader(TTrack.class).loadObject(control, obj);
   ParticleModel p = (ParticleModel) obj;
   p.mass = control.getDouble("mass"); // $NON-NLS-1$
   p.inspectorX = control.getInt("inspector_x"); // $NON-NLS-1$
   p.inspectorY = control.getInt("inspector_y"); // $NON-NLS-1$
   p.inspectorH = control.getInt("inspector_h"); // $NON-NLS-1$
   p.showInspector = control.getBoolean("inspector_visible"); // $NON-NLS-1$
   Parameter[] params = (Parameter[]) control.getObject("user_parameters"); // $NON-NLS-1$
   p.getParamEditor().setParameters(params);
   params = (Parameter[]) control.getObject("initial_values"); // $NON-NLS-1$
   // remove trailing "0" from initial condition parameters
   for (int i = 0; i < params.length; i++) {
     Parameter param = params[i];
     String name = param.getName();
     int n = name.lastIndexOf("0"); // $NON-NLS-1$
     if (n > -1) {
       // replace parameter with new one
       name = name.substring(0, n);
       Parameter newParam = new Parameter(name, param.getExpression());
       newParam.setDescription(param.getDescription());
       newParam.setNameEditable(false);
       params[i] = newParam;
     }
   }
   p.getInitEditor().setParameters(params);
   UserFunction[] functions =
       (UserFunction[]) control.getObject("main_functions"); // $NON-NLS-1$
   p.getFunctionEditor().setMainFunctions(functions);
   functions = (UserFunction[]) control.getObject("support_functions"); // $NON-NLS-1$
   if (functions != null) {
     for (int i = 0; i < functions.length; i++) {
       p.getFunctionEditor().addObject(functions[i], false);
     }
   }
   p.functionPanel.refreshFunctions();
   int n = control.getInt("start_frame"); // $NON-NLS-1$
   if (n != Integer.MIN_VALUE) p.startFrame = n;
   else {
     p.startFrameUndefined = true;
   }
   n = control.getInt("end_frame"); // $NON-NLS-1$
   if (n != Integer.MIN_VALUE) p.endFrame = n;
   return obj;
 }
Beispiel #6
0
  /**
   * Chooses data functions from a DataBuilder XMLControl.
   *
   * @param control the XMLControl
   * @param description "Save" or "Load"
   * @param selectedFunctions collection of DataFunction choices
   * @return true if user clicked OK
   */
  @SuppressWarnings("unchecked")
  protected boolean chooseBuilderDataFunctions(
      XMLControl control, String description, Collection<String[]> selectedFunctions) {
    ListChooser listChooser =
        new ListChooser(
            TrackerRes.getString(
                "TrackerPanel.DataBuilder." + description + ".Title"), // $NON-NLS-1$ //$NON-NLS-2$
            TrackerRes.getString(
                "TrackerPanel.DataBuilder."
                    + description
                    + ".Message"), //$NON-NLS-1$ //$NON-NLS-2$
            this);
    listChooser.setSeparator(" = "); // $NON-NLS-1$
    // choose the elements and save
    ArrayList<String[]> originals = new ArrayList<String[]>();
    ArrayList<String[]> choices = new ArrayList<String[]>();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> expressions = new ArrayList<String>();
    ArrayList<String> trackTypes = new ArrayList<String>();

    Map<String, XMLControl> xmlControlMap = new TreeMap<String, XMLControl>();
    Map<String, ArrayList<Parameter>> parameterMap = new TreeMap<String, ArrayList<Parameter>>();
    Map<String, ArrayList<String[]>> functionMap = new TreeMap<String, ArrayList<String[]>>();
    for (Object obj : control.getPropertyContent()) {
      if (obj instanceof XMLProperty) {
        XMLProperty prop = (XMLProperty) obj;
        for (XMLControl xmlControl : prop.getChildControls()) {
          if (xmlControl.getObjectClass() != DataFunctionPanel.class) continue;

          // get track type (description) and map to panel xmlControl
          String trackType = xmlControl.getString("description"); // $NON-NLS-1$
          xmlControlMap.put(trackType, xmlControl);

          // get the list of functions for this track type
          ArrayList<String[]> functions = functionMap.get(trackType);
          if (functions == null) {
            functions = new ArrayList<String[]>();
            functionMap.put(trackType, functions);
          }
          // add functions found in this xmlControl unless already present
          ArrayList<String[]> panelFunctions =
              (ArrayList<String[]>) xmlControl.getObject("functions"); // $NON-NLS-1$
          outer:
          for (String[] f : panelFunctions) {
            // check for duplicate function names
            for (String[] existing : functions) {
              if (existing[0].equals(f[0])) continue outer;
            }
            functions.add(f);
          }

          // get the list of parameters for this track type
          ArrayList<Parameter> params = parameterMap.get(trackType);
          if (params == null) {
            params = new ArrayList<Parameter>();
            parameterMap.put(trackType, params);
          }
          // add parameters found in this xmlControl unless already present
          Parameter[] panelParams =
              (Parameter[]) xmlControl.getObject("user_parameters"); // $NON-NLS-1$
          outer:
          for (Parameter p : panelParams) {
            if (trackType.endsWith("PointMass")
                && p.getName().equals("m")) { // $NON-NLS-1$ //$NON-NLS-2$
              continue outer;
            }
            // check for duplicate parameter names
            for (Parameter existing : params) {
              if (existing.getName().equals(p.getName())) continue outer;
            }
            params.add(p);
          }
        }
      }
    }

    for (String trackType : functionMap.keySet()) {
      ArrayList<String[]> functions = functionMap.get(trackType);
      for (String[] f : functions) {
        originals.add(f);
        choices.add(f);
        names.add(f[0]);
        expressions.add(f[1]);
        String shortName = XML.getExtension(trackType);
        String localized = TrackerRes.getString(shortName + ".Name"); // $NON-NLS-1$
        if (!localized.startsWith("!")) shortName = localized; // $NON-NLS-1$
        trackTypes.add("[" + shortName + "]"); // $NON-NLS-1$ //$NON-NLS-2$
      }
    }
    // select all by default
    boolean[] selected = new boolean[choices.size()];
    for (int i = 0; i < selected.length; i++) {
      selected[i] = true;
    }

    if (listChooser.choose(choices, names, expressions, trackTypes, selected)) {
      // compare choices with originals and remove unwanted object content
      for (String[] function : originals) {
        if (!choices.contains(function)) {
          for (String trackType : xmlControlMap.keySet()) {
            ArrayList<String[]> functions = functionMap.get(trackType);
            functions.remove(function);
          }
        }
      }
      // set functions in xmlControl for each trackType
      for (String trackType : xmlControlMap.keySet()) {
        ArrayList<String[]> functions = functionMap.get(trackType);
        ArrayList<Parameter> paramList = parameterMap.get(trackType);
        Parameter[] params = paramList.toArray(new Parameter[paramList.size()]);
        XMLControl xmlControl = xmlControlMap.get(trackType);
        xmlControl.setValue("functions", functions); // $NON-NLS-1$
        xmlControl.setValue("user_parameters", params); // $NON-NLS-1$
      }

      // keep only xmlControls that have functions and are in xmlControlMap
      for (Object next : control.getPropertyContent()) {
        if (next instanceof XMLProperty
            && ((XMLProperty) next).getPropertyName().equals("functions")) { // $NON-NLS-1$
          XMLProperty panels = (XMLProperty) next;
          java.util.List<Object> content = panels.getPropertyContent();
          ArrayList<Object> toRemove = new ArrayList<Object>();
          for (Object child : content) {
            XMLControl xmlControl = ((XMLProperty) child).getChildControls()[0];
            if (!xmlControlMap.values().contains(xmlControl)) {
              toRemove.add(child);
            } else { // check to see if functions is empty
              ArrayList<String[]> functions =
                  (ArrayList<String[]>) xmlControl.getObject("functions"); // $NON-NLS-1$
              if (functions == null || functions.isEmpty()) {
                toRemove.add(child);
              }
            }
          }
          for (Object remove : toRemove) {
            content.remove(remove);
          }
        }
      }

      return true;
    }
    return false;
  }