summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetrulesConfigurator.java
blob: 3284091335b5e2d17d7eece67d28ff5fa700d80a (plain) (blame)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package org.openslx.dozmod.gui.configurator;

import java.awt.Color;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.LectureRead;
import org.openslx.bwlp.thrift.iface.NetDirection;
import org.openslx.bwlp.thrift.iface.NetRule;
import org.openslx.bwlp.thrift.iface.PresetNetRule;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.changemonitor.GenericControlWindow;
import org.openslx.dozmod.gui.configurator.NetrulesConfigurator.StateWrapper;
import org.openslx.dozmod.gui.control.WordWrapLabel;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.TextChangeListener;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
import org.openslx.util.Util;

/**
 * Widget for netrules configuration of lectures
 */
public class NetrulesConfigurator extends NetrulesConfiguratorLayout implements GenericControlWindow<StateWrapper> {
	
	public static class StateWrapper {
		public List<NetRule> customRules;
		public List<Integer> selectedPresets;
		@Override
		public boolean equals(Object obj) {
			if (!(obj instanceof StateWrapper))
				return false;
			StateWrapper o = (StateWrapper) obj;
			if (selectedPresets != o.selectedPresets
					&& (selectedPresets == null || !selectedPresets.equals(o.selectedPresets)))
				return false;
			if (customRules == o.customRules)
				return true;
			return customRules != null && customRules.equals(o.customRules);
		}
		@Override
		protected StateWrapper clone() {
			StateWrapper r = new StateWrapper();
			if (this.customRules != null) {
				r.customRules = new ArrayList<>(this.customRules);
			}
			if (this.selectedPresets != null) {
				r.selectedPresets = new ArrayList<>(this.selectedPresets);
			}
			return r;
		}
	}

	private static final long serialVersionUID = -3497629601818983994L;
	private final static Logger LOGGER = Logger.getLogger(NetrulesConfigurator.class);
	
	private boolean checkChange = false;
	private StateWrapper currentState = new StateWrapper();
	private LectureRead lecture;
	private List<ChangeListener> listeners;
	private List<PresetNetRule> predefinedRules;

	/**
	 * Character defining how the rules are parsed, e.g. for whitespace \\s
	 * Example: "8.8.8.8 80 in" would be split in -hostname "8.8.8.8" -port "80"
	 * -direction "in"
	 */
	private static final String FIELD_DELIMITER = "\\s+";
	
	private static final Color FOREGROUND_TEXT_COLOR;
	
	static {
		Color fgOrigColor = UIManager.getDefaults().getColor("ColorChooser.foreground");
		if (fgOrigColor == null) {
			// use black as fallback
			fgOrigColor = Color.BLACK;
		}
		FOREGROUND_TEXT_COLOR = fgOrigColor;
	}

