package com.onpositive.ui.dummy.editors;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.registry.EditorDescriptor;
import org.eclipse.ui.texteditor.AbstractTextEditor;
public class ContentTypeEditorProvider
{
/**
* Returns an inner editor for some content.
* In the filename, we really need only extension - it's fake
* @param fakeFileName Fake file name, which must have content type corresponding extension, e.g.
* "a.java" for java contents
* @param contents Real content to create an editor for
* @param parent Parent composite for an editor
* @return Editor composite
*/
public static AbstractTextEditor getEditorForContentTypeWithFakeFileName(IEditorInput contents, Composite parent)
{
String fakeFileName=contents.getName();
if (PlatformUI.getWorkbench() == null)
throw new RuntimeException("No workbench found! You must create a workbench for using ContentTypeEditorProvider!");
IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry();
try
{
IEditorDescriptor defaultEditor = editorRegistry.getDefaultEditor(contents.getName());
if (defaultEditor == null){
defaultEditor = editorRegistry.getDefaultEditor("a.txt");
}
IEditorPart newEditor =null;
try{
newEditor=((EditorDescriptor)defaultEditor).createEditor();
}catch (Exception e) {
e.printStackTrace();
newEditor=((EditorDescriptor)editorRegistry.getDefaultEditor("a.txt")).createEditor();
}
if (defaultEditor instanceof EditorDescriptor)
{
newEditor.init(new DummyEditorSite(newEditor, "editor", parent), contents);
Composite con=new Composite(parent,SWT.BORDER);
con.setLayout(new FillLayout());
newEditor.createPartControl(con);
return (AbstractTextEditor) newEditor;
}
return (AbstractTextEditor) newEditor;
} catch (CoreException e)
{
e.printStackTrace();
}
return null;
}
/**
* Returns an inner editor for some content.
* In the filename, we really need only extension - it's fake
* @param fakeFileName Fake file name, which must have content type corresponding extension, e.g.
* "a.java" for java contents
* @param contents Real content to create an editor for
* @param parent Parent composite for an editor
* @return Editor composite
*/
public static Composite getEditorForContentTypeWithFakeFileName(String fakeFileName, String contents, Composite parent)
{
if (PlatformUI.getWorkbench() == null)
throw new RuntimeException("No workbench found! You must create a workbench for using ContentTypeEditorProvider!");
IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry();
try
{
int lastIdx = fakeFileName.lastIndexOf(".");
if (lastIdx == -1) return null;
String substring = fakeFileName.substring(0,lastIdx);
StringBuilder sb = new StringBuilder();
for (int i = substring.length() - 1; i >= 0; i--)
{
if (Character.isJavaIdentifierPart(substring.charAt(i)))
sb.append(substring.charAt(i));
else
break;
}
sb.reverse();
if (sb.length() < 3) substring = "aaa";
else
substring = sb.toString();
File tmpFile = File.createTempFile(substring, fakeFileName.substring(lastIdx, fakeFileName.length()));
tmpFile.deleteOnExit();
IEditorDescriptor defaultEditor = editorRegistry.getDefaultEditor(tmpFile.getAbsolutePath());
if (defaultEditor == null){
defaultEditor = editorRegistry.getDefaultEditor("a.txt");
}
IEditorPart newEditor =null;
try{
newEditor=((EditorDescriptor)defaultEditor).createEditor();
}catch (Exception e) {
e.printStackTrace();
newEditor=((EditorDescriptor)editorRegistry.getDefaultEditor("a.txt")).createEditor();
}
if (defaultEditor instanceof EditorDescriptor)
{
newEditor.init(new DummyEditorSite(newEditor, "editor", parent), new StringEditorInput(contents));
Composite con=new Composite(parent,SWT.BORDER);
con.setLayout(new FillLayout());
newEditor.createPartControl(con);
return con;
}
} catch (CoreException e)
{
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Composite getEditorForContentType(String fileName, Composite parent)
{
try
{
File inputFile = new File(fileName);
byte[] arr = new byte[(int) inputFile.length()];
FileInputStream stream = new FileInputStream(inputFile);
stream.read(arr);
return getEditorForContentTypeWithFakeFileName(fileName, new String(arr), parent);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}