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

Robert Stryker rawblem at gmail.com
Sat Aug 12 01:04:44 EDT 2006


  User: rawb    
  Date: 06/08/12 01:04:44

  Added:       as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/editors 
                        ServiceXMLCorrectionAssistantProvider.java
  Log:
  Quick Fix functionality
  
  Revision  Changes    Path
  1.1      date: 2006/08/12 05:04:44;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/editors/ServiceXMLCorrectionAssistantProvider.java
  
  Index: ServiceXMLCorrectionAssistantProvider.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.editors;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.jdt.core.IMethod;
  import org.eclipse.jdt.core.IType;
  import org.eclipse.jface.text.contentassist.CompletionProposal;
  import org.eclipse.jface.text.contentassist.ContentAssistant;
  import org.eclipse.jface.text.contentassist.ICompletionProposal;
  import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
  import org.eclipse.jface.text.contentassist.IContentAssistant;
  import org.eclipse.jface.text.source.ISourceViewer;
  import org.eclipse.wst.sse.ui.internal.StructuredTextViewer;
  import org.eclipse.wst.sse.ui.internal.contentassist.ContentAssistUtils;
  import org.eclipse.wst.sse.ui.internal.correction.IQuickAssistProcessor;
  import org.eclipse.wst.sse.ui.internal.correction.IQuickFixProcessor;
  import org.eclipse.wst.xml.core.internal.document.DocumentImpl;
  import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
  import org.eclipse.wst.xml.core.text.IXMLPartitions;
  import org.eclipse.wst.xml.ui.internal.correction.CorrectionAssistantProviderXML;
  import org.eclipse.wst.xml.ui.internal.correction.CorrectionProcessorXML;
  import org.eclipse.wst.xml.ui.internal.correction.QuickAssistProcessorXML;
  import org.eclipse.wst.xml.ui.internal.correction.QuickFixProcessorXML;
  import org.jboss.ide.eclipse.as.core.util.ASDebug;
  import org.jboss.ide.eclipse.as.ui.util.ServiceXMLEditorUtil;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class ServiceXMLCorrectionAssistantProvider extends CorrectionAssistantProviderXML {
  
  	public ServiceXMLCorrectionAssistantProvider() {
  		super();
  		ASDebug.p("999 constructor", this);
  	}
  
  	public IContentAssistant getCorrectionAssistant(ISourceViewer sourceViewer) {
  		IContentAssistant ca = null;
  
  		if (sourceViewer != null) {
  			ContentAssistant assistant = new ContentAssistant();
  
  			if (sourceViewer != null) {
  				IContentAssistProcessor correctionProcessor = new CorrectionProcessorServiceXML(sourceViewer);
  				assistant.setContentAssistProcessor(correctionProcessor, IXMLPartitions.XML_DEFAULT);
  				assistant.setContentAssistProcessor(correctionProcessor, IXMLPartitions.XML_CDATA);
  				assistant.setContentAssistProcessor(correctionProcessor, IXMLPartitions.XML_COMMENT);
  				assistant.setContentAssistProcessor(correctionProcessor, IXMLPartitions.XML_DECLARATION);
  				assistant.setContentAssistProcessor(correctionProcessor, IXMLPartitions.XML_PI);
  				assistant.setContentAssistProcessor(correctionProcessor, IXMLPartitions.DTD_SUBSET);
  			}
  			ca = assistant;
  		}
  
  		return ca;
  	}
  
  	public static class CorrectionProcessorServiceXML extends CorrectionProcessorXML {
  		protected IQuickAssistProcessor fQuickAssistProcessor;
  		protected IQuickFixProcessor fQuickFixProcessor;
  
  		public CorrectionProcessorServiceXML(ISourceViewer sourceViewer) {
  			super(sourceViewer);
  		}
  
  		protected IQuickAssistProcessor getQuickAssistProcessor() {
  			if (fQuickAssistProcessor == null)
  				fQuickAssistProcessor = new QuickAssistProcessorServiceXML();
  
  			return fQuickAssistProcessor;
  		}
  
  		protected IQuickFixProcessor getQuickFixProcessor() {
  			if (fQuickFixProcessor == null)
  				fQuickFixProcessor = new QuickFixProcessorXML();
  
  			return fQuickFixProcessor;
  		}
  	}
  	
  	public static class QuickAssistProcessorServiceXML extends QuickAssistProcessorXML {
  		public boolean canAssist(StructuredTextViewer viewer, int offset) {
  			if( super.canAssist(viewer, offset)) return true;
  
  			IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
  			if( mbeanHasAdditionalAttributes(node)) return true;
  
  			return false;
  		}
  		
  
  		private boolean mbeanHasAdditionalAttributes( IDOMNode node ) {
  			int length = mbeanGetMissingAttributes(node).length;
  			ASDebug.p("length is " + length, this);
  			return length == 0 ? false : true;
  		}
  		
  		private String[] mbeanGetMissingAttributes( IDOMNode node ) {
  			Node parentNode = node.getParentNode();
  			
  			if( node.getNodeName().equals("mbean") || (node.getNodeName().equals("#text") && parentNode.getNodeName().equals("mbean"))) {
  				Node mbeanNode = node.getNodeName().equals("mbean") ? node : parentNode;
  				NamedNodeMap attributes = mbeanNode.getAttributes();
  				for( int i = 0; i < attributes.getLength(); i++ ) {
  					if( attributes.item(i).getNodeName().equals("code")) {
  						// we found our code element, now lets get the IType. 
  						String codeClass = attributes.item(i).getNodeValue();
  						IType type = ServiceXMLEditorUtil.findType(codeClass);
  						if( type != null ) {
  							IMethod[] methods = ServiceXMLEditorUtil.getAllMethods(type);
  							ArrayList attributeNames = new ArrayList(Arrays.asList(ServiceXMLEditorUtil.findAttributesFromMethods(methods, "")));
  							NodeList mbeanChildren = mbeanNode.getChildNodes();
  							
  							// count children named 'attribute'
  							for( int j = 0; j < mbeanChildren.getLength(); j++ ) {
  								if( mbeanChildren.item(j).getNodeName().equals("attribute")) {
  									try {
  									Node t = mbeanChildren.item(j).getAttributes().getNamedItem("name");
  									String attName = t.getNodeValue();
  									attributeNames.remove(attName);
  									} catch( Exception e ) {
  										e.printStackTrace();
  									}
  								}
  							}
  							return (String[]) attributeNames.toArray(new String[attributeNames.size()]);
  						}
  					}
  				}
  			}
  			return new String[] { };
  		}
  		
  		public ICompletionProposal[] getProposals(StructuredTextViewer viewer, int offset) throws CoreException {
  			ArrayList props = new ArrayList();
  			if( super.canAssist(viewer, offset)) props.addAll(Arrays.asList(super.getProposals(viewer, offset)));
  
  			IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
  			addMissingAttributesProposal(node, props);
  			
  			return (ICompletionProposal[]) props.toArray(new ICompletionProposal[props.size()]);
  		}
  		
  		protected void addMissingAttributesProposal(IDOMNode node, ArrayList proposals) {
  			String[] missing = mbeanGetMissingAttributes(node);
  			if( missing.length == 0 ) return;
  
  			Node parentNode = node.getParentNode();
  			Node mbeanNode = node.getNodeName().equals("mbean") ? node : parentNode;
  
  			
  			// how many tabs?
  			Node newParent = mbeanNode;
  			StringBuffer attributeIndent = new StringBuffer();
  			StringBuffer mbeanClosingIndent = new StringBuffer();
  			boolean starting = true;
  			attributeIndent.append("\n");
  			mbeanClosingIndent.append("\n");
  			while( newParent != null && !(newParent instanceof DocumentImpl)) {
  				newParent = newParent.getParentNode();
  				attributeIndent.append("\t");
  				if( starting ) 
  					starting = false;
  				else 
  					mbeanClosingIndent.append("\t");
  			}
  			
  			
  			StringBuffer buf = new StringBuffer();
  			for( int i = 0; i < missing.length; i++ ) {
  				buf.append(attributeIndent);
  				buf.append("<attribute name=\"");
  				buf.append(missing[i]);
  				buf.append("\"></attribute>");
  			}
  			buf.append(mbeanClosingIndent);
  			
  			// where to put it
  			IDOMNode lastChild = (IDOMNode)mbeanNode.getLastChild();
  			int endOffset = lastChild.getEndOffset();
  			
  			
  			
  			proposals.add( new CompletionProposal(buf.toString(), endOffset, 0, 0,
  								null, "Add missing attribute tags", null, null) );
  		}
  
  	}
  }
  
  
  



More information about the jboss-cvs-commits mailing list