Math2mat
|
00001 00020 package m2m.frontend.actions; 00021 00022 import java.io.File; 00023 00024 import org.eclipse.core.filesystem.EFS; 00025 import org.eclipse.core.filesystem.IFileStore; 00026 import org.eclipse.core.runtime.CoreException; 00027 import org.eclipse.swt.SWT; 00028 import org.eclipse.swt.widgets.FileDialog; 00029 import org.eclipse.swt.widgets.MessageBox; 00030 import org.eclipse.ui.IWorkbenchPage; 00031 import org.eclipse.ui.IWorkbenchWindow; 00032 import org.eclipse.ui.PlatformUI; 00033 00034 import m2m.backend.project.M2MProject; 00035 import m2m.frontend.actions.M2MAction; 00036 import m2m.frontend.view.Editor; 00037 import m2m.frontend.view.M2MFileStoreEditorInput; 00038 import m2m.frontend.view.NavigationView; 00039 00040 00041 public class NewProjectAction extends M2MAction { 00042 00043 boolean empty; 00044 00045 public NewProjectAction(IWorkbenchWindow window, String label,boolean empty) { 00046 super(window,label); 00047 this.empty=empty; 00048 00049 if (empty) { 00050 // The id is used to refer to the action in a menu or toolbar 00051 setId(ICommandIds.CMD_NEW); 00052 // Associate the action with a pre-defined command, to allow key bindings. 00053 setActionDefinitionId(ICommandIds.CMD_NEW); 00054 } 00055 else { 00056 // The id is used to refer to the action in a menu or toolbar 00057 setId(ICommandIds.CMD_NEWFROMFILE); 00058 // Associate the action with a pre-defined command, to allow key bindings. 00059 setActionDefinitionId(ICommandIds.CMD_NEWFROMFILE); 00060 } 00061 setImageDescriptor(m2m.frontend.Activator.getImageDescriptor("/icons/new.gif")); 00062 } 00063 00067 public void run() { 00068 00069 String fileName = new String(); 00070 00071 String path; 00072 //open a file dialog box 00073 FileDialog dialog = new FileDialog(window.getShell(), SWT.SAVE); 00074 dialog.setText("Create new project"); 00075 String[] filterExt = { "*.m2m"}; 00076 dialog.setFilterExtensions(filterExt); 00077 path = dialog.open(); 00078 if (path == null) 00079 return; 00080 00081 //test if the name of the file the user wants to save contains the chosen extension or not 00082 //if not, add the extension to the file name 00083 if ((path.lastIndexOf(".") == -1 ? "" : path.substring(path.lastIndexOf(".")+1, path.length())).equalsIgnoreCase(dialog.getFilterExtensions()[dialog.getFilterIndex()].substring(2))) { 00084 fileName = path; 00085 } else { 00086 String filterExtension = dialog.getFilterExtensions()[dialog.getFilterIndex()]; 00087 fileName = path+filterExtension.substring(1, filterExtension.length()); 00088 } 00089 00090 File file = new File(fileName); 00091 00092 String octaveFileName=""; 00093 00094 if (!file.exists()) { 00095 if (!empty) { 00096 //open file dialog 00097 FileDialog dlg = new FileDialog(this.window.getShell(), SWT.OPEN); 00098 dlg.setText("Choose the Octave/Matlab file"); 00099 String[] filterExt1 = { "*.m"}; 00100 String[] filterNames = {"Source file (*.m)"}; 00101 dlg.setFilterExtensions(filterExt1); 00102 dlg.setFilterNames(filterNames); 00103 octaveFileName= dlg.open(); 00104 00105 /* Check if a path has been selected */ 00106 if(octaveFileName == null) 00107 return; 00108 if (octaveFileName.isEmpty()) 00109 return; 00110 } 00111 00112 createProject(file,octaveFileName); 00113 } 00114 else { 00115 MessageBox mess=new MessageBox(window.getShell(), SWT.ICON_ERROR | SWT.OK); 00116 mess.setMessage("The project file already exists! Pleae choose another one."); 00117 mess.setText("Error"); 00118 mess.open(); 00119 } 00120 } 00121 00122 private void createProject(File file,String octaveFileName) { 00123 // String projectPath = file.getAbsolutePath().replace("\\", "/").substring(0, file.getAbsolutePath().replace("\\", "/").lastIndexOf("/")+1); 00124 // File sourceFile = new File(projectPath + M2MProject.getSourceDirName()+"/" + file.getName().substring(0, file.getName().lastIndexOf(".")+1)+"m"); 00125 00126 M2MProject project=new M2MProject(); 00127 if (empty) { 00128 if (!project.CreateEmptyProject(file.getAbsolutePath())) { 00129 MessageBox mess=new MessageBox(window.getShell(), SWT.ICON_ERROR | SWT.OK); 00130 mess.setMessage("The project can not be created. It seems that there is a problem accessing the choosen directory."); 00131 mess.setText("Error"); 00132 mess.open(); 00133 return; 00134 } 00135 } 00136 else { 00137 if (!project.CreateNewProjectFromMath(file.getAbsolutePath(),octaveFileName)) { 00138 MessageBox mess=new MessageBox(window.getShell(), SWT.ICON_ERROR | SWT.OK); 00139 mess.setMessage("The project can not be created. It seems that there is a problem accessing the choosen directory."); 00140 mess.setText("Error"); 00141 mess.open(); 00142 return; 00143 } 00144 } 00145 //open a new editor for the file 00146 IWorkbenchPage page= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 00147 try { 00148 IFileStore fileStore= EFS.getStore(file.toURI()); 00149 Editor editor=(Editor)page.openEditor(new M2MFileStoreEditorInput(fileStore,project), Editor.ID, true); 00150 NavigationView.addList(file.getAbsolutePath()); 00151 editor.setParseDone(true); 00152 } catch (CoreException e) { 00153 e.printStackTrace(); 00154 } 00155 } 00156 }