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