summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/AwtBox.java
blob: 9ac9b87bd7eb34d315feaf5c53d10bbdf4effdd9 (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
package org.openslx.dozmod;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Show a message box using AWT - used for when SWT fails.
 */
public class AwtBox extends Dialog implements ActionListener {

	private static final long serialVersionUID = 6254641637349233126L;
	private Button okButton, cancelButton;
	public boolean okClicked = false;

	/**
	 * @param frame parent frame
	 * @param message message to be displayed
	 * @param addCancelButton whether we want a cancel button too, instead of
	 *            just OK
	 */
	AwtBox(Frame frame, String message, boolean addCancelButton) {
		super(frame, "Message", true);
		setLayout(new BorderLayout());
		TextArea ta = new TextArea(message, 1 + (message.length() - message.replace("\n", "").length()), 90,
				TextArea.SCROLLBARS_VERTICAL_ONLY);
		ta.setEditable(false);
		add("Center", ta);
		addOKCancelPanel(addCancelButton);
		createFrame();
		pack();
		setVisible(true);
	}

	AwtBox(Frame frame, String msg) {
		this(frame, msg, false);
	}

	private void addOKCancelPanel(boolean okcan) {
		Panel p = new Panel();
		p.setLayout(new FlowLayout());
		createOKButton(p);
		if (okcan == true)
			createCancelButton(p);
		add("South", p);
	}

	private void createOKButton(Panel p) {
		p.add(okButton = new Button("OK"));
		okButton.addActionListener(this);
	}

	private void createCancelButton(Panel p) {
		p.add(cancelButton = new Button("Cancel"));
		cancelButton.addActionListener(this);
	}

	private void createFrame() {
		Dimension d = getToolkit().getScreenSize();
		setLocation(d.width / 3, d.height / 3);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == okButton) {
			okClicked = true;
			setVisible(false);
		} else if (ae.getSource() == cancelButton) {
			setVisible(false);
		}
		this.dispose();
	}

}