-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConfig.java
More file actions
351 lines (310 loc) · 10.7 KB
/
ServerConfig.java
File metadata and controls
351 lines (310 loc) · 10.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package speedmatch;
import javax.swing.*;
import javax.swing.SwingWorker.StateValue;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.net.InetAddress;
public class ServerConfig extends JFrame implements PropertyChangeListener {
private String[] eventarr = new String[0];
private int lselect = -1; // Selected Item Index
private DBConnect data;
private int connected = 0;
private String ip;
private Task task;
private int index = -99;
private InetAddress ca;
private ProgressMonitor pm;
private DefaultListModel<String> dlm = new DefaultListModel<String>();
private JButton add = new JButton("Add new Event");
public ServerConfig() {
data = new DBConnect();
initGUI();
}
public ServerConfig(DBConnect d) {
data = d;
this.connected = 1;
initGUI();
setArr(data);
refreshList();
add.setEnabled(true);
}
class Task extends SwingWorker<Void, Void> {
public Void doInBackground() {
try {
setProgress(0);
ip = DBTools.getLocalIP();
InetAddress[] ia = new InetAddress[255];
int counter = 0;
double pr = 0;
InetAddress local = InetAddress.getByName(ip);
byte[] local_ip = local.getAddress();
for (int i = 1; i <= 254; i++) {
local_ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(local_ip);
// setNote("Pinging " + address.getHostAddress());
if (address.isReachable(20)) {
ia[counter] = address;
counter++;
} else if (!address.getHostAddress().equals(address.getHostName())) {
ia[counter] = address;
counter++;
}
pr += 0.365; // CAUTION: Magic number
setProgress((int) pr);
}
Object[] obj = new Object[counter];
setProgress(99);
for (int j = 0; j < counter; j++) {
obj[j] = j + ": " + ia[j].getHostAddress() + " - " + ia[j].getHostName();
}
String select = (String) JOptionPane.showInputDialog(null,
"SpeedMatch detected " + counter
+ " devices on your network. Please select the one running your Database",
"Select device", JOptionPane.QUESTION_MESSAGE, null, obj, obj[0]);
index = Character.getNumericValue(select.charAt(0));
if (index >= 0) {
ca = ia[index];
}
setProgress(100);
return null;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "An error occured while searching for server: " + e.getMessage(),
"NETWORK ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
}
}
class conncect implements Runnable {
public void run() {
while (!(task.getState().equals(StateValue.DONE))) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// Ignore
}
}
if (index >= 0) {
data.initConnection(ca);
setArr(data);
if (eventarr != null) {
refreshList();
connected = 1;
add.setEnabled(true);
}
}
}
}
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
pm.setProgress(Math.min(progress, 100));
String message = String.format("Completed %d%%.\n", progress);
pm.setNote(message);
}
}
public void setArr(DBConnect d) {
try {
this.eventarr = d.getEventsFromDB();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error while retrieving events: " + e.getMessage(), "ERROR",
JOptionPane.ERROR_MESSAGE);
dlm.addElement("---PLEASE CONNECT TO A DATABASE SERVER---");
}
}
public void refreshList() {
dlm.removeAllElements();
for (int i = 0; i < eventarr.length; i++) {
dlm.addElement(eventarr[i]);
}
}
public void initGUI() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false);
this.getContentPane().add(panel);
this.getContentPane().setBackground(new Color(255, 229, 204));
setExtendedState(JFrame.MAXIMIZED_BOTH);
// Title and Subtitle
JLabel title = new JLabel("Server Configuration");
title.setForeground(new Color(250, 92, 92));
title.setFont(new Font("Arial", Font.BOLD, 40));
JLabel subtitle = new JLabel("To Begin, Please Select an Existing Event or Create a New One");
subtitle.setForeground(new Color(250, 92, 92));
subtitle.setFont(new Font("Arial", Font.BOLD, 25));
// Seperator
JSeparator sep = new JSeparator();
sep.setPreferredSize(new Dimension(500, 20));
// Labels
JLabel label = new JLabel("Events available on server");
label.setForeground(new Color(250, 92, 92));
label.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 15));
// Buttons to change page or exit
JPanel pagePanel = new JPanel();
pagePanel.setOpaque(false);
JButton back = new JButton("Back to standard mode");
pagePanel.add(back);
JButton next = new JButton("Next >");
pagePanel.add(next);
next.setEnabled(false);
JButton exit = new JButton("Exit");
pagePanel.add(exit);
// Buttons to create or select events
JPanel eventPanel = new JPanel();
eventPanel.setOpaque(false);
JButton connect = new JButton("Connect to Server");
eventPanel.add(connect);
eventPanel.add(add);
add.setEnabled(false);
JButton del = new JButton("Delete");
eventPanel.add(del);
del.setEnabled(false);
// List of Events
JList<String> list = new JList<String>(dlm);
dlm.addElement("---PLEASE CONNECT TO A DATABASE SERVER---");
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Action Listener for List
ListSelectionModel lsm = list.getSelectionModel();
lsm.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
lselect = list.getSelectedIndex();
if (connected == 1) {
next.setEnabled(true);
del.setEnabled(true);
}
}
}
});
// Action Listener for Server Connection
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ip = JOptionPane.showInputDialog(null,
"Enter IP or Hostname of Server" + Processing.getLineSep()
+ "Leave empty to automatically search for servers on local network",
"Connect to Database", JOptionPane.PLAIN_MESSAGE);
if (ip == null) {
// User pressed "Cancel" -> Do nothing
} else if (ip.equals("")) {
// dbase.initConnection();
JOptionPane.showMessageDialog(null,
"SpeedMatch is now searching for devices on the network. This may take up to 2 Minutes. Please stand by.",
"Searching...", JOptionPane.INFORMATION_MESSAGE);
pm = new ProgressMonitor(ServerConfig.this, "Searching for Servers", "Searching...", 0, 100);
pm.setProgress(0);
task = new Task();
task.addPropertyChangeListener(ServerConfig.this);
task.execute();
Thread t = new Thread(new conncect());
t.start();
} else {
ca = InetAddress.getByName(ip);
data.initConnection(ca);
setArr(data);
if (eventarr != null) {
refreshList();
connected = 1;
add.setEnabled(true);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Unable to connect to server. " + ex.getMessage());
}
}
});
// Action Listener for Exit
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (connected == 1) {
data.close();
}
dispose();
}
});
// ActionListener for Back
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (connected == 1) {
data.close();
}
MainDesign md = new MainDesign();
md.setVisible(true);
dispose();
}
});
// Action Listener for Next
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
NetPartPage np = new NetPartPage(eventarr[lselect], data);
np.setVisible(true);
dispose();
}
});
// Action Listener for Add
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
NewDiscEvent nde = new NewDiscEvent(data, ServerConfig.this);
nde.setVisible(true);
}
});
// Action Listener delete
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int askdelete = JOptionPane.showConfirmDialog(ServerConfig.this,
"Are you sure you want to permanently delete " + eventarr[lselect] + "? This cannot be undone.",
"Delete Speeddating", JOptionPane.INFORMATION_MESSAGE);
if (askdelete == JOptionPane.YES_OPTION) {
data.setEventname(eventarr[lselect]);
data.delete();
setArr(data);
refreshList();
}
}
});
// GridBagConstraints
/*
* // Adding GridBagConstraints gbc = new GridBagConstraints(); gbc.fill =
* GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.RELATIVE;
*
* gbc.gridx = 0; gbc.gridy = 0; gbc.ipady = 20; gbc.anchor =
* GridBagConstraints.PAGE_START; panel.add(title, gbc);
*
* gbc.gridx = 0; gbc.gridy = 1; panel.add(subtitle, gbc);
*
* gbc.gridx = 0; gbc.gridy = 2; panel.add(sep, gbc);
*
* gbc.gridx = 0; gbc.gridy = 3; panel.add(label, gbc);
*
* gbc.gridx = 0; gbc.gridy = 4; panel.add(new JScrollPane(list), gbc);
*
* gbc.gridx = 0; gbc.gridy = 5; gbc.insets = new Insets(10,0,0,0);
* panel.add(eventPanel, gbc);
*
* gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 5; panel.add(pagePanel, gbc);
*
* this.pack();
*
* this.setVisible(true);
*/
panel.add(title, new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0, GridBagConstraints.PAGE_START,
GridBagConstraints.VERTICAL, new Insets(20, 0, 20, 0), 0, 0));
panel.add(subtitle, new GridBagConstraints(1, 1, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(0, 0, 10, 0), 0, 0));
panel.add(sep, new GridBagConstraints(0, 2, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(0, 0, 30, 0), 0, 0));
panel.add(label, new GridBagConstraints(0, 3, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(30, 0, 10, 0), 0, 0));
JScrollPane sp = new JScrollPane(list);
sp.setMinimumSize(new Dimension(300, 200));
panel.add(sp, new GridBagConstraints(0, 4, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(10, 0, 30, 0), 0, 0));
panel.add(eventPanel, new GridBagConstraints(0, 5, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(30, 0, 30, 0), 0, 0));
panel.add(pagePanel, new GridBagConstraints(0, 6, GridBagConstraints.REMAINDER, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(50, 0, 20, 0), 0, 0));
this.pack();
this.setVisible(true);
this.revalidate();
}
}