KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > io > IOUtil


1 /**
2  * <p> Project: com.nightlabs.base </p>
3  * <p> Copyright: Copyright (c) 2005 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 20.06.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.rcp.io;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
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 JavaDoc getFullFileName(String JavaDoc pathName, String JavaDoc fileName)
32   {
33     if (pathName == null)
34             throw new IllegalArgumentException JavaDoc("Param pathName must not be null!");
35     
36     if (fileName == null)
37             throw new IllegalArgumentException JavaDoc("Param fileName must not be null!");
38     
39     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
40     String JavaDoc pathSeparator = File.separator;
41     sb.append(pathName);
42     sb.append(pathSeparator);
43     sb.append(fileName);
44     return sb.toString();
45   }
46   
47   /**
48    * Opens an Editor (IEditorPart) for the given file, based on the FileExtension of the Editor
49    * and the EditorRegistry of the Workbench.
50    * @param file The file to open
51    * @param saved determines if the created FileEditorInput should be marked as saved or not
52    * return true if an editor could be found for this file and the editors opened
53    * return false if no editor could be found for this file
54    */

55   public static boolean openFile(File JavaDoc file, boolean saved)
56   throws PartInitException
57   {
58     if (file == null)
59             throw new IllegalArgumentException JavaDoc("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 JavaDoc editorID = editorDescriptor.getId();
66         String JavaDoc 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 JavaDoc file)
89   throws PartInitException
90   {
91     return openFile(file, true);
92   }
93     
94   public static IPath getPath(File JavaDoc file)
95   {
96     String JavaDoc path = null;
97     try {
98             path = file.getCanonicalPath();
99         } catch (IOException JavaDoc e) {
100             path = file.getAbsolutePath();
101         }
102         return new Path(path);
103   }
104 }
105
Popular Tags