-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
179 lines (142 loc) · 5.33 KB
/
Window.java
File metadata and controls
179 lines (142 loc) · 5.33 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
package readiefur.xml_ui.controls;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.w3c.dom.Node;
import readiefur.misc.Event;
import readiefur.misc.ManualResetEvent;
import readiefur.xml_ui.attributes.ChildBuilderAttribute;
import readiefur.xml_ui.attributes.SetterAttribute;
import readiefur.xml_ui.exceptions.InvalidXMLException;
import readiefur.xml_ui.factory.UIBuilderFactory;
import readiefur.xml_ui.interfaces.IRootComponent;
public class Window extends JFrame implements IRootComponent
{
private Boolean addedChild = false;
private ManualResetEvent dialogueResetEvent = new ManualResetEvent(false);
public final Event<WindowEvent> onWindowClosed = new Event<>();
public final Event<WindowEvent> onWindowClosing = new Event<>();
public final Event<WindowEvent> onWindowOpened = new Event<>();
public final Event<WindowEvent> onWindowIconified = new Event<>();
public final Event<WindowEvent> onWindowDeiconified = new Event<>();
public final Event<WindowEvent> onWindowActivated = new Event<>();
public final Event<WindowEvent> onWindowDeactivated = new Event<>();
public Window()
{
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//https://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html
addWindowListener(new WindowListener()
{
@Override
public void windowClosing(WindowEvent e) { OnWindowClosing(e); }
@Override
public void windowClosed(WindowEvent e) { OnWindowClosed(e); }
@Override
public void windowOpened(WindowEvent e) { OnWindowOpened(e); }
@Override
public void windowIconified(WindowEvent e) { OnWindowIconified(e); }
@Override
public void windowDeiconified(WindowEvent e) { OnWindowDeiconified(e); }
@Override
public void windowActivated(WindowEvent e) { OnWindowActivated(e); }
@Override
public void windowDeactivated(WindowEvent e) { OnWindowDeactivated(e); }
});
}
@SetterAttribute("Title")
public void SetTitle(String title)
{
setTitle(title);
}
@SetterAttribute("Width")
public void SetWidth(String width)
{
setSize(Integer.parseInt(width), getHeight());
}
@SetterAttribute("Height")
public void SetHeight(String height)
{
setSize(getWidth(), Integer.parseInt(height));
}
@SetterAttribute("MinWidth")
public void SetMinWidth(String minWidth)
{
setMinimumSize(new java.awt.Dimension(Integer.parseInt(minWidth), getMinimumSize().height));
}
@SetterAttribute("MinHeight")
public void SetMinHeight(String minHeight)
{
setMinimumSize(new java.awt.Dimension(getMinimumSize().width, Integer.parseInt(minHeight)));
}
@SetterAttribute("MaxWidth")
public void SetMaxWidth(String maxWidth)
{
setMaximumSize(new java.awt.Dimension(Integer.parseInt(maxWidth), getMaximumSize().height));
}
@SetterAttribute("MaxHeight")
public void SetMaxHeight(String maxHeight)
{
setMaximumSize(new java.awt.Dimension(getMaximumSize().width, Integer.parseInt(maxHeight)));
}
@SetterAttribute("Resizable")
public void SetResizable(String resizable)
{
setResizable(Boolean.parseBoolean(resizable));
}
@SetterAttribute("Background")
public void SetBackground(String colour)
{
getContentPane().setBackground(Color.decode(colour));
}
@ChildBuilderAttribute
public void AddChild(UIBuilderFactory builder, List<Node> children) throws InvalidXMLException
{
for (Node child : children)
{
//A window can only have one child.
if (addedChild)
throw new InvalidXMLException("A '" + Window.class.getSimpleName() + "' can only have one child.");
//If the child is a resource dictionary, skip it.
if (child.getNodeName().equals(Window.class.getSimpleName() + ".Resources"))
continue;
//Add the child to the window.
add(builder.ParseXMLNode(child));
addedChild = true;
}
}
public void RemoveChild()
{
removeAll();
addedChild = false;
}
/**
* Shows the window and does not wait for it to be closed.
*/
public void Show()
{
setVisible(true);
}
/**
* Shows the window and waits for it to be closed.
*/
public void ShowDialog()
{
setVisible(true);
dialogueResetEvent.WaitOne();
}
protected void OnWindowClosing(WindowEvent e) { onWindowClosing.Invoke(e); }
protected void OnWindowClosed(WindowEvent e)
{
dialogueResetEvent.Set();
onWindowClosing.Invoke(e);
}
protected void OnWindowOpened(WindowEvent e) { onWindowOpened.Invoke(e); }
protected void OnWindowIconified(WindowEvent e) { onWindowIconified.Invoke(e); }
protected void OnWindowDeiconified(WindowEvent e) { onWindowDeiconified.Invoke(e); }
protected void OnWindowActivated(WindowEvent e) { onWindowActivated.Invoke(e); }
protected void OnWindowDeactivated(WindowEvent e) { onWindowDeactivated.Invoke(e); }
}