Hello Plugin

Create a project

  1. Open Intellij and select File -> New Project
  2. From the options on the left, select Gradle and then IntelliJ Platform Plugin. Then you can choose Java, Kotlin, etc
  3. GroupId – New GroupId project. If you plan to deploy the project locally, you can omit this field.

    ArtifactId – ArtifactId is added as the name of the new project.

    Version New project. By default, this field is automatically specified.

    Click when the Settings are completeThe next step
  4. On the next page of the wizard, configure automatic import, Gradle version, and so on for your project, using the default options

Hello My Plugin

  1. Create a new package in the SRC /main/ Java/directory named groupId:com.first.plugin as previously entered
  2. All behavior in IntelliJ inherits from the AnAction class, so let’s create a new class inheriting AnAcction and pop up a message popover in its actionPerformed method
  3. Once the Action is written, just like Android development, we need to add the newly written Action to the plugin.xml actions TAB as an Activity. Where the add-to-group represents where we need to add the action.
  4. After completing the above steps, we can run directly and open an IDE to run the action we just wrote.

Publish the Plugin with Gradle

  1. Find the compiled Helloplugin.jar in the build/libs directory
  2. Log on to plugins.jetbrains.com/, select uploadPlugin, upload our JAR package and fill in the information. Note that description and changeNotes in plugin.xml must be modified in English, otherwise the audit will fail
  3. After the upload is confirmed, it will be reviewed in 2-3 working days. After the review, we can find our plug-in in the Plugin market of Intellij.

Put this to use

Here is how to develop an image compression plug-in based on TinyPng, a popular image compression website. The effect is as follows:

Adding a dependency library

Add the tinyPng API JAR package directly to the lib. Here we rely on RxJava2 for ease of development

Create Action events

Just like HelloAciton, create CompressAction to inherit from AnAction, and then define the location of the event: right mouse button and in the top Tools toolbar. Add the following configuration to plugin.xml:

<actions>
    <action id="com.noober.plugin.tiny" class="com.noober.plugin.tiny.CompressAction" text="TinyCompress"
            description="a plugin to compress images">
        <add-to-group group-id="ProjectViewPopupMenu" anchor="after" relative-to-action="ReplaceInPath"/>
        <add-to-group group-id="ToolsMenu" anchor="last"/>
    </action>
</actions>
Copy the code

Gets the selected image file

private VirtualFile[] getSelectFiles(AnActionEvent e) { return DataKeys.VIRTUAL_FILE_ARRAY.getData(e.getDataContext()); } private ArrayList<String> getFileArrayList(VirtualFile file) { ArrayList<String> pathList = new ArrayList<>(); if (! file.isDirectory()) { if (file.getPath().endsWith(".jpg") || file.getPath().endsWith(".jpeg") || file.getPath().endsWith(".png")) { pathList.add(file.getPath()); } } else { for (VirtualFile file1 : file.getChildren()) { pathList.addAll(getFileArrayList(file1)); } } return pathList; }Copy the code

Use getSelectFiles method to get the selected folder file array, and then iterate, we use getFileArrayList method to get all the image files.

             ArrayList<String> imagePaths = new ArrayList<>();
                        for (VirtualFile file : getSelectFiles(anActionEvent)) {
                            imagePaths.addAll(getFileArrayList(file));
                        }
Copy the code

Create an inputable popover

The popover UI interaction is as follows:

  1. Since TinyPng requires a special key to be entered, we need to create a popover to provide the developer with the input key. If the user does not have a key, we provide a default key to the user.
  2. Also, we need to alert users when uploading compression starts and when compression is complete, so a system-provided Notifications control can be used here.
  3. How to compress we just need to use TinyPng Api.

Inherit DialogWrapper and override createCenterPanel and doOKAction. CreateCenterPanel is used to create a graphical interface that calls Java Swing apis directly, while doOKAction is a callback method for clicking ok. The complete code is as follows:

public class SampleDialogWrapper extends DialogWrapper { String msg; public SampleDialogWrapper(String msg) { super(true); this.msg = msg; init(); getCancelAction().setEnabled(false); setTitle("TinyCompress"); } @override protected JComponent createCenterPanel() {JPanel dialogPanel = new JPanel(); jTextField = new JTextField(hint); dialogPanel.add(jTextField); return dialogPanel; } @Override protected void doOKAction() { super.doOKAction(); String key; if(jTextField.getText().equals(hint)){ key = "LHZoJXCysEceDReZIsQPWPxdODBxhavW"; }else { key = jTextField.getText(); } Observable.create((ObservableOnSubscribe<Boolean>) observableEmitter -> { observableEmitter.onNext(true); Tinify.setKey(key); ArrayList<String> imagePaths = new ArrayList<>(); for (VirtualFile file : getSelectFiles(anActionEvent)) { imagePaths.addAll(getFileArrayList(file)); } boolean result = true; for (String path : imagePaths) { Source source; Source = Tinify. FromFile (path); source.toFile(path); } catch (Exception e1) { e1.printStackTrace(); If (e1 instanceof AccountException){result = false; if(e1 instanceof AccountException){result = false; observableEmitter.onError(e1); break; }else { observableEmitter.onError(e1); } } } if(result){ observableEmitter.onComplete(); }}). Subscribe (result -> {if(result){// popup Notifications that start compression Notifications.Bus. Notify (new Notification(groupId, "TinyCompress", "start compress", NotificationType.INFORMATION, null)); Notifications.Bus. Notify (new Notification(groupId, "TinyCompress", error.getMessage(), NotificationType.WARNING, null)); }, () -> {// popup Notifications completed Notifications Notifications.Bus. Notify (new Notification(groupId, "TinyCompress", "compress complete", NotificationType.INFORMATION, null)); }); }}Copy the code

After the Dialog is written, we simply override the actionPerformed method of AnAction to display the Dialog.

@Override
public void actionPerformed(AnActionEvent e) {
    anActionEvent = e;
    SampleDialogWrapper startDialog = new SampleDialogWrapper("start compress");
    startDialog.show();
}
Copy the code

finishing

Now that the code is complete, all we need to do is change the version number, ID, introduction, and update instructions in plugin.xml.

conclusion

The TinyCompress plugin is available in the Android Studio Plugin marketplace. The project address is github.com/JavaNoober/… . For more information about the Plugin API, please refer to the official documentation IntelliJ Platform SDK.