Package de.innovationgate.eclipse.editors.tmlscript.parsing

Source Code of de.innovationgate.eclipse.editors.tmlscript.parsing.TMLScriptMethod

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are 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:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tmlscript.parsing;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import de.innovationgate.eclipse.editors.tmlscript.TMLScriptCompletionProposal;

public class TMLScriptMethod {
   
    private String _name;
    private String _type;
    private List<TMLScriptMethodParameter> _parameters = new ArrayList<TMLScriptMethodParameter>();
    private String _classname;
    private boolean _static = false;
    private boolean _deprecated = false;


    private int _beanMode = ReflectionManager.ReflectionModeVisitor.BEAN_MODE_LOWERCASED;

    public void setBeanMode(int mode) {
        _beanMode = mode;
    }

    public boolean isStatic() {
        return _static;
    }

    public void setStatic(boolean static1) {
        _static = static1;
    }

    public TMLScriptMethod(String name, String type, String classname) {
        _name = name;
        _type = type;
        _classname = classname;
    }

    public List<TMLScriptMethodParameter> getParameters() {
        return _parameters;
    }

    public String getName() {
        return _name;
    }

    public String getType() {
        return _type;
    }
   
    public boolean isAccessibleAsProperty() {
        if (_beanMode == ReflectionManager.ReflectionModeVisitor.BEAN_MODE_LOWERCASED) {
            return _name.startsWith("get") && _name.equals(_name.toLowerCase()) && _parameters.size() == 0;
        } else if (_beanMode == ReflectionManager.ReflectionModeVisitor.BEAN_MODE_ALL) {
            return (_name.startsWith("get") || _name.startsWith("is")) && _parameters.size() == 0;
        }
        return false;
    }

    public String getPropertyName() {
        if (!isAccessibleAsProperty()) {
            return null;
        } else {
            String propName = getName().substring(3);
            propName = propName.substring(0, 1).toLowerCase() + propName.substring(1);
            return propName;
        }
    }
   
    public List<TMLScriptCompletionProposal> createProposals(String prefix, int cursorOffset) {
        List<TMLScriptCompletionProposal> proposals = new ArrayList<TMLScriptCompletionProposal>();
       
       
        if (!isDeprecated()) {
            String methodName = getName();       
            if (!_classname.equals("de.innovationgate.wgpublisher.webtml.utils.TMLContext") || methodName.equals(methodName.toLowerCase())) {
                if (isAccessibleAsProperty()) {                 
                    if (prefix == null || getPropertyName().toLowerCase().startsWith(prefix.toLowerCase())) { 
                        StringBuffer display = new StringBuffer();
                        StringBuffer replacement = new StringBuffer();
                        display.append(getPropertyName());
                        replacement.append(getPropertyName());
                        int cursorAfterProposal = replacement.length();
                        display.append(" : " + ReflectionManager.buildSimpleClassname(getType()));
                        int replacementOffset = 0;
                        int replacementLength = 0;
                        if (prefix != null) {
                            replacementOffset = cursorOffset - prefix.length();
                            replacementLength = prefix.length();
                        }
                        TMLScriptCompletionProposal proposal = new TMLScriptCompletionProposal(replacement.toString(), replacementOffset, replacementLength, cursorAfterProposal, display.toString());
                        proposal.enableLRU(_classname);
                        proposals.add(proposal);
                    }
                }
                if (prefix == null || methodName.toLowerCase().startsWith(prefix.toLowerCase())) {                                                                   
                    //build completion proposal
                    int cursorAfterProposal = -1;
                    int selectionOffset = -1;
                    int selectionLength = -1;
                    StringBuffer display = new StringBuffer();
                    StringBuffer replacement = new StringBuffer();
                  
                        display.append(methodName);
                        replacement.append(methodName);
   
                        display.append("(");
                        replacement.append("(");
                        List<TMLScriptMethodParameter> params = getParameters();
                        for (int i=0; i < params.size(); i++) {
                            TMLScriptMethodParameter param = params.get(i);
                            String paramType = param.getType();
                           
                            paramType = ReflectionManager.buildSimpleClassname(paramType);
                           
                            display.append(paramType);
                            display.append(" ");
                            String paramName = param.getName();
                            if (param.isVararg()) {
                                paramName += "[1..n]";
                            }
                            display.append(paramName);
                            replacement.append(paramName);
                            if (i == 0) {
                                cursorAfterProposal = replacement.length();
                                selectionOffset = cursorAfterProposal - paramName.length();
                                selectionLength = paramName.length();
                            }
                            if (i < (params.size() - 1)) {
                                display.append(", ");
                                replacement.append(", ");
                            }                                           
                        }                                      
                        display.append(")");
                        replacement.append(")");
                       
   
                        if (cursorAfterProposal == -1) {
                            cursorAfterProposal = replacement.length();
                        }
                       
                        display.append(" : " + ReflectionManager.buildSimpleClassname(getType()));
            
                    int replacementOffset = 0;
                    int replacementLength = 0;
                    if (prefix != null) {
                        replacementOffset = cursorOffset - prefix.length();
                        replacementLength = prefix.length();
                    }
                    TMLScriptCompletionProposal proposal = new TMLScriptCompletionProposal(replacement.toString(), replacementOffset, replacementLength, cursorAfterProposal, display.toString());
                    proposal.enableLRU(_classname);
                    if (selectionLength != -1 && selectionOffset != -1) {
                        proposal.setSelection(selectionOffset, selectionLength);
                    }
                    proposals.add(proposal);
                }           
            }           
        }
       
        return proposals;
    }


    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof TMLScriptMethod) {
            TMLScriptMethod other = (TMLScriptMethod) obj;
            EqualsBuilder builder = new EqualsBuilder();
            builder.append(hashCode(), other.hashCode());
            return builder.isEquals();
        }
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(getName());       
        Iterator<TMLScriptMethodParameter> myParams = getParameters().iterator();
        while (myParams.hasNext()) {
            builder.append(myParams.next().hashCode());
        }
        return builder.toHashCode();
    }

   
    public boolean isDeprecated() {
        return _deprecated;
    }

    public void setDeprecated(boolean deprecated) {
        _deprecated = deprecated;
    }

    public boolean matchName(String name) {
        if (_beanMode == ReflectionManager.ReflectionModeVisitor.BEAN_MODE_LOWERCASED) {
            return getName().equalsIgnoreCase(name);
        } else {
            return getName().equals(name);
        }
    }
   
}
TOP

Related Classes of de.innovationgate.eclipse.editors.tmlscript.parsing.TMLScriptMethod

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.