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