/* * v1.0 Edited by Jing and Omair (05/11/2005) * -Fixed FileDialog box; made modal with respect to "this class" so that when it is clicked it stays on top. * * -Fixed Save as .bat --> so that now when there's incorrect *.ma filename or if there's no filename * you won't be able to save a *.bat file. * * -Fixed Error in testString() * currently the function is setup so that it seeks numbers in the string [no matter where] and contatenates them, adds 1 to the concatenated number and appends the number to the end of the string. * so if you make a file name called 4567i and you overwrite the file [using automatic] you will get a new file called: 4567i4568 when it should be 4567i1 * Fix: rewrote the function by checking from the back instead of from the front to find the location of a character. Then the numbers [after the char] will be chopped off as a substring and returned. * * -Fixed Error in setDigit() * When trying to save a *.bat file called "2" and saving another file called "2" using the automatic save will result in either window not responding or nothing happens. * Fix: altered the setDigit() [no more index out of bounds exception] so that when filename is a number it returns a string representation of the newly updated number. * * -Fixed Error when closing window * Bug: When window is first opened, it will not close when clicking on the "X" (close button at the top right corner of window) * Fix: set OK2Close = true by default so that window will close. * * -Fixed Error for when canceling browse * BUG: When there's a file in the text box [text box filled] beside the browse button and if browse is clicked and you hit cancel to "cancel" file selection. * The file name will be erased from the text box [empty text box] and you will have to reselect the file. * Fix: Check for if the file returned from the file selection is equal to "" [nothing] if so then don't update text box field. * * -Fixed error which occurs if the root directory of eclipse has spaces in it * Bug: If the root directory of eclipse has spaces in it then this button didn't function at all * Fix: We need to enclose IPath objects namely path and internalPath in double quotes by simply concatenating * their string representations by "/"" * * -Fixed error which occurs if any fileNames have a space in them * Bug: If the for e.g. say a .val file has spaces in it then an error will occur terminating the function * immediately * Fix: All filenames were enclosed in double quotes which was done in collect parameters */ package CELLDEV.buttons; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; 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.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.PlatformUI; import CDBuilder.CDBuilderPlugin; import CDBuilder.buttons.OverwriteGui; import CDBuilder.console.ConsoleDocument; import CDBuilder.console.StreamHandler; /** * @author ssim, Ali Monadi * * This class is responsible for the popup gui which appears when the SimuAction button * is pressed. When proceed button is pressed, the requested application will run as a separate * process. */ public class GraflogGUI extends Dialog{ private Shell parent; private String title; private String message1; private String message2; private String message3; private String message4; private String message5; private String message6; private Button simuButton; private Button doneButton; private Button killButton; private Button saveSetButton; private Button loadSetButton; private Button saveBatButton; private Button loadBatButton; private Label colon1;//, colon2, colon3; private Button checkBoxOut; private Button checkBoxAdv; private Button checkBoxTime; private Button fileButtonDRW; private Button fileButtonOut; private Button checkBoxComment; private Button checkBox3D; private Button checkBox2D; private Button checkBoxV; private Text textHour, textMin, textSec, textmilliS; private Text textComment; private Text textOut; private Text textPalette; private Text textAdvanceOverTime; private Text textScale; private Text textX,textY; private Text textAdv; private static boolean checkedOut = true; private static boolean checkedAdv = false; private static boolean checkedComment = false; private boolean simuKilled = false; private RunSimu rsimu; private Process simuProcess; private Label errorMessageLabel; private IInputValidator validator; private String COLFileName; private IPath path; private IPath internalPath; private Composite composite; private int n; //window will only cose if OK2Close is true. This is set to true automatically at beginning [not set in constructor] private boolean OK2Close=true; class conWrite implements Runnable { String line; public conWrite(String line) { this.line = line; } public void run() { ConsoleDocument.getCDOS().set(line); } } class RunSimu extends Thread { Dialog d; String command; public RunSimu(String command, Dialog d) { this.command = command; this.d = d; } public void run() { try { ConsoleDocument cdos = ConsoleDocument.getCDOS(); IContainer container = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(path); FileWriter tempFile = new FileWriter(path.append("tempSimu.bat").toString()); //testing purposes tempFile.write("\""+internalPath+"GrafLog\"" + command); //internalPath is where the cd++.exe is located tempFile.close(); simuProcess = Runtime.getRuntime().exec("cmd /c \"" + path.toString() + "tempSimu.bat\"", null, new File(path.toString())); StreamHandler outputHandler = new StreamHandler(simuProcess.getInputStream(), "OUTPUT", true); StreamHandler errorHandler = new StreamHandler(simuProcess.getErrorStream(), "ERROR", true); outputHandler.start(); errorHandler.start(); int exitVal = simuProcess.waitFor(); container.refreshLocal(1,null); ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path.append("tempSimu.bat")).delete(true,null); Display.getDefault().syncExec(new Runnable() { public void run(){ doneButton.setEnabled(true); killButton.setEnabled(false); simuButton.setEnabled(true); d.close(); } }); if (simuKilled) { Display.getDefault().syncExec(new conWrite("***User has killed the simulation***" + "\n")); simuKilled = false; } } catch (Throwable T) { T.printStackTrace(); } } } public GraflogGUI(Shell parentShell,IPath path, IPath internalPath,String COLFileName) { super(parentShell); parent = parentShell; this.title = "GrafLog3D"; message1 = "Pallete file (.col)"; message2 = "Draw log file (.drw)"; message3 = "Enter scale(e) and Number of advances over time(s)"; message4 = "Advanced users Only. Enter desired paramters"; message5 = "Comments"; this.COLFileName = COLFileName; this.internalPath = internalPath; this.path = path; } /* * Determines if a text box is left empty and displays error message * @return false of empty true otherwise */ private boolean isEmpty(String parameter,Text textBox){ String value = textBox.getText().trim(); if(value.length()== 0){ MessageBox mbox = new MessageBox(new Shell(),SWT.ICON_ERROR | SWT.OK); mbox.setText("Simulation Stopped"); mbox.setMessage(parameter+" is not entered"); mbox.open(); try { mbox.wait(); } catch (InterruptedException e) { e.printStackTrace(); } doneButton.setEnabled(true); killButton.setEnabled(false); simuButton.setEnabled(true); return true; } return false; } /* * checks to see if the given string matches a file name found in the project directory * * @param String file, name of file * @param String errMsg, message to be displayed IF file was not found */ private boolean checkFileExistence(String file, String errMsg){ IWorkspaceRoot root = (IWorkspaceRoot)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput(); IResource[] files; IFile fileHandle = null; try { files = root.getContainerForLocation(path).members(); for(int i=0;i='0') && (c<='9')){ test = test.substring(0,test.length()-1); c=test.charAt(test.length()-1); } else{ done = true; } } test= test.trim(); test= test.concat(Integer.toString(n)); return test; } //this method takes a filename(stripped off ".bat") and // returns the digits at the end as a string private String testString(String fileName){ String test; String number=new String(""); char c; boolean done = false; test = new String(fileName); c = test.charAt(0); int counter = test.length(); while(!done){ counter--; if((c>='0') && (c<='9')){ number = number.concat(String.valueOf(c)); } if(counter!=0){ test = test.substring(1, test.length()); c = test.charAt(0); } if(counter == 0){done=true;} } if(number.equals(""))number= "0"; number = number.trim(); return number; } //this function is used by load.bat to load a new file. private String NewFileGui(String filter) { composite.setEnabled(false); simuButton.setEnabled(false); doneButton.setEnabled(false); OK2Close = false; FileDialog fileDialog = new FileDialog(parent); String[] extention = new String[1]; extention[0] = filter; fileDialog.setFilterPath(path.toString()); fileDialog.setFilterExtensions(extention); fileDialog.open(); composite.setEnabled(true); simuButton.setEnabled(true); doneButton.setEnabled(true); loadBatButton.setEnabled(true); saveBatButton.setEnabled(true); OK2Close = true; return fileDialog.getFileName(); } /* * This function is responsible for saving a batch file */ private void SaveBatchFileGui(String filter) { String[] param = new String[10]; String Command = collectParameters(); String temp; if (Command == null){ composite.setEnabled(true); doneButton.setEnabled(true); simuButton.setEnabled(true); saveBatButton.setEnabled(true); loadBatButton.setEnabled(true); OK2Close = true; return; } composite.setEnabled(false); simuButton.setEnabled(false); doneButton.setEnabled(false); FileDialog fileDialog = new FileDialog(parent, SWT.SAVE); String[] extention = new String[1]; extention[0] = filter; fileDialog.setFilterPath(path.toString()); fileDialog.setFilterExtensions(extention); fileDialog.open(); String fileName = fileDialog.getFileName(); try { if (fileName.endsWith(".bat")) { fileName = fileName.substring(0, fileName.length()-4); } if(fileName == ""){ fileDialog.setFilterPath(""); fileDialog.setFilterExtensions(null); composite.setEnabled(true); simuButton.setEnabled(true); doneButton.setEnabled(true); saveBatButton.setEnabled(true); loadBatButton.setEnabled(true); OK2Close = true; return; } File existFile = new File(path.toString() + fileName + ".bat"); boolean cancelled = false; if (existFile.exists()) { IPreferenceStore store = CDBuilderPlugin.getDefault().getPreferenceStore(); if(store.getInt("batchChoice") == 3) { OverwriteGui ovG = new OverwriteGui(new Shell()); ovG.open(); if (ovG.buttonPressed() == 0) { System.out.print("canceled saving bat"); cancelled = true; composite.setEnabled(true); simuButton.setEnabled(true); doneButton.setEnabled(true); saveBatButton.setEnabled(true); loadBatButton.setEnabled(true); OK2Close = true; } if (ovG.buttonPressed() == 2) { boolean looking = true; int n = 0; while(looking) { n=(Integer.decode(testString(fileName))).intValue(); n++; temp = setDigit(fileName,n); File existFileCount = new File(path.toString() + temp+ ".bat"); System.out.println(path.toString() + temp + ".bat"); fileName = temp; if(!existFileCount.exists()) { looking = false; MessageBox mbox = new MessageBox(new Shell(),SWT.ICON_INFORMATION | SWT.OK); mbox.setText("Saving File"); mbox.setMessage("The file has been saved as "+ fileName+".bat"); mbox.open(); composite.setEnabled(true); simuButton.setEnabled(true); doneButton.setEnabled(true); saveBatButton.setEnabled(true); loadBatButton.setEnabled(true); } } if(ovG.noAskAgain() ) store.setValue("batchChoice", 2); } if (ovG.buttonPressed() == 1) { if(ovG.noAskAgain() ) store.setValue("batchChoice", 1); } } else if(store.getInt("batchChoice") == 2) { boolean looking = true; int n = 0; while(looking) { n++; File existFileCount = new File(path.toString() + fileName + "-" + n + ".bat"); System.out.println(path.toString() + fileName +"-" + n + ".bat"); if(!existFileCount.exists()) { looking = false; fileName = fileName.concat("-" + Integer.toString(n)); } } } else { } } if (!cancelled) { composite.setEnabled(false); simuButton.setEnabled(false); doneButton.setEnabled(false); OK2Close = false; FileWriter batchFile = new FileWriter(path.toString() + fileName + ".bat"); System.out.println(path.toString() + fileName + ".bat"); String command = collectParameters(); if (command == null){ batchFile.close(); return; } System.out.println(command); batchFile.write(command + "\n"); batchFile.write("rem " + textComment.getText() + "\n"); batchFile.write("pause"); batchFile.close(); composite.setEnabled(true); simuButton.setEnabled(true); doneButton.setEnabled(true); saveBatButton.setEnabled(true); loadBatButton.setEnabled(true); OK2Close = true; } } catch (Exception e) { e.printStackTrace(); } } }