1. 新建一个intellij idea的插件工程
2. 添加2个类
Mvp.java
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Mvp extends AnAction {
String ACTIVITY = "Activity.java";
String FRAGMENT = "Fragment.java";
public void actionPerformed(AnActionEvent e) {
Project project = e.getProject();
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if (project == null || editor == null) {
return;
}
PsiFile currentEditorFile = PsiUtilBase.getPsiFileInEditor(editor, project);
if (currentEditorFile == null) {
return;
}
String currentEditorFileName = currentEditorFile.getName();
String modelName = currentEditorFileName;
if (currentEditorFileName.endsWith(ACTIVITY)) {
modelName = currentEditorFileName.replace(ACTIVITY, "");
} else if (currentEditorFileName.endsWith(FRAGMENT)) {
modelName = currentEditorFileName.replace(FRAGMENT, "");
}
PsiDirectory directory = currentEditorFile.getParent();
List<String> list = new ArrayList<>();
while (directory != null) {
list.add(directory.getName());
if (directory.getName().equals("com")) {
break;
}
directory = directory.getParent();
}
if (list.size() == 0) {
return;
}
Collections.reverse(list);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
stringBuilder.append(list.get(i));
if (i < list.size() - 1) {
stringBuilder.append(".");
}
}
String basePackage = stringBuilder.toString();
String basePath = getCurrentPath(e);
try {
createPresenterClass(basePackage, basePath, modelName);
createModelClass(basePackage, basePath, modelName);
createMvpClass(basePackage, basePath, modelName);
} catch (IOException e1) {
Messages.showMessageDialog("create file failed", "Error", Messages.getErrorIcon());
return;
}
Messages.showMessageDialog("created success! please wait a moment", "Success", Messages.getInformationIcon());
refreshProject(e);
}
private void refreshProject(AnActionEvent e) {
e.getProject().getBaseDir().refresh(false, true);
}
private void createMvpClass(String basePackage, String path, String modelName) throws IOException {
String filename = "I" + modelName + "Mvp.java";
File file = new File(path, filename);
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String content = String.format(MvpTemplate.MVP_TEMPLATE, basePackage, modelName);
writer.write(content);
writer.flush();
writer.close();
}
private void createModelClass(String basePackage, String path, String modelName) throws IOException {
String filename = modelName + "ModelImpl.java";
File file = new File(path, filename);
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String content = String.format(MvpTemplate.MODEL_TEMPLATE, basePackage, modelName);
writer.write(content);
writer.flush();
writer.close();
}
private void createPresenterClass(String basePackage, String path, String modelName) throws IOException {
String filename = modelName + "PresenterImpl.java";
File file = new File(path, filename);
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String content = String.format(MvpTemplate.PRESENTER_TEMPLATE, basePackage, modelName);
writer.write(content);
writer.flush();
writer.close();
}
private String getCurrentPath(AnActionEvent e) {
VirtualFile currentFile = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
if (currentFile != null) {
return currentFile.getParent().getPath();
}
return null;
}
}
MvpTemplate.java
interface MvpTemplate {
String MVP_TEMPLATE =
"package %1$s;\n" +
"\n" +
"interface I%2$sMvp {\n" +
"interface I%2$sModel extends IMvp.IModel {}\n" +
"interface I%2$sPresenter extends IMvp.IPresenter {}\n" +
"interface I%2$sView extends IMvp.IView {}\n" +
"}";
String PRESENTER_TEMPLATE =
"package %1$s;\n" +
"import android.content.Context;\n" +
"import java.lang.Override;\n" +
"class %2$sPresenterImpl extends BasePresenter implements I%2$sMvp.I%2$sPresenter{\n" +
"private Context mContext;\n" +
"private I%2$sMvp.I%2$sView mView;\n" +
"private I%2$sMvp.I%2$sModel mModel;\n" +
"%2$sPresenterImpl(Context context, I%2$sMvp.I%2$sView view) {\n" +
"mContext = context;\n" +
"mView = view;\n" +
"mModel = new %2$sModelImpl(context);\n" +
"}\n" +
"@Override\n" +
"public void start() {}\n" +
"@Override\n" +
"public void destroyView() {mView = null;}" +
"}";
String MODEL_TEMPLATE =
"package %1$s;\n" +
"import android.content.Context;\n" +
"class %2$sModelImpl implements I%2$sMvp.I%2$sModel {\n" +
"private Context mContext;\n" +
"%2$sModelImpl(Context context) {\n" +
"mContext = context;\n" +
"}\n" +
"}";
}
3. plugin.xml文件<actions></actions>中添加
<action id="mvp_generator" class="Mvp" text="mvp" description="generator mvp">
<add-to-group group-id="GenerateGroup" anchor="last"/>
</action>
4. 打包 Build-Prepare Plugin Module
5. AndroidStudio 安装插件
6. 新建一个Activity或Fragment,文件名Activity.java或Fragment.java
7. 按Alt+Insert快捷键选择mvp
8. 在同文件夹下生成了其他的个文件
interface IMainMvp {
interface IMainModel extends IMvp.IModel {
}
interface IMainPresenter extends IMvp.IPresenter {
}
interface IMainView extends IMvp.IView {
}
}
class MainModelImpl implements IMainMvp.IMainModel {
private Context mContext;
MainModelImpl(Context context) {
mContext = context;
}
}
class MainPresenterImpl extends BasePresenter implements IMainMvp.IMainPresenter {
private Context mContext;
private IMainMvp.IMainView mView;
private IMainMvp.IMainModel mModel;
MainPresenterImpl(Context context, IMainMvp.IMainView view) {
mContext = context;
mView = view;
mModel = new MainModelImpl(context);
}
@Override
public void start() {
}
@Override
public void destroyView() {
mView = null;
}
}
9. 加入Mvp.java ,BasePresenter.java
Mvp.java
public interface IMvp {
interface IModel {
}
interface IPresenter {
void start();
void destroy();
}
interface IView {
}
}
BasePresenter.java
public abstract class BasePresenter implements IMvp.IPresenter {
public abstract void destroyView();
@CallSuper
@Override
public void destroy() {
destroyView();
}
}