1 package edu.jiangxin.apktoolbox.convert.relationship; 2 3 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel; 4 import org.apache.commons.lang3.StringUtils; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.io.Serial; 11 import java.util.HashMap; 12 import java.util.Map; 13 import java.util.Stack; 14 15 public class RelationShipConvertPanel extends EasyPanel { 16 @Serial 17 private static final long serialVersionUID = 1L; 18 19 private static final Map<String, Map<String, String>> dataMap = new HashMap<>(); 20 21 private String now = "你"; 22 23 private JTextField inputTextField; 24 private JTextField outputTextField; 25 26 private final Stack<String> stack = new Stack<>(); 27 28 static { 29 dataMap.put("不存在", Map.of("父", "不存在", "母", "不存在", "兄", "不存在", "弟", "不存在", "姐", "不存在", "妹", "不存在", "夫", "不存在", "妻", "不存在", "儿", "不存在", "女", "不存在")); 30 dataMap.put("你", Map.of("父", "爸爸", "母", "妈妈", "兄", "哥哥", "弟", "弟弟", "姐", "姐姐", "妹", "妹妹", "夫", "老公", "妻", "老婆", "儿", "儿子", "女", "女儿")); 31 dataMap.put("爸爸", Map.of("父", "爷爷", "母", "奶奶", "兄", "伯伯", "弟", "叔叔", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "妈妈", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 32 dataMap.put("妈妈", Map.of("父", "姥爷", "母", "姥姥", "兄", "舅舅", "弟", "舅舅", "姐", "大姨", "妹", "小姨", "夫", "爸爸", "妻", "不存在", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 33 dataMap.put("哥哥", Map.of("父", "爸爸", "母", "妈妈", "兄", "哥哥", "弟", "<哥哥><弟弟>", "姐", "姐姐", "妹", "<姐姐><妹妹>", "夫", "不存在", "妻", "嫂子", "儿", "侄子", "女", "侄女")); 34 dataMap.put("弟弟", Map.of("父", "爸爸", "母", "妈妈", "兄", "<哥哥><弟弟>", "弟", "弟弟", "姐", "<姐姐><妹妹>", "妹", "妹妹", "夫", "不存在", "妻", "弟媳", "儿", "侄子", "女", "侄女")); 35 dataMap.put("姐姐", Map.of("父", "爸爸", "母", "妈妈", "兄", "哥哥", "弟", "<哥哥><弟弟>", "姐", "姐姐", "妹", "<姐姐><妹妹>", "夫", "姐夫", "妻", "不存在", "儿", "外甥", "女", "外甥女")); 36 dataMap.put("妹妹", Map.of("父", "爸爸", "母", "妈妈", "兄", "<哥哥><弟弟>", "弟", "弟弟", "姐", "<姐姐><妹妹>", "妹", "妹妹", "夫", "妹夫", "妻", "不存在", "儿", "外甥", "女", "外甥女")); 37 dataMap.put("老婆", Map.of("父", "岳父", "母", "岳母", "兄", "大舅", "弟", "小舅", "姐", "大姨", "妹", "小姨", "夫", "你", "妻", "不存在", "儿", "儿子", "女", "女儿")); 38 dataMap.put("儿子", Map.of("父", "你", "母", "老婆", "兄", "儿子", "弟", "儿子", "姐", "女儿", "妹", "女儿", "夫", "不存在", "妻", "儿媳", "儿", "孙子", "女", "孙女")); 39 dataMap.put("女儿", Map.of("父", "你", "母", "老婆", "兄", "儿子", "弟", "儿子", "姐", "女儿", "妹", "女儿", "夫", "女婿", "妻", "不存在", "儿", "外孙", "女", "外孙女")); 40 dataMap.put("伯伯", Map.of("父", "爷爷", "母", "奶奶", "兄", "伯伯", "弟", "<伯伯><叔叔>", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "婶婶", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 41 dataMap.put("叔叔", Map.of("父", "爷爷", "母", "奶奶", "兄", "<伯伯><叔叔>", "弟", "叔叔", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "婶婶", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 42 dataMap.put("姑姑", Map.of("父", "爷爷", "母", "奶奶", "兄", "<伯伯><叔叔>", "弟", "<伯伯><叔叔>", "姐", "姑姑", "妹", "姑姑", "夫", "姑父", "妻", "不存在", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 43 dataMap.put("岳父", Map.of("父", "爷爷", "母", "奶奶", "兄", "伯伯", "弟", "叔叔", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "岳母", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 44 dataMap.put("岳母", Map.of("父", "姥爷", "母", "姥姥", "兄", "舅舅", "弟", "舅舅", "姐", "大姨", "妹", "小姨", "夫", "岳父", "妻", "不存在", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 45 } 46 47 @Override 48 public void initUI() { 49 setLayout(new GridBagLayout()); 50 GridBagConstraints gbc = new GridBagConstraints(); 51 52 inputTextField = new MyTextField(); 53 54 gbc.insets = new Insets(5, 5, 5, 5); 55 gbc.gridx = 0; 56 gbc.gridy = 0; 57 gbc.gridwidth = 4; 58 gbc.fill = GridBagConstraints.HORIZONTAL; 59 add(inputTextField, gbc); 60 61 outputTextField = new MyTextField(); 62 outputTextField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 63 64 gbc.gridy = 1; 65 add(outputTextField, gbc); 66 67 ButtonListener btnListener = new ButtonListener(); 68 69 JButton fatherButton = new MyButton("父"); 70 fatherButton.addActionListener(btnListener); 71 72 gbc.gridx = 0; 73 gbc.gridy = 2; 74 gbc.gridwidth = 1; 75 add(fatherButton, gbc); 76 77 JButton motherButton = new MyButton("母"); 78 motherButton.addActionListener(btnListener); 79 80 gbc.gridx = 1; 81 add(motherButton, gbc); 82 83 JButton backButton = new MyButton("←"); 84 backButton.addActionListener(btnListener); 85 86 gbc.gridx = 2; 87 add(backButton, gbc); 88 89 JButton acButton = new MyButton("AC"); 90 acButton.addActionListener(btnListener); 91 92 gbc.gridx = 3; 93 add(acButton, gbc); 94 95 JButton brotherButton = new MyButton("兄"); 96 brotherButton.addActionListener(btnListener); 97 98 gbc.gridx = 0; 99 gbc.gridy = 3; 100 add(brotherButton, gbc); 101 102 JButton littleBrotherButton = new MyButton("弟"); 103 littleBrotherButton.addActionListener(btnListener); 104 105 gbc.gridx = 1; 106 add(littleBrotherButton, gbc); 107 108 JButton husbandButton = new MyButton("夫"); 109 husbandButton.addActionListener(btnListener); 110 111 gbc.gridx = 2; 112 add(husbandButton, gbc); 113 114 JButton obligateButton = new MyButton(" "); 115 obligateButton.setEnabled(false); 116 117 gbc.gridx = 3; 118 add(obligateButton, gbc); 119 120 JButton sisterButton = new MyButton("姐"); 121 sisterButton.addActionListener(btnListener); 122 123 gbc.gridx = 0; 124 gbc.gridy = 4; 125 add(sisterButton, gbc); 126 127 JButton littleSisterButton = new MyButton("妹"); 128 littleSisterButton.addActionListener(btnListener); 129 130 gbc.gridx = 1; 131 add(littleSisterButton, gbc); 132 133 JButton wifeButton = new MyButton("妻"); 134 wifeButton.addActionListener(btnListener); 135 136 gbc.gridx = 2; 137 add(wifeButton, gbc); 138 139 JButton deButton = new MyButton("的"); 140 deButton.addActionListener(btnListener); 141 142 gbc.gridx = 3; 143 add(deButton, gbc); 144 145 JButton sonButton = new MyButton("儿"); 146 sonButton.addActionListener(btnListener); 147 148 gbc.gridx = 0; 149 gbc.gridy = 5; 150 add(sonButton, gbc); 151 152 JButton daughterbButton = new MyButton("女"); 153 daughterbButton.addActionListener(btnListener); 154 155 gbc.gridx = 1; 156 add(daughterbButton, gbc); 157 158 JButton resultButton = new MyButton("="); 159 resultButton.addActionListener(btnListener); 160 161 gbc.gridx = 2; 162 gbc.gridwidth = 2; 163 add(resultButton, gbc); 164 } 165 166 static class MyTextField extends JTextField { 167 @Serial 168 private static final long serialVersionUID = 1L; 169 170 public MyTextField() { 171 super(); 172 Font font = new Font("宋体", Font.BOLD, 15); 173 setFont(font); 174 setEditable(false); 175 } 176 } 177 178 static class MyButton extends JButton { 179 @Serial 180 private static final long serialVersionUID = 1L; 181 182 public MyButton(String text) { 183 super(text); 184 Font font = new Font("宋体", Font.BOLD, 30); 185 setFont(font); 186 } 187 } 188 189 class ButtonListener implements ActionListener { 190 public void actionPerformed(ActionEvent e) { 191 String which = ((JButton) e.getSource()).getText(); 192 switch (which) { 193 case "AC" -> { 194 now = "你"; 195 inputTextField.setText(""); 196 outputTextField.setText(""); 197 } 198 case "=" -> outputTextField.setText(now); 199 case "←" -> { 200 String inputText = inputTextField.getText(); 201 String removedText = null; 202 if (inputText != null && inputText.length() >= 1) { 203 inputTextField.setText(inputText.substring(0, inputText.length() - 1)); 204 removedText = inputText.substring(inputText.length() - 1); 205 } 206 if (!stack.isEmpty() && isRelationString(removedText)) { 207 now = stack.pop(); 208 outputTextField.setText(now); 209 } 210 } 211 case "的" -> inputTextField.setText(inputTextField.getText() + which); 212 default -> { 213 inputTextField.setText(inputTextField.getText() + which); 214 stack.push(now); 215 String result = getRelation(now, which); 216 if (isPossibleRelation(result)) { 217 result = getExactRelation(result); 218 } 219 if (result != null) { 220 now = result; 221 outputTextField.setText(now); 222 } else { 223 outputTextField.setText("超出计算范围"); 224 } 225 } 226 } 227 } 228 } 229 230 private static boolean isRelationString(String relationString) { 231 return relationString != null && "父母兄弟姐妹夫妻儿女".contains(relationString); 232 } 233 234 private static String getRelation(String now, String what) { 235 Map<String, String> relationsMap = dataMap.get(now); 236 if (relationsMap == null) { 237 return null; 238 } 239 return relationsMap.get(what); 240 } 241 242 private static boolean isPossibleRelation(String possibleRelation) { 243 return possibleRelation != null && possibleRelation.charAt(0) == '<'; 244 } 245 246 private static String getExactRelation(String possibleRelation) { 247 String[] tmp = StringUtils.substringsBetween(possibleRelation, "<", ">"); 248 if (tmp != null && tmp.length == 2) { 249 String message = "是否比"; 250 if ("伯伯".equals(tmp[0])) { 251 message += "爸爸"; 252 } else if ("姐姐".equals(tmp[0]) || "哥哥".equals(tmp[0])) { 253 message += "你"; 254 } 255 message += "大?"; 256 int userChoose = JOptionPane.showConfirmDialog(null, message, "提示", JOptionPane.YES_NO_OPTION); 257 boolean isOlder = userChoose == JOptionPane.YES_OPTION; 258 return isOlder ? tmp[0] : tmp[1]; 259 } else { 260 return null; 261 } 262 } 263 }