Skip to content

Commit 1f69629

Browse files
author
darksheep
committed
fix canot add new tag
1 parent 2a60283 commit 1f69629

File tree

1 file changed

+82
-82
lines changed

1 file changed

+82
-82
lines changed

src/main/java/com/darksheep/sheepnote/CodeNoteAction.java

+82-82
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private JPanel createNotePanel(String selectedText, DialogBuilder dialogBuilder,
125125
notePanel.add(titleTextField, gbc);
126126

127127
// 标签部分
128-
JLabel tagsLabel = new JLabel("Tags");
128+
JLabel tagsLabel = new JLabel("Tags(Enter to add New tag when no tags to select)");
129129
tagsLabel.setFont(labelFont);
130130
tagsLabel.setForeground(inputForeground);
131131
gbc.gridy = 2;
@@ -169,65 +169,51 @@ private JPanel createNotePanel(String selectedText, DialogBuilder dialogBuilder,
169169
// 标签输入监听
170170
tagsTextField.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
171171
private void updatePopup() {
172-
String input = tagsTextField.getText().trim();
173-
String[] parts = input.split(",");
174-
String currentTag = parts[parts.length - 1].trim().toLowerCase();
175-
176-
if (currentTag.isEmpty()) {
177-
autoCompletePopup.setVisible(false);
178-
return;
179-
}
172+
SwingUtilities.invokeLater(() -> {
173+
String input = tagsTextField.getText().trim();
174+
String[] parts = input.split(",");
175+
String currentTag = parts[parts.length - 1].trim().toLowerCase();
176+
177+
if (currentTag.isEmpty()) {
178+
autoCompletePopup.setVisible(false);
179+
return;
180+
}
180181

181-
autoCompletePopup.removeAll();
182-
boolean hasMatches = false;
183-
184-
for (String tag : existingTags) {
185-
if (tag.toLowerCase().contains(currentTag)) {
186-
hasMatches = true;
187-
JMenuItem item = new JMenuItem(tag);
188-
item.setFont(inputFont);
189-
item.setBackground(inputBackground);
190-
item.setForeground(inputForeground);
191-
item.addMouseListener(new java.awt.event.MouseAdapter() {
192-
public void mouseEntered(java.awt.event.MouseEvent evt) {
193-
item.setBackground(new Color(0, 122, 255, 30));
194-
}
195-
public void mouseExited(java.awt.event.MouseEvent evt) {
196-
item.setBackground(inputBackground);
197-
}
198-
});
199-
item.setBorder(BorderFactory.createEmptyBorder(6, 12, 6, 12));
200-
item.addActionListener(e -> {
201-
String existingTags = getTagsFromPanel(tagsPanel);
202-
String newTag = tag.trim();
182+
autoCompletePopup.removeAll();
183+
boolean hasMatches = false;
184+
185+
// 只显示匹配的现有标签
186+
for (String tag : existingTags) {
187+
if (tag.toLowerCase().contains(currentTag)) {
188+
hasMatches = true;
189+
JMenuItem item = new JMenuItem(tag);
190+
item.setFont(inputFont);
191+
item.setBackground(inputBackground);
192+
item.setForeground(inputForeground);
193+
item.setBorder(BorderFactory.createEmptyBorder(6, 12, 6, 12));
203194

204-
// 检查标签是否已存在
205-
if (!existingTags.isEmpty()) {
206-
String[] tagArray = existingTags.split(",");
207-
for (String t : tagArray) {
208-
if (t.trim().equalsIgnoreCase(newTag)) {
209-
return; // 如果标签已存在,直接返回
210-
}
211-
}
212-
existingTags += "," + newTag;
213-
} else {
214-
existingTags = newTag;
215-
}
195+
item.addActionListener(e -> {
196+
addTagToPanel(tag, tagsPanel, tagsTextField);
197+
tagsTextField.setText("");
198+
autoCompletePopup.setVisible(false);
199+
tagsTextField.requestFocusInWindow();
200+
});
216201

217-
updateTagsDisplay(existingTags, tagsPanel, tagsTextField);
218-
tagsTextField.setText("");
219-
autoCompletePopup.setVisible(false);
220-
});
221-
autoCompletePopup.add(item);
202+
autoCompletePopup.add(item);
203+
}
222204
}
223-
}
224205

225-
if (hasMatches) {
226-
autoCompletePopup.setPopupSize(tagsTextField.getWidth(), Math.min(200, autoCompletePopup.getComponentCount() * 35));
227-
autoCompletePopup.show(tagsTextField, 0, tagsTextField.getHeight());
228-
} else {
229-
autoCompletePopup.setVisible(false);
230-
}
206+
if (hasMatches) {
207+
if (!autoCompletePopup.isVisible()) {
208+
autoCompletePopup.show(tagsTextField, 0, tagsTextField.getHeight());
209+
}
210+
autoCompletePopup.setPopupSize(tagsTextField.getWidth(),
211+
Math.min(200, autoCompletePopup.getComponentCount() * 35));
212+
tagsTextField.requestFocusInWindow();
213+
} else {
214+
autoCompletePopup.setVisible(false);
215+
}
216+
});
231217
}
232218

233219
public void insertUpdate(javax.swing.event.DocumentEvent e) { updatePopup(); }
@@ -244,34 +230,13 @@ public void keyPressed(java.awt.event.KeyEvent e) {
244230
if (text.endsWith(",")) {
245231
text = text.substring(0, text.length() - 1);
246232
}
247-
248-
// 获取当前已有的标签
249-
String existingTags = getTagsFromPanel(tagsPanel);
250-
String newTag = text.trim();
251-
252-
// 检查标签是否已存在
253-
boolean tagExists = false;
254-
if (!existingTags.isEmpty()) {
255-
String[] tagArray = existingTags.split(",");
256-
for (String t : tagArray) {
257-
if (t.trim().equalsIgnoreCase(newTag)) {
258-
tagExists = true;
259-
break;
260-
}
261-
}
262-
if (!tagExists) {
263-
existingTags += "," + newTag;
264-
}
265-
} else {
266-
existingTags = newTag;
267-
}
268-
269-
if (!tagExists) {
270-
updateTagsDisplay(existingTags, tagsPanel, tagsTextField);
271-
}
233+
// 直接添加用户输入的标签,不管是否存在
234+
addTagToPanel(text, tagsPanel, tagsTextField);
272235
tagsTextField.setText("");
273236
e.consume();
274237
}
238+
} else if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE) {
239+
autoCompletePopup.setVisible(false);
275240
}
276241
}
277242
});
@@ -307,12 +272,12 @@ public void keyPressed(java.awt.event.KeyEvent e) {
307272
JBScrollPane scrollPane = new JBScrollPane(codeTextArea);
308273
scrollPane.setBorder(BorderFactory.createLineBorder(borderColor, 1, true));
309274
scrollPane.setBackground(inputBackground);
310-
275+
311276
gbc.gridy = 5;
312277
gbc.fill = GridBagConstraints.BOTH;
313278
gbc.weighty = 1;
314279
gbc.insets = new Insets(0, 16, 16, 16);
315-
280+
316281
// 设置首选大小
317282
Dimension preferredSize = new Dimension(500, 400);
318283
scrollPane.setPreferredSize(preferredSize);
@@ -494,4 +459,39 @@ private String getTagsFromPanel(JPanel tagsPanel) {
494459
return tags.toString();
495460
}
496461

462+
private void addTagToPanel(String newTag, JPanel tagsPanel, JTextField tagsTextField) {
463+
newTag = newTag.trim();
464+
if (newTag.isEmpty()) return;
465+
466+
// 检查标签是否已存在
467+
for (Component comp : tagsPanel.getComponents()) {
468+
if (comp instanceof JPanel) {
469+
for (Component subComp : ((JPanel) comp).getComponents()) {
470+
if (subComp instanceof JLabel) {
471+
String existingTag = ((JLabel) subComp).getText();
472+
if (!existingTag.equals("×") && existingTag.trim().equalsIgnoreCase(newTag)) {
473+
return; // 标签已存在,直接返回
474+
}
475+
}
476+
}
477+
}
478+
}
479+
480+
// 创建新标签
481+
Color[] tagColors = {
482+
new Color(88, 157, 246), // 蓝色
483+
new Color(130, 108, 246), // 紫色
484+
new Color(246, 108, 188), // 粉色
485+
new Color(246, 157, 88), // 橙色
486+
new Color(88, 246, 157) // 绿色
487+
};
488+
int tagIndex = tagsPanel.getComponentCount() % tagColors.length;
489+
Color tagColor = tagColors[tagIndex];
490+
491+
JPanel tagPanel = createTagPanel(newTag, tagColor, tagsPanel, tagsTextField);
492+
tagsPanel.add(tagPanel);
493+
tagsPanel.revalidate();
494+
tagsPanel.repaint();
495+
}
496+
497497
}

0 commit comments

Comments
 (0)