Math2mat
|
00001 00020 package m2m.frontend.actions; 00021 00022 import java.io.File; 00023 00024 00025 import org.eclipse.core.filesystem.EFS; 00026 import org.eclipse.core.filesystem.IFileStore; 00027 import org.eclipse.core.runtime.CoreException; 00028 import org.eclipse.jface.dialogs.MessageDialog; 00029 import org.eclipse.swt.SWT; 00030 import org.eclipse.swt.widgets.FileDialog; 00031 import org.eclipse.ui.IEditorReference; 00032 import org.eclipse.ui.IWorkbenchPage; 00033 import org.eclipse.ui.IWorkbenchWindow; 00034 import org.eclipse.ui.PartInitException; 00035 import org.eclipse.ui.PlatformUI; 00036 import org.eclipse.ui.ide.FileStoreEditorInput; 00037 00038 import m2m.backend.octaveparser.ParsingException; 00039 import m2m.backend.project.M2MProject; 00040 import m2m.frontend.ApplicationActionBarAdvisor; 00041 import m2m.frontend.actions.M2MAction; 00042 import m2m.frontend.GUIProperties; 00043 import m2m.frontend.view.Editor; 00044 import m2m.frontend.view.M2MConsole; 00045 import m2m.frontend.view.M2MFileStoreEditorInput; 00046 import m2m.frontend.view.NavigationView; 00047 00048 00049 public class OpenFileAction extends M2MAction { 00050 00051 public OpenFileAction(IWorkbenchWindow window, String label) { 00052 super(window,label); 00053 00054 // The id is used to refer to the action in a menu or toolbar 00055 setId(ICommandIds.CMD_OPEN); 00056 00057 // Associate the action with a pre-defined command, to allow key bindings. 00058 setActionDefinitionId(ICommandIds.CMD_OPEN); 00059 setImageDescriptor(m2m.frontend.Activator.getImageDescriptor("/icons/open.gif")); 00060 } 00061 00065 public void run() { 00066 //open file dialog 00067 FileDialog dialog = new FileDialog(this.window.getShell(), SWT.OPEN); 00068 dialog.setText("Open File"); 00069 String[] filterExt = { "*.m2m"}; 00070 String[] filterNames = {"Math2Mat project (*.m2m)"}; 00071 dialog.setFilterExtensions(filterExt); 00072 dialog.setFilterNames(filterNames); 00073 String path= dialog.open(); 00074 00075 /* Check il a path has been selected */ 00076 if(path == null) 00077 return; 00078 if (!openFile(path)) 00079 MessageDialog.openError(window.getShell(), "File error", "File project is corrupted\nThe source file path does not refer to a file"); 00080 00081 } 00082 00083 static public boolean openFile(String path) { 00084 00085 if (path==null) 00086 return false; 00087 00088 File file = new File(path); 00089 00090 if (!createProject(file)) 00091 return false; 00092 00093 GUIProperties GUIProps = GUIProperties.getReference(); 00094 00095 /* Set the external tools properties */ 00096 String[] rof = new String[5]; 00097 rof = GUIProps.getReOpenFile(); 00098 int k = 4; 00099 if(rof != null) 00100 { 00101 for(int i = 0; i < rof.length; i++) 00102 if(rof[i].equalsIgnoreCase(path)) 00103 { 00104 k = i; 00105 break; 00106 } 00107 00108 for(int i = 0; i < rof.length ; i++) 00109 GUIProps.setReOpenFileAtIndex(rof[i].equalsIgnoreCase("null") ? "No file" : rof[i], i); 00110 00111 for(int i = k; i > 0; i--) 00112 GUIProps.setReOpenFileAtIndex(rof[i-1], i); 00113 } 00114 00115 GUIProps.setReOpenFileAtIndex(path, 0); 00116 00117 /* Write the M2MProperties.prop file */ 00118 GUIProps.writeSettings(); 00119 00120 /* Update reopen Action */ 00121 ReOpenFile[] rofActionTab = ApplicationActionBarAdvisor.getInstance().getReOpenFile(); 00122 for(int i = 0; i < rofActionTab.length; i++) 00123 rofActionTab[i].setPath(GUIProps.getReOpenFileAtIndex(i) == null ? "No File" : GUIProps.getReOpenFileAtIndex(i)); 00124 ApplicationActionBarAdvisor.getInstance().repopulateFileMenu(); 00125 00126 return true; 00127 } 00128 00129 00130 static public boolean createProject(File file) { 00131 M2MProject m2mProject = new M2MProject(); 00132 m2mProject.openProject(file.getAbsolutePath()); 00133 00134 boolean reload=false; 00135 if (!m2mProject.sourceFileExist()) { 00136 m2mProject.checkFolders(); 00137 m2mProject.restoreOctave(); 00138 } 00139 else if (!m2mProject.checkSourceFile()) { 00140 00141 if (MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Warning", "The Octave seems to have been modified outside of Math2mat. Reload?")) { 00142 reload=true; 00143 } 00144 else { 00145 m2mProject.restoreOctave(); 00146 } 00147 } 00148 00149 if (m2mProject.getSourceFile() != null) { 00150 00151 //open a new editor with the file's content 00152 IWorkbenchPage page= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 00153 try { 00154 IFileStore fileStore= EFS.getStore(file.toURI()); 00155 00156 //test if the project is already opened 00157 00158 IEditorReference myReferences[]; 00159 //get all opened editors with ID = Editor.ID 00160 myReferences = page.findEditors(new FileStoreEditorInput(fileStore), Editor.ID, org.eclipse.ui.IWorkbenchPage.MATCH_ID); 00161 for (int i = 0; i < myReferences.length; i++) { 00162 Editor editor = (Editor)myReferences[i].getEditor(true); 00163 if (editor.getM2MProject().compare(m2mProject)) 00164 { 00165 page.activate(editor); 00166 return true; 00167 } 00168 } 00169 00170 page.openEditor(new M2MFileStoreEditorInput(fileStore,m2mProject), Editor.ID, true); 00171 NavigationView.addList(file.getAbsolutePath()); 00172 00173 /* Open the console */ 00174 try { 00175 page.showView(M2MConsole.ID); 00176 } catch (PartInitException e1) { 00177 e1.printStackTrace(); 00178 } 00179 00180 Editor editor = ((Editor)page.getActiveEditor()); 00181 00182 if (reload) { 00183 /* Initialisation of the structure */ 00184 editor.getM2MProject().clearStructTreatment(); 00185 editor.setParseDone(true); 00186 00187 /* Parsing of the text of the editor */ 00188 try { 00189 editor.getM2MProject().getStructTreatment().parse(editor.getStyledText().getText()); 00190 } 00191 catch (ParsingException e1) { 00192 editor.setParseDone(false); 00193 } 00194 } 00195 else 00196 editor.setParseDone(true); 00197 00198 } catch (CoreException e) { 00199 e.printStackTrace(); 00200 return false; 00201 } 00202 } else { 00203 return false; 00204 } 00205 return true; 00206 } 00207 }