[jbosstools-commits] JBoss Tools SVN: r17678 - trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Sep 21 08:32:55 EDT 2009


Author: scabanovich
Date: 2009-09-21 08:32:55 -0400 (Mon, 21 Sep 2009)
New Revision: 17678

Added:
   trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/CssLinkAttributeValueLoader.java
   trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/PaletteTaglibInserter.java
Modified:
   trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/FileTagProposalLoader.java
   trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/JSPPaletteInsertHelper.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-4913
Splitting PaletteInsertHelper into jsp and non-jsp related parts.

Added: trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/CssLinkAttributeValueLoader.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/CssLinkAttributeValueLoader.java	                        (rev 0)
+++ trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/CssLinkAttributeValueLoader.java	2009-09-21 12:32:55 UTC (rev 17678)
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/ 
+package org.jboss.tools.jst.jsp.jspeditor.dnd;
+
+import org.jboss.tools.common.model.ui.editors.dnd.AbsoluteFilePathAttributeValueLoader;
+import org.jboss.tools.common.model.ui.editors.dnd.IDropWizardModel;
+
+public class CssLinkAttributeValueLoader extends AbsoluteFilePathAttributeValueLoader {
+
+	public CssLinkAttributeValueLoader(String pathAttributeName) {
+		super(pathAttributeName, null, null);
+	}
+
+	public void fillTagAttributes(IDropWizardModel model) {
+		super.fillTagAttributes(model);
+		model.setAttributeValue("rel", "stylesheet"); //$NON-NLS-1$ //$NON-NLS-2$
+		model.setAttributeValue("type", "text/css"); //$NON-NLS-1$ //$NON-NLS-2$
+	}
+
+}

Modified: trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/FileTagProposalLoader.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/FileTagProposalLoader.java	2009-09-21 12:31:32 UTC (rev 17677)
+++ trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/FileTagProposalLoader.java	2009-09-21 12:32:55 UTC (rev 17678)
@@ -14,7 +14,6 @@
 import java.util.Map;
 
 import org.jboss.tools.common.model.ui.editors.dnd.AbsoluteFilePathAttributeValueLoader;
-import org.jboss.tools.common.model.ui.editors.dnd.CssLinkAttributeValueLoader;
 import org.jboss.tools.common.model.ui.editors.dnd.DropURI;
 import org.jboss.tools.common.model.ui.editors.dnd.IAttributeValueLoader;
 import org.jboss.tools.common.model.ui.editors.dnd.IDropWizardModel;
@@ -22,7 +21,6 @@
 import org.jboss.tools.common.model.ui.editors.dnd.ITagProposalLoader;
 import org.jboss.tools.common.model.ui.editors.dnd.LoadBundleBaseNameAttributeValueLoader;
 import org.jboss.tools.common.model.ui.editors.dnd.context.DropContext;
