/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.actions;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.models.Snippet;
public class SurroundWithSnippetAction extends Action {
private Snippet _snippet;
public SurroundWithSnippetAction(Snippet snippet) {
setText(snippet.getName());
if (snippet.getDescription() != null) {
setToolTipText(snippet.getDescription());
}
_snippet = snippet;
}
@Override
public void run() {
IEditorPart editorPart = Plugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart != null && editorPart instanceof ITextEditor) {
ITextEditor editor = (ITextEditor) editorPart;
ISelection selection = editor.getEditorSite().getSelectionProvider().getSelection();
if (selection != null && selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection)selection;
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
if (doc != null) {
try {
_snippet.apply(doc, textSelection, editor);
} catch (BadLocationException e) {
}
}
}
}
}
}