aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/uk/co/notori/gol/MainScreen.java
blob: 5829f4298c378f1dbf7b47e494018a39cd537836 (plain)
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
package uk.co.notori.gol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;

/**
 * Main screen manager
 */
public class MainScreen {

    private static final Logger log = LoggerFactory.getLogger(MainScreen.class);
        
    private JPanel currentUI;
    private final Container root;
    private final ExitHook mainExitHook;

    public interface ExitHook {
        void exit();
    }
    
    public interface NewGameHook {
        void newGame();
    }

    public MainScreen(Container root, ExitHook exitHook) {
        this(root, exitHook, null);
    }
    
    public MainScreen(Container root, ExitHook exitHook, String savePath) {
        this.root = root;
        this.mainExitHook = exitHook;
                
        if (!Util.isKindle()) {
            root.setSize(new Dimension(800, 600));
        }
		// start new game grid on load
        newGame();
    }
    
	public void start() {
		log.info("Starting Game of Life application");
		
		if (currentUI != null) {
			currentUI.setVisible(true);
			
			// force a complete refresh
			root.invalidate();
			root.validate();
			root.repaint();
			
			// very ugly fix for weird component visibility behaviour
			if (Util.isKindle()) {
				scheduleRefresh(300);
				scheduleRefresh(600);
				scheduleRefresh(1200);
				scheduleRefresh(2000);
				scheduleRefresh(3000);
				
				new java.util.Timer().schedule(
					new java.util.TimerTask() {
						public void run() {
							try {
								SwingUtilities.invokeAndWait(new Runnable() {
									public void run() {
										refreshButtonsOnly(root);
									}
								});
							} catch (Exception e) {
								log.error("Error during button refresh", e);
							}
						}
					},
					3500
				);
			}
		}
		
		log.info("Application started");
	}
	
	// another very ugly fix for weird component visibility behaviour
	private void scheduleRefresh(final int delay) {
		new java.util.Timer().schedule(
			new java.util.TimerTask() {
				public void run() {
					try {
						SwingUtilities.invokeAndWait(new Runnable() {
							public void run() {
								refreshComponentsRecursively(root);
								
								currentUI.invalidate();
								currentUI.validate();
								currentUI.repaint();
								
								root.invalidate();
								root.validate();
								root.repaint();
								
								log.info("Completed UI refresh after " + delay + "ms");
							}
						});
					} catch (Exception e) {
						log.error("Error during UI refresh", e);
					}
				}
			},
			delay
		);
	}

	// another very ugly fix for weird component visibility behaviour
	private void refreshComponentsRecursively(Container container) {
		Component[] components = container.getComponents();
		for (int i = 0; i < components.length; i++) {
			Component component = components[i];
			component.invalidate();
			component.validate();
			component.repaint();
			
			// handling for buttons to ensure visibility
			if (component instanceof JButton) {
				JButton button = (JButton)component;
				button.setVisible(true);
			}
			
			if (component instanceof Container) {
				refreshComponentsRecursively((Container)component);
			}
		}
	}

	private void refreshButtonsOnly(Container container) {
		Component[] components = container.getComponents();
		for (int i = 0; i < components.length; i++) {
			Component component = components[i];
			
			if (component instanceof JButton) {
				JButton button = (JButton)component;
				button.setVisible(true);
				button.invalidate();
				button.validate();
				button.repaint();
				log.info("Refreshed button: " + button.getText());
			}
			
			if (component instanceof Container) {
				refreshButtonsOnly((Container)component);
			}
		}
	}

    public void newGame() {
        log.info("Starting new game");
        
        // remove current UI if it exists
        if (currentUI != null) {
            root.remove(currentUI);
        }
        
        // create main UI with new panel
        Panel gamePanel = new Panel(root.getSize());
        currentUI = new MainUI(
            new ExitHook() {
                public void exit() {
                    mainExitHook.exit();
                }
            },
            gamePanel
        );
        
        currentUI.setSize(root.getSize());
        currentUI.setPreferredSize(root.getSize());
        
        root.add(currentUI, BorderLayout.CENTER);
        
        currentUI.setVisible(true);
        gamePanel.setVisible(true);
        
        root.validate();
        root.repaint();
    }
}