MainWindow.java
1 |
|
2 |
import javax.swing.*; |
3 |
import java.util.List; |
4 |
|
5 |
import java.awt.event.WindowAdapter; |
6 |
import java.awt.event.WindowEvent; |
7 |
/** |
8 |
* MainWindow - Abstraction layer for a window in an MVC architecture. It abstracts a lot of fluff caused by Swing being Swing. It also takes care of the state and layout of the window itself, allows for dialog creating, therefore making 80% of what's most used in simple programs with a GUI possible in just 20% of the time it used to take. |
9 |
* @author Maarten Vangeneugden - 1438256 |
10 |
*/ |
11 |
class MainWindow extends AbstractView { |
12 |
|
13 |
private JFrame window; |
14 |
|
15 |
/** |
16 |
* Constructor. It creates an empty window. Period. |
17 |
* @param windowTitle The title of the created window. |
18 |
* @param widgets A list of widgets that can be added to the window. |
19 |
*/ |
20 |
public MainWindow(String windowTitle, List<JComponent> widgets) { |
21 |
|
22 |
super(null, null); |
23 |
// First, we create the window itself: |
24 |
JFrame window = new JFrame(windowTitle); // Create the new window. |
25 |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set its close action. (Laymen's terms: Make it close down when you close it down.) |
26 |
setWindow(window); |
27 |
|
28 |
// Then, we add the given widgets to it: |
29 |
if(widgets != null) { |
30 |
for (JComponent widget : widgets) { |
31 |
addWidget(widget); |
32 |
} |
33 |
} |
34 |
} |
35 |
|
36 |
public void setWindow(JFrame window) { |
37 |
this.window = window; |
38 |
} |
39 |
|
40 |
public JFrame getWindow() { |
41 |
return window; |
42 |
} |
43 |
|
44 |
/** |
45 |
* Adds a given widget to the window. |
46 |
* It's highly recommended to have the widget's fluff (i.e. Actionlisteners) all prepped before adding. |
47 |
* @param widget The widget to be added to the window. |
48 |
*/ |
49 |
public void addWidget(JComponent widget) { |
50 |
getWindow().getContentPane().add(widget); |
51 |
} |
52 |
|
53 |
/** |
54 |
* Returns a list containing all widgets in this window. |
55 |
* @return A list of widgets. |
56 |
*/ |
57 |
public List<JComponent> getWidgets() { |
58 |
return new ArrayList(getWindow().getContentPane().getComponents()); |
59 |
} |
60 |
|
61 |
/** |
62 |
* Removes the given widget from the window. |
63 |
* @param widget The widget that should be removed. |
64 |
* @post The given widget is no longer in the window. |
65 |
*/ |
66 |
public void removeWidget(JComponent widget) { |
67 |
getWindow().getContentPane().removeComponent(widget); |
68 |
} |
69 |
|
70 |
} |
71 |