Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.editor.utils;
 
 import  org.eclipse.core.commands.ExecutionEvent;
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.jdt.core.IJarEntryResource;
 import  org.eclipse.jdt.internal.ui.javaeditor.JarEntryEditorInput;
 import  org.eclipse.jface.preference.PreferenceConverter;
 import  org.eclipse.jface.resource.DataFormatException;
 import  org.eclipse.jface.resource.JFaceResources;
 import  org.eclipse.jface.resource.StringConverter;
 import  org.eclipse.swt.graphics.Color;
 import  org.eclipse.swt.graphics.Font;
 import  org.eclipse.swt.graphics.FontData;
 import  org.eclipse.swt.graphics.RGB;
 import  org.eclipse.ui.IEditorInput;
 import  org.eclipse.ui.IEditorPart;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkbenchPage;
 import  org.eclipse.ui.IWorkbenchWindow;
 import  org.eclipse.ui.PlatformUI;
 import  org.eclipse.ui.handlers.HandlerUtil;
 import  org.eclipse.ui.part.FileEditorInput;

Author(s):
Dennis H´┐Żbner - Initial contribution and API
Peter Friese
 
 public class EditorUtils {
 
 	private static final Logger log = Logger.getLogger(EditorUtils.class);
 
 	public static Font fontFromFontData(FontData[] fontDataArray) {
 		if (fontDataArray != null && fontDataArray.length > 0) {
 			String fontData = PreferenceConverter.getStoredRepresentation(fontDataArray);
 			if (!JFaceResources.getFontRegistry().hasValueFor(fontData)) {
 				FontData[] fData = PreferenceConverter.basicGetFontData(fontData);
 				JFaceResources.getFontRegistry().put(fontDatafData);
 			}
 			Font font = JFaceResources.getFontRegistry().get(fontData);
 			return font;
 		}
 		return null;
 	}
 
 	public static Color colorFromString(String rgbString) {
 		if (rgbString != null && rgbString.trim().length() > 0) {
 			Color col = JFaceResources.getColorRegistry().get(rgbString);
 			try {
 				if (col == null) {
 					RGB rgb = StringConverter.asRGB(rgbString);
 					JFaceResources.getColorRegistry().put(rgbStringrgb);
 					col = JFaceResources.getColorRegistry().get(rgbString);
 				}
 			}
 			catch (DataFormatException e) {
 				.error("Corrupt color value: " + rgbStringe);
 			}
 			return col;
 		}
 		return null;
 	}
 
 	public static Color colorFromRGB(RGB rgb) {
 		if (rgb == null)
 			return null;
 		return colorFromString(StringConverter.asString(rgb));
 	}
 	
 	public static XtextEditor getActiveXtextEditor(ExecutionEvent event) {
 		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
 		if (activeEditor == null)
 			return null;
 		XtextEditor xtextEditor = (XtextEditoractiveEditor.getAdapter(XtextEditor.class);
 		return xtextEditor;
 	}
 
 	public static XtextEditor getActiveXtextEditor() {
 		IWorkbench workbench = PlatformUI.getWorkbench();
 		IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
 		if (workbenchWindow == null
 			return null;
 		IWorkbenchPage activePage = workbenchWindow.getActivePage();
 		if (activePage == null
 			return null;
 		IEditorPart activeEditor = activePage.getActiveEditor();
 		if (activeEditor == null)
 			return null;
		XtextEditor xtextEditor = (XtextEditoractiveEditor.getAdapter(XtextEditor.class);
		return xtextEditor;
	}
	public static XtextEditor getXtextEditor(IEditorPart openEditor) {
		if (openEditor != null) {
			return (XtextEditoropenEditor.getAdapter(XtextEditor.class);
		}
		return null;
	}

Since:
2.8
	public static IEditorInput createEditorInput(IStorage storage) {
		if (storage instanceof IFile)
			return new FileEditorInput((IFile) storage);
		try {
			if (storage instanceof IJarEntryResource)
				return new JarEntryEditorInput(storage);
catch (NoClassDefFoundError e) {
			// ignore. can happen if JDT is not available.
		}
		return new XtextReadonlyEditorInput(storage);
	}
New to GrepCode? Check out our FAQ X