blob: f0122fbc213cc3f076cb2cd1da1adc329b924dd0 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009 IBM Corporation 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
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.css.ui.internal.handlers;
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.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.wst.css.ui.internal.Logger;
public class AbstractCommentHandler extends AbstractHandler {
static final String OPEN_COMMENT = "/*"; //$NON-NLS-1$
static final String CLOSE_COMMENT = "*/"; //$NON-NLS-1$
public Object execute(ExecutionEvent event) throws ExecutionException {
ITextEditor textEditor = getTextEditor(event);
if (textEditor != null) {
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
if (document != null) {
// get current text selection
ITextSelection textSelection = getCurrentSelection(textEditor);
if (textSelection.isEmpty()) {
return null;
}
processAction(textEditor, document, textSelection);
}
}
return null;
}
protected void processAction(ITextEditor editor, IDocument document, ITextSelection textSelection) {
}
protected ITextEditor getTextEditor(ExecutionEvent event) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
ITextEditor textEditor = null;
if (editor instanceof ITextEditor)
textEditor = (ITextEditor) editor;
else {
Object o = editor.getAdapter(ITextEditor.class);
if (o != null)
textEditor = (ITextEditor) o;
}
return textEditor;
}
protected ITextSelection getCurrentSelection(ITextEditor textEditor) {
ISelectionProvider provider = textEditor.getSelectionProvider();
if (provider != null) {
ISelection selection = provider.getSelection();
if (selection instanceof ITextSelection) {
return (ITextSelection) selection;
}
}
return TextSelection.emptySelection();
}
protected void removeOpenCloseComments(IDocument document, int offset, int length) {
try {
int adjusted_length = length;
// remove open comments
String string = document.get(offset, length);
int index = string.lastIndexOf(OPEN_COMMENT);
while (index != -1) {
document.replace(offset + index, OPEN_COMMENT.length(), ""); //$NON-NLS-1$
index = string.lastIndexOf(OPEN_COMMENT, index - 1);
adjusted_length -= OPEN_COMMENT.length();
}
// remove close comments
string = document.get(offset, adjusted_length);
index = string.lastIndexOf(CLOSE_COMMENT);
while (index != -1) {
document.replace(offset + index, CLOSE_COMMENT.length(), ""); //$NON-NLS-1$
index = string.lastIndexOf(CLOSE_COMMENT, index - 1);
}
}
catch (BadLocationException e) {
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
}
}
}