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