Author: akazakov
Date: 2007-11-05 11:40:19 -0500 (Mon, 05 Nov 2007)
New Revision: 4710
Modified:
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteInsertHelper.java
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteTaglibInserter.java
Log:
http://jira.jboss.com/jira/browse/JBIDE-1263
Modified:
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteInsertHelper.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteInsertHelper.java 2007-11-05
16:35:43 UTC (rev 4709)
+++
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteInsertHelper.java 2007-11-05
16:40:19 UTC (rev 4710)
@@ -32,7 +32,6 @@
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.eclipse.ui.texteditor.ITextEditor;
-
import org.jboss.tools.common.model.ServiceDialog;
import org.jboss.tools.common.model.XModelObject;
import org.jboss.tools.common.model.options.PreferenceModelUtilities;
@@ -45,7 +44,7 @@
* @author Jeremy
*/
public class PaletteInsertHelper {
-
+
public static final String PROPOPERTY_TAG_NAME = "tag name";
public static final String PROPOPERTY_START_TEXT = TLDToPaletteHelper.START_TEXT;
public static final String PROPOPERTY_END_TEXT = TLDToPaletteHelper.END_TEXT;
@@ -79,12 +78,12 @@
ModelUIPlugin.getPluginLog().logError(x);
}
}
-
-
+
static boolean isEditable(ITextEditor editor) {
if(editor == null) return false;
return editor.isEditable();
}
+
static boolean isEditable(IEditorInput input) {
if(input instanceof IFileEditorInput) {
IFile f = ((IFileEditorInput)input).getFile();
@@ -102,19 +101,26 @@
String startText = p.getProperty(PROPOPERTY_START_TEXT);
String endText = p.getProperty(PROPOPERTY_END_TEXT);
String uri = p.getProperty(PROPOPERTY_TAGLIBRARY_URI);
-
+
ISelectionProvider selProvider =
(ISelectionProvider)p.get(PROPOPERTY_SELECTION_PROVIDER);
if(selProvider == null) p.put(PROPOPERTY_SELECTION_PROVIDER,
v.getSelectionProvider());
-
IDocument d = v.getDocument();
String[] texts = new String[]{startText, endText};
- p = PaletteTaglibInserter.inserTaglib(v, p);
+
+ if(startText!=null && startText.startsWith("<%@ taglib")) {
+ if(PaletteTaglibInserter.inserTaglibInXml(v, p)) {
+ return;
+ }
+ } else {
+ p = PaletteTaglibInserter.inserTaglib(v, p);
+ }
+
String defaultPrefix = p.getProperty(PROPOPERTY_DEFAULT_PREFIX);
applyPrefix(texts, d, tagname, uri, defaultPrefix);
startText = texts[0];
endText = texts[1];
-
+
if(startText != null) p.setProperty(PROPOPERTY_START_TEXT, startText);
if(endText != null) p.setProperty(PROPOPERTY_END_TEXT, endText);
@@ -154,11 +160,11 @@
ISelectionProvider selProvider =
(ISelectionProvider)p.get(PROPOPERTY_SELECTION_PROVIDER);
if (doc == null || selProvider == null) return;
-
+
ITextSelection selection = (ITextSelection)selProvider.getSelection();
int offset = selection.getOffset();
int length = selection.getLength();
-
+
//Changed due to new WTP version 1.5 R 2006.06.28 get selected text from document.
String body = null;
try {
@@ -173,7 +179,7 @@
else endText = prepare(prepare(endText, "\\n", getLineDelimiter(doc)),
"\\t", "\t");
String text = reformat ? format (doc, offset, length, body, startText, endText,
newline) : (startText + body + endText);
-
+
//Remove empty line before startText if text starts with creating new line
String lineDelimiter = getLineDelimiter(doc);
if(reformat && text.startsWith(lineDelimiter)) {
@@ -194,7 +200,7 @@
ModelUIPlugin.getPluginLog().logError(e);
}
}
-
+
int pos = text.indexOf("|");
if (pos >= 0) {
text = text.substring(0, pos) + text.substring(pos + 1);
@@ -211,7 +217,7 @@
ITextSelection sel = new TextSelection(offset + pos, 0);
selProvider.setSelection(sel);
}
-
+
private static String prepare (String text, String pattern, String replacer) {
String res = text;
int index;
@@ -220,7 +226,7 @@
}
return res;
}
-
+
private static String format(IDocument d, int offset, int length, String body, String
startText, String endText, String newline) {
String lineDelimiter = getLineDelimiter(d);
@@ -248,7 +254,7 @@
}
if (body == null || body.length() == 0) appendFirstDelimiter = false;
-
+
boolean appendLastDelimiter = true;
try {
int line = d.getLineOfOffset(offset + length);
@@ -256,7 +262,7 @@
int lineLength = d.getLineInformation(line).getLength();
lastLineIndent = getIndentOfLine(d.get(offset + length, lineOffset + lineLength -
offset - length), lineDelimiter);
-
+
if (lineOffset + lineLength - offset - length == 0)
appendLastDelimiter = false;
} catch (Exception ex) {
@@ -281,7 +287,7 @@
}
if (!"false".equals(newline))
buffer.append(lineDelimiter);
-
+
body = (body == null || body.length() == 0) ? "" : firstLineIndent +
body.substring(getIndentOfLine(body, lineDelimiter).length());
}
int deltaSize = indentBody ? getTabWidth() : 0;
@@ -290,7 +296,7 @@
for (final Iterator iterator= new LineIterator(body); iterator.hasNext();) {
Object o = iterator.next();
String line= (o == null) ? null : o.toString();
-
+
String lineIndent = getIndentOfLine(line, getLineDelimiter(d));
String lineContent= line.substring(lineIndent.length());
appendPreEndLineDelimiter = true;
@@ -313,7 +319,7 @@
calculateDisplayedWidth(firstLineIndent, getTabWidth()) + deltaSize,
useSpaces(), getTabWidth());
buffer.append(lineIndent);
-
+
if ((startText.indexOf('|') == -1)&&
(endText.indexOf('|') == -1))
buffer.append('|');
@@ -335,7 +341,7 @@
return buffer.toString();
}
-
+
private static int getTabWidth() {
try {
return Platform.getPreferencesService().getInt("org.eclipse.ui.editors",
AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH, 4, new
IScopeContext[]{new InstanceScope()});
@@ -377,15 +383,13 @@
if (column > displayedWidth)
return string;
-
+
if (useSpaces) {
while (column != displayedWidth) {
buffer.append(' ');
++column;
}
-
} else {
-
while (column != displayedWidth) {
if (column + tabWidth - (column % tabWidth) <= displayedWidth) {
buffer.append('\t');
@@ -399,7 +403,7 @@
return buffer.toString();
}
-
+
public static String getLineDelimiter(IDocument document) {
try {
if (document.getNumberOfLines() > 1)
@@ -410,7 +414,7 @@
return System.getProperty("line.separator"); //$NON-NLS-1$
}
-
+
private static String getIndentOfFirstLine(IDocument d, int offset) {
String indent = "";
if(d == null) return indent;
@@ -492,7 +496,6 @@
public void remove() {
throw new UnsupportedOperationException();
}
-
}
/**
@@ -516,7 +519,7 @@
String body = doc.get();
applyPrefix(text, body, tagname, uri, defaultPrefix);
}
-
+
public static void applyPrefix(String[] text, String body, String tagname, String uri,
String defaultPrefix) {
if(uri == null || uri.length() == 0) return;
Properties p = getPrefixes(body);
@@ -587,7 +590,7 @@
}
return p;
}
-
+
static void getPrefix(Properties p, String taglib) {
int i = taglib.indexOf("uri=\"");
if(i < 0) return;
@@ -601,5 +604,4 @@
String prefix = taglib.substring(i + 8, j);
p.setProperty(uri, prefix);
}
-
}
\ No newline at end of file
Modified:
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteTaglibInserter.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteTaglibInserter.java 2007-11-05
16:35:43 UTC (rev 4709)
+++
trunk/common/plugins/org.jboss.tools.common.model.ui/src/org/jboss/tools/common/model/ui/views/palette/PaletteTaglibInserter.java 2007-11-05
16:40:19 UTC (rev 4710)
@@ -26,62 +26,58 @@
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
+import org.jboss.tools.common.model.ui.ModelUIPlugin;
+import org.jboss.tools.jst.web.tld.TaglibData;
+import org.jboss.tools.jst.web.tld.VpeTaglibManager;
+import org.jboss.tools.jst.web.tld.VpeTaglibManagerProvider;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-import org.jboss.tools.common.model.ui.ModelUIPlugin;
-import org.jboss.tools.jst.web.tld.TaglibData;
-import org.jboss.tools.jst.web.tld.VpeTaglibManager;
-import org.jboss.tools.jst.web.tld.VpeTaglibManagerProvider;
+public class PaletteTaglibInserter {
-public class PaletteTaglibInserter {
-// private static final String JSP_SOURCE_CONTENTTYPE_ID =
"org.eclipse.jst.jsp.core.jspsource";
private static final String JSP_SOURCE_ROOT_ELEMENT = "jsp:root";
public static final String JSP_URI = "http://java.sun.com/JSP/Page";
-// private static final String HTML_SOURCE_CONTENTTYPE_ID =
"org.eclipse.wst.html.core.htmlsource";
-// private static final String HTML_SOURCE_ROOT_ELEMENT = "html";
public static final String faceletUri = "http://java.sun.com/jsf/facelets";
-
+
private static final String TAGLIB_START = "<%@ taglib";
public Properties inserTaglib(ISourceViewer v, Properties p) {
+ if(!inserTaglibInXml(v, p)) {
+ inserTaglibInOldJsp(v, p);
+ }
+ return p;
+ }
+
+ private boolean checkProperties(Properties p) {
+ return
"true".equalsIgnoreCase(p.getProperty(PaletteInsertHelper.PROPOPERTY_ADD_TAGLIB))
&&
+ p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI) != null &&
+ p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI).length() > 0
&&
+ !p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI).equals(JSP_URI)
&&
+ p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX) != null &&
+ p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX).length() > 0
&&
+ p.getProperty(PaletteInsertHelper.PROPOPERTY_START_TEXT) != null;
+ }
+
+ public boolean inserTaglibInOldJsp(ISourceViewer v, Properties p) {
+ if(!checkProperties(p)) {
+ return false;
+ }
+
IDocument d = v.getDocument();
IStructuredModel model = null;
- if(!"true".equalsIgnoreCase(p.getProperty(PaletteInsertHelper.PROPOPERTY_ADD_TAGLIB))
||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI) == null ||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI).length() == 0 ||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI).equals(JSP_URI) ||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX) == null ||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX).length() == 0 ||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_START_TEXT) == null ||
- p.getProperty(PaletteInsertHelper.PROPOPERTY_START_TEXT).startsWith(TAGLIB_START)) {
- return p;
- }
-
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(d);
IDOMDocument xmlDocument = (model instanceof IDOMModel) ? ((IDOMModel)
model).getDocument() : null;
if (xmlDocument == null) {
- return p;
+ return false;
}
Properties tl = getPrefixes(v);
if(tl == null) tl = PaletteInsertHelper.getPrefixes(d.get());
Element root = xmlDocument.getDocumentElement();
- // for xhtml facelets
- if (root != null && xmlDocument.getDoctype() != null /* &&
tagLibListConainsFacelet(tl)*/ ) {
- String publicId = xmlDocument.getDoctype().getPublicId();
- if (publicId!=null && publicId.toUpperCase().startsWith("-//W3C//DTD
XHTML")) { // &&
root.getNodeName().equalsIgnoreCase(HTML_SOURCE_ROOT_ELEMENT)) {
- return checkTL(root, p, d);
- }
- // for jsp:root
- } else if (root != null && root.getNodeName().equals(JSP_SOURCE_ROOT_ELEMENT))
{
- return checkTL(root, p, d);
- }
- //for others
String uri_p = p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI);
String defaultPrefix_p =
p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX);
String lineDelimiter = PaletteInsertHelper.getLineDelimiter(d);
@@ -97,26 +93,65 @@
if (checkplace(xmlDocument, d, "jsp:directive.taglib", tg, p, v) == false)
{
d.replace(0, 0, tg.toString());
mouveFocusOnPage(p,v, tg.toString().length(), 0);
+ return true;
}
}
} else if(xmlDocument instanceof DocumentImpl) {
- DocumentImpl docImpl = (DocumentImpl)xmlDocument;
- // Only for JSP
- if(docImpl.isJSPType()) {
- if (checkplace(xmlDocument, d, "jsp:directive.page", tg, p, v) == false)
{
- d.replace(0, 0, tg.toString());
- mouveFocusOnPage(p,v, tg.toString().length(), 0);
- }
+ DocumentImpl docImpl = (DocumentImpl)xmlDocument;
+ // Only for JSP
+ if(docImpl.isJSPType()) {
+ if (checkplace(xmlDocument, d, "jsp:directive.page", tg, p, v) == false)
{
+ d.replace(0, 0, tg.toString());
+ mouveFocusOnPage(p,v, tg.toString().length(), 0);
+ return true;
}
}
+ }
} catch (Exception e) {
ModelUIPlugin.getPluginLog().logError(e);
} finally {
if (model != null) model.releaseFromRead();
}
- return p;
+ return false;
}
+ public boolean inserTaglibInXml(ISourceViewer v, Properties p) {
+ if(!checkProperties(p)) {
+ return false;
+ }
+
+ IDocument d = v.getDocument();
+ IStructuredModel model = null;
+
+ try {
+ model = StructuredModelManager.getModelManager().getExistingModelForRead(d);
+ IDOMDocument xmlDocument = (model instanceof IDOMModel) ? ((IDOMModel)
model).getDocument() : null;
+ if (xmlDocument == null) {
+ return false;
+ }
+ Properties tl = getPrefixes(v);
+ if(tl == null) tl = PaletteInsertHelper.getPrefixes(d.get());
+ Element root = xmlDocument.getDocumentElement();
+ // for xhtml facelets
+ if (root != null && xmlDocument.getDoctype() != null /* &&
tagLibListConainsFacelet(tl)*/ ) {
+ String publicId = xmlDocument.getDoctype().getPublicId();
+ if (publicId!=null && publicId.toUpperCase().startsWith("-//W3C//DTD
XHTML")) { // &&
root.getNodeName().equalsIgnoreCase(HTML_SOURCE_ROOT_ELEMENT)) {
+ checkTL(root, p, d);
+ return true;
+ }
+ // for jsp:root
+ } else if (root != null && root.getNodeName().equals(JSP_SOURCE_ROOT_ELEMENT))
{
+ checkTL(root, p, d);
+ return true;
+ }
+ } catch (Exception e) {
+ ModelUIPlugin.getPluginLog().logError(e);
+ } finally {
+ if (model != null) model.releaseFromRead();
+ }
+ return false;
+ }
+
// private static boolean tagLibListConainsFacelet(List tagLibList) {
// if (tagLibList != null && !tagLibList.isEmpty()) {
// for (int i = 0; i < tagLibList.size(); i++) {
@@ -148,7 +183,7 @@
}
return null;
}
-
+
/*
* for jsp:root and html check the taglib if exist check the prefix else add the taglib
* with text formatting
@@ -209,12 +244,11 @@
}
private static void mouveFocusOnPage(Properties p, ISourceViewer v, int length, int
pos){
-
ISelectionProvider selProvider =
(ISelectionProvider)p.get(PaletteInsertHelper.PROPOPERTY_SELECTION_PROVIDER);
IDocument doc = v.getDocument();
if (doc== null || selProvider == null) return;
-
+
ITextSelection selection = (ITextSelection)selProvider.getSelection();
if (selection.getOffset() == 0) {
v.setSelectedRange(length,0);
@@ -309,5 +343,5 @@
}
}
return false;
- }
-}
+ }
+}
\ No newline at end of file