*/
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);