]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.editors.win32/src/org/simantics/editors/win32/Editors.java b/bundles/org.simantics.editors.win32/src/org/simantics/editors/win32/Editors.java
new file mode 100644 (file)
index 0000000..cb1557b
--- /dev/null
@@ -0,0 +1,134 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.editors.win32;\r
+\r
+import java.io.File;\r
+import java.net.URL;\r
+\r
+import org.eclipse.core.resources.IFile;\r
+import org.eclipse.core.resources.IWorkspace;\r
+import org.eclipse.core.resources.ResourcesPlugin;\r
+import org.eclipse.ui.IEditorDescriptor;\r
+import org.eclipse.ui.IEditorPart;\r
+import org.eclipse.ui.IEditorRegistry;\r
+import org.eclipse.ui.IMemento;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.part.FileEditorInput;\r
+import org.simantics.editors.BrowserInput;\r
+import org.simantics.editors.internal.SystemFile;\r
+import org.simantics.editors.win32.ole.EditorDefinition;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+\r
+\r
+public class Editors {\r
+\r
+       @Deprecated\r
+       /**\r
+        * Opens extenal application for a file.\r
+        * \r
+        * Deprecated: Since the functionality is not Windows specific feature, this method has been moved to org.simantics.editors.Editors class.\r
+        * @param file\r
+        * @return\r
+        * @throws PartInitException\r
+        */\r
+       public static IEditorPart openExternalEditor(File file) throws PartInitException {\r
+               IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);\r
+               IWorkspace ws = (IWorkspace) ResourcesPlugin.getWorkspace();\r
+               FileEditorInput input = new ExternalFileEditorInput(new SystemFile(file, ws));\r
+               IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
+               IWorkbenchPage page = window.getActivePage();\r
+               return page.openEditor(input, desc.getId());\r
+               \r
+       }\r
+       \r
+       private static class ExternalFileEditorInput extends FileEditorInput {\r
+\r
+               public ExternalFileEditorInput(IFile file) {\r
+                       super(file);\r
+               }\r
+               \r
+               @Override\r
+               public void saveState(IMemento memento) {\r
+                       \r
+               }\r
+               \r
+       }\r
+       \r
+       public static IEditorPart openEmbeddedEditor(File file) {\r
+               return openEmbeddedEditor(file,false);\r
+       }\r
+       \r
+       public static boolean isSupported(String filename) {\r
+               String suffix = "";\r
+               int index = filename.lastIndexOf(".");\r
+               if (index != -1)\r
+                       suffix = filename.substring(index+1).toLowerCase();\r
+               else\r
+                       suffix = filename.toLowerCase();\r
+               EditorDefinition factory = EditorDefinitionManager.getInstance().getFactoryForSuffix(suffix);\r
+               return factory != null;\r
+       }\r
+       \r
+       public static IEditorPart openEmbeddedEditor(File file, boolean fullscreen) {\r
+               return openEmbeddedEditor(file, file.getName(), fullscreen);\r
+       }\r
+       public static IEditorPart openEmbeddedEditor(File file, String title, boolean fullscreen) {\r
+               String filename = file.getAbsoluteFile().toString();\r
+               String suffix = "";\r
+               int index = filename.lastIndexOf(".");\r
+               if (index != -1)\r
+                       suffix = filename.substring(index+1).toLowerCase();\r
+               else\r
+                       suffix = filename.toLowerCase();\r
+               \r
+               \r
+               \r
+               try {\r
+                       EditorDefinition factory = EditorDefinitionManager.getInstance().getFactoryForSuffix(suffix);\r
+                       if (factory == null) {\r
+                               throw new RuntimeException("Cannot find editorDefinition for " + suffix);\r
+                       }\r
+                       \r
+\r
+                       if (!factory.useBrowser()) {\r
+                               OLEEditorInput input = new OLEEditorInput(factory, file, title, fullscreen);\r
+                               return WorkbenchUtils.openEditor("org.simantics.editors.win32.oleeditor", input);\r
+                       }\r
+                       URL url = file.toURI().toURL();\r
+                       BrowserInput input = new BrowserInput(url,false, fullscreen);\r
+                       input.setName(title);\r
+                       return WorkbenchUtils.openEditor("org.simantics.editors.browser", input);\r
+               \r
+               } catch (Exception e) {\r
+                       ErrorLogger.defaultLogError("Cannot use internal editor, trying external editor", e);\r
+               }\r
+\r
+               //ErrorLogger.defaultLogError("Cannot embed ActiveX contol, using external application.", null);\r
+               try {\r
+                       // show the file contents in external editor / application\r
+                       return org.simantics.editors.Editors.openExternalEditor(file);\r
+               } catch (Exception err) {\r
+                       ErrorLogger.defaultLogError(err);\r
+               }\r
+               return null;\r
+       }\r
+       \r
+//     public static void openEmbeddedEditor(URL url) {\r
+//             \r
+//             \r
+//     }\r
+}\r