-import org.jboss.tools.common.model.ui.views.palette.PaletteTaglibInserter;
 
 public class FileTagProposalLoader implements ITagProposalLoader {
 

Modified: trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/JSPPaletteInsertHelper.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/JSPPaletteInsertHelper.java	2009-09-21 12:31:32 UTC (rev 17677)
+++ trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/JSPPaletteInsertHelper.java	2009-09-21 12:32:55 UTC (rev 17678)
@@ -1,5 +1,10 @@
 package org.jboss.tools.jst.jsp.jspeditor.dnd;
 
+import java.util.Properties;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.ui.texteditor.ITextEditor;
 import org.jboss.tools.common.model.ui.views.palette.PaletteInsertHelper;
 
 public class JSPPaletteInsertHelper extends PaletteInsertHelper {
@@ -10,6 +15,126 @@
 		return instance;
 	}
 
-	public JSPPaletteInsertHelper() {}
+    static PaletteTaglibInserter PaletteTaglibInserter = new PaletteTaglibInserter();
 
+    public JSPPaletteInsertHelper() {}
+
+	protected void modify(ISourceViewer v, Properties p, String[] texts) {
+		String tagname = p.getProperty(PROPOPERTY_TAG_NAME);
+		String uri = p.getProperty(PROPOPERTY_TAGLIBRARY_URI);
+		String startText = texts[0];
+		if(startText != null && startText.startsWith("<%@ taglib")) { //$NON-NLS-1$
+			if(PaletteTaglibInserter.inserTaglibInXml(v, p)) {
+				return;
+			}
+		} else {
+			p = PaletteTaglibInserter.inserTaglib(v, p);
+		}
+	
+		String defaultPrefix = p.getProperty(PROPOPERTY_DEFAULT_PREFIX);
+		IDocument d = v.getDocument();
+		applyPrefix(texts, d, tagname, uri, defaultPrefix);						
+	}
+
+
+
+	/**
+	 * adding prefix to tag
+	 */
+	public static void applyPrefix(String[] text, ITextEditor editor, String tagname, String uri, String defaultPrefix) {
+		if(defaultPrefix == null || defaultPrefix.length() == 0) return;
+		IDocument doc = null;
+    	if(editor != null && editor.getDocumentProvider() != null) {
+    		doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+    	}
+    	applyPrefix(text, doc, tagname, uri, defaultPrefix);
+	}
+
+	public static void applyPrefix(String[] text, IDocument doc, String tagname, String uri, String defaultPrefix) {
+        if(doc == null) return;
+        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);
+		String prefix = p.getProperty(uri, defaultPrefix);
+		if(prefix == null || prefix.length() == 0) return;				
+		for (int i = 0; i < text.length; i++) text[i] = applyPrefix(text[i], tagname, prefix, p);
+	}
+
+	static String applyPrefix(String text, String tagname, String prefix, Properties prefixes) {
+		if(text == null || text.length() == 0) return text;
+		if(tagname == null || tagname.length() == 0) return text;
+		while(true) {
+			int i = text.indexOf("%prefix|"); //$NON-NLS-1$
+			if(i < 0) break;
+			int j = text.indexOf("%", i + 8); //$NON-NLS-1$
+			if(j < 0) break;
+			int j1 = text.indexOf("|", i + 8); //$NON-NLS-1$
+			String uri = ""; //$NON-NLS-1$
+			String defaultPrefix = ""; //$NON-NLS-1$
+			String pr = ""; //$NON-NLS-1$
+			uri = text.substring(i + 8, j1);
+			defaultPrefix = text.substring(j1 + 1, j);
+			pr = prefixes.getProperty(uri, defaultPrefix);
+			if(pr.length() > 0) {
+				text = text.substring(0, i) + pr + ":" + text.substring(j + 1); //$NON-NLS-1$
+			} else {
+				text = text.substring(0, i) + text.substring(j + 1);
+			}
+		}
+
+		int k = text.toLowerCase().indexOf(":" + tagname.toLowerCase()); //$NON-NLS-1$
+		if(k >= 0) {
+			int g = text.indexOf("</"); //$NON-NLS-1$
+			if(g >= 0 && g < k) {
+				return text.substring(0, g + 2) + prefix + text.substring(k);
+			}
+			g = text.indexOf("<"); //$NON-NLS-1$
+			if(g >= 0 && g < k) {
+				return text.substring(0, g + 1) + prefix + text.substring(k);
+			}
+		}
+		k = text.toLowerCase().indexOf("<" + tagname.toLowerCase()); //$NON-NLS-1$
+		if(k >= 0) {
+			return text.substring(0, k + 1) + prefix + ":" + text.substring(k + 1); //$NON-NLS-1$
+		}
+		k = text.toLowerCase().indexOf("</" + tagname.toLowerCase()); //$NON-NLS-1$
+		if(k >= 0) {
+			return text.substring(0, k + 2) + prefix + ":" + text.substring(k + 2); //$NON-NLS-1$
+		}
+		return text;
+	}
+
+	static Properties getPrefixes(String body) {
+		Properties p = new Properties();
+		int i = 0;
+		while(i >= 0 && i < body.length()) {
+			i = body.indexOf("<%@ taglib ", i); //$NON-NLS-1$
+			if(i < 0) break;
+			int j = body.indexOf("%>", i); //$NON-NLS-1$
+			if(j < 0) j = body.length();
+			String taglib = body.substring(i, j);
+			getPrefix(p, taglib);
+			i = j + 1;
+		}
+		return p;
+	}
+
+	static void getPrefix(Properties p, String taglib) {
+		int i = taglib.indexOf("uri=\""); //$NON-NLS-1$
+		if(i < 0) return;
+		int j = taglib.indexOf("\"", i + 5); //$NON-NLS-1$
+		if(j < 0) return;
+		String uri = taglib.substring(i + 5, j);
+		i = taglib.indexOf("prefix=\""); //$NON-NLS-1$
+		if(i < 0) return;
+		j = taglib.indexOf("\"", i + 8); //$NON-NLS-1$
+		if(j < 0) return;
+		String prefix = taglib.substring(i + 8, j);
+		p.setProperty(uri, prefix);
+	}
+
 }

Added: trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/PaletteTaglibInserter.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/PaletteTaglibInserter.java	                        (rev 0)
+++ trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/jspeditor/dnd/PaletteTaglibInserter.java	2009-09-21 12:32:55 UTC (rev 17678)
@@ -0,0 +1,404 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/ 
+package org.jboss.tools.jst.jsp.jspeditor.dnd;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Properties;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
+import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
+import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
+import org.eclipse.wst.sse.ui.internal.contentassist.ContentAssistUtils;
+import org.eclipse.wst.xml.core.internal.document.DocumentImpl;
+import org.eclipse.wst.xml.core.internal.document.ElementImpl;
+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.common.model.ui.views.palette.PaletteInsertHelper;
+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;
+
+public class PaletteTaglibInserter {
+
+	private static final String JSP_SOURCE_ROOT_ELEMENT = "jsp:root"; //$NON-NLS-1$
+	public static final String JSP_URI = "http://java.sun.com/JSP/Page"; //$NON-NLS-1$
+	public static final String faceletUri = "http://java.sun.com/jsf/facelets"; //$NON-NLS-1$
+
+	private static final String TAGLIB_START = "<%@ taglib";  //$NON-NLS-1$
+
+	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)) && //$NON-NLS-1$
+				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;
+
+		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 = JSPPaletteInsertHelper.getPrefixes(d.get());
+			Element root = xmlDocument.getDocumentElement();
+
+			String uri_p = p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI);
+			String defaultPrefix_p = p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX);
+			String lineDelimiter = PaletteInsertHelper.getLineDelimiter(d);
+			StringBuffer tg = new StringBuffer(TAGLIB_START).append(" uri=\"").append(uri_p).append("\"").append(" prefix=\"").append(defaultPrefix_p).append("\"%>").append(lineDelimiter); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+
+			if (tl != null && !tl.isEmpty()) {
+				//If taglib already exist check the prefix if changed
+				if (tl.containsKey(uri_p)) {
+					if (!tl.get(uri_p).equals(defaultPrefix_p)) {
+						p.setProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX, (String)tl.get(uri_p));
+					}
+				} else if(!tl.containsValue(defaultPrefix_p)) {
+					if (checkplace(xmlDocument, d, "jsp:directive.taglib", tg, p, v) == false) { //$NON-NLS-1$
+						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) { //$NON-NLS-1$
+						d.replace(0, 0, tg.toString());
+						mouveFocusOnPage(p,v, tg.toString().length(), 0);
+						return true;
+					}
+				}
+			}
+		} catch (BadLocationException e) {
+			ModelUIPlugin.getPluginLog().logError(e);
+		} finally {
+			if (model != null)	model.releaseFromRead();
+		}
+		return false;
+	}
+	
+	private static Node getSelectedNode(ISourceViewer v, Properties p){
+		ISelectionProvider selProvider = (ISelectionProvider)p.get(PaletteInsertHelper.PROPOPERTY_SELECTION_PROVIDER);
+		if(selProvider == null) return null;
+		
+		ITextSelection selection = null;
+		
+		if(selProvider.getSelection() instanceof ITextSelection)
+			selection = (ITextSelection)selProvider.getSelection();
+		else return null;
+		
+		IndexedRegion region = ContentAssistUtils.getNodeAt(v, selection.getOffset());
+		if(region == null) return null;
+		
+		if(!(region instanceof Node)) return null;
+		
+		Node text = (Node)region;
+		
+		
+		if("#text".equals(text.getNodeName())) //$NON-NLS-1$
+			return text.getParentNode();
+		else
+			return text;
+	}
+	
+	private static boolean checkSelectedElement(HashMap<String,String> map, ISourceViewer v, Properties p){
+		String taglibUri = p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI);
+		if(taglibUri == null) return false;
+		
+		Node selectedNode = getSelectedNode(v, p);
+		if(selectedNode == null) return false;
+		
+		return checkElement(map, selectedNode, taglibUri);
+	}
+	
+	private static boolean checkElement(HashMap<String,String> map, Node node, String taglibUri){
+		
+		NamedNodeMap attrs = node.getAttributes();
+		for (int j = 0; attrs != null && j < attrs.getLength(); j++) {
+			Node a = attrs.item(j);
+			String name = a.getNodeName();
+
+			if (name.startsWith("xmlns:")) { //$NON-NLS-1$
+				map.put(a.getNodeValue(), name.substring("xmlns:".length())); //$NON-NLS-1$
+			}
+		}
+
+		if (map.containsKey(taglibUri)) return true;
+		else{
+			if(node.getParentNode() == null) return false;
+			else return checkElement(map, node.getParentNode(), taglibUri);
+		}
+	}
+
+	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 = JSPPaletteInsertHelper.getPrefixes(d.get());
+			Element root = xmlDocument.getDocumentElement();
+			if(root != null) {
+				// for xhtml and jsp:root 
+				if (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)) { //$NON-NLS-1$
+						checkTL(root, v, p, d);
+						return true;
+					}
+				} else if(xmlDocument.isXMLType() || root.getNodeName().equals(JSP_SOURCE_ROOT_ELEMENT)) {
+					checkTL(root, v, p, d);
+					return true;
+				}
+			}
+		} 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++) {
+//				TaglibData tgld = (TaglibData)tagLibList.get(i);
+//				if(faceletUri.equals(tgld.getUri())) {
+//					return true;
+//				}
+//			}
+//		}
+//		return false;
+//	}
+
+	/*
+	 * analyse source for taglib, return the list of taglib
+	 */
+	private static Properties getPrefixes(ISourceViewer viewer) {
+		VpeTaglibManager tldManager = null;
+		if((tldManager == null) && (viewer instanceof VpeTaglibManagerProvider)) {
+			tldManager = ((VpeTaglibManagerProvider)viewer).getTaglibManager();
+			if(tldManager != null) {
+				List list = tldManager.getTagLibs();
+				Properties p = new Properties();
+				for (int i = 0; i < list.size(); i++) {
+					TaglibData data = (TaglibData)list.get(i);
+					p.setProperty(data.getUri(), data.getPrefix());
+				}
+				return p;
+			}			
+		}
+		return null;
+	}
+
+	/*
+	 * for jsp:root and html check the taglib if exist check the prefix else add the taglib
+	 * with text formatting
+	 */
+	private static Properties checkTL(Element root, ISourceViewer v, Properties p, IDocument d) {
+		String uri_p = p.getProperty(PaletteInsertHelper.PROPOPERTY_TAGLIBRARY_URI);
+		String defaultPrefix_p = p.getProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX);
+
+		HashMap<String,String> map = new HashMap<String,String>();
+		NamedNodeMap attrs = root.getAttributes();
+		for (int j = 0; attrs != null && j < attrs.getLength(); j++) {
+			Node a = attrs.item(j);
+			String name = a.getNodeName();
+
+			if (name.startsWith("xmlns:")) { //$NON-NLS-1$
+				map.put(a.getNodeValue(), name.substring("xmlns:".length())); //$NON-NLS-1$
+			}
+		}
+		
+		if (map.containsKey(uri_p) || checkSelectedElement(map, v, p)) {
+			if (!map.get(uri_p).equals(defaultPrefix_p)) {
+				p.setProperty(PaletteInsertHelper.PROPOPERTY_DEFAULT_PREFIX, (String) map.get(uri_p));
+			}
+		} else if(!map.containsValue(defaultPrefix_p)) {
+			StringBuffer attribute = new StringBuffer("xmlns:").append(defaultPrefix_p).append("=\"").append(uri_p).append("\"");  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+			int so = ((IDOMElement)root).getStartOffset();
+			int seo = ((IDOMElement)root).getStartEndOffset();
+			try {
+				String lineDelimiter = PaletteInsertHelper.getLineDelimiter(d);
+				StringBuffer selectedSource = new StringBuffer().append(d.get(so, seo-so));
+				int xmlns = selectedSource.indexOf("xmlns"); //$NON-NLS-1$
+				attribute = new StringBuffer().append(createEmptyCharArray(xmlns)).append("xmlns:").append(defaultPrefix_p).append("=\"").append(uri_p).append("\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+				if (d.getLineOffset(d.getLineOfOffset(so)) != so) {										
+					attribute.insert(0, analyseSubstring(d.get(d.getLineOffset(d.getLineOfOffset(so)), so-d.getLineOffset(d.getLineOfOffset(so)))));
+				}				
+				if(xmlns>0) {
+					attribute.insert(0, lineDelimiter);
+				} else {
+					attribute.insert(0, ' ');
+				}
+				selectedSource.insert(selectedSource.length()-1, attribute);
+				d.replace(so, seo-so, selectedSource.toString());
+			} catch (BadLocationException t) {
+				ModelUIPlugin.getPluginLog().logError("", t); //$NON-NLS-1$
+			}
+		}
+		return p;
+	}
+
+	private static char[] createEmptyCharArray(int n){
+		if(n<1) {
+			return new char[0];
+		}
+		char[] ca = new char[n];
+		for (int i = 0; i < n; i++)
+			ca[i]=' ';
+		return ca;
+	}
+
+	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);
+			 p.put(PaletteInsertHelper.PROPOPERTY_SELECTION_PROVIDER,v.getSelectionProvider());
+		}
+		else 
+		if (selection.getOffset() == pos ){
+			v.setSelectedRange(length, 0);
+			p.put(PaletteInsertHelper.PROPOPERTY_SELECTION_PROVIDER,v.getSelectionProvider());
+		}
+	}
+
+	/*
+	 * analyse the space between the left corner and the start offset o the text
+	 */ 
+	private static StringBuffer analyseSubstring(String str){
+		StringBuffer st = new StringBuffer().append(str);		
+		for (int i = 0; i < st.length(); i++) {
+			if (st.charAt(i) != ' ' && st.charAt(i) !='\t' ) {
+				st.setCharAt(i, ' ');
+			}
+		}
+		return st;		
+	}
+
+	private static boolean checkplace(IDOMDocument xmlDocument, IDocument d, String st, StringBuffer tg, Properties p, ISourceViewer v) throws BadLocationException {
+		NodeList nl = xmlDocument.getChildNodes();
+		boolean docType = false;
+		IndexedRegion irdt = null;
+
+		if (xmlDocument.getDoctype() != null) {
+			docType = true;
+			String publicId = xmlDocument.getDoctype().getPublicId();
+			if (publicId!=null && publicId.toUpperCase().startsWith("-//W3C//DTD HTML")) { //$NON-NLS-1$
+				irdt = (xmlDocument.getDoctype() instanceof IndexedRegion) ?
+						(IndexedRegion)xmlDocument.getDoctype(): null;
+			}
+		}
+
+		if (nl != null && nl.getLength() != 0) {
+			for (int i=0; i < nl.getLength(); i++) {
+				Node n = nl.item(i);
+				//fing the first taglib to insert before
+				if (n.getNodeName().equals(st) && st.equals("jsp:directive.taglib")) { //$NON-NLS-1$
+					//calculate the space between taglib and left page corner
+					int so = ((ElementImpl)n).getStartOffset();
+					//taglib is at left corner 
+					if (d.getLineOffset(d.getLineOfOffset(so)) == so) {
+						d.replace(so, 0, tg.toString());
+					} else {
+						StringBuffer left = new StringBuffer().
+						append(analyseSubstring(d.get(d.getLineOffset(d.getLineOfOffset(so)), so-d.getLineOffset(d.getLineOfOffset(so)))));
+						tg.insert(tg.length(), left);
+						d.replace(so, 0, tg.toString());
+					}
+					return true;
+				}
+				if ((n.getNodeName().equals(st) && st.equals("jsp:directive.page"))	) { //$NON-NLS-1$
+					tg.delete(tg.lastIndexOf(PaletteInsertHelper.getLineDelimiter(d)), tg.length());
+					int so = ((ElementImpl)n).getStartOffset();
+					int eo = ((ElementImpl)n).getEndStartOffset();
+					StringBuffer tgleft = new StringBuffer().append(PaletteInsertHelper.getLineDelimiter(d));
+					if (d.getLineOffset(d.getLineOfOffset(so)) == so) {
+						tgleft.append(tg);
+						d.replace(eo, 0, tgleft.toString());
+						mouveFocusOnPage(p,v, eo + tgleft.length(), eo);
+					} else {
+						tgleft.append(analyseSubstring(d.get(d.getLineOffset(d.getLineOfOffset(so)), so-d.getLineOffset(d.getLineOfOffset(so)))));
+						tgleft.append(tg);
+						d.replace(eo, 0, tgleft.toString());
+						mouveFocusOnPage(p,v, eo + tgleft.length(), eo);
+					}
+					return true;
+				}
+				if (docType && irdt != null) {
+					tg.delete(tg.lastIndexOf(PaletteInsertHelper.getLineDelimiter(d)), tg.length());
+					int so = irdt.getStartOffset();
+					int eo = irdt.getEndOffset();
+					StringBuffer tgleft = new StringBuffer().append(PaletteInsertHelper.getLineDelimiter(d));
+					if (d.getLineOffset(d.getLineOfOffset(so)) == so) {
+						tgleft.append(tg);
+						d.replace(eo, 0, tgleft.toString());
+						mouveFocusOnPage(p,v, eo + tgleft.length(), eo);
+					} else {
+						tgleft.append(analyseSubstring(d.get(d.getLineOffset(d.getLineOfOffset(so)), so-d.getLineOffset(d.getLineOfOffset(so)))));
+						tgleft.append(tg);
+						d.replace(eo, 0, tgleft.toString());
+						mouveFocusOnPage(p,v, eo + tgleft.length(), eo);
+					}
+					return true;
+				}
+			}
+		}
+		return false;
+	}
+}
\ No newline at end of file



More information about the jbosstools-commits mailing list