View Javadoc
1   package edu.jiangxin.apktoolbox.file.zhconvert;
2   
3   import com.github.houbb.opencc4j.util.ZhConverterUtil;
4   import edu.jiangxin.apktoolbox.swing.extend.FileListPanel;
5   import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
6   import edu.jiangxin.apktoolbox.utils.Constants;
7   import org.apache.commons.io.FileUtils;
8   import org.apache.commons.lang3.StringUtils;
9   
10  import javax.swing.*;
11  import java.awt.*;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  import java.io.File;
15  import java.io.IOException;
16  import java.util.List;
17  import java.util.*;
18  
19  public class ZhConvertPanel extends EasyPanel {
20      private JPanel northPanel;
21  
22      private JSplitPane centerPanel;
23  
24      private FileListPanel fileListPanel;
25  
26      private JTextField suffixTextField;
27  
28      private JCheckBox recursiveCheckBox;
29  
30      private JComboBox comboBox;
31  
32      private JTextField keyText;
33  
34      private JTextField valueText;
35  
36      private JTextArea textArea;
37  
38      private JList transformList;
39  
40      private static ZHConverterUtils myZHConverterUtils = new ZHConverterUtils();
41  
42      public ZhConvertPanel() throws HeadlessException {
43          super();
44      }
45  
46      @Override
47      public void initUI() {
48          // JSplitPanel只能用BorderLayout,JFrame默认是BorderLayout,JPanel默认是BoxLayout
49          BorderLayout boxLayout = new BorderLayout();
50          setLayout(boxLayout);
51  
52          createNorthPanel();
53          add(northPanel, BorderLayout.NORTH);
54  
55          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
56  
57          createCenterPanel();
58          add(centerPanel, BorderLayout.CENTER);
59      }
60  
61      private void createNorthPanel() {
62          northPanel = new JPanel();
63          northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.Y_AXIS));
64  
65          fileListPanel = new FileListPanel();
66          northPanel.add(fileListPanel);
67          northPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
68  
69          JPanel optionPanel = new JPanel();
70          optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
71          northPanel.add(optionPanel);
72  
73          JLabel suffixLabel = new JLabel("Suffix:");
74          suffixTextField = new JTextField();
75          suffixTextField.setToolTipText("an array of extensions, ex. {\"java\",\"xml\"}. If this parameter is empty, all files are returned.");
76          suffixTextField.setText(conf.getString("osconvert.suffix"));
77          optionPanel.add(suffixLabel);
78          optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
79          optionPanel.add(suffixTextField);
80          optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
81  
82          recursiveCheckBox = new JCheckBox("Recursive");
83          recursiveCheckBox.setSelected(true);
84          optionPanel.add(recursiveCheckBox);
85          optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
86  
87          comboBox = new JComboBox();
88          comboBox.addItem(Constants.zhSimple2zhTw);
89          comboBox.addItem(Constants.zhTw2zhSimple);
90          optionPanel.add(comboBox);
91  
92          JButton convertBtn = new JButton("确认转换");
93          convertBtn.addActionListener(new ConvertBtnActionListener());
94          northPanel.add(convertBtn);
95  
96          JFileChooser fileChooser = new JFileChooser();
97          fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
98      }
99  
100     private void createCenterPanel() {
101         JPanel centerLeftTopPanel = new JPanel();
102         centerLeftTopPanel.setLayout(new BoxLayout(centerLeftTopPanel, BoxLayout.Y_AXIS));
103 
104         JPanel keyValuePanel = new JPanel();
105         keyValuePanel.setLayout(new BoxLayout(keyValuePanel, BoxLayout.X_AXIS));
106         centerLeftTopPanel.add(keyValuePanel);
107 
108         keyText = new JTextField(10);
109         keyValuePanel.add(keyText);
110 
111         valueText = new JTextField(10);
112         keyValuePanel.add(valueText);
113 
114         JButton saveBtn = new JButton("添加词组定义");
115         saveBtn.addActionListener(new SaveBtnActionListener());
116         centerLeftTopPanel.add(saveBtn);
117 
118         JScrollPane centerLeftBottomPanel = new JScrollPane();
119         textArea = new JTextArea();
120         textArea.setMargin(new Insets(10, 10, 10, 10));
121         //自动换行
122         textArea.setLineWrap(true);
123         textArea.setEditable(false);
124         centerLeftBottomPanel.add(textArea);
125 
126         //垂直滚动条自动出现
127         centerLeftBottomPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
128         JSplitPane centerLeftSplitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, centerLeftTopPanel, centerLeftBottomPanel);
129 
130         transformList = new JList();
131         refreshListData();
132         transformList.setFont(new Font("Dialog", 1, 18));
133         transformList.setBorder(BorderFactory.createTitledBorder("词组转换定义"));
134         JScrollPane centerRightScrollPanel = new JScrollPane(transformList);
135 
136         centerPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerLeftSplitPanel, centerRightScrollPanel);
137         centerPanel.setDividerLocation(0.7f);
138     }
139 
140     private final class ConvertBtnActionListener implements ActionListener {
141         @Override
142         public void actionPerformed(ActionEvent e) {
143             new Thread(() -> {
144                 String converType = comboBox.getSelectedItem().toString();
145                 System.out.println(converType);
146 
147                 List<File> fileList = new ArrayList<>();
148                 for (File file : fileListPanel.getFileList()) {
149                     String[] extensions = null;
150                     if (StringUtils.isNotEmpty(suffixTextField.getText())) {
151                         extensions = suffixTextField.getText().split(",");
152                     }
153                     fileList.addAll(FileUtils.listFiles(file, extensions, recursiveCheckBox.isSelected()));
154                 }
155                 Set<File> fileSet = new TreeSet<>(fileList);
156                 fileList.clear();
157                 fileList.addAll(fileSet);
158 
159                 textArea.setCaretPosition(textArea.getText().length());
160                 try {
161                     scanFolderAndConver(fileList, converType, textArea);
162                     JOptionPane.showMessageDialog(getFrame().getFrames()[0], "转换成功" , "提示",JOptionPane.WARNING_MESSAGE);
163                     textArea.append("done..." + "\n");
164                     textArea.setCaretPosition(textArea.getText().length());
165                 } catch (IOException e1) {
166                     JOptionPane.showMessageDialog(getFrame().getFrames()[0], "异常:" + e1.getMessage(), "异常",JOptionPane.ERROR_MESSAGE);
167                     textArea.append("转换异常..." + "\n");
168                     textArea.setCaretPosition(textArea.getText().length());
169                 }
170             }).start();
171         }
172     }
173 
174     private final class SaveBtnActionListener implements ActionListener {
175         @Override
176         public void actionPerformed(ActionEvent e) {
177             String key = keyText.getText();
178             String value = valueText.getText();
179 
180             if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)){
181                 myZHConverterUtils.storeDataToProperties(key,value);
182                 JOptionPane.showMessageDialog(ZhConvertPanel.this, "成功插入一条词组对应信息", "提示",JOptionPane.WARNING_MESSAGE);
183                 refreshListData();
184                 textArea.append("成功插入一条词组对应信息:" + key + " <===> " + value + "\n");
185             }else{
186                 JOptionPane.showMessageDialog(ZhConvertPanel.this, "键值对不能为空", "提示",JOptionPane.WARNING_MESSAGE);
187             }
188         }
189     }
190 
191     private void refreshListData(){
192         java.util.List<String> listModel = new ArrayList<>();
193         Properties properties = myZHConverterUtils.getCharMap();
194         for (String key2 : properties.stringPropertyNames()) {
195             listModel.add(key2 + " <===> " + properties.getProperty(key2));
196         }
197         transformList.setListData(listModel.toArray());
198     }
199 
200     private static void scanFolderAndConver(List<File> fileList, String converType, JTextArea jTextArea) throws IOException {
201         jTextArea.append("文件转换开始:\n");
202         for (File file : fileList){
203             jTextArea.append("开始转换:"+file + "\n");
204             String content = FileUtils.readFileToString(file, "utf-8");
205 
206             if (converType.equals(Constants.zhSimple2zhTw)){
207                 String str = myZHConverterUtils.myConvertToTW(content);
208                 String result = ZhConverterUtil.toTraditional(str);
209                 FileUtils.write(file,result,"UTF-8");
210             }else{
211                 String str = myZHConverterUtils.myConvertToSimple(content);
212                 String result = ZhConverterUtil.toSimple(str);
213                 FileUtils.write(file,result,"UTF-8");
214             }
215             jTextArea.append("转换完成:"+file + "\n");
216             jTextArea.setCaretPosition(jTextArea.getText().length());
217         }
218         jTextArea.append("文件转换结束:\n");
219         jTextArea.append("==========================================================================:\n");
220     }
221 }