1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 | import java.awt.RenderingHints;
|
---|
8 | import java.awt.Toolkit;
|
---|
9 | import java.awt.event.ComponentEvent;
|
---|
10 | import java.awt.event.ComponentListener;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.awt.event.WindowListener;
|
---|
13 | import java.awt.event.WindowStateListener;
|
---|
14 |
|
---|
15 | import javax.swing.JFrame;
|
---|
16 |
|
---|
17 | import org.expeditee.AbsoluteLayout;
|
---|
18 | import org.expeditee.actions.Actions;
|
---|
19 | import org.expeditee.actions.Misc;
|
---|
20 | import org.expeditee.io.Logger;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * The Main GUI class, comprises what people will see on the screen.<br>
|
---|
24 | * Note: Each Object (Item) is responsible for drawing itself on the screen.<br>
|
---|
25 | * Note2: The Frame is registered as a MouseListener and KeyListener, and
|
---|
26 | * processes any Events.
|
---|
27 | *
|
---|
28 | * @author jdm18
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public class Browser extends JFrame implements ComponentListener,
|
---|
32 | WindowListener, WindowStateListener {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Default version - just to stop eclipse from complaining about it.
|
---|
36 | */
|
---|
37 | private static final long serialVersionUID = 1L;
|
---|
38 |
|
---|
39 | // private static final JScrollPane scrollPane = new JScrollPane();
|
---|
40 |
|
---|
41 | public static Browser _theBrowser;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new Browser object, then launches it
|
---|
45 | *
|
---|
46 | * @param args
|
---|
47 | */
|
---|
48 | public static void main(String[] args) {
|
---|
49 | _theBrowser = new Browser();
|
---|
50 | // Why do we want to ignore repaint?
|
---|
51 | // b.setIgnoreRepaint(true);
|
---|
52 | _theBrowser.requestFocus();
|
---|
53 | // FrameGraphics.ForceRepaint();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void setSizes(Dimension size) {
|
---|
57 | setSize(size);
|
---|
58 | setPreferredSize(size);
|
---|
59 | addWindowListener(this);
|
---|
60 | addWindowStateListener(this);
|
---|
61 |
|
---|
62 | // center the frame on the screen
|
---|
63 | Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
64 | double xpos = screen.getWidth() / 2;
|
---|
65 | double ypos = screen.getHeight() / 2;
|
---|
66 | setLocation((int) (xpos - (size.getWidth() / 2)), (int) (ypos - (size
|
---|
67 | .getHeight() / 2)));
|
---|
68 |
|
---|
69 | FrameGraphics.setMaxSize(this.getContentPane().getSize());
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Browser() {
|
---|
73 | // Use the default values initially so we can load the profile frame
|
---|
74 | setSizes(new Dimension(UserSettings.InitialWidth,
|
---|
75 | UserSettings.InitialHeight));
|
---|
76 |
|
---|
77 | UserSettings.Init();
|
---|
78 | UserSettings.Username = FrameIO.ConvertToValidFramesetName(System.getProperty("user.name"));
|
---|
79 | String userName = UserSettings.Username;
|
---|
80 | Frame profile = FrameIO.LoadProfile(userName);
|
---|
81 | if (profile == null) {
|
---|
82 | try {
|
---|
83 | profile = FrameIO.CreateNewProfile(userName);
|
---|
84 | } catch (Exception e) {
|
---|
85 | //TODO tell the user that there was a problem creating the profile frame and close nicely
|
---|
86 | e.printStackTrace();
|
---|
87 | assert(false);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | FrameUtils.ParseProfile(profile);
|
---|
91 |
|
---|
92 | // Now reset the size of the windows to the size specified in the users
|
---|
93 | // profile
|
---|
94 | setSizes(new Dimension(UserSettings.InitialWidth,
|
---|
95 | UserSettings.InitialHeight));
|
---|
96 |
|
---|
97 | //set the layout to absolute layout for widgets
|
---|
98 | this.getContentPane().setLayout(new AbsoluteLayout());
|
---|
99 | //enable the glasspane-for capturing all mouse events
|
---|
100 | this.setGlassPane(new MouseEventRouter(getJMenuBar(), getContentPane()));
|
---|
101 | this.getGlassPane().setVisible(true);
|
---|
102 | this.getContentPane().setBackground(Color.white);
|
---|
103 | this.getContentPane().setFocusTraversalKeysEnabled(false);
|
---|
104 |
|
---|
105 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
106 |
|
---|
107 | addComponentListener(this);
|
---|
108 | pack();
|
---|
109 | try {
|
---|
110 | go();
|
---|
111 | } catch (Exception e) {
|
---|
112 | e.printStackTrace();
|
---|
113 | Logger.Log(e);
|
---|
114 | }
|
---|
115 | // FrameGraphics.ForceRepaint();
|
---|
116 | }
|
---|
117 |
|
---|
118 | Graphics2D g;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Loads in the Menus and the first Frame.
|
---|
122 | */
|
---|
123 | public void go() {
|
---|
124 |
|
---|
125 | Actions.Init();
|
---|
126 | DisplayIO.Init(this);
|
---|
127 | // Set visible must be just after DisplayIO.Init for the message box to
|
---|
128 | // be the right size
|
---|
129 | setVisible(true);
|
---|
130 |
|
---|
131 | setupGraphics();
|
---|
132 |
|
---|
133 | // required to accept TAB key
|
---|
134 | setFocusTraversalKeysEnabled(false);
|
---|
135 |
|
---|
136 | assert (UserSettings.FirstFrame != null);
|
---|
137 | Frame firstFrame = FrameIO.LoadFrame(UserSettings.FirstFrame);
|
---|
138 |
|
---|
139 | DisplayIO.setCurrentFrame(firstFrame);
|
---|
140 | DisplayIO.UpdateTitle();
|
---|
141 |
|
---|
142 | FrameKeyboardActions keyboardListner = new FrameKeyboardActions();
|
---|
143 | this.getContentPane().addKeyListener(keyboardListner);
|
---|
144 | this.addKeyListener(keyboardListner);
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void setupGraphics() {
|
---|
148 | g = (Graphics2D) this.getContentPane().getGraphics();
|
---|
149 | assert g != null;
|
---|
150 | g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
|
---|
151 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
---|
152 | g.setFont(g.getFont().deriveFont(40f));
|
---|
153 | FrameGraphics.setDisplayGraphics(g);
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | public void paint(Graphics g) {
|
---|
158 | super.paint(g);
|
---|
159 | FrameGraphics.Repaint();
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * @inheritDoc
|
---|
164 | */
|
---|
165 | public void componentResized(ComponentEvent e) {
|
---|
166 | setSizes(this.getSize());
|
---|
167 | setupGraphics();
|
---|
168 | repaint();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * @inheritDoc
|
---|
173 | */
|
---|
174 | public void componentMoved(ComponentEvent e) {
|
---|
175 | // FrameGraphics.setMaxSize(this.getSize());
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * @inheritDoc
|
---|
180 | */
|
---|
181 | public void componentShown(ComponentEvent e) {
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @inheritDoc
|
---|
186 | */
|
---|
187 | public void componentHidden(ComponentEvent e) {
|
---|
188 | }
|
---|
189 |
|
---|
190 | public void windowClosing(WindowEvent e) {
|
---|
191 | Misc.Exit();
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void windowClosed(WindowEvent e) {
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void windowOpened(WindowEvent e) {
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void windowIconified(WindowEvent e) {
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void windowDeiconified(WindowEvent e) {
|
---|
204 | }
|
---|
205 |
|
---|
206 | public void windowActivated(WindowEvent e) {
|
---|
207 | // System.out.println("Activated");
|
---|
208 | }
|
---|
209 |
|
---|
210 | public void windowDeactivated(WindowEvent e) {
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * public void windowGainedFocus(WindowEvent e) { FrameGraphics.Repaint(); }
|
---|
215 | *
|
---|
216 | * public void windowLostFocus(WindowEvent e) { //FrameGraphics.Repaint(); }
|
---|
217 | */
|
---|
218 | public void windowStateChanged(WindowEvent e) {
|
---|
219 | //FrameGraphics.Repaint();
|
---|
220 | //System.out.println('C');
|
---|
221 | }
|
---|
222 |
|
---|
223 | public int getDrawingAreaX() {
|
---|
224 | // return scrollPane.getLocationOnScreen().x;
|
---|
225 | return this.getLocationOnScreen().x;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public int getDrawingAreaY() {
|
---|
229 | // return scrollPane.getLocationOnScreen().y;
|
---|
230 | return this.getLocationOnScreen().y;
|
---|
231 | }
|
---|
232 | }
|
---|