/* * V1.0 Edited by Jing Cao and Omair (05/12/2005) * * - Fixed problem with run * Problem: on the Navigator bar you can select ANY file that ends with *drw [generic file with no extension] * even though it may not be a *.drw file and the program will load the file name in to the path [not desired] * Fix: changed "drw" to ".drw" this way ONLY files with extension .drw will be shown at the path when selected and compareAction button is pressed. * * - Fixed problem regarding selection of two or more .drw files. * Problem: error when sellecting two draw log files from the navigator view * Now: user can select one or more draw log files from the navigator view. * if more than 2 files are selected then there's an error notification * if 2 .drw files are selected then the button will function normally by comparing the two files. * if 1 .drw file is selected then the .drw fill will show up in one of the browse text box. * */ package CELLDEV.buttons; import java.util.ArrayList; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; 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; import CDBuilder.console.ConsoleDocument; /** * @author ssim, Ali Monadi * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class CompareAction extends Action implements IWorkbenchWindowActionDelegate{ private IContainer container; private String[] drwFileName = new String[2]; private IPath[]projectPath = new IPath[2]; public CompareAction() { super(); setText("Extract"); setDescription("This is a Extracter"); drwFileName[0] = null; drwFileName[1] = null; } public void run() { try { //variable declarations Shell parent = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); //was previously before the try-catch block ConsoleDocument cdos = ConsoleDocument.getCDOS(); IWorkspaceRoot root = (IWorkspaceRoot)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput(); IPath internalPath = root.getLocation().removeLastSegments(1); String internalPathForUnix = internalPath.toString().charAt(0) + internalPath.toString().substring(2); PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("ConsoleView");//show console view //get paths projectPath[0] = root.getLocation(); projectPath[1] = root.getLocation(); //gets path of the selected file(s) String selectPath = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IPageLayout.ID_RES_NAV).toString(); /* there are two types of strings that are returned for selectPath 1. [*] no comma 2. [*,*] with comma; comma indicates multiple files selected -string should look like: [L/maze/maze1.drw, L/maze/maze2.drw] if multiple files are selected -string looks like: [L/maze/maze1.drw] when there's one file selected */ //check for string type 1. if (selectPath.indexOf(",") == -1) { drwFileName[0] = selectPath.substring( selectPath.lastIndexOf("/")+1, selectPath.lastIndexOf("]") ); selectPath = selectPath.substring(selectPath.indexOf("/")+1, selectPath.lastIndexOf("/")); //concatenate the string to get rid of [] projectPath[0] = projectPath[0].append(selectPath +"/"); //set projectPath[0] drwFileName[1] = null;//redundant it should be null by default but just incase! projectPath[1] = null; } else {//string is of type 2. if(selectPath.indexOf(",") != selectPath.lastIndexOf(","))//check for more than two files selected {//beg if MessageBox errorMsg = new MessageBox(new Shell(), SWT.ICON_ERROR | SWT.OK); errorMsg.setMessage("Too many files selected for comparison.\nYou can only select two *.drw files."); errorMsg.setText("ERROR"); errorMsg.open(); return; }//end if else{//beg else1 //there are TWO FILES [not necessarily of type *.drw] selected. //Now get those file names into drwFileName[0] and drwFileName[1] //temporary string variables String tempString1 = selectPath.substring(0, selectPath.lastIndexOf(",")+1);//let tempString1 be the string from the first '[' to the ',' String tempString2 = tempString1.substring(tempString1.indexOf("/")+1, tempString1.lastIndexOf("/") );// contains path of folder that contains drwFileName[0] String tempString3 = selectPath.substring(selectPath.lastIndexOf(",")+1, selectPath.lastIndexOf("]")+1 );//let tempString3 be the string from the first ',' to the ']' String tempString4 = tempString3.substring(tempString3.indexOf("/")+1, tempString3.lastIndexOf("/") );//let tempString4 contain path of folder that contains drwFileName[1] //get drwFileName[0] drwFileName[0] = tempString1.substring( tempString1.lastIndexOf("/")+1, tempString1.lastIndexOf(",") ); //get drwFileName[1] drwFileName[1] = selectPath.substring( selectPath.lastIndexOf("/")+1, selectPath.lastIndexOf("]") ); //set projectPaths projectPath[0] = projectPath[0].append(tempString2 + "/"); projectPath[1] = projectPath[1].append(tempString4 + "/"); if (!projectPath[0].equals(projectPath[1])) {//checks if the paths for the two files selected are the same if not then error message results MessageBox samePathMsg = new MessageBox(new Shell(), SWT.ICON_ERROR | SWT.OK); samePathMsg.setMessage("The path for the two *.drw files must be the same!"); samePathMsg.setText("ERROR"); samePathMsg.open(); return; } }//end else1 }//end else internalPath = internalPath.append("/plugins/CD++Builder_1.1.0/internal/"); /*debugging code System.out.println("selectPath: " + selectPath); // System.out.println("drwFileName[0]: " + drwFileName[0]); System.out.println("drwFileName[1]: " + drwFileName[1]); System.out.println("projectPath[0]: " + projectPath[0]); System.out.println("projectPath[1]: " + projectPath[1]); System.out.println("Internal Path: " + internalPath); */ //check for .drw extension if (drwFileName[0] !=null && (!(drwFileName[0].toLowerCase()).endsWith(".drw")))//changes the file to lower case and checks if it ends with ".drw" drwFileName[0] = null; if (drwFileName[1] !=null && (!(drwFileName[1].toLowerCase()).endsWith(".drw")))//changes the file to lower case and checks if it ends with ".drw" drwFileName[1] = null; /*-------------------------------------------------------------------------------- * The following code [for unix] doesn't seem to be very useful. It is not used anywhere! * * There's a problem here because one projectPath can be null this needs to be addressed. */ /* IContainer container1 = root.getContainerForLocation(projectPath[0]); IContainer container2 = root.getContainerForLocation(projectPath[1]); String projectPathForUnix1 = projectPath[0].toString().charAt(0) + projectPath[0].toString().substring(2); String projectPathForUnix2 = projectPath[1].toString().charAt(0) + projectPath[1].toString().substring(2); System.out.println("got after projectPathForUnix2"); */ for (int x = 0; x <2; x++)//this for loop goes through both project paths [if they are not null] and checks if there's drw files in both paths. {//open for if (projectPath[x]==null) continue; try { IResource[] projectFiles; IFile drwFile = null; String line; ArrayList typeFiles = new ArrayList(); projectFiles = root.getContainerForLocation(projectPath[x]).members(); for(int i = 0; i < projectFiles.length; i++){ if( (projectFiles[i] instanceof IFile) && (projectFiles[i].getName().toLowerCase().endsWith(".drw"))){ drwFile = (IFile) projectFiles[i]; break; }//end if } if (drwFile == null){ throw new NullPointerException(); } } catch (Exception e1) { e1.printStackTrace(); MessageBox noSimu = new MessageBox(parent, SWT.ICON_ERROR | SWT.OK); noSimu.setMessage("Can not extract, Drawlog file not found"); noSimu.setText("Drawlog file Not Found"); noSimu.open(); return; } }//end for cdos.clear(); CompareGUI dialog = new CompareGUI(parent, projectPath[0], internalPath, drwFileName[0], drwFileName[1]); dialog.create(); dialog.open(); dialog.close(); //container1.refreshLocal(1, null); //container2.refreshLocal(1, null); }catch (Exception np) { np.printStackTrace(); MessageBox npGui = new MessageBox(new Shell(), SWT.ICON_ERROR | SWT.OK); npGui.setMessage("You must select a file or project folder to start simulations! "); npGui.setText("Project Not Selected"); npGui.open(); ConsoleDocument.getCDOS().set("Please select a project or folder from the navigator."); } } public void run(IAction action) { run(); } public void dispose() { // do nothing. } public void init(IWorkbenchWindow window) { // do nothing. } public void selectionChanged(IAction action, ISelection selection) { } }