Пример #1
0
  /**
   * Create a new application.
   *
   * @param appName Application name
   * @param appDir Application directory
   * @param imgDir Image directory
   */
  public void createNewApp(String appName, String appDir, String imgDir) {
    // Application
    App newApp = null;
    // Application configuration file
    String appFileName = null;
    // Application resource directory name
    String appResourceDir = null;
    // Application engine
    AppEngine engine = AppEngine.getInstance();

    // Application name
    if (appName == null || (appName.trim()).equals("")) {
      MessageUtils.displayError(this, "The application name should be set!");
      return;
    }

    // Application directory
    if (appDir == null || (appDir.trim()).equals("")) {
      MessageUtils.displayError(this, "The application directory should be set!");
      return;
    }
    // Make the appName conforms to the OS
    String title = appName;
    title = title.replace('?', '_');
    title = title.replace(':', '_');
    title = title.replace('\\', '_');
    title = title.replace('/', '_');
    title = title.replace('*', '_');
    title = title.replace('\"', '_');
    title = title.replace('<', '_');
    title = title.replace('>', '_');
    title = title.replace('|', '_');
    title = title.replace(' ', '_');
    title = title.replace('\t', '_');
    // Try the name "appName.xml" first
    StringBuffer full = new StringBuffer();
    full.append(appDir);
    full.append(File.separator);
    full.append(title);
    appFileName = full.toString() + ".xml";
    File configFile = new File(appFileName);
    if (configFile.exists()) {
      // Get a unique name
      SimpleDateFormat formatter = new SimpleDateFormat("_MM_dd_yy_hh_mm_ss");
      String formatted = formatter.format(new Date());
      full.append(formatted);
      // The default name of the configuration file, can be changed
      // manually
      appFileName = full.toString() + ".xml";
      // New configuration file
      configFile = new File(appFileName);
    }
    try {
      boolean created = configFile.createNewFile();
      if (!created) {
        throw new RuntimeException("The file name '" + appFileName + "' already exists.");
      }
    } catch (Exception ioe) {
      MessageUtils.displayError(
          this,
          "Can not create the configuration file. You can empty the selected application directory to continue.");
      return;
    }

    // Get the app file name
    appFileName = appFileName.substring(appFileName.lastIndexOf(File.separator) + 1);

    // Application resource directory
    if (imgDir != null && !(imgDir.trim()).equals("")) {
      // Resource directory name
      appResourceDir = imgDir;
      // Create the resource directory.
      File resourceFile = new File(appDir + File.separator + imgDir);
      try {
        resourceFile.mkdir();
      } catch (Exception ioe) {
        MessageUtils.displayError(
            this,
            "Can not create the resource directory. The directory name should be valid accoding to the target operating system.");
        return;
      }
    }

    // Create an new application to represent it
    newApp = engine.appManager.newApp();
    newApp.setAppDir(appDir);
    newApp.setAppFileName(appFileName);
    newApp.setAppName(appName);
    newApp.setDirty(true);
    if (appResourceDir != null) {
      newApp.setAppResourceDir(appResourceDir);
    }
    try {
      engine.appManager.initApp(newApp);
    } catch (Exception e) {
      MessageUtils.error(
          this,
          "onOK",
          new sim.util.SimException(
              "LOAD-APPLICATION-INIT-001A", "Can not initialize application.", e));
      return;
    }
    engine.appManager.setCurrentApp(newApp);

    // Prepare the background image and relative path for the simulation
    // environment
    engine.system.env.resetToApp();

    AppStatusbar.getInstance().changeMessage("A new application is created successfully.");
    AppStatusbar.getInstance().changeAppStatus("App created");

    // Save the application information to external file --- Added on
    // 04092009
    new sim.core.AppTask().run();

    // Update title
    owner.setTitle(appName + " - " + configFile.getAbsolutePath());

    // Setup recent files
    sim.ui.menus.MainMenuBar.getInstance(null, null)
        .getRecentFilesHandler()
        .add(configFile.getAbsolutePath());
    sim.ui.menus.MainMenuBar.getInstance(null, null).getRecentFilesHandler().updateProperties();

    try {
      // Dispose this window
      DefineAppWindow.this.setVisible(false);
      DefineAppWindow.this.dispose();
    } catch (Exception e) {
    }
  }
