The method used to create the file
Get common tool classes
myFactory = JavaPsiFacade.getElementFactory(mProject);Copy the code
Gets the directory with the mouse selected
Get the Ideview via AnActionEvent, then call getOrChooseDiretory() to get the selected directory
IdeView ideView = anActionEvent.getRequiredData(LangDataKeys.IDE_VIEW);
PsiDirectory directory = ideView.getOrChooseDirectory();Copy the code
Create a Java class
Create Java classes through DirectoryService
myDirectoryService = JavaDirectoryService.getInstance();
PsiClass psiClass = myDirectoryService.createClass(directory, "Text", JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME);Copy the code
Set the package name
PsiJavaFile javaFile = (PsiJavaFile) psiClass.getContainingFile();
PsiPackage psiPackage = myDirectoryService.getPackage(directory);
javaFile.setPackageName(psiPackage.getQualifiedName());Copy the code
Set the permissions of the class
psiClass.getModifierList().setModifierProperty(PsiModifier.PUBLIC,true);Copy the code
Add an interface to the psiClass class
PsiClass view = myFactory.createInterface("View");
psiClass.add(view);Copy the code
Find PsiClass globally by name
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;
}Copy the code
Find PsiClass based on PsiFile
if ((psiFile1 instanceof PsiJavaFile) && ((PsiJavaFile) psiFile1).getClasses().length > 0) {
psiClass = ((PsiJavaFile) psiFile1).getClasses()[0];
}Copy the code
Manually set the name and icon of the Action
Presentation presentation = getTemplatePresentation();
presentation.setText(fileType);
presentation.setIcon(IconLoader.getIcon("/icons/icon_tf.png"));Copy the code
Where ICONS will be placed under the Resource directory:
Note the naming conventions for images
Add Acton manually in ActionGroup
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)); }}}Copy the code
Where AddFile is Acton, setPopup(true) is needed to achieve the following effect; Otherwise the Action is tiled and cannot be placed in the MVPFile
Control whether an Action is visible by overwriting its update
@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);
}Copy the code
e.getPresentation().setEnabled(true); = LLDB etPresentation().setenabledAndVisible (true); Used to make the Action available, visible, and optional
Display error message
Messages.showErrorDialog("Generation failed, " +
"your class name MUST END WITH 'Contract' or 'Presenter'."."Class Name Error");Copy the code
The Dialog to set
// Set the title of the DialogsetTitle("New Mvp File"); // Set the minimum size of the DialogsetMinimumSize(new Dimension(260, 120)); // Set Dialog in the middle of the screen, public voidsetLocationRelativeTo(Component C) Sets the position of the window relative to the specified Component. // If the component is not currently displayed, or if C is null, this window will be placed in the center of the screen.setLocationRelativeTo(null);Copy the code
SetLocationRelativeTo (NULL) can center the screen, but it won’t look good if the IDE isn’t full screen. To center the Dialog in the IDE window, set it like this:
setLocationRelativeTo(WindowManager.getInstance().getFrame(actionEvent.getProject())
Note the sequential position of setMinimumSize and setLocationRelativeTo. If setLocationRelativeTo is in front, the upper left corner of the created window is centered because the window is not yet large.
Import the required classes
To import an import file using the following method, the prerequisite is that the class to be imported must contain the package name. For example, log. e must be written as Android.util.log. e(TAG, field.tostring ()). :
JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
styleManager.optimizeImports(file);
styleManager.shortenClassReferences(targetClass);Copy the code
Get all PsiStatement under PsiElment
for(PsiStatement psiStatement : psiMethod.getBody().getStatements()) { EventLogger.log(psiStatement.getText()); / / to findsetContentView
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; }}}Copy the code
c