下面介紹一下如何基於知名圖片壓縮網站TinyPng開發一款圖片壓縮插件。效果以下:
html
直接在lib中加入tinyPng提供的api jar包便可,這裏爲了便於開發,依賴了rxjava2java
像HelloAciton同樣,建立CompressAction繼承AnAction便可,而後咱們定義一下事件的位置:鼠標右鍵和頂部Tools工具欄內。
在plugin.xml加入以下配置:android
<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>
複製代碼
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;
}
複製代碼
經過getSelectFiles方法獲取選中的文件夾的文件數組,而後進行遍歷,咱們經過getFileArrayList方法取出全部圖片文件便可。git
ArrayList<String> imagePaths = new ArrayList<>();
for (VirtualFile file : getSelectFiles(anActionEvent)) {
imagePaths.addAll(getFileArrayList(file));
}
複製代碼
彈窗ui交互以下:github
繼承DialogWrapper,重寫createCenterPanel、doOKAction方法便可。
其中createCenterPanel用於建立圖形界面,直接調用java swing的api便可,而doOKAction則是點擊ok事件的回調方法。
完整代碼以下:api
public class SampleDialogWrapper extends DialogWrapper {
String msg;
public SampleDialogWrapper(String msg) {
super(true);
this.msg = msg;
init();
getCancelAction().setEnabled(false);
setTitle("TinyCompress");
}
@Nullable
@Override
protected JComponent createCenterPanel() {
//經過java swing的方法建立界面
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;
try {
//進行圖片壓縮
source = Tinify.fromFile(path);
source.toFile(path);
} catch (Exception e1) {
e1.printStackTrace();
//若是是帳戶問題,好比key無效、使用次數達到限制,則再也不調用api接口
if(e1 instanceof AccountException){
result = false;
observableEmitter.onError(e1);
break;
}else {
observableEmitter.onError(e1);
}
}
}
if(result){
observableEmitter.onComplete();
}
}).subscribe(result -> {
if(result){
//彈出開始壓縮的通知
Notifications.Bus.notify(new Notification(groupId, "TinyCompress", "start compress", NotificationType.INFORMATION, null));
}
}, error -> {
//出錯時彈出錯誤的通知
Notifications.Bus.notify(new Notification(groupId, "TinyCompress", error.getMessage(), NotificationType.WARNING, null));
}, () -> {
//彈出壓縮完成的通知
Notifications.Bus.notify(new Notification(groupId, "TinyCompress", "compress complete", NotificationType.INFORMATION, null));
});
}
}
複製代碼
dialog寫完以後,咱們只須要重寫AnAction的actionPerformed方法,將dialog展現便可。數組
@Override
public void actionPerformed(AnActionEvent e) {
anActionEvent = e;
SampleDialogWrapper startDialog = new SampleDialogWrapper("start compress");
startDialog.show();
}
複製代碼
代碼已經完成,接下來咱們只須要修改plugin.xml中的版本號、id、介紹以及更新說明便可。app
TinyCompress這個插件已經能夠在android studio的plugin市場中搜到,歡迎你們使用。項目地址以下:github.com/JavaNoober/…。
關於plugin更多的api,能夠參考官方文檔IntelliJ Platform SDK。ide