1 package edu.jiangxin.apktoolbox.file;
2
3 import edu.jiangxin.apktoolbox.file.core.OsPatternConvert;
4 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
5 import edu.jiangxin.apktoolbox.swing.extend.FileListPanel;
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.util.ArrayList;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.TreeSet;
19
20
21
22
23
24
25 public class OsConvertPanel extends EasyPanel {
26 private static final long serialVersionUID = 1L;
27
28 private FileListPanel srcPanel;
29
30 private JPanel optionPanel;
31
32 private JTextField suffixTextField;
33
34 private JCheckBox recursiveCheckBox;
35
36 private JComboBox<String> typeComboBox;
37
38 private JPanel operationPanel;
39
40 public OsConvertPanel() throws HeadlessException {
41 super();
42 }
43
44 @Override
45 public void initUI() {
46 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
47 setLayout(boxLayout);
48
49 createSrcPanel();
50 add(srcPanel);
51 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
52
53 createOptionPanel();
54 add(optionPanel);
55 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
56
57 createOperationPanel();
58 add(operationPanel);
59 }
60
61 private void createOperationPanel() {
62 operationPanel = new JPanel();
63 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
64
65 JButton convertButton = new JButton("Convert");
66 convertButton.addActionListener(new ConvertButtonActionListener());
67
68 operationPanel.add(convertButton);
69 }
70
71 private void createOptionPanel() {
72 optionPanel = new JPanel();
73 optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
74
75 JLabel suffixLabel = new JLabel("Suffix:");
76 suffixTextField = new JTextField();
77 suffixTextField.setToolTipText("an array of extensions, ex. {\"java\",\"xml\"}. If this parameter is empty, all files are returned.");
78 suffixTextField.setText(conf.getString("osconvert.suffix"));
79 optionPanel.add(suffixLabel);
80 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
81 optionPanel.add(suffixTextField);
82 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
83
84 recursiveCheckBox = new JCheckBox("Recursive");
85 recursiveCheckBox.setSelected(true);
86 optionPanel.add(recursiveCheckBox);
87 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
88
89 JLabel typeLabel = new JLabel("Type:");
90 typeComboBox = new JComboBox<>();
91 typeComboBox.addItem("Convert to UNIX(LF Only)");
92 typeComboBox.addItem("Convert to Macintosh(CR Only)");
93 typeComboBox.addItem("Convert to Windows(CR+LF)");
94
95 optionPanel.add(typeLabel);
96 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
97 optionPanel.add(typeComboBox);
98 }
99
100 private void createSrcPanel() {
101 srcPanel = new FileListPanel();
102 }
103
104 private final class ConvertButtonActionListener implements ActionListener {
105 @Override
106 public void actionPerformed(ActionEvent e) {
107 conf.setProperty("osconvert.suffix", suffixTextField.getText());
108 List<File> fileList = new ArrayList<>();
109 for (File file : srcPanel.getFileList()) {
110 String[] extensions = null;
111 if (StringUtils.isNotEmpty(suffixTextField.getText())) {
112 extensions = suffixTextField.getText().split(",");
113 }
114 fileList.addAll(FileUtils.listFiles(file, extensions, recursiveCheckBox.isSelected()));
115 }
116 Set<File> fileSet = new TreeSet<>(fileList);
117 fileList.clear();
118 fileList.addAll(fileSet);
119 OsPatternConvert.osConvertFiles(fileList, convertShowNameToPattern(typeComboBox.getSelectedItem().toString()));
120 logger.info("convert finish");
121 }
122 }
123
124 private String convertShowNameToPattern(String showName) {
125 switch (showName) {
126 case "Convert to UNIX(LF Only)":
127 return "tounix";
128 case "Convert to Macintosh(CR Only)":
129 return "tomac";
130 case "Convert to Windows(CR+LF)":
131 return "towindows";
132 }
133 return "towindows";
134 }
135 }