1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.editors.win32;
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;
37 public class Editors {
41 * Opens extenal application for a file.
43 * Deprecated: Since the functionality is not Windows specific feature, this method has been moved to org.simantics.editors.Editors class.
46 * @throws PartInitException
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());
58 private static class ExternalFileEditorInput extends FileEditorInput {
60 public ExternalFileEditorInput(IFile file) {
65 public void saveState(IMemento memento) {
71 public static IEditorPart openEmbeddedEditor(File file) {
72 return openEmbeddedEditor(file,false);
75 public static boolean isSupported(String filename) {
77 int index = filename.lastIndexOf(".");
79 suffix = filename.substring(index+1).toLowerCase();
81 suffix = filename.toLowerCase();
82 EditorDefinition factory = EditorDefinitionManager.getInstance().getFactoryForSuffix(suffix);
83 return factory != null;
86 public static IEditorPart openEmbeddedEditor(File file, boolean fullscreen) {
87 return openEmbeddedEditor(file, file.getName(), fullscreen);
89 public static IEditorPart openEmbeddedEditor(File file, String title, boolean fullscreen) {
90 String filename = file.getAbsoluteFile().toString();
92 int index = filename.lastIndexOf(".");
94 suffix = filename.substring(index+1).toLowerCase();
96 suffix = filename.toLowerCase();
101 EditorDefinition factory = EditorDefinitionManager.getInstance().getFactoryForSuffix(suffix);
102 if (factory == null) {
103 throw new RuntimeException("Cannot find editorDefinition for " + suffix);
107 if (!factory.useBrowser()) {
108 OLEEditorInput input = new OLEEditorInput(factory, file, title, fullscreen);
109 return WorkbenchUtils.openEditor("org.simantics.editors.win32.oleeditor", input);
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);
116 } catch (Exception e) {
117 ErrorLogger.defaultLogError("Cannot use internal editor, trying external editor", e);
120 //ErrorLogger.defaultLogError("Cannot embed ActiveX contol, using external application.", null);
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);
130 // public static void openEmbeddedEditor(URL url) {