Пример #2
0
  /**
   * Open the application of a full-path configuration
   *
   * @param path The full-path configuration file
   */
  public void openApp(String path) {

    // Application
    App newApp = null;
    // Application name
    String appName = null;
    // Application configuration file
    String appFileName = null;
    // Application directory full-path
    String appDir = null;
    // Application engine
    AppEngine engine = AppEngine.getInstance();

    // Get the configuration file full path
    if (path.equals("")) {
      MessageUtils.displayError("Please select an configuration file.");
      return;
    }

    // Load application name
    try {
      appName = engine.preloadAppDef(path);
    } catch (Exception e) {
      AppStatusbar.getInstance()
          .changeMessage(
              "The application is not loaded. Make sure the configuration file is correct.");
      return;
    }

    // Load application other information
    try {
      appFileName = path.substring(path.lastIndexOf(File.separator) + 1);
      appDir = path.substring(0, path.lastIndexOf(File.separator));
    } catch (Exception e) {
      AppStatusbar.getInstance().changeMessage("Can not obtain configuration file and directory.");
      return;
    }

    // Create an new application to represent it
    newApp = engine.appManager.newApp();
    newApp.setAppDir(appDir);
    newApp.setAppFileName(appFileName);
    newApp.setAppName(appName);
    newApp.setDirty(false);
    try {
      engine.appManager.initApp(newApp);
    } catch (Exception e) {
      MessageUtils.error(
          DefineAppWindow.class,
          "onOK",
          new sim.util.SimException(
              "LOAD-APPLICATION-INIT-001A", "Can not initialize application.", e));
      return;
    }
    engine.appManager.setCurrentApp(newApp);

    // Load the application configuration
    sim.core.AppLoader.loadAppFromFile(path, true);

    // Setup recent files
    sim.ui.menus.MainMenuBar.getInstance(null, null).getRecentFilesHandler().add(path);
    sim.ui.menus.MainMenuBar.getInstance(null, null).getRecentFilesHandler().updateProperties();

    try {

      // Update title
      owner.setTitle(appName + " - " + path);
      // Dispose this window
      DefineAppWindow.this.setVisible(false);
      DefineAppWindow.this.dispose();
    } catch (Exception e) {
    }
  }
Пример #3
0
/**
 * View of System editor. It includes a left-side navigation panel and a right-side app views
 *
 * @author Pavel, Fasheng Qiu
 * @version 1.0
 */
public class EditorView extends AppView implements SimulationListener {

  /** */
  private static final long serialVersionUID = -9106097929807432550L;

  /** Icon of the editor view */
  private static final String iconPath = "/sim/ui/images/editor.gif";

  /** Image icon object */
  private ImageIcon viewIcon = null;

  /** Navigation panel in the left side of the view */
  private NavigationPanel navPanel = null;

  /** Application engine */
  private AppEngine engineRef = AppEngine.getInstance();

  /** Tabs of application views */
  private AppView views[] = new AppView[2];

  /**
   * Constructor
   *
   * @param eng Application engine
   * @throws Exception If any exception occurs
   */
  public EditorView() throws Exception {

    // Icons
    viewIcon = new ImageIcon(engineRef.jrl.getImage(iconPath));

    // Left navigation panel
    navPanel = new NavigationPanel(engineRef, this);
    engineRef.registerNavigationPanel(navPanel);

    Border b = BorderFactory.createEmptyBorder(0, 5, 5, 5);
    setBorder(b);
    setLayout(new BorderLayout());

    add(navPanel, BorderLayout.WEST);

    // Tab of system editor and behavior network
    views[0] = new SystemEditView(engineRef, navPanel);
    add(views[0], BorderLayout.CENTER);
  }

  /** Settings of simulation environment are changed. Repaint the environment. */
  public void updateSystemEditorView() {
    ((SystemEditView) views[0]).updateSystemEditorView();
  }

  /** Settings of display component are changed. Repaint the editor */
  public void updateCategory(String categoryName, Category category) {
    ((SystemEditView) views[0]).updateCategory(categoryName, category);
  }

  /** Return the title of this editor view */
  public String getTitle() {
    return new String("Editor");
  }

  /** Return the icon of this view */
  public Icon getViewIcon() {
    return viewIcon;
  }

  /** Return the behavior view */
  public BehaviorView getBehaviorView() {
    return (BehaviorView) views[1];
  }

  /** Return the system editor view */
  public SystemEditView getSystemEditView() {
    return (SystemEditView) views[0];
  }

  /**
   * Update the entity position
   *
   * @param entity The entity to update
   * @param x The new x position
   * @param y The new y position
   */
  public void updateEntityPosition(Entity entity, int x, int y) {
    ((SystemEditView) views[0]).updateEntityPositionFromNavPanel(entity, x, y);
  }

  /**
   * Update the dynamically created category's property
   *
   * <p>If the property is "iconPath", the picture of the entity is changed. Hence, the entity
   * should be re-init.
   *
   * @param entity The entity whose property value is to be set
   * @param name The property name
   * @param value The new value
   */
  public void updateEntityProperty(Entity entity, String name, String value) {
    ((SystemEditView) views[0]).updateEntityProperty(entity, name, value);
  }

  /**
   * Simulation stopped event.
   *
   * @param time Current time when the simulation stopped
   */
  public void simulationStopped(int time) {

    // Simulation stopped, update position of all entities in repository
    engineRef.system.updatePositionOfEntities(time);

    // Update position in navigation tree
    engineRef.refreshPositionsInNavigationTree();
  }

  /** Refresh the view */
  public void refresh() {

    // Refresh parents
    super.refresh();

    // Reflect the new entity positions
    updateSystemEditorView();
  }

  /** Clear the view */
  public void clearAll() {
    navPanel.removeAllEntityNodes();
    views[0].clearAll();
  }

  /**
   * Clear the view of the specified category
   *
   * @param catName Name of the category entity to remove
   */
  public void clear(String catName) {
    // Remove all entities of the category
    java.util.List all = engineRef.system.getEntityByCategoryName(catName);
    for (int i = 0; i < all.size(); i++) {
      navPanel.removeEntity(((Category) all.get(i)));
    }
    // Repaint the system editor view
    views[0].clearAll();
  }

  /** Reload all information of this view */
  public void loadAll() {
    navPanel.removeAllEntityNodes();
    navPanel.addAllEntities();
    views[0].clearAll();
  }
}