Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

针对生成Adapter的ViewHolder方法的改造 #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added LayoutCreator.jar
Binary file not shown.
41 changes: 33 additions & 8 deletions src/utils/LayoutCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl;
import com.intellij.psi.search.EverythingGlobalScope;
import entity.Element;
import org.apache.http.util.TextUtils;
Expand All @@ -25,6 +24,7 @@ public class LayoutCreator extends WriteCommandAction.Simple {
protected String mLayoutFileName;
protected String mFieldNamePrefix;
protected boolean mCreateHolder;
public static final String ANDROID_RECYCLER_VIEW_VIEWHOLDER_CLASS = "android.support.v7.widget.RecyclerView.ViewHolder";

public LayoutCreator(PsiFile file, PsiClass clazz, String command, ArrayList<Element> elements, String layoutFileName, String fieldNamePrefix, boolean createHolder) {
super(clazz.getProject(), command);
Expand Down Expand Up @@ -59,17 +59,29 @@ public void run() throws Throwable {
* Create ViewHolder for adapters with injections
*/
protected void generateAdapter() {
//remove old viewHolder method added by ZhangYiGe at 2017年1月9日17:41:13
PsiClass[] psiClasses = mClass.getInnerClasses();
for (PsiClass psiClass : psiClasses) {
if(psiClass.getName().contains("ViewHolder")){
psiClass.delete();
}
}
//end

// view holder class
String holderClassName = Utils.getViewHolderClassName();
StringBuilder holderBuilder = new StringBuilder();

// generator of view holder class
StringBuilder generator = new StringBuilder();

generator.append("public " + holderClassName + "(android.view.View rootView) {\n");

// rootView
String rootViewName = "rootView";
holderBuilder.append("public " + "android.view.View " + rootViewName + ";\n");
//call parent method added by zhangyige at 2016-12-22
generator.append("super(rootView);\n");
generator.append("this." + rootViewName + " = " + rootViewName + ";\n");

for (Element element : mElements) {
Expand All @@ -90,9 +102,22 @@ protected void generateAdapter() {

PsiClass viewHolder = mFactory.createClassFromText(holderBuilder.toString(), mClass);
viewHolder.setName(holderClassName);
//添加父类 added by ZhangYiGe at 2017年1月9日17:40:53
PsiClass rvHolderClass = findClass(mProject, ANDROID_RECYCLER_VIEW_VIEWHOLDER_CLASS);
viewHolder.getExtendsList().add(mFactory.createClassReferenceElement(rvHolderClass));
//end
mClass.add(viewHolder);
mClass.addBefore(mFactory.createKeyword("public", mClass), mClass.findInnerClassByName(holderClassName, true));
mClass.addBefore(mFactory.createKeyword("static", mClass), mClass.findInnerClassByName(holderClassName, true));

}
public PsiClass findClass(Project project, String className) {
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
PsiClass viewClass = psiFacade.findClass(className, new EverythingGlobalScope(project));
if (viewClass == null) {
Utils.showErrorNotification(project, "Class not found: " + className);
}
return viewClass;
}

/**
Expand Down Expand Up @@ -167,7 +192,7 @@ protected void generateFindViewById() {
}
}

if(!hasInitViewStatement && setContentViewStatement != null) {
if (!hasInitViewStatement && setContentViewStatement != null) {
// Insert initView() after setContentView()
onCreate.getBody().addAfter(mFactory.createStatementFromText("initView();", mClass), setContentViewStatement);
}
Expand All @@ -193,15 +218,15 @@ protected void generateFindViewById() {
if (statement instanceof PsiReturnStatement) {
returnStatement = (PsiReturnStatement) statement;
returnValue = returnStatement.getReturnValue().getText();
} else if(statement.getFirstChild() instanceof PsiMethodCallExpression) {
} else if (statement.getFirstChild() instanceof PsiMethodCallExpression) {
PsiReferenceExpression methodExpression = ((PsiMethodCallExpression) statement.getFirstChild()).getMethodExpression();
if (methodExpression.getText().equals("initView")) {
hasInitViewStatement = true;
}
}
}

if(!hasInitViewStatement && returnStatement != null && returnValue != null) {
if (!hasInitViewStatement && returnStatement != null && returnValue != null) {
// Insert initView() before return statement
onCreateView.getBody().addBefore(mFactory.createStatementFromText("initView(" + returnValue + ");", mClass), returnStatement);
}
Expand Down Expand Up @@ -251,7 +276,7 @@ private void generatorLayoutCode(String contextName, String findPre) {
String idName = element.id;
int index = idName.lastIndexOf("_");
String name = index == -1 ? idName : idName.substring(index + 1);
if(name.equals(idName)) {
if (name.equals(idName)) {
name += "String";
}

Expand Down Expand Up @@ -348,11 +373,11 @@ private void generatorLayoutCode(String contextName, String findPre) {
// append non-repeated field
PsiCodeBlock onClickMethodBody = onClickMethods[0].getBody();

for(PsiElement element : onClickMethodBody.getChildren()) {
if(element instanceof PsiSwitchStatement) {
for (PsiElement element : onClickMethodBody.getChildren()) {
if (element instanceof PsiSwitchStatement) {
PsiSwitchStatement switchStatement = (PsiSwitchStatement) element;
PsiCodeBlock body = switchStatement.getBody();
if(body != null) {
if (body != null) {
for (Element clickableElement : clickableElements) {
String caseStr = "case " + clickableElement.getFullID() + " :";
body.add(mFactory.createStatementFromText(caseStr, body));
Expand Down