-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathChatToolWindowTabbedPane.java
More file actions
278 lines (232 loc) · 8.46 KB
/
ChatToolWindowTabbedPane.java
File metadata and controls
278 lines (232 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package ee.carlrobert.codegpt.toolwindow.chat;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.JBMenuItem;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTabbedPane;
import com.intellij.util.ui.JBUI;
import ee.carlrobert.codegpt.actions.toolwindow.RenameSessionAction;
import ee.carlrobert.codegpt.conversations.ConversationService;
import ee.carlrobert.codegpt.conversations.ConversationsState;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.UUID;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class ChatToolWindowTabbedPane extends JBTabbedPane {
private final Project project;
private final Map<String, ChatToolWindowTabPanel> activeTabMapping = new TreeMap<>(
(o1, o2) -> {
String nums1 = o1.replaceAll("\\D", "");
String nums2 = o2.replaceAll("\\D", "");
if (!nums1.isEmpty() && !nums2.isEmpty()) {
int n1 = Integer.parseInt(nums1);
int n2 = Integer.parseInt(nums2);
return Integer.compare(n1, n2);
}
if (!nums1.isEmpty()) {
return -1;
}
if (!nums2.isEmpty()) {
return 1;
}
return o1.compareToIgnoreCase(o2);
});
private final Disposable parentDisposable;
public ChatToolWindowTabbedPane(Project project, Disposable parentDisposable) {
this.project = project;
this.parentDisposable = parentDisposable;
setTabComponentInsets(null);
setComponentPopupMenu(new TabPopupMenu());
addChangeListener(e -> refreshTabState());
}
public Map<String, ChatToolWindowTabPanel> getActiveTabMapping() {
return activeTabMapping;
}
public void addNewTab(ChatToolWindowTabPanel toolWindowPanel) {
var tabIndices = activeTabMapping.keySet().toArray(new String[0]);
var nextIndex = 0;
for (String title : tabIndices) {
if (title.matches("Chat \\d+")) {
String numberPart = title.replaceAll("\\D+", "");
int tabNum = Integer.parseInt(numberPart);
if ((tabNum - 1) == nextIndex) {
nextIndex++;
} else {
break;
}
}
}
String title = getTitle(toolWindowPanel, nextIndex);
super.insertTab(title, null, toolWindowPanel.getContent(), null, nextIndex);
activeTabMapping.put(title, toolWindowPanel);
super.setSelectedIndex(nextIndex);
if (nextIndex > 0) {
setTabComponentAt(nextIndex, createCloseableTabButtonPanel(title));
toolWindowPanel.requestFocusForTextArea();
}
Disposer.register(parentDisposable, toolWindowPanel);
}
private String getTitle(ChatToolWindowTabPanel toolWindowTabPanel, int nextIndex) {
var conversation = toolWindowTabPanel.getConversation();
String conversationTitle = (conversation != null) ? conversation.getTitle() : null;
String customName = toolWindowTabPanel.getChatSession().getDisplayName();
if (conversationTitle != null && !conversationTitle.trim().isEmpty()) {
return ensureUniqueName(conversationTitle, null);
}
if (customName != null) {
return ensureUniqueName(customName, null);
}
return "Chat " + (nextIndex + 1);
}
public Optional<String> tryFindTabTitle(UUID conversationId) {
return activeTabMapping.entrySet().stream()
.filter(entry -> {
var panelConversation = entry.getValue().getConversation();
return panelConversation != null && conversationId.equals(panelConversation.getId());
})
.findFirst()
.map(Map.Entry::getKey);
}
public Optional<ChatToolWindowTabPanel> tryFindActiveTabPanel() {
var selectedIndex = getSelectedIndex();
if (selectedIndex == -1) {
return Optional.empty();
}
return Optional.ofNullable(activeTabMapping.get(getTitleAt(selectedIndex)));
}
public void clearAll() {
removeAll();
activeTabMapping.clear();
}
public void renameTab(int tabIndex, String newName) {
if (tabIndex < 0 || tabIndex >= getTabCount()) {
return;
}
String oldTitle = getTitleAt(tabIndex);
ChatToolWindowTabPanel panel = activeTabMapping.get(oldTitle);
if (panel == null) {
return;
}
String uniqueName = ensureUniqueName(newName, oldTitle);
setTitleAt(tabIndex, uniqueName);
if (tabIndex > 0) {
setTabComponentAt(tabIndex, createCloseableTabButtonPanel(uniqueName));
}
activeTabMapping.remove(oldTitle);
activeTabMapping.put(uniqueName, panel);
var conversation = panel.getConversation();
if (conversation != null) {
conversation.setTitle(uniqueName);
}
panel.getChatSession().setName(uniqueName);
}
String ensureUniqueName(String desiredName, String currentTitle) {
String baseName = desiredName.trim();
String uniqueName = baseName;
int counter = 2;
while (activeTabMapping.containsKey(uniqueName) && !uniqueName.equals(currentTitle)) {
uniqueName = baseName + " (" + counter + ")";
counter++;
}
return uniqueName;
}
private void refreshTabState() {
var selectedIndex = getSelectedIndex();
if (selectedIndex == -1) {
return;
}
var toolWindowPanel = activeTabMapping.get(getTitleAt(selectedIndex));
if (toolWindowPanel != null) {
var conversation = toolWindowPanel.getConversation();
if (conversation != null) {
ConversationsState.getInstance(project).setCurrentConversation(conversation);
}
}
}
public void resetCurrentlyActiveTabPanel(Project project) {
tryFindActiveTabPanel().ifPresent(tabPanel -> {
Disposer.dispose(tabPanel);
activeTabMapping.remove(getTitleAt(getSelectedIndex()));
removeTabAt(getSelectedIndex());
addNewTab(new ChatToolWindowTabPanel(
project,
ConversationService.getInstance(project).startConversation()));
repaint();
revalidate();
});
}
private JPanel createCloseableTabButtonPanel(String title) {
var closeIcon = AllIcons.Actions.Close;
var button = new JButton(closeIcon);
button.addActionListener(new CloseActionListener(title));
button.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));
button.setBorder(BorderFactory.createEmptyBorder());
button.setContentAreaFilled(false);
button.setToolTipText("Close Chat");
button.setRolloverIcon(AllIcons.Actions.CloseHovered);
return JBUI.Panels.simplePanel(4, 0)
.addToLeft(new JBLabel(title))
.addToRight(button)
.andTransparent();
}
class CloseActionListener implements ActionListener {
private final String title;
public CloseActionListener(String title) {
this.title = title;
}
public void actionPerformed(ActionEvent evt) {
var tabIndex = indexOfTab(title);
if (tabIndex >= 0) {
Disposer.dispose(activeTabMapping.get(title));
removeTabAt(tabIndex);
activeTabMapping.remove(title);
}
}
}
class TabPopupMenu extends JPopupMenu {
private int selectedPopupTabIndex = -1;
TabPopupMenu() {
add(createPopupMenuItem("Rename Title", e -> {
if (selectedPopupTabIndex > 0) {
RenameSessionAction.renameSession(ChatToolWindowTabbedPane.this, selectedPopupTabIndex);
}
}));
addSeparator();
add(createPopupMenuItem("Close", e -> {
if (selectedPopupTabIndex > 0) {
activeTabMapping.remove(getTitleAt(selectedPopupTabIndex));
removeTabAt(selectedPopupTabIndex);
}
}));
add(createPopupMenuItem("Close Other Tabs", e -> {
var selectedPopupTabTitle = getTitleAt(selectedPopupTabIndex);
var tabPanel = activeTabMapping.get(selectedPopupTabTitle);
clearAll();
addNewTab(tabPanel);
}));
}
@Override
public void show(Component invoker, int x, int y) {
selectedPopupTabIndex = ChatToolWindowTabbedPane.this.getUI()
.tabForCoordinate(ChatToolWindowTabbedPane.this, x, y);
if (selectedPopupTabIndex > 0) {
super.show(invoker, x, y);
}
}
private JBMenuItem createPopupMenuItem(String label, ActionListener listener) {
var menuItem = new JBMenuItem(label);
menuItem.addActionListener(listener);
return menuItem;
}
}
}