	public NetrulesConfigurator() {
		super();

		final TextChangeListener docListener = new TextChangeListener() {
			@Override
			public void changed() {
				checkChange = true;
				fireChangeEvent();
			}
		};
		final SimpleAttributeSet as = new SimpleAttributeSet();
		StyleConstants.setForeground(as, FOREGROUND_TEXT_COLOR);
		tpNetworkRules.getDocument().addDocumentListener(docListener);
		tpNetworkRules.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						int pos = tpNetworkRules.getCaretPosition();
						if (pos < 0) {
							return;
						}
						// Odd: On windows, getText() returns the text with CRLF line breaks,
						// regardless of what you put into the text box.
						// The offsets passed to setCharAttrs() below and the caret position
						// you get from getCaretPosition() however have to adhere to the
						// text version with just LF, exactly how we created the document.
						String text = tpNetworkRules.getText().replace("\r", "");
						if (pos >= text.length()) {
							return;
						}
						int start = text.lastIndexOf('\n', pos == 0 ? 0 : pos - 1);
						int end = text.indexOf('\n', pos);
						if (start == -1) {
							start = 0;
						}
						if (end == -1) {
							end = text.length() - 1;
						}
						if (end <= start) {
							return;
						}
						tpNetworkRules.getStyledDocument().setCharacterAttributes(start, end - start, as, true);
					}
				});
			}
		});
		btnCheckRules.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getState(false);
			}
		});
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				predefinedRules = MetaDataCache.getPredefinedNetRules();
				if (predefinedRules.isEmpty()) {
					Gui.asyncExec(new Runnable() {
						@Override
						public void run() {
							btnShowPresets.setVisible(false);
						}
					});
				}
			}
		});
		btnShowPresets.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showPresetSelector();
			}
		});
	}
	
	private void showPresetSelector() {
		if (predefinedRules == null) {
			Gui.showMessageBox("Wah wah wah! Null preset list", MessageType.ERROR, null, null);
			return;
		}
		final JDialog dialog = new JDialog(SwingUtilities.getWindowAncestor(this),
				"Auswahl", ModalityType.APPLICATION_MODAL);
		JPanel pane = new JPanel();
		pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
		pane.getActionMap().put("cancel", new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
		pane.setBorder(new EmptyBorder(new Insets(10, 5, 5, 10)));
		dialog.setContentPane(pane);
		Dimension dd = Gui.getScaledDimension(200, 150 + 15 * predefinedRules.size());
		dialog.setMinimumSize(dd);
		dialog.setPreferredSize(dd);
		GridManager grid = new GridManager(pane, 3, true, new Insets(2, 2, 2, 2));
		grid.add(new WordWrapLabel("Vordefinierte Regelsets:"), 3);
		grid.nextRow();
		final Map<ButtonModel, Integer> mapper = new HashMap<>();
		for (PresetNetRule ruleSet : predefinedRules) {
			JCheckBox button = new JCheckBox(ruleSet.displayName);
			grid.add(button, 3);
			grid.nextRow();
			mapper.put(button.getModel(), ruleSet.ruleId);
			if (lecture != null && lecture.presetNetworkExceptionIds != null && lecture.presetNetworkExceptionIds.contains(ruleSet.ruleId)) {
				button.setSelected(true);
			}
		}
		grid.add(Box.createVerticalGlue(), 3).expand(true, true).fill(true, true);
		grid.nextRow();
		grid.add(Box.createHorizontalGlue()).expand(true, false).fill(true, false);
		JButton btnCancel = new JButton("Abbrechen");
		JButton btnOk = new JButton("Speichern");
		grid.add(btnCancel);
		grid.add(btnOk);
		grid.finish(false);
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
		final AtomicReference<List<Integer>> selectedRules = new AtomicReference<>();
		btnOk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				List<Integer> selected = new ArrayList<>();
				for (Entry<ButtonModel, Integer> button : mapper.entrySet()) {
					if (button.getKey().isSelected()) {
						selected.add(button.getValue());
					}
				}
				selectedRules.set(selected);
				dialog.dispose();
			}
		});
		dialog.pack();
		Gui.centerShellOverShell(SwingUtilities.getWindowAncestor(this), dialog);
		dialog.setVisible(true);
		// Call blocks as it's a modal window
		if (selectedRules.get() != null) {
			// User clicked OK
			lecture.presetNetworkExceptionIds = currentState.selectedPresets = selectedRules.get();
			checkChange = true;
			fireChangeEvent();
		}
	}

	/**
	 * Gets the state of the widget as a list of netrules. Internally it first
	 * transforms the input text in a list of netrules.
	 * 
	 * @return the list of rules as parsed by parseNetRules()
	 */
	@Override
	public StateWrapper getState() {
		return getState(true);
	}
	
	public StateWrapper getState(boolean silent) {
		if (checkChange || !silent) {
			currentState.customRules = parseNetRules(silent);
		}
		return currentState.clone();
	}

	@Override
	public void addChangeListener(ChangeListener l) {
		if (listeners == null) {
			listeners = new ArrayList<>();
		}
		listeners.add(l);
	}
	
	private void fireChangeEvent() {
		if (listeners == null)
			return;
		for (ChangeListener cl : listeners) {
			cl.stateChanged(null);
		}
	}

	/**
	 * Sets the state of this widget to the given list of netrules. This will
	 * internally transform the list to its string representation
	 * 
	 * @param lecture
	 *            as a list of NetRule to set the state to
	 */
	public void setState(final LectureRead lecture) {
		currentState.customRules = lecture.networkExceptions;
		currentState.selectedPresets = lecture.presetNetworkExceptionIds;
		checkChange = true;
		this.lecture = lecture;
		this.tpNetworkRules.setText(decodeNetRulesToText(lecture.networkExceptions));
		fireChangeEvent();
	}

	/**
	 * "Decodes" the given list of NetRule to a single String. This should be
	 * used to set the text in the TextPane for the network rules
	 * 
	 * @param netRulesList
	 *            list of NetRule to decode
	 * @return String representation of the list of rules
	 */
	public static String decodeNetRulesToText(final List<NetRule> netRulesList) {
		if (netRulesList == null || netRulesList.isEmpty())
			return "";

		String decodedRules = "";
		Iterator<NetRule> it = netRulesList.iterator();
		while (it.hasNext()) {
			String currentLine = "";
			NetRule currentRule = it.next();
			// simple test for validity (since this comes from the server it
			// should be correct anyways)
			if (currentRule.host.isEmpty() || currentRule.port > 65535) {
				LOGGER.error("Invalid rule! Ignoring: " + currentRule.host
						+ ":" + currentRule.port);
				continue;
			}
			currentLine += currentRule.host + " \t ";
			currentLine += currentRule.port + " \t ";
			currentLine += currentRule.direction.name();
			decodedRules += currentLine
					+ (it.hasNext() ? "\n" : "");
		}
		return decodedRules;
	}

	/**
	 * Parses the given rawNetRules String to a list of NetRule
	 * 
	 * @param rawNetRules
	 *            the raw text to be parsed
	 * @return list of netrules if successful. If any errors occured while
	 *         parsing, null is returned.
	 */
	public List<NetRule> parseNetRules(boolean silent) {
		String rawNetRules = tpNetworkRules.getText().trim();
		List<NetRule> rulesList = new ArrayList<NetRule>();
		if (rawNetRules.isEmpty()) {
			return rulesList;
		}

		// split it line by line
		boolean invalid = false; // True if the rules are invalid and null should be returned
		DefaultStyledDocument newdoc = null;
		if (!silent) {
			newdoc = new DefaultStyledDocument(); // Used to build new document with colors
		}
		StringBuilder errors = new StringBuilder(); // Error messages to show (if not silent)
		int lineNo = 0; // Show line numbers in error messages
		for (String ruleLine : rawNetRules.split("[\r\n]+")) {
			if (silent && invalid)
				return null;
			Color lineColor = null;
			LOGGER.debug("Parsing rule: " + ruleLine);
			// split the fields and check if we have 3 as expected.
			String[] fields = ruleLine.trim().split(FIELD_DELIMITER);
			if (fields.length != 3) {
				lineNo += addLine(newdoc, ruleLine, Color.RED, true);
				// log numbers for fields independently...
				LOGGER.debug("Invalid number of fields! Expected 3, got: " + fields.length);
				if (fields.length > 3) {
					errors.append("Zeile " + lineNo + ": Zu viele Felder.\n");
				} else {
					errors.append("Zeile " + lineNo + ": Zu wenig Felder.\n");
				}
				invalid = true;
				continue;
			}
			// Have 3 fields, pretty up
			String ruleDirection = fields[2].toUpperCase();
			ruleLine = fields[0] + " \t " + fields[1] + " \t " + ruleDirection;

			// start to check fields one by one from the last to the first ....
			// check net direction: accept either 'in' or 'out' (case
			// insensitive)
			// TODO support combined 'in/out' rules
			if (!ruleDirection.equals("IN") && !ruleDirection.equals("OUT")) {
				lineNo += addLine(newdoc, ruleLine, Color.RED, true);
				LOGGER.debug("Invalid net direction! Expected 'in' or out'. Got: " + ruleDirection);
				errors.append("Zeile " + lineNo + ": Ungültige Richtung. Bitte nutzen Sie 'IN' bzw. 'OUT'.\n");
				invalid = true;
				continue;
			}
			// check port: accept if >= 0 and <= 65535
			int port = Util.parseInt(fields[1], -1);

			// port = 0 means match only host (all protocols and ports)
			if (port < 0 || port > 65535) {
				lineNo += addLine(newdoc, ruleLine, Color.RED, true);
				LOGGER.debug("Invalid port! Got: " + port);
				errors.append("Zeile " + lineNo + ": Ungültiger Port. Gültiger Bereich ist 0-65535.\n");
				invalid = true;
				continue;
			}
			// check hostname: bit more to do here
			// for IPs and/or resolvable hostnames we make use of java.net's
			// InetAddress.getByName() method which checks the validity of
			// an IP-Address represented by a string or if the given hostname
			// is resolvable. If any of these happen, we have a valid hostname.
			// Non-resolvable hostnames are handled differently, see after the
			// try/catch-block
			String checkRes = checkHostnameSimple(fields[0]);
			if (checkRes != null) {
				lineNo += addLine(newdoc, ruleLine, Color.RED, true);
				errors.append("Zeile " + lineNo + ": " + checkRes + "\n");
				invalid = true;
				continue;
			}
			// Made it to here - line is valid
			lineNo += addLine(newdoc, ruleLine, lineColor, false);
			rulesList.add(new NetRule(NetDirection.valueOf(ruleDirection), fields[0], port));
		}
		if (newdoc != null) {
			tpNetworkRules.setDocument(newdoc);
			resetTabStops();
		}
		if (!silent && errors.length() != 0) {
			Gui.showMessageBox("Fehler beim Auswerten der angegebenen Netzwerkregeln.\n\n" + errors.toString()
					+ "\nBitte geben Sie die Regeln zeilenweise im Format\n"
					+ "<host> <port> <IN|OUT>\n"
					+ "an.",
					MessageType.ERROR, null, null);
		}
		if (invalid) {
			return null;
		}
		// Success
		return rulesList;
	}

	private int addLine(DefaultStyledDocument doc, String line, Color color, boolean bold) {
		if (doc == null)
			return 0;
		if (color == null) {
			color = FOREGROUND_TEXT_COLOR;
		}
		SimpleAttributeSet attrs = new SimpleAttributeSet();
		StyleConstants.setForeground(attrs, color);
		StyleConstants.setBold(attrs, bold);
		try {
			doc.insertString(doc.getLength(), line + "\n", attrs);
		} catch (BadLocationException e) {
			LOGGER.warn("Cannot append to new textbox document", e);
		}
		return 1;
	}

	/**
	 * Very simple hostname check for the given String. This will only check for
	 * some requirements of valid hostnames as stated in
	 * http://tools.ietf.org/html/rfc1034#section-3.1 To recap: max length of
	 * the whole hostname must be < 254 ASCII characters, all domain labels
	 * (between two dots) must be between 1 and 63 chars long and domain labels
	 * can only contain digits, letters and hyphen. (Note: we also accept
	 * forward slash to accept subnets!)
	 * 
	 * @param hostname the hostname to check for syntactical validity
	 * @return null if valid, error string otherwise
	 */
	private String checkHostnameSimple(String hostname) {
		if (hostname.length() > 254) {
			return "Hostname ist zu lang.";
		}
		boolean allNumeric = true;
		int netmask = -1;
		String[] domainLabels = null;
		int ls = hostname.lastIndexOf('/');
		if (ls != -1) {
			netmask = Util.parseInt(hostname.substring(ls + 1), -1);
			if (netmask == -1) {
				return "Ungültige Netzmaske.";
			}
			hostname = hostname.substring(0, ls);
		}
		if (hostname.matches("^\\[.*\\]$")) {
			hostname = hostname.substring(1, hostname.length() - 2);
		} else {
			// split by '.' to get domain levels
			domainLabels = hostname.split("\\.");
		}
		if (domainLabels == null || (domainLabels.length <= 1 && hostname.indexOf(':') != -1)) {
			// v6
			if ((hostname.startsWith(":") && !hostname.startsWith("::"))
					|| (hostname.endsWith(":") && ! hostname.endsWith("::"))) {
				return "IPv6-Adresse darf nicht mit einem Doppelpunkt beginnen oder enden.";
			}
			int numCompressed = (hostname.length() - hostname.replace("::", "").length()) / 2;
			if (numCompressed > 1) {
				return "IPv6-Adresse darf nicht mehr als einen komprimierten Teil enthalten.";
			}
			if (netmask > 128) {
				return "IPv6 Netzmaske kann nicht größer 128 Bit sein.";
			}
			domainLabels = hostname.split(":");
			if (domainLabels.length > 8) {
				return "IPv6-Adresse enthält zu viele Hextets."; // Yes it's called that apparently
			}
			for (String domainLabel : domainLabels) {
				if (domainLabel.isEmpty())
					continue;
				try {
					int test = Integer.parseInt(domainLabel, 16);
					if (test < 0 || test > 65535) {
						return "IPv6-Adresse enthält ungültiges Hextet.";
					}
				} catch (Exception e) {
					return "IPv6-Adresse enthält nicht-hexadezimale Zeichen."; 
				}
			}
			if (!allNumeric
					|| ((domainLabels.length == 8 || numCompressed > 0) && (netmask < -1 || netmask > 128))
					|| (domainLabels.length < 8 && numCompressed == 0 && (netmask < 0 || netmask > 128))) {
				return "Fehlerhafte IPv6-Adresse/Netzmaske.";
			}
		} else {
			// v4 or hostname
			if (netmask > 32) {
				return "IPv4 Netzmaske kann nicht größer 32 Bit sein.";
			}
			for (String domainLabel : domainLabels) {
				if (domainLabel.length() > 63) {
					// fail since domain level should be max 63 chars
					return "Domain-Ebene '" + domainLabel + "' länger als 63 Zeichen.";
				}
				int i = Util.parseInt(domainLabel, -1);
				if (i < 0 || i > 255) {
					allNumeric = false;
				}
				// checking for valid chars is pointless with punycode
			}
			if (allNumeric) {
				if ((domainLabels.length == 4 && (netmask < -1 || netmask > 32))
						|| domainLabels.length > 4
						|| (domainLabels.length < 4 && (netmask < 0 || netmask > 32))) {
					return "Fehlerhafte IPv4-Adresse/Netzmaske.";
				}
			}
		}
		return null;
	}

}

