]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.editors.win32/src/org/simantics/editors/win32/Editors.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.editors.win32 / src / org / simantics / editors / win32 / Editors.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.editors.win32;\r
13 \r
14 import java.io.File;\r
15 import java.net.URL;\r
16 \r
17 import org.eclipse.core.resources.IFile;\r
18 import org.eclipse.core.resources.IWorkspace;\r
19 import org.eclipse.core.resources.ResourcesPlugin;\r
20 import org.eclipse.ui.IEditorDescriptor;\r
21 import org.eclipse.ui.IEditorPart;\r
22 import org.eclipse.ui.IEditorRegistry;\r
23 import org.eclipse.ui.IMemento;\r
24 import org.eclipse.ui.IWorkbenchPage;\r
25 import org.eclipse.ui.IWorkbenchWindow;\r
26 import org.eclipse.ui.PartInitException;\r
27 import org.eclipse.ui.PlatformUI;\r
28 import org.eclipse.ui.part.FileEditorInput;\r
29 import org.simantics.editors.BrowserInput;\r
30 import org.simantics.editors.internal.SystemFile;\r
31 import org.simantics.editors.win32.ole.EditorDefinition;\r
32 import org.simantics.utils.ui.ErrorLogger;\r
33 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
34 \r
35 \r
36 \r
37 public class Editors {\r
38 \r
39         @Deprecated\r
40         /**\r
41          * Opens extenal application for a file.\r
42          * \r
43          * Deprecated: Since the functionality is not Windows specific feature, this method has been moved to org.simantics.editors.Editors class.\r
44          * @param file\r
45          * @return\r
46          * @throws PartInitException\r
47          */\r
48         public static IEditorPart openExternalEditor(File file) throws PartInitException {\r
49                 IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);\r
50                 IWorkspace ws = (IWorkspace) ResourcesPlugin.getWorkspace();\r
51                 FileEditorInput input = new ExternalFileEditorInput(new SystemFile(file, ws));\r
52                 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
53                 IWorkbenchPage page = window.getActivePage();\r
54                 return page.openEditor(input, desc.getId());\r
55                 \r
56         }\r
57         \r
58         private static class ExternalFileEditorInput extends FileEditorInput {\r
59 \r
60                 public ExternalFileEditorInput(IFile file) {\r
61                         super(file);\r
62                 }\r
63                 \r
64                 @Override\r
65                 public void saveState(IMemento memento) {\r
66                         \r
67                 }\r
68                 \r
69         }\r
70         \r
71         public static IEditorPart openEmbeddedEditor(File file) {\r
72                 return openEmbeddedEditor(file,false);\r
73         }\r
74         \r
75         public static boolean isSupported(String filename) {\r
76                 String suffix = "";\r
77                 int index = filename.lastIndexOf(".");\r
78                 if (index != -1)\r
79                         suffix = filename.substring(index+1).toLowerCase();\r
80                 else\r
81                         suffix = filename.toLowerCase();\r
82                 EditorDefinition factory = EditorDefinitionManager.getInstance().getFactoryForSuffix(suffix);\r
83                 return factory != null;\r
84         }\r
85         \r
86         public static IEditorPart openEmbeddedEditor(File file, boolean fullscreen) {\r
87                 return openEmbeddedEditor(file, file.getName(), fullscreen);\r
88         }\r
89         public static IEditorPart openEmbeddedEditor(File file, String title, boolean fullscreen) {\r
90                 String filename = file.getAbsoluteFile().toString();\r
91                 String suffix = "";\r
92                 int index = filename.lastIndexOf(".");\r
93                 if (index != -1)\r
94                         suffix = filename.substring(index+1).toLowerCase();\r
95                 else\r
96                         suffix = filename.toLowerCase();\r
97                 \r
98                 \r
99                 \r
100                 try {\r
101                         EditorDefinition factory = EditorDefinitionManager.getInstance().getFactoryForSuffix(suffix);\r
102                         if (factory == null) {\r
103                                 throw new RuntimeException("Cannot find editorDefinition for " + suffix);\r
104                         }\r
105                         \r
106 \r
107                         if (!factory.useBrowser()) {\r
108                                 OLEEditorInput input = new OLEEditorInput(factory, file, title, fullscreen);\r
109                                 return WorkbenchUtils.openEditor("org.simantics.editors.win32.oleeditor", input);\r
110                         }\r
111                         URL url = file.toURI().toURL();\r
112                         BrowserInput input = new BrowserInput(url,false, fullscreen);\r
113                         input.setName(title);\r
114                         return WorkbenchUtils.openEditor("org.simantics.editors.browser", input);\r
115                 \r
116                 } catch (Exception e) {\r
117                         ErrorLogger.defaultLogError("Cannot use internal editor, trying external editor", e);\r
118                 }\r
119 \r
120                 //ErrorLogger.defaultLogError("Cannot embed ActiveX contol, using external application.", null);\r
121                 try {\r
122                         // show the file contents in external editor / application\r
123                         return org.simantics.editors.Editors.openExternalEditor(file);\r
124                 } catch (Exception err) {\r
125                         ErrorLogger.defaultLogError(err);\r
126                 }\r
127                 return null;\r
128         }\r
129         \r
130 //      public static void openEmbeddedEditor(URL url) {\r
131 //              \r
132 //              \r
133 //      }\r
134 }\r