Search in sources :

Example 1 with IButterKnife

use of com.avast.android.butterknifezelezny.butterknife.IButterKnife in project android-butterknife-zelezny by avast.

the class InjectAction method showDialog.

protected void showDialog(Project project, Editor editor, ArrayList<Element> elements) {
    PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (file == null) {
        return;
    }
    PsiClass clazz = getTargetClass(editor, file);
    final IButterKnife butterKnife = ButterKnifeFactory.findButterKnifeForPsiElement(project, file);
    if (clazz == null || butterKnife == null) {
        return;
    }
    // get parent classes and check if it's an adapter
    boolean createHolder = false;
    PsiReferenceList list = clazz.getExtendsList();
    if (list != null) {
        for (PsiJavaCodeReferenceElement element : list.getReferenceElements()) {
            if (Definitions.adapters.contains(element.getQualifiedName())) {
                createHolder = true;
            }
        }
    }
    // get already generated injections
    ArrayList<String> ids = new ArrayList<String>();
    PsiField[] fields = clazz.getAllFields();
    String[] annotations;
    String id;
    for (PsiField field : fields) {
        annotations = field.getFirstChild().getText().split(" ");
        for (String annotation : annotations) {
            id = Utils.getInjectionID(butterKnife, annotation.trim());
            if (!Utils.isEmptyString(id)) {
                ids.add(id);
            }
        }
    }
    EntryList panel = new EntryList(project, editor, elements, ids, createHolder, this, this);
    mDialog = new JFrame();
    mDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    mDialog.getRootPane().setDefaultButton(panel.getConfirmButton());
    mDialog.getContentPane().add(panel);
    mDialog.pack();
    mDialog.setLocationRelativeTo(null);
    mDialog.setVisible(true);
}
Also used : ArrayList(java.util.ArrayList) EntryList(com.avast.android.butterknifezelezny.form.EntryList) IButterKnife(com.avast.android.butterknifezelezny.butterknife.IButterKnife)

Example 2 with IButterKnife

use of com.avast.android.butterknifezelezny.butterknife.IButterKnife in project android-butterknife-zelezny by avast.

the class InjectWriter method run.

@Override
public void run() throws Throwable {
    final IButterKnife butterKnife = ButterKnifeFactory.findButterKnifeForPsiElement(mProject, mFile);
    if (butterKnife == null) {
        // Butterknife library is not available for project
        return;
    }
    if (mCreateHolder) {
        generateAdapter(butterKnife);
    } else {
        if (Utils.getInjectCount(mElements) > 0) {
            generateFields(butterKnife);
        }
        generateInjects(butterKnife);
        if (Utils.getClickCount(mElements) > 0) {
            generateClick();
        }
        Utils.showInfoNotification(mProject, String.valueOf(Utils.getInjectCount(mElements)) + " injections and " + String.valueOf(Utils.getClickCount(mElements)) + " onClick added to " + mFile.getName());
    }
    // reformat class
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(mProject);
    styleManager.optimizeImports(mFile);
    styleManager.shortenClassReferences(mClass);
    new ReformatCodeProcessor(mProject, mClass.getContainingFile(), null, false).runWithoutProgress();
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IButterKnife(com.avast.android.butterknifezelezny.butterknife.IButterKnife) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Aggregations

IButterKnife (com.avast.android.butterknifezelezny.butterknife.IButterKnife)2 EntryList (com.avast.android.butterknifezelezny.form.EntryList)1 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 ArrayList (java.util.ArrayList)1