/**
 * Internal layout class for this widget
 */
class NetrulesConfiguratorLayout extends JPanel {

	private static final long serialVersionUID = 5266120380443817325L;
	private final static String STR_RULES_DESCRIPTION = "Wenn Sie den Internetzugriff deaktiviert haben,"
			+ " können Sie hier Ausnahmen definieren (Whitelist)."
			+ " Bitte definieren Sie Ihre Regeln im Format\n<host> <port> <in|out>.\n"
			+ "Sie können Port 0 angeben, was sämtlichen TCP und UDP Ports eines Hosts entspricht.";
	private final static String STR_RULES_ADD = "Wenn Sie Internetzugriff aktivieren,"
			+ " hat diese Liste den gegenteiligen Effekt (Blacklist).";
	private final static String STR_TITLE = "Netzwerkregeln";

	protected final JTextPane tpNetworkRules;
	protected final JButton btnCheckRules;
	protected final JButton btnShowPresets;

	public NetrulesConfiguratorLayout() {

		GridManager grid = new GridManager(this, 2, true,
				new Insets(5, 5, 5, 5));

		// middle panel for network rules
		this.setBorder(BorderFactory.createTitledBorder(STR_TITLE));
		tpNetworkRules = new JTextPane();
		resetTabStops();

		grid
				.add(new WordWrapLabel(STR_RULES_DESCRIPTION), 2)
				.fill(true, false).expand(true, false);
		grid.nextRow();

		grid
				.add(new WordWrapLabel(STR_RULES_ADD))
				.fill(true, false).expand(true, false);
		btnCheckRules = new JButton("Regeln überprüfen");
		grid.add(btnCheckRules);
		grid.nextRow();

		grid.add(new JScrollPane(tpNetworkRules,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), 2)
				.fill(true, true).expand(true, true);
		grid.nextRow();
		
		btnShowPresets = new JButton("Vordefinierte Regelsets...");
		grid.add(btnShowPresets, 2).anchor(GridBagConstraints.LINE_END);
		grid.nextRow();

		grid.finish(false);
	}

	protected void resetTabStops() {
		Style tabs = tpNetworkRules.getLogicalStyle();
		StyleConstants.setTabSet(tabs,  new TabSet(new TabStop[] {
				new TabStop(300, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE),
				new TabStop(310, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE),
		}));
		tpNetworkRules.setLogicalStyle(tabs);
	}
}