1.1.1 - Remove missing api key notification on startup

This commit is contained in:
Carl-Robert Linnupuu 2023-02-27 09:41:46 +00:00
parent 07c2f6a0d7
commit 36c8255aa6
5 changed files with 25 additions and 86 deletions

View file

@ -1,48 +0,0 @@
package ee.carlrobert.chatgpt.ide.notification;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import ee.carlrobert.chatgpt.ide.settings.SettingsConfigurable;
import org.jetbrains.annotations.NotNull;
public class NotificationService {
private Notification notification;
public void createAndNotify(@NotNull Project project) {
createNotification();
notify(project);
}
public void expire() {
if (notification != null) {
notification.expire();
}
}
private void notify(@NotNull Project project) {
this.notification
.addAction(new AnAction("Open Settings") {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
DataContext dataContext = event.getDataContext();
Project project = PlatformDataKeys.PROJECT.getData(dataContext);
ShowSettingsUtil.getInstance().showSettingsDialog(project, SettingsConfigurable.class);
}
})
.notify(project);
}
private void createNotification() {
this.notification = NotificationGroupManager.getInstance()
.getNotificationGroup("ChatGPT-Empty-API-Key")
.createNotification("ChatGPT API key not set", NotificationType.WARNING);
}
}

View file

@ -1,19 +0,0 @@
package ee.carlrobert.chatgpt.ide.notification;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import ee.carlrobert.chatgpt.ide.settings.SettingsState;
import org.jetbrains.annotations.NotNull;
public class ShowNotificationActivity implements StartupActivity {
@Override
public void runActivity(@NotNull Project project) {
var notificationService = ApplicationManager.getApplication().getService(NotificationService.class);
var apiKey = SettingsState.getInstance().apiKey;
if (apiKey == null || apiKey.isEmpty()) {
notificationService.createAndNotify(project);
}
}
}

View file

@ -3,6 +3,7 @@ package ee.carlrobert.chatgpt.ide.toolwindow;
import static ee.carlrobert.chatgpt.ide.toolwindow.ToolWindowUtil.createIconLabel;
import static ee.carlrobert.chatgpt.ide.toolwindow.ToolWindowUtil.createTextArea;
import static ee.carlrobert.chatgpt.ide.toolwindow.ToolWindowUtil.justifyLeft;
import static java.lang.String.format;
import com.intellij.ide.ui.LafManager;
import com.intellij.ide.ui.LafManagerListener;
@ -60,16 +61,11 @@ public class ToolWindowService implements LafManagerListener {
addIconLabel(Icons.DefaultImageIcon, "ChatGPT:");
addSpacing(8);
var apiKey = SettingsState.getInstance().apiKey;
if (apiKey == null || apiKey.isEmpty()) {
var label = new JLabel("<html>API key not provided. <font color='#589df6'><u>Open Settings</u></font> to set one.</html>");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
ShowSettingsUtil.getInstance().showSettingsDialog(project, SettingsConfigurable.class);
}
});
scrollablePanel.add(justifyLeft(label));
var settings = SettingsState.getInstance();
if (settings.isGPTOptionSelected && settings.apiKey.isEmpty()) {
notifyMissingCredential(project, "API key not provided.");
} else if (settings.isChatGPTOptionSelected && settings.accessToken.isEmpty()) {
notifyMissingCredential(project, "Access token not provided.");
} else {
var textArea = new SyntaxTextArea();
scrollablePanel.add(textArea);
@ -135,6 +131,17 @@ public class ToolWindowService implements LafManagerListener {
scrollablePanel.add(justifyLeft(createIconLabel(imageIcon, text)));
}
private void notifyMissingCredential(Project project, String text) {
var label = new JLabel(format("<html>%s <font color='#589df6'><u>Open Settings</u></font> to set one.</html>", text));
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
ShowSettingsUtil.getInstance().showSettingsDialog(project, SettingsConfigurable.class);
}
});
scrollablePanel.add(justifyLeft(label));
}
@Override
public void lookAndFeelChanged(@NotNull LafManager source) {
for (var textArea : textAreas) {