/*******************************************************************************
* 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.models;
import java.util.Comparator;
import java.util.StringTokenizer;
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.ui.texteditor.ITextEditor;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
import de.innovationgate.utils.WGUtils;
public class Snippet {
public static final String PLACEHOLDER_CURSOR = "$CURSOR$";
public static final String PLACEHOLDER_SELECTION = "$SELECTION$";
public static final String CONTEXT_TML = "TML";
public static final String CONTEXT_TMLScript = "TMLScript";
private String _name;
private String _description;
private String _context;
private boolean _enabled = true;
private String _code = "";
public static final class SnippetComparator implements Comparator<Snippet> {
public int compare(Snippet o1, Snippet o2) {
String name1 = o1.getName();
if (name1 == null) {
name1 = "";
}
String name2 = o2.getName();
if (name2 == null) {
name2 = "";
}
return name1.compareTo(name2);
}
}
public void apply(IDocument doc, ITextSelection textSelection, ITextEditor editor) throws BadLocationException {
String lineDelimiter = doc.getLegalLineDelimiters()[0];
String indent = TMLRegion.computeIndent("", textSelection.getOffset(), doc);
StringBuffer replacement = new StringBuffer();
String prefix = computeReplacementPrefix();
if (prefix != null) {
appendResult(replacement, prefix, indent, "");
}
appendResult(replacement, textSelection.getText(), computeIndentOfSelectionPlaceholder(), "");
String suffix = computeReplacementSuffix();
if (suffix != null) {
appendResult(replacement, suffix, indent, "");
}
String finalReplacement = replacement.toString();
int cursorOffset = finalReplacement.indexOf(PLACEHOLDER_CURSOR);
finalReplacement = WGUtils.strReplace(finalReplacement, PLACEHOLDER_CURSOR, "", false);
if (cursorOffset < 0) {
cursorOffset = textSelection.getOffset() + finalReplacement.length();
} else {
cursorOffset = textSelection.getOffset() + cursorOffset;
}
doc.replace(textSelection.getOffset(), textSelection.getLength(), finalReplacement);
editor.getEditorSite().getSelectionProvider().setSelection(new TextSelection(cursorOffset, 0));
}
private String computeReplacementPrefix() {
String prefix = getCode();
if (prefix != null) {
int selectionOffset = prefix.indexOf(PLACEHOLDER_SELECTION);
if (selectionOffset != -1) {
prefix = prefix.substring(0, selectionOffset);
} else {
prefix = getCode();
}
}
return prefix;
}
private String computeReplacementSuffix() {
String suffix = getCode();
if (suffix != null) {
int selectionOffset = suffix.indexOf(PLACEHOLDER_SELECTION);
if (selectionOffset != -1) {
suffix = suffix.substring(selectionOffset + PLACEHOLDER_SELECTION.length());
} else {
suffix = "";
}
}
return suffix;
}
private String normalizeDelimiter(String input) {
String result = input;
if (result != null) {
result = WGUtils.strReplace(result, "\r\n", "\n", true);
result = WGUtils.strReplace(result, "\r", "\n", true);
}
return result;
}
private void appendResult(StringBuffer buffer, String input, String indent, String indentFirstLine) {
input = normalizeDelimiter(input);
StringTokenizer tokenizer = new StringTokenizer(input, "\n", true);
boolean firstToken = true;
while (tokenizer.hasMoreTokens()) {
if (firstToken) {
buffer.append(indentFirstLine);
firstToken = false;
} else {
buffer.append(indent);
}
buffer.append(tokenizer.nextToken());
}
}
private String computeIndentOfSelectionPlaceholder() {
String indent = "";
if (getCode() != null) {
StringTokenizer tokenizer = new StringTokenizer(getCode(), "\n");
// find line with selection placeholder
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
int pos = token.indexOf(PLACEHOLDER_SELECTION);
if (pos != -1) {
String linePrefixOfSelection = token.substring(0, pos);
for (int i = 0; i < linePrefixOfSelection.length(); i++) {
char c = linePrefixOfSelection.charAt(i);
if (c == '\t') {
indent += "\t";
}
if (Character.isWhitespace(c) || Character.isIdentifierIgnorable(c)) {
continue;
} else {
break;
}
}
}
}
}
return indent;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public String getDescription() {
return _description;
}
public void setDescription(String description) {
_description = description;
}
public String getContext() {
if (_context == null) {
return CONTEXT_TML;
}
return _context;
}
public void setContext(String context) {
_context = context;
}
public boolean isEnabled() {
return _enabled;
}
public void setEnabled(boolean enabled) {
_enabled = enabled;
}
public String getCode() {
return _code;
}
public void setCode(String code) {
_code = code;
}
}