-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserEditMenu.java
More file actions
255 lines (227 loc) · 8.38 KB
/
UserEditMenu.java
File metadata and controls
255 lines (227 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package japanese;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Collections;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class UserEditMenu extends JFrame{
JFrame choose = new JFrame("chose your level");
static LinkedList<Slova> slovv = new LinkedList<>();
UserEditMenu(){
super("chose your words");
setVisible(false);
choose.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int locationX = (screenSize.width - 300) / 2;
int locationY = (screenSize.height - 200) / 2;
choose.setBounds(locationX, locationY,250,80);
Container container = choose.getContentPane();
container.setLayout(new FlowLayout());
JButton n2 = new JButton("N2");
JButton n3 = new JButton("N3");
JButton n4 = new JButton("N4");
JButton n5 = new JButton("N5");
container.add(n2);
container.add(n3);
container.add(n4);
container.add(n5);
n4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser("4");
}
});
n3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser("3");
}
});
n2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser("2");
}
});
n5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser("5");
}
});
}
public void chooser(String option){
try{
reader("japanese/slova"+ option + ".xml");
}catch(Exception ee){}
String[] colle = new String[slovv.size()];
int i = 0;
for (Slova col : slovv){
colle[i] = col.getKanji() + " " + col.getImi() + " " + col.getYomi();
i++;
}
CheckBoxGroup group = new CheckBoxGroup(option, colle);
add(group);
setSize(300, 300);
choose.setVisible(false);
}
public static void reader(String put) throws Exception{
LinkedList<Slova> slova = new LinkedList<>();
slovv.clear();
try{
Reader unicodeFileReader = new InputStreamReader(new FileInputStream(put), "UTF-8");
StringBuilder out = new StringBuilder();
char[] buf = new char[10000];
int rsz = unicodeFileReader.read(buf, 0, buf.length);
out.append(buf, 0, rsz);
String[] opa = out.toString().split("\n");
for(int i = 0; i<opa.length ; i++){
String line = opa[i];
if(line.contains("<Slova ")){
String[] elem = line.split(" ");
String[] elemnew = line.split("\"");
String imi = "abstractt";
String yomi = "abstractt";
String kanji = "abstractt";
imi = elemnew[1];
yomi = elemnew[3];
kanji = elemnew[5];
slova.add(new Slova(imi, yomi, kanji));
}
}
}
catch(IOException e){
System.out.println("не найден файл");
}
slovv.addAll(slova);
}
}
class CheckBoxGroup extends JPanel {
private JCheckBox all;
private List<JCheckBox> checkBoxes;
ArrayList<String> userEdit = new ArrayList<String>();
public ArrayList<String> getColl(){
return this.userEdit;
}
public CheckBoxGroup(String option,String... options) {
checkBoxes = new ArrayList<>(25);
setLayout(new BorderLayout());
JPanel header = new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 1));
all = new JCheckBox("Выбрать все");
all.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JCheckBox cb : checkBoxes) {
cb.setFont(Japanese.japFt);
cb.setSelected(all.isSelected());
}
}
});
header.add(all);
add(header, BorderLayout.NORTH);
JPanel content = new ScrollablePane(new GridBagLayout());
content.setBackground(UIManager.getColor("List.background"));
if (options.length > 0) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
for (int index = 0; index < options.length - 1; index++) {
JCheckBox cb = new JCheckBox(options[index]);
final String str = options[index];
cb.setOpaque(false);
checkBoxes.add(cb);
content.add(cb, gbc);
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(cb.isSelected()){
System.out.println(str);
userEdit.add(str);
System.out.println(userEdit);
try{
save(userEdit,option);
}catch(IOException ex){}
}
else{
userEdit.remove(str);
}
}
});}
JCheckBox cb = new JCheckBox(options[options.length - 1]);
cb.setOpaque(false);
checkBoxes.add(cb);
gbc.weighty = 1;
content.add(cb, gbc);
}
add(new JScrollPane(content));
}
public void save(ArrayList<String> opt, String option) throws IOException
{
String userPort = "japanese/"+"userEditor"+ option +".xml";
BufferedWriter pw = new BufferedWriter(new FileWriter(userPort));
try{
pw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
pw.write("<Slova>\n");
opt.stream().sorted().forEach(p -> {
String[] ayayaya = p.split(" ");
String[] strtr = new String[ayayaya.length-2];
for(int i = 1; i<(ayayaya.length-1); i++){
strtr[i-1] = ayayaya[i];
}
String fin = String.join(" ",strtr);
try{
pw.write("<Slova imi=\"" + fin + "\" yomi=\"" + ayayaya[ayayaya.length-1] + "\" kanji=\"" + ayayaya[0] + "\"/>\n");
}catch(IOException aiai){aiai.printStackTrace();}
});
pw.write("</Slova>");
pw.close();
}catch(IOException oioi){oioi.printStackTrace();}
}
public class ScrollablePane extends JPanel implements Scrollable {
public ScrollablePane(LayoutManager layout) {
super(layout);
}
public ScrollablePane() {
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(100, 100);
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 32;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 32;
}
@Override
public boolean getScrollableTracksViewportWidth() {
boolean track = false;
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport vp = (JViewport) parent;
track = vp.getWidth() > getPreferredSize().width;
}
return track;
}
@Override
public boolean getScrollableTracksViewportHeight() {
boolean track = false;
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport vp = (JViewport) parent;
track = vp.getHeight() > getPreferredSize().height;
}
return track;
}
}
}