/*******************************************************************************
* 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.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.all.AbstractWGATextEditor;
import de.innovationgate.eclipse.editors.tml.TMLEditor;
import de.innovationgate.eclipse.editors.tml.TMLPartitionScanner;
import de.innovationgate.eclipse.editors.tmlscript.TMLScriptEditor;
import de.innovationgate.eclipse.editors.tmlscript.TMLScriptPartitionScanner;
public class ToggleComment extends AbstractHandler {
public static final String ID = "de.innovationgate.eclipse.ids.editors.commands.ToggleComment";
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editorPart = Plugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart != null && editorPart instanceof AbstractWGATextEditor) {
AbstractWGATextEditor editor = (AbstractWGATextEditor) 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 {
ITypedRegion region = doc.getPartition(textSelection.getOffset());
// comment handling for TMLScriptEditor
if (editorPart instanceof TMLScriptEditor) {
if (region.getType().equals(TMLScriptPartitionScanner.MCOMMENT)) {
removeTMLScriptComment(doc, region);
} else if(editorPart instanceof TMLScriptEditor){
createTMLScriptComment(doc, textSelection);
}
}
// comment handling for TMLEditor
if(editorPart instanceof TMLEditor){
// remove comments if we are already in a comment section
if(region.getType().equals(TMLScriptPartitionScanner.MCOMMENT)) {
removeTMLScriptComment(doc, region);
}
if(region.getType().equals(TMLPartitionScanner.TML_COMMENT)) {
removeTMLComment(doc, region);
}
// create comments depending on current partition
if(region.getType().equals(TMLScriptPartitionScanner.TMLSCRIPT)){
createTMLScriptComment(doc, textSelection);
} else {
createTMLComment(doc, textSelection);
}
}
} catch (BadLocationException e) {
}
}
}
}
return null;
}
private void createTMLScriptComment(IDocument doc, ITextSelection textSelection) throws BadLocationException {
if (textSelection.getLength() > 0) {
String replacement = "/*" + textSelection.getText() + "*/";
doc.replace(textSelection.getOffset(), textSelection.getLength(), replacement);
}
}
private void removeTMLScriptComment(IDocument doc, ITypedRegion region) throws BadLocationException {
String commentContent = doc.get(region.getOffset()+"/*".length(), region.getLength() - "/*".length() - "*/".length());
doc.replace(region.getOffset(), region.getLength(), commentContent);
}
private void createTMLComment(IDocument doc, ITextSelection textSelection) throws BadLocationException {
if (textSelection.getLength() > 0) {
String replacement = "<tml:comment>" + textSelection.getText() + "</tml:comment>";
doc.replace(textSelection.getOffset(), textSelection.getLength(), replacement);
}
}
private void removeTMLComment(IDocument doc, ITypedRegion region) throws BadLocationException {
String commentContent = doc.get(region.getOffset() + "<tml:comment>".length(), region.getLength() - "<tml:comment>".length() - "</tml:comment>".length());
doc.replace(region.getOffset(), region.getLength(), commentContent);
}
}