1 8 package com.nightlabs.rcp.io; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 13 import org.eclipse.core.runtime.IPath; 14 import org.eclipse.core.runtime.Path; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.widgets.MessageBox; 17 import org.eclipse.ui.IEditorDescriptor; 18 import org.eclipse.ui.IEditorRegistry; 19 import org.eclipse.ui.IPerspectiveDescriptor; 20 import org.eclipse.ui.IPerspectiveRegistry; 21 import org.eclipse.ui.IWorkbench; 22 import org.eclipse.ui.PartInitException; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.ui.WorkbenchException; 25 26 import com.nightlabs.base.NLBasePlugin; 27 import com.nightlabs.rcp.action.Editor2PerspectiveRegistry; 28 29 public class IOUtil 30 { 31 public static String getFullFileName(String pathName, String fileName) 32 { 33 if (pathName == null) 34 throw new IllegalArgumentException ("Param pathName must not be null!"); 35 36 if (fileName == null) 37 throw new IllegalArgumentException ("Param fileName must not be null!"); 38 39 StringBuffer sb = new StringBuffer (); 40 String pathSeparator = File.separator; 41 sb.append(pathName); 42 sb.append(pathSeparator); 43 sb.append(fileName); 44 return sb.toString(); 45 } 46 47 55 public static boolean openFile(File file, boolean saved) 56 throws PartInitException 57 { 58 if (file == null) 59 throw new IllegalArgumentException ("Param file must not be null!"); 60 61 IPerspectiveRegistry perspectiveRegistry = PlatformUI.getWorkbench().getPerspectiveRegistry(); 62 IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry(); 63 IEditorDescriptor editorDescriptor = editorRegistry.getDefaultEditor(file.getName()); 64 if (editorDescriptor != null) { 65 String editorID = editorDescriptor.getId(); 66 String perspectiveID = Editor2PerspectiveRegistry.getSharedInstance().getPerspectiveID(editorID); 67 if (perspectiveID != null) { 68 IPerspectiveDescriptor perspectiveDescriptor = perspectiveRegistry.findPerspectiveWithId(perspectiveID); 69 if (perspectiveDescriptor != null) 70 { 71 try { 72 IWorkbench workbench = PlatformUI.getWorkbench(); 73 workbench.showPerspective(perspectiveID, 74 workbench.getActiveWorkbenchWindow()); 75 } catch (WorkbenchException e) { 76 throw new PartInitException("Perspective width ID "+perspectiveID+" could not be opend", e); 77 } 78 } 79 } 80 FileEditorInput input = new FileEditorInput(file); 81 input.setSaved(saved); 82 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, editorID); 83 return true; 84 } 85 return false; 86 } 87 88 public static boolean openFile(File file) 89 throws PartInitException 90 { 91 return openFile(file, true); 92 } 93 94 public static IPath getPath(File file) 95 { 96 String path = null; 97 try { 98 path = file.getCanonicalPath(); 99 } catch (IOException e) { 100 path = file.getAbsolutePath(); 101 } 102 return new Path(path); 103 } 104 } 105 | Popular Tags |