[jboss-cvs] jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/util ...

Robert Stryker rawblem at gmail.com
Mon Jul 31 15:43:41 EDT 2006


  User: rawb    
  Date: 06/07/31 15:43:41

  Added:       as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/util  
                        PackageTypeSearcher.java BaseXMLHyperlinkUtil.java
  Log:
  Added first revision of an xml editor for *-service.xml files. 
  Also added jboss header to several source files. 
  
  Revision  Changes    Path
  1.1      date: 2006/07/31 19:43:41;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/util/PackageTypeSearcher.java
  
  Index: PackageTypeSearcher.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.ui.util;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.Iterator;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.NullProgressMonitor;
  import org.eclipse.jdt.core.IClassFile;
  import org.eclipse.jdt.core.ICompilationUnit;
  import org.eclipse.jdt.core.IPackageFragment;
  import org.eclipse.jdt.core.IType;
  import org.eclipse.jdt.core.JavaModelException;
  import org.eclipse.jdt.core.search.IJavaSearchConstants;
  import org.eclipse.jdt.core.search.IJavaSearchScope;
  import org.eclipse.jdt.core.search.SearchEngine;
  import org.eclipse.jdt.core.search.SearchMatch;
  import org.eclipse.jdt.core.search.SearchParticipant;
  import org.eclipse.jdt.core.search.SearchPattern;
  import org.eclipse.jdt.core.search.SearchRequestor;
  import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
  import org.eclipse.jface.text.contentassist.CompletionProposal;
  import org.eclipse.jface.text.contentassist.ICompletionProposal;
  
  public class PackageTypeSearcher {
  
  	private String packageName;
  	private String remainder;
  	private String fullString;
  	private ResultFilter filter;
  	public PackageTypeSearcher(String string) {
  		fullString = string;
  		int lastDot = string.lastIndexOf(".");
  		if( lastDot == -1 ) {
  			packageName = string;
  			remainder = null;
  		} else {
  			packageName = string.substring(0, lastDot);
  			remainder = string.substring(lastDot+1);
  		}
  	}
  	
  	public PackageTypeSearcher(String string, ResultFilter filter) {
  		this(string);
  		this.filter = filter;
  	}
  	
  	public interface ResultFilter {
  		public boolean accept(Object found);
  	}
  	
  	public ArrayList getPackageProposals() {
  
  		IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
  		SearchPattern packagePattern = SearchPattern.createPattern(fullString, IJavaSearchConstants.PACKAGE,
  			IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PREFIX_MATCH);
  
  		if (packagePattern == null)
  			return new ArrayList();
  
  		SearchEngine searchEngine = new SearchEngine();
  
  		LocalTextfieldSearchRequestor requestor = new LocalTextfieldSearchRequestor();
  		try {
  			searchEngine.search(packagePattern, new SearchParticipant[]
  		           {SearchEngine.getDefaultSearchParticipant()}, scope, requestor, new NullProgressMonitor());
  
  			ArrayList results = requestor.getResults();
  			Collections.sort(results, new Comparator() {
  				
  				public int compare(Object o1, Object o2) {
  		              if (!(o1 instanceof IPackageFragment))
  		                  return 0;
  		               if (!(o2 instanceof IPackageFragment))
  		                  return 0;
  
  	             	IPackageFragment o1a = (IPackageFragment) o1;
  	            	IPackageFragment o2a = (IPackageFragment) o2;
  	            	return o1a.getElementName().compareTo(o2a.getElementName());
  				}
  			});
  
          return results;
  		}
  		catch (CoreException ce) {
  
  		}
  		return new ArrayList();
  	}
  	   
  	   private class LocalTextfieldSearchRequestor extends SearchRequestor {
  		      private ArrayList results;
  
  		      public LocalTextfieldSearchRequestor() {
  		         results = new ArrayList();
  		      }
  
  		      public void acceptSearchMatch(SearchMatch match) throws CoreException {
  		    	  if( filter == null || filter.accept(match.getElement()))
  		    		  results.add(match.getElement());
  		      }
  
  		      public void endReporting() {
  		      }
  
  		      public ArrayList getResults() {
  		         return results;
  		      }
  		   }
  	   
  	   public IPackageFragment getPackage() {
  	      IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
  	      SearchPattern packagePattern = SearchPattern.createPattern(packageName, IJavaSearchConstants.PACKAGE,
  	            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
  
  	      if (packagePattern == null)
  	         return null;
  
  	      SearchEngine searchEngine = new SearchEngine();
  
  	      LocalTextfieldSearchRequestor requestor = new LocalTextfieldSearchRequestor();
  	      try {
  	         searchEngine.search(packagePattern, new SearchParticipant[]
  	         {SearchEngine.getDefaultSearchParticipant()}, scope, requestor, new NullProgressMonitor());
  
  	         ArrayList results = requestor.getResults();
  	         if (results.size() != 1)
  	            return null;
  
  	         return (IPackageFragment) results.get(0);
  	      }
  	      catch (CoreException ce)  {
  
  	      }
  	      return null;
  	   }
  	   
  	   
  	   public ArrayList getTypeMatches()  {
  		  IPackageFragment packageElement = getPackage();
  	      if (packageElement != null && remainder != null)
  	      {
  	         try
  	         {
  	            IClassFile[] classFiles = packageElement.getClassFiles();
  	            ICompilationUnit[] compUnits = packageElement.getCompilationUnits();
  	            ArrayList returnList = new ArrayList();
  
  	            for (int i = 0; i < classFiles.length; i++)
  	            {
  	               String typeName = classFiles[i].getType().getElementName();
  	               if (typeName.equals(""))
  	                  continue;
  	               if (typeName.toLowerCase().startsWith(remainder.toLowerCase()) && filter.accept(classFiles[i].getType()))
  	                  returnList.add(classFiles[i].getType());
  	            }
  
  	            for (int i = 0; i < compUnits.length; i++)
  	            {
  	               IType type = compUnits[i].findPrimaryType();
  	               String typeName = type.getElementName();
  	               if (typeName.toLowerCase().startsWith(remainder.toLowerCase()) && filter.accept(type)) 
  	                  returnList.add(type);
  	            }
  
  	            return returnList;
  	         }
  	         catch (JavaModelException jme)
  	         {
  	         }
  	      }
  
  	      return new ArrayList();
  	   }
  
  	   public ICompletionProposal[] generateProposals(int beginIndex) {
  		  return generateProposals(beginIndex, "", "");
  	   }
  
  	   public ICompletionProposal[] generateProposals(int beginIndex, 
  			   		String proposalPrefix, String proposalSuffix) {
  			ArrayList packages = getPackageProposals();
  		    ArrayList types = getTypeMatches();
  
  			   JavaUILabelProvider imageDelegate = new JavaUILabelProvider();
  
  	      ArrayList list = new ArrayList();
  
  	      for (Iterator i = types.iterator(); i.hasNext();) {
  	         IType type = (IType) i.next();
  	         String replaceString = proposalPrefix + type.getFullyQualifiedName() + proposalSuffix;
  	         CompletionProposal p = new CompletionProposal(replaceString, beginIndex, fullString.length(), 
  	        		 replaceString.length(), imageDelegate.getImage(type), type.getElementName(), null, null);
  	         list.add(p);
  	      }
  
  	      for (Iterator i = packages.iterator(); i.hasNext();) {
  	         IPackageFragment fragment = (IPackageFragment) i.next();
  	         String replaceString = proposalPrefix + fragment.getElementName() + proposalSuffix;
  	         CompletionProposal p = new CompletionProposal(replaceString, beginIndex, fullString.length(), 
  	        		 replaceString.length(), imageDelegate.getImage(fragment), null, null, null);
  	         list.add(p);
  	      }
  
  	      ICompletionProposal props[] = new ICompletionProposal[list.size()];
  	      list.toArray(props);
  	      return props;
  	   }
  }
  
  
  
  1.1      date: 2006/07/31 19:43:41;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/util/BaseXMLHyperlinkUtil.java
  
  Index: BaseXMLHyperlinkUtil.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.ui.util;
  
  import org.eclipse.jface.text.IDocument;
  import org.eclipse.jface.text.IRegion;
  import org.eclipse.jface.text.Region;
  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.xml.core.internal.provisional.document.IDOMAttr;
  import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
  import org.w3c.dom.Attr;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  
  /**
   * Stolen from hibernate tools. Potential candidate to be moved to core
   *
   */
  public class BaseXMLHyperlinkUtil {
  	protected IRegion getHyperlinkRegion(Node node) {
  		IRegion hyperRegion = null;
  
  		if (node != null) {
  			short nodeType = node.getNodeType();
  			if (nodeType == Node.DOCUMENT_TYPE_NODE) {
  				// handle doc type node
  				IDOMNode docNode = (IDOMNode) node;
  				hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset() );
  			}
  			else if (nodeType == Node.ATTRIBUTE_NODE) {
  				// handle attribute nodes
  				IDOMAttr att = (IDOMAttr) node;
  				// do not include quotes in attribute value region
  				int regOffset = att.getValueRegionStartOffset();
  				int regLength = att.getValueRegion().getTextLength();
  				String attValue = att.getValueRegionText();
  				if (org.eclipse.wst.sse.core.utils.StringUtils.isQuoted(attValue) ) {
  					regOffset = ++regOffset;
  					regLength = regLength - 2;
  				}
  				hyperRegion = new Region(regOffset, regLength);
  			}
  		}
  		return hyperRegion;
  	}
  	
  	/**
  	 * API addition
  	 * @param document
  	 * @param offset
  	 * @return
  	 */
  	protected Attr getCurrentAttrNode(IDocument document, int offset) {
  		return getCurrentAttrNode(getCurrentNode(document, offset), offset);
  	}
  	
  	protected Attr getCurrentAttrNode(Node node, int offset) {
  		if( node == null ) return null;
  		
  		if ( (node instanceof IndexedRegion) && ( (IndexedRegion) node).contains(offset) && (node.hasAttributes() ) ) {
  			NamedNodeMap attrs = node.getAttributes();
  			// go through each attribute in node and if attribute contains
  			// offset, return that attribute
  			for (int i = 0; i < attrs.getLength(); ++i) {
  				// assumption that if parent node is of type IndexedRegion,
  				// then its attributes will also be of type IndexedRegion
  				IndexedRegion attRegion = (IndexedRegion) attrs.item(i);
  				if (attRegion.contains(offset) ) {
  					return (Attr) attrs.item(i);
  				}
  			}
  		}
  		return null;
  	}
  	
  		
  	/**
  	 * Returns the element the cursor is currently on in the document. 
  	 * null if no node is selected
  	 * 
  	 * @param offset
  	 * @return Node either element, doctype, text, or null
  	 */
  	protected Node getCurrentNode(IDocument document, int offset) {
  		// get the current node at the offset (returns either: element,
  		// doctype, text)
  		IndexedRegion inode = null;
  		IStructuredModel sModel = null;
  		try {
  			sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
  			inode = sModel.getIndexedRegion(offset);
  			if (inode == null)
  				inode = sModel.getIndexedRegion(offset - 1);
  		}
  		finally {
  			if (sModel != null)
  				sModel.releaseFromRead();
  		}
  
  		if (inode instanceof Node) {
  			return (Node) inode;
  		}
  		return null;
  	}
  }
  
  
  



More information about the jboss-cvs-commits mailing list