|
36 | 36 | import com.star.easydoc.common.util.HttpUtil;
|
37 | 37 | import org.apache.commons.io.FileUtils;
|
38 | 38 | import org.apache.commons.lang3.StringUtils;
|
| 39 | +import com.star.easydoc.service.RemoteWordMapService; |
39 | 40 |
|
40 | 41 | /**
|
41 | 42 | * @author wangchao
|
@@ -106,6 +107,12 @@ public class CommonSettingsView {
|
106 | 107 | private JBList<String> projectList;
|
107 | 108 | private JBList<Entry<String, String>> projectTypeMapList;
|
108 | 109 |
|
| 110 | + // 远程单词映射URL相关的UI组件 |
| 111 | + private JLabel remoteWordMapUrlLabel; |
| 112 | + private JTextField remoteWordMapUrlTextField; |
| 113 | + private JButton remoteWordMapTestButton; |
| 114 | + private JButton remoteWordMapImportButton; |
| 115 | + |
109 | 116 | private static final String CUSTOM_HELP_URL
|
110 | 117 | = "https://github.com/starcwang/easy_javadoc/blob/master/doc/%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E.md";
|
111 | 118 |
|
@@ -223,6 +230,10 @@ public CommonSettingsView() {
|
223 | 230 | openaiTestButton.addActionListener(event -> testOpenAiConnection());
|
224 | 231 |
|
225 | 232 | projectList.addListSelectionListener(e -> refreshProjectWordMap());
|
| 233 | + |
| 234 | + // 远程单词映射URL相关的事件监听器 |
| 235 | + remoteWordMapTestButton.addActionListener(event -> testRemoteWordMapUrl()); |
| 236 | + remoteWordMapImportButton.addActionListener(event -> importRemoteWordMap()); |
226 | 237 | }
|
227 | 238 |
|
228 | 239 | private void setVisible(Object selectedItem) {
|
@@ -659,6 +670,29 @@ public void customize(JList list, Entry<String, String> value, int index, boolea
|
659 | 670 | });
|
660 | 671 | wordMapPanel = toolbarDecorator.createPanel();
|
661 | 672 |
|
| 673 | + // 创建远程单词映射URL相关的UI组件 |
| 674 | + remoteWordMapUrlLabel = new JLabel("远程词库:"); |
| 675 | + remoteWordMapUrlTextField = new JTextField(); |
| 676 | + remoteWordMapTestButton = new JButton("测试连接"); |
| 677 | + remoteWordMapImportButton = new JButton("导入映射"); |
| 678 | + |
| 679 | + // 将远程URL组件添加到wordMapPanel |
| 680 | + JPanel remoteUrlPanel = new JPanel(new BorderLayout()); |
| 681 | + JPanel urlInputPanel = new JPanel(new BorderLayout()); |
| 682 | + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 683 | + |
| 684 | + urlInputPanel.add(remoteWordMapUrlLabel, BorderLayout.WEST); |
| 685 | + urlInputPanel.add(remoteWordMapUrlTextField, BorderLayout.CENTER); |
| 686 | + buttonPanel.add(remoteWordMapTestButton); |
| 687 | + buttonPanel.add(remoteWordMapImportButton); |
| 688 | + urlInputPanel.add(buttonPanel, BorderLayout.EAST); |
| 689 | + |
| 690 | + remoteUrlPanel.add(urlInputPanel, BorderLayout.NORTH); |
| 691 | + remoteUrlPanel.add(wordMapPanel, BorderLayout.CENTER); |
| 692 | + |
| 693 | + // 用包含远程URL组件的面板替换原来的wordMapPanel |
| 694 | + wordMapPanel = remoteUrlPanel; |
| 695 | + |
662 | 696 | projectTypeMapList = new JBList<>(new CollectionListModel<>(Lists.newArrayList()));
|
663 | 697 | projectTypeMapList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
664 | 698 | projectTypeMapList.setCellRenderer(new ListCellRendererWrapper<Entry<String, String>>() {
|
@@ -731,6 +765,7 @@ public void refresh() {
|
731 | 765 | setOpenaiTemperatureTextField(config.getOpenaiTemperature() != null ? config.getOpenaiTemperature().toString() : "");
|
732 | 766 | setOpenaiTopKTextField(config.getOpenaiTopK() != null ? config.getOpenaiTopK().toString() : "");
|
733 | 767 | setCustomUrlTextField(config.getCustomUrl());
|
| 768 | + setRemoteWordMapUrlTextField(config.getRemoteWordMapUrl()); |
734 | 769 | refreshWordMap();
|
735 | 770 | projectList.clearSelection();
|
736 | 771 | refreshProjectWordMap();
|
@@ -933,6 +968,81 @@ public void setOpenaiTopKTextField(String openaiTopK) {
|
933 | 968 | this.openaiTopKTextField.setText(openaiTopK);
|
934 | 969 | }
|
935 | 970 |
|
| 971 | + public JTextField getRemoteWordMapUrlTextField() { |
| 972 | + return remoteWordMapUrlTextField; |
| 973 | + } |
| 974 | + |
| 975 | + public void setRemoteWordMapUrlTextField(String remoteWordMapUrl) { |
| 976 | + this.remoteWordMapUrlTextField.setText(remoteWordMapUrl); |
| 977 | + } |
| 978 | + |
| 979 | + /** |
| 980 | + * 测试远程单词映射URL是否可访问 |
| 981 | + */ |
| 982 | + private void testRemoteWordMapUrl() { |
| 983 | + String url = remoteWordMapUrlTextField.getText(); |
| 984 | + if (url == null || url.trim().isEmpty()) { |
| 985 | + showTestResult("远程URL测试", "请先输入远程单词映射URL", false); |
| 986 | + return; |
| 987 | + } |
| 988 | + |
| 989 | + try { |
| 990 | + boolean isValid = RemoteWordMapService.validateRemoteUrl(url); |
| 991 | + if (isValid) { |
| 992 | + showTestResult("远程URL测试", "远程URL连接成功!", true); |
| 993 | + } else { |
| 994 | + showTestResult("远程URL测试", "远程URL连接失败,请检查URL是否正确", false); |
| 995 | + } |
| 996 | + } catch (Exception e) { |
| 997 | + showTestResult("远程URL测试", "测试失败:" + e.getMessage(), false); |
| 998 | + } |
| 999 | + } |
| 1000 | + |
| 1001 | + /** |
| 1002 | + * 从远程URL导入单词映射 |
| 1003 | + */ |
| 1004 | + private void importRemoteWordMap() { |
| 1005 | + String url = remoteWordMapUrlTextField.getText(); |
| 1006 | + if (url == null || url.trim().isEmpty()) { |
| 1007 | + showTestResult("远程导入", "请先输入远程单词映射URL", false); |
| 1008 | + return; |
| 1009 | + } |
| 1010 | + |
| 1011 | + try { |
| 1012 | + Map<String, String> remoteWordMap = RemoteWordMapService.fetchRemoteWordMap(url); |
| 1013 | + if (remoteWordMap.isEmpty()) { |
| 1014 | + showTestResult("远程导入", "未获取到任何单词映射", false); |
| 1015 | + return; |
| 1016 | + } |
| 1017 | + |
| 1018 | + // 将远程映射合并到本地全局映射中 |
| 1019 | + SortedMap<String, String> localWordMap = config.getWordMap(); |
| 1020 | + if (localWordMap == null) { |
| 1021 | + localWordMap = Maps.newTreeMap(); |
| 1022 | + } |
| 1023 | + |
| 1024 | + int overwriteCount = 0; |
| 1025 | + for (Map.Entry<String, String> entry : remoteWordMap.entrySet()) { |
| 1026 | + if (localWordMap.containsKey(entry.getKey())) { |
| 1027 | + overwriteCount++; |
| 1028 | + } |
| 1029 | + localWordMap.put(entry.getKey(), entry.getValue()); |
| 1030 | + } |
| 1031 | + |
| 1032 | + config.setWordMap(localWordMap); |
| 1033 | + refreshWordMap(); |
| 1034 | + |
| 1035 | + String message = String.format("成功导入 %d 个单词映射", remoteWordMap.size()); |
| 1036 | + if (overwriteCount > 0) { |
| 1037 | + message += String.format(",覆盖了 %d 个已存在的映射", overwriteCount); |
| 1038 | + } |
| 1039 | + showTestResult("远程导入", message, true); |
| 1040 | + |
| 1041 | + } catch (Exception e) { |
| 1042 | + showTestResult("远程导入", "导入失败:" + e.getMessage(), false); |
| 1043 | + } |
| 1044 | + } |
| 1045 | + |
936 | 1046 | private void testOpenAiConnection() {
|
937 | 1047 | try {
|
938 | 1048 | // 获取当前配置
|
|
0 commit comments