1 /******************************************************************************* 2 * Copyright (c) 2010 - 2013 by Timotei Dolean <timotei21@gmail.com> 3 * 4 * This program and the accompanying materials are made available 5 * 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 8 *******************************************************************************/ 9 package org.wesnoth.utils; 10 11 import org.eclipse.core.filesystem.EFS; 12 import org.eclipse.core.filesystem.IFileStore; 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.runtime.Path; 15 import org.eclipse.jface.text.BadLocationException; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.ITextSelection; 18 import org.eclipse.ui.IEditorPart; 19 import org.eclipse.ui.IWorkbenchPage; 20 import org.eclipse.ui.PartInitException; 21 import org.eclipse.ui.ide.IDE; 22 import org.eclipse.ui.texteditor.AbstractTextEditor; 23 import org.eclipse.ui.texteditor.IDocumentProvider; 24 import org.eclipse.ui.texteditor.ITextEditor; 25 26 import org.wesnoth.Logger; 27 28 29 /** 30 * An utils class that handles Eclipse's editor 31 */ 32 public class EditorUtils 33 { 34 /** 35 * Writes the specified content in current opened editor 36 * 37 * @param content 38 * the string content to write 39 */ writeInEditor( String content )40 public static void writeInEditor( String content ) 41 { 42 writeInEditor( getEditedFile( ), content ); 43 } 44 45 /** 46 * Writes the specified content in the specified editor 47 * 48 * @param targetEditor 49 * The editor part to write the content in. 50 * 51 * @param content 52 * the string content to write 53 */ writeInEditor( IEditorPart targetEditor, String content )54 public static void writeInEditor( IEditorPart targetEditor, String content ) 55 { 56 int offset = ( ( ITextSelection ) getTextEditor( targetEditor ) 57 .getSelectionProvider( ).getSelection( ) ).getOffset( ); 58 try { 59 getEditorDocument( targetEditor ).replace( offset, 0, content ); 60 } catch( BadLocationException e ) { 61 } 62 } 63 64 /** 65 * Replaces the text in current opened editor with the specified one 66 * 67 * @param content 68 * the string to replace the current content 69 */ replaceEditorText( String content )70 public static void replaceEditorText( String content ) 71 { 72 replaceEditorText( getEditedFile( ), content ); 73 } 74 75 /** 76 * Replaces the text in the specified editor with the specified one 77 * 78 * @param targetEditor 79 * The editor part to replace the text for. 80 * 81 * @param content 82 * the string to replace the current content 83 */ replaceEditorText( IEditorPart targetEditor, String content )84 public static void replaceEditorText( IEditorPart targetEditor, 85 String content ) 86 { 87 if( targetEditor == null ) { 88 return; 89 } 90 try { 91 getEditorDocument( targetEditor ).replace( 0, 92 getEditorDocument( targetEditor ).getLength( ), content ); 93 } catch( BadLocationException e ) { 94 } 95 } 96 97 /** 98 * Gets the current opened editor's document 99 * 100 * @return An {@link IDocument} instance. 101 */ getEditorDocument( )102 public static IDocument getEditorDocument( ) 103 { 104 return getEditorDocument( getEditedFile( ) ); 105 } 106 107 /** 108 * Gets the specified editor's document 109 * 110 * @param targetEditor 111 * The editor part to get the document for. 112 * 113 * @return An {@link IDocument} instance. 114 */ getEditorDocument( IEditorPart targetEditor )115 public static IDocument getEditorDocument( IEditorPart targetEditor ) 116 { 117 if( targetEditor == null ) { 118 return null; 119 } 120 121 IDocumentProvider dp = getTextEditor( targetEditor ) 122 .getDocumentProvider( ); 123 return dp.getDocument( targetEditor.getEditorInput( ) ); 124 } 125 126 /** 127 * Gets the text editor of the current opened editor 128 * 129 * @return An {@link ITextEditor} instance. 130 */ getTextEditor( )131 public static ITextEditor getTextEditor( ) 132 { 133 return getTextEditor( getEditedFile( ) ); 134 } 135 136 /** 137 * Gets the text editor of the specified editor 138 * 139 * @param targetEditor 140 * The editor part to get the text editor for. 141 * 142 * @return An {@link ITextEditor} instance 143 */ getTextEditor( IEditorPart targetEditor )144 public static ITextEditor getTextEditor( IEditorPart targetEditor ) 145 { 146 if( targetEditor == null ) { 147 return null; 148 } 149 150 IEditorPart part = targetEditor; 151 if( ! ( part instanceof AbstractTextEditor ) ) { 152 return null; 153 } 154 return ( ITextEditor ) part; 155 } 156 157 /** 158 * Gets the editor part of the current edited file 159 * 160 * @return An {@link IEditorPart} instance. 161 */ getEditedFile( )162 public static IEditorPart getEditedFile( ) 163 { 164 return WorkspaceUtils.getWorkbenchWindow( ).getPages( )[0] 165 .getActiveEditor( ); 166 } 167 168 /** 169 * Opens the editor on the specified file 170 * 171 * @param file 172 * The file to open 173 * @param activatePage 174 * True to activate the opened file 175 * @return An {@link IEditorPart} instance 176 */ openEditor( IFile file, boolean activatePage )177 public static IEditorPart openEditor( IFile file, boolean activatePage ) 178 { 179 IWorkbenchPage page = WorkspaceUtils.getWorkbenchWindow( ) 180 .getActivePage( ); 181 try { 182 return IDE.openEditor( page, file, activatePage ); 183 } catch( PartInitException e ) { 184 Logger.getInstance( ).logException( e ); 185 return null; 186 } 187 } 188 189 /** 190 * Opens the editor on the specified file (will use IFileStore) 191 * 192 * @param file 193 * The file to open 194 * @return An {@link IEditorPart} instance. 195 */ openEditor( String file )196 public static IEditorPart openEditor( String file ) 197 { 198 return openEditor( EFS.getLocalFileSystem( ) 199 .getStore( new Path( file ) ) ); 200 } 201 202 /** 203 * Opens the editor on the specified file (will use IFileStore) 204 * 205 * @param file 206 * The file to open 207 * @return An {@link IEditorPart} instance. 208 */ openEditor( IFileStore file )209 public static IEditorPart openEditor( IFileStore file ) 210 { 211 try { 212 return IDE.openEditorOnFileStore( WorkspaceUtils 213 .getWorkbenchWindow( ).getActivePage( ), file ); 214 } catch( Exception e ) { 215 Logger.getInstance( ).logException( e ); 216 return null; 217 } 218 } 219 220 } 221