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