/** * 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) { } }
/** * 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) { } }