TemplateEditor te = getTemplateEditor();
WodEditor we = getWodEditor();
InsertComponentSpecification ics = getComponentSpecification();
System.out.println("InsertHtmlAndWodAction.insert: " + te + ", " + we + ", " + ics);
if (te != null && we != null && ics != null) {
SimpleWodElement wodElement = new SimpleWodElement(ics.getComponentInstanceName(), ics.getComponentName());
List<Binding> bindings = ics.getRequiredBindings();
if (bindings != null) {
for (Binding binding : bindings) {
wodElement.addBinding(new SimpleWodBinding(null, binding.getName(), "", true));
}
}
// If the component name is blank, then this is an HTML tag
if (ics.getComponentName() == null || ics.getComponentName().length() == 0) {
wodElement.setTagName(ics.getTagName());
}
SimpleWodElement htmlElement;
if (ics.isInline()) {
htmlElement = wodElement;
} else {
htmlElement = new SimpleWodElement("", "");
htmlElement.setTagName(ics.getTagName());
}
Map<String, String> htmlAttributes = ics.getHtmlAttributes();
if (htmlAttributes != null) {
for (Map.Entry<String, String> htmlAttribute : htmlAttributes.entrySet()) {
htmlElement.addBinding(new SimpleWodBinding(null, htmlAttribute.getKey(), htmlAttribute.getValue(), true));
}
}
IDocument teDoc = te.getHtmlEditDocument();
IDocument weDoc = we.getWodEditDocument();
ITextSelection teDocTSel = (ITextSelection) te.getSourceEditor().getSelectionProvider().getSelection();
// insert the WebObjects component into the template portion.
try {
ITextViewerExtension teExt = (ITextViewerExtension) te.getSourceEditor().getViewer();
teExt.getRewriteTarget().beginCompoundChange();
try {
int selectionStartOffset = teDocTSel.getOffset();
int selectionEndOffset = teDocTSel.getOffset() + teDocTSel.getLength();
if (ics.isComponentContent()) {
int selectionStartLine = teDocTSel.getStartLine();
int selectionEndLine = teDocTSel.getEndLine();
StringWriter startTagWriter = new StringWriter();
htmlElement.writeInlineFormat(startTagWriter, "", true, true, false, false, "$", "");
String startTag = startTagWriter.toString();
StringWriter endTagWriter = new StringWriter();
htmlElement.writeInlineFormat(endTagWriter, "", true, false, false, true, "$", "");
String endTag = endTagWriter.toString();
String indentText = getIndentText(teDoc, selectionStartOffset);
IRegion startLineRegion = teDoc.getLineInformationOfOffset(selectionStartOffset);
IRegion endLineRegion = teDoc.getLineInformationOfOffset(selectionEndOffset);
// MS: If the selection starts within the indent
// area, then you're actually selecting
// from the beginning of the line, not splitting an
// existing line of HTML.
int selectionLineStartOffset = (selectionStartOffset - startLineRegion.getOffset());
boolean selectionStartedInIndent = (indentText.length() >= selectionLineStartOffset);
if (selectionStartLine == selectionEndLine) {
if (selectionEndOffset == endLineRegion.getOffset() && selectionEndOffset > 0) {
teDoc.replace(selectionEndOffset - 1, 0, endTag);
} else {
teDoc.replace(selectionEndOffset, 0, endTag);
}
if (selectionStartedInIndent) {
if (indentText.length() == 0) {
int offset = Math.max(startLineRegion.getOffset() - 1, 0);
teDoc.replace(offset, 0, startTag);
} else {
teDoc.replace(startLineRegion.getOffset() + indentText.length(), 0, startTag);
}
} else {
teDoc.replace(selectionStartOffset, 0, startTag);
}
} else {
int indentEndOffset;
String lastLineIndentText = getIndentText(teDoc, selectionEndOffset);
int selectionLineEndOffset = (selectionEndOffset - endLineRegion.getOffset());
if (lastLineIndentText.length() >= selectionLineEndOffset) {
String endText = indentText + endTag + "\n";
teDoc.replace(endLineRegion.getOffset(), 0, endText);
indentEndOffset = 1;
} else {
String endText = "\n" + indentText + endTag + "\n" + indentText;
teDoc.replace(selectionEndOffset, 0, endText);
indentEndOffset = 2;
}
int indentStartOffset;
if (selectionStartedInIndent) {
indentStartOffset = 1;
String startText = startTag + "\n" + indentText;
teDoc.replace(startLineRegion.getOffset() + indentText.length(), 0, startText);
} else {
indentStartOffset = 2;
String startText = "\n" + indentText + startTag + "\n" + indentText;
teDoc.replace(selectionStartOffset, 0, startText);
}
for (int line = selectionStartLine + indentStartOffset; line <= selectionEndLine + indentEndOffset; line++) {
int lineOffset = teDoc.getLineOffset(line);
teDoc.replace(lineOffset, 0, "\t");
}
}
} else {
StringWriter startTagWriter = new StringWriter();
htmlElement.writeInlineFormat(startTagWriter, null, true, true, false, true, "$", "");
String tag = startTagWriter.toString();
teDoc.replace(selectionStartOffset, 0, tag);
}
} finally {
teExt.getRewriteTarget().endCompoundChange();