/** * This class will function to allow models to be simulated in 3D given a *.log file. * The way this class works is that it copies two files into the project folder of a model sim where the .log file is located. * And the DEVSview program is run from that project folder [log file and the 2 files must be in the same folder for DEVSview to function]. * The project folder will be checked for any *.log files. If no log files exists then error window pops up. * * Possible enhancement could be that the user can select the location of log file and given * the location it copies the 2 files necessary to animate in 3D into the user defined path * Currently the DEVSView Button will only work if a log file is located in the project directory. * * Note: DEVSview can be run anywere and files can be imported by typing the whole path + name, however this is not very efficient. * I've just made it so that DEVSview appears in the same directory as the log file this way there's NO need to type * the WHOLE path name [paste is not permitted in DEVSview.exe] which is tedious work. * @author Jing Cao * @version 1.0 */ package CELLDEV.buttons; //this class is part of the CELLDEV package import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.runtime.IPath; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IPageLayout; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.PlatformUI; public class DEVSViewAction extends Action implements IWorkbenchWindowActionDelegate{ //sets the parent shell; responsible for modailty of windows. private Shell parent = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();//parent shell responsible for modality of message boxes public void run(IAction action) { try { //Setting path variables IWorkspaceRoot root =(IWorkspaceRoot)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput(); IPath projectPath = root.getLocation(); //location project is saved. eg. where log files and ma files are located String selectPath = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IPageLayout.ID_RES_NAV).toString(); selectPath = selectPath.substring(selectPath.indexOf("/")+1, selectPath.lastIndexOf("/")); //concatenate the string to get rid of [] projectPath = projectPath.append(selectPath +"/");//project path is now set IPath devsViewPath = root.getLocation().removeLastSegments(1); //devsViewPath is where DEVSview.exe and the GLUT32.dll files are found devsViewPath = devsViewPath.append("/plugins/CD++Builder_1.1.0/DEVSview/"); //end setting paths if (!logFileExist(projectPath, root)) return;//if no log files then do nothing //else log file exists. String unixCMD = "cp -u \"" + devsViewPath + "DEVSview.exe\" \"" + devsViewPath + "GLUT32.dll\" \"" + projectPath + "\""; //command to be executed [copies 2 files into the project directory] System.out.println("UnixCMD: " + unixCMD); Runtime.getRuntime().exec(unixCMD);//copies 2 files into the project folder /********************IMPORTANT*********************/ /* The following format for unixCMD MUST be used in order to execute the DEVSview.exe -------> cmd /c start /D"C:/Program Files/eclipse test/runtime-workspace/bounce/" "C:/Program Files/eclipse test/runtime-workspace/bounce/" "C:/Program Files/eclipse test/runtime-workspace/bounce/DEVSview.exe" It is possible to open the file by other means however you will NOT be able to import [e.g vis_import_logfile bouncelog.log] log files and run DEVSview.exe properly. */ unixCMD = "cmd /c start /D\""+projectPath+"\" " + "\""+projectPath+"\" " + "\""+projectPath+"DEVSview.exe\""; Runtime.getRuntime().exec(unixCMD);//opens DEVSview.exe from project folder } catch (Exception e) { e.printStackTrace(); //print the stack trace //prompt user of error and possible way to fix problem MessageBox mb = new MessageBox(parent, SWT.RETRY | SWT.ICON_ERROR | SWT.APPLICATION_MODAL); mb.setText("ERROR"); if (e instanceof StringIndexOutOfBoundsException) mb.setMessage("\nYou must select a project file or folder to begin."); else mb.setMessage("Check that DEVSView and GLUT32.dll are placed in CD++Builder_1.1.0/DEVSview/ folder."); mb.open(); } } public void dispose() { } public void init(IWorkbenchWindow window) { } public void selectionChanged(IAction action, ISelection selection) { } /** * This method will return true if a log file exists in the project folder * and method will return false if otherwise and also prompt user with a message box displaying the problem. * @param projectPath * @param root * @return boolean */ private boolean logFileExist(IPath projectPath, IWorkspaceRoot root) throws Exception{ IResource[] projectFiles; //stores array of files projectFiles = root.getContainerForLocation(projectPath).members(); //gets files to store in array //for loop that checks for files that end in .LOG for (int i = 0; i < projectFiles.length; i++) { if ((projectFiles[i].getName().toUpperCase()).endsWith(".LOG")) return true; } //else message box to prompt user of error MessageBox mb = new MessageBox(parent,SWT.RETRY | SWT.ICON_ERROR | SWT.APPLICATION_MODAL); mb.setText("ERROR"); mb.setMessage("Missing *.log file in project folder. \nPlease run Simu and try again."); mb.open(); return false; } }//end class