Skip to content

Commit

Permalink
feat: add fetch action and update add tab action
Browse files Browse the repository at this point in the history
  • Loading branch information
LiLittleCat committed Mar 21, 2023
1 parent b83e41b commit e78e48f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 24 deletions.
30 changes: 10 additions & 20 deletions src/main/java/com/lilittlecat/chatgpt/action/AddTabAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.lilittlecat.chatgpt.message.ChatGPTBundle;
import com.lilittlecat.chatgpt.setting.ChatGPTSettingsState;
import com.lilittlecat.chatgpt.window.ChatGPTToolWindow;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.List;
import java.util.Objects;

/**
* todo change it to group popup
* @author <a href="https://github.com/LiLittleCat">LiLittleCat</a>
* @since 2023/3/9
*/
Expand All @@ -26,32 +29,19 @@ public AddTabAction(@NotNull @Nls String text) {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String[] options = new String[] {"Content 1", "Content 2", "Content 3"};
List<String> urlList = ChatGPTSettingsState.getInstance().urlList;
String[] options = urlList.toArray(new String[0]);
// String selected = Messages.showChooseDialog("Select Content", "Choose Content to Add", options, options[0], null);
String selected = Messages.showEditableChooseDialog("Select Content", "Choose Content to Add", AllIcons.General.Add, options, options[0], null);
String selected = Messages.showEditableChooseDialog("Select URL", "Choose a URL to Add", AllIcons.General.Add, options, options[0], null);
if (selected == null) {
return;
}
ToolWindowManager instance = ToolWindowManager.getInstance(Objects.requireNonNull(e.getProject()));
ToolWindow toolWindow = instance.getToolWindow(ChatGPTBundle.message("name"));
assert toolWindow != null;
ContentManager contentManager = toolWindow.getContentManager();
switch (selected) {
case "Content 1": {
Content content = contentManager.getFactory().createContent(new JLabel("Content 1"), "Content 1", true);
contentManager.addContent(content);
break;
}
case "Content 2": {
Content content = contentManager.getFactory().createContent(new JLabel("Content 2"), "Content 2", true);
contentManager.addContent(content);
break;
}
case "Content 3": {
Content content = contentManager.getFactory().createContent(new JLabel("Content 3"), "Content 3", true);
contentManager.addContent(content);
break;
}
}
Content content = contentManager.getFactory().createContent(new ChatGPTToolWindow(selected).getContent(), selected, false);
contentManager.addContent(content);
contentManager.setSelectedContent(content);
}
}
76 changes: 76 additions & 0 deletions src/main/java/com/lilittlecat/chatgpt/action/FetchURLAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.lilittlecat.chatgpt.action;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.lilittlecat.chatgpt.message.ChatGPTBundle;
import com.lilittlecat.chatgpt.setting.ChatGPTSettingsState;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;

import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author <a href="https://github.com/LiLittleCat">LiLittleCat</a>
* @since 2023/3/20
*/
public class FetchURLAction extends DumbAwareAction {
public FetchURLAction(@NotNull @Nls String text) {
super(() -> text, AllIcons.Actions.CheckOut);
}

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(3, TimeUnit.SECONDS)
.readTimeout(3, TimeUnit.SECONDS)
.build();

String primaryUrl = ChatGPTBundle.message("free.chatgpt.file.url.original");
String fallbackUrl = ChatGPTBundle.message("free.chatgpt.file.url.fallback");

try {
JSONArray jsonArray = fetchJsonArray(client, primaryUrl);
processJsonArray(jsonArray);
} catch (IOException e) {
try {
JSONArray jsonArray = fetchJsonArray(client, fallbackUrl);
processJsonArray(jsonArray);
} catch (IOException e2) {
JOptionPane.showMessageDialog(null, ChatGPTBundle.message("cannot.fetch.message"), "Error", JOptionPane.ERROR_MESSAGE);
}
}

}

private static JSONArray fetchJsonArray(OkHttpClient client, String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
assert response.body() != null;
String jsonData = response.body().string();
return new JSONArray(jsonData);
}
}

private static void processJsonArray(JSONArray jsonArray) {
List<String> urlList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
String url = jsonArray.getString(i);
ChatGPTSettingsState.getInstance().urlList.add(url);
}
// Process the List<String> here
System.out.println(urlList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import com.intellij.openapi.ui.cellvalidators.ValidationUtils;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColoredTableCellRenderer;
import com.intellij.ui.SeparatorComponent;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.*;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.fields.ExtendableTextField;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.ListTableModel;
import com.lilittlecat.chatgpt.action.FetchURLAction;
import com.lilittlecat.chatgpt.message.ChatGPTBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -47,6 +45,7 @@ public class ChatGPTSettingsConfigurable implements SearchableConfigurable {
private ListTableModel<String> myModel = new ListTableModel<>() {
@Override
public void addRow() {
// add item to table
addRow("");
}

Expand All @@ -57,7 +56,9 @@ public void removeRow(int idx) {
// popup a dialog to tell user that the default url can't be removed
JOptionPane.showMessageDialog(null, ChatGPTBundle.message("chatgpt.settings.defaultUrlCanNotBeRemoved"));
} else {
// remove item from table
super.removeRow(idx);
// remove item from comboBox
defaultUrlComboBox.removeItemAt(idx);
}
}
Expand All @@ -82,6 +83,7 @@ public ChatGPTSettingsConfigurable() {
}

private JComponent init() {
// init table
myModel.addRows(ChatGPTSettingsState.getInstance().urlList);
myTable = new JBTable(myModel) {
@Override
Expand Down Expand Up @@ -180,7 +182,9 @@ protected SimpleTextAttributes modifyAttributes(SimpleTextAttributes attributes)
}
}).bindToEditorSize(cellEditor::getPreferredSize));

AnActionButton fetchUrlListFromGithub = AnActionButton.fromAction(new FetchURLAction("Fetch url list from github"));
return ToolbarDecorator.createDecorator(myTable)
.addExtraAction(fetchUrlListFromGithub)
.disableUpDownActions()
.createPanel();
}
Expand Down

0 comments on commit e78e48f

Please sign in to comment.