myFactory = JavaPsiFacade.getElementFactory(mProject);複製代碼
經過AnActionEvent獲取到Ideview,而後調用getOrChooseDiretory()
獲取鼠標右擊選中的目錄java
IdeView ideView = anActionEvent.getRequiredData(LangDataKeys.IDE_VIEW);
PsiDirectory directory = ideView.getOrChooseDirectory();複製代碼
經過DirectoryService建立Java類android
myDirectoryService = JavaDirectoryService.getInstance(); PsiClass psiClass = myDirectoryService.createClass(directory, "Text", JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME);複製代碼
PsiJavaFile javaFile = (PsiJavaFile) psiClass.getContainingFile();
PsiPackage psiPackage = myDirectoryService.getPackage(directory);
javaFile.setPackageName(psiPackage.getQualifiedName());複製代碼
psiClass.getModifierList().setModifierProperty(PsiModifier.PUBLIC,true);複製代碼
PsiClass view = myFactory.createInterface("View"); psiClass.add(view);複製代碼
private PsiClass getPsiClassByName(String name) { PsiClass[] psiClasses = myShortNamesCache.getClassesByName(name, myProjectScope);//NotNull PsiClass psiClass = null; if (psiClasses.length != 0) {//if the class already exist. psiClass = psiClasses[0]; }//and return psiClass; }複製代碼
if ((psiFile1 instanceof PsiJavaFile) && ((PsiJavaFile) psiFile1).getClasses().length > 0) { psiClass = ((PsiJavaFile) psiFile1).getClasses()[0]; }複製代碼
Presentation presentation = getTemplatePresentation(); presentation.setText(fileType); presentation.setIcon(IconLoader.getIcon("/icons/icon_tf.png"));複製代碼
其中icons要放到Resource目錄下:bash
注意圖片的命名規則markdown
public class AddMVPFile extends DefaultActionGroup implements DumbAware { public AddMVPFile() { setPopup(true); Presentation presentation = getTemplatePresentation(); presentation.setText("MVPFile"); presentation.setIcon(IconLoader.getIcon("/icons/icon_tf.png")); List<String> fileTypes = new ArrayList<>(); fileTypes.add("Contract"); fileTypes.add("PresenterImpl"); fileTypes.add("ModelImpl"); for (String fileType:fileTypes){ add(new AddFile(fileType)); } } }複製代碼
其中AddFile是Acton,要實現以下所示的效果,須要加入 setPopup(true);
不然Action是平鋪開來的,沒辦法放到MVPFile下ide
@Override public void update(AnActionEvent e) { super.update(e); IdeView ideView = e.getRequiredData(LangDataKeys.IDE_VIEW); PsiDirectory directory = ideView.getOrChooseDirectory(); if (directory.getName().equals("contract")) e.getPresentation().setEnabledAndVisible(true); else e.getPresentation().setEnabledAndVisible(false); }複製代碼
e.getPresentation().setEnabled(true);
用來設置該Action是否可用,e.getPresentation().setEnabledAndVisible(true);
用來設置該Action可用而且可見,能夠靈活選用工具
Messages.showErrorDialog("Generation failed, " + "your class name MUST END WITH 'Contract' or 'Presenter'.", "Class Name Error");複製代碼
//設置Dialog的標題 setTitle("New Mvp File"); //設置Dialog的最小大小 setMinimumSize(new Dimension(260, 120)); //設置Dialog在屏幕中間,public void setLocationRelativeTo(Component c)設置窗口相對於指定組件的位置。 //若是組件當前未顯示,或者 c 爲 null,則此窗口將置於屏幕的中央。 setLocationRelativeTo(null);複製代碼
setLocationRelativeTo(null)
能夠使其屏幕居中,但若是IDE不全屏,顯示的效果就很差看了,想使Dialog在IDE窗口居中顯示,能夠這樣設置:ui
setLocationRelativeTo(WindowManager.getInstance().getFrame(actionEvent.getProject())
spa
注意setMinimumSize和setLocationRelativeTo的前後位置,若是setLocationRelativeTo在前,則建立出來的窗口的左上角居中,由於這時窗口尚未大小。code
要想使用以下的方法導入import,前提條件是須要導入的類必須包含包名,例如Log.e
須要寫成android.util.Log.e(TAG,field.toString());
:orm
JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
styleManager.optimizeImports(file);
styleManager.shortenClassReferences(targetClass);複製代碼
for (PsiStatement psiStatement : psiMethod.getBody().getStatements()) { EventLogger.log(psiStatement.getText()); // 查找setContentView if (psiStatement.getFirstChild() instanceof PsiMethodCallExpression) { PsiReferenceExpression methodExpression = ((PsiMethodCallExpression) psiStatement.getFirstChild()).getMethodExpression(); if (methodExpression.getText().equals("setContentView")) { setContentViewStatement = psiStatement; } else if (methodExpression.getText().equals("initView")) { hasInitViewStatement = true; } } }複製代碼
c