-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigWindow.java
More file actions
221 lines (195 loc) · 5.02 KB
/
ConfigWindow.java
File metadata and controls
221 lines (195 loc) · 5.02 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
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ConfigWindow extends JFrame implements ActionListener
{
private final String FILE = "data/options.cfg";
private final String ADD = "Create";
private final String REPLACE = "Replace";
private JScrollPane jsp;
private ConfigList list;
private JTextField key, value;
private JButton jb;
private int lastIndex = 0;
private static final ConfigWindow instance = new ConfigWindow();
public ConfigWindow()
{
setSize(400,400);
setLocation(200,200);
setTitle("Config file editor");
Config.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
setJMenuBar(new JMenuBar());
JMenu options = new JMenu("Options");
JMenuItem sort = new JMenuItem("Alphabetical Sort");
JMenuItem def = new JMenuItem("Actual File");
JMenuItem reload = new JMenuItem("Reload File");
sort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
list.sort();
}
});
def.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
list.unsort();
}
});
reload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
list.reload();
}
});
options.add(def);
options.add(sort);
options.add(reload);
getJMenuBar().add(options);
JPanel inputs = new JPanel(new BorderLayout());
jb = makeButton();
inputs.add(jb, BorderLayout.EAST);
JPanel labels = new JPanel(new GridLayout(2,1));
labels.add(new JLabel("Key:"));
labels.add(new JLabel("Value:"));
JPanel buffer = new JPanel(new BorderLayout());
buffer.add(GUtil.filler(10), BorderLayout.WEST);
buffer.add(GUtil.filler(10), BorderLayout.EAST);
buffer.add(labels, BorderLayout.CENTER);
inputs.add(buffer, BorderLayout.WEST);
JPanel fields = new JPanel(new GridLayout(2,1));
key = new JTextField();
value = new JTextField();
ActionListener ae = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jb.doClick();
}
};
key.addActionListener(ae);
value.addActionListener(ae);
key.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
list.goTo(key.getText());
}
});
fields.add(key);
fields.add(value);
inputs.add(fields, BorderLayout.CENTER);
getContentPane().add(inputs, BorderLayout.SOUTH);
list = new ConfigList();
jsp = new JScrollPane();// do stuff
jsp.getViewport().add(list);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public static void showWindow()
{
instance.show();
}
private JButton makeButton()
{
JButton temp = new JButton(ADD);
temp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
lastIndex = list.getSelectedIndex();
if(Config.write(key.getText(), value.getText()))
clear();
list.setSelectedIndex(lastIndex);
}
});
return temp;
}
private void clear()
{
key.setText("");
value.setText("");
jb.setText(ADD);
}
private void set(String s)
{
StringTokenizer st = new StringTokenizer(s);
String temp = st.nextToken();
key.setText(temp);
StringBuffer sb = new StringBuffer();
while(st.hasMoreTokens())
sb.append(st.nextToken()).append(' ');
value.setText(sb.toString().trim());
jb.setText(REPLACE);
}
public void actionPerformed(ActionEvent ae)
{
list.reload();
}
/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
private class ConfigList extends JList
{
Object[] data;
private boolean sorted;
public ConfigList()
{
sorted = false;
addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent l) {
String s = (String) getSelectedValue();
if(s == null)
return;
s = s.trim();
if(s.length() == 0 || s.indexOf("//") == 0)
clear();
else
set(s);
}
});
reload();
}
public void sort()
{
ArrayList temp = new ArrayList();
for(int i = 0; i < data.length; i++)
{
String s = ((String) data[i]).trim();
if(s.indexOf("//") != 0 && s.trim().length() > 0)
temp.add(s);
}
Collections.sort(temp);
setListData(temp.toArray());
sorted = true;
}
public void goTo(String s)
{
String out = Config.read(s);
if(out != null)
{
value.setText(out);
jb.setText(REPLACE);
}
else
jb.setText(ADD);
}
public void unsort()
{
setListData(data);
sorted = false;
}
public void reload()
{
try
{
BufferedReader br = new BufferedReader(new FileReader(FILE));
ArrayList temp = new ArrayList();
while(br.ready())
temp.add(br.readLine());
setListData(data = temp.toArray());
}
catch(Exception e) {}
if(sorted)
sort();
}
}
/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
public static void main(String[] args)
{
new ConfigWindow().show();
}
}