source: trunk/src/org/expeditee/gui/Browser.java@ 23

Last change on this file since 23 was 23, checked in by ra33, 16 years ago

Fixed bug in session stats that caused an exception when there were no stats... this was preventing profile frame from being created correctly!

File size: 5.7 KB
Line 
1package org.expeditee.gui;
2
3import java.awt.Color;
4import java.awt.Dimension;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.RenderingHints;
8import java.awt.Toolkit;
9import java.awt.event.ComponentEvent;
10import java.awt.event.ComponentListener;
11import java.awt.event.WindowEvent;
12import java.awt.event.WindowListener;
13import java.awt.event.WindowStateListener;
14
15import javax.swing.JFrame;
16
17import org.expeditee.AbsoluteLayout;
18import org.expeditee.actions.Actions;
19import org.expeditee.actions.Misc;
20import 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 */
31public 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 = System.getProperty("user.name");
79 String userName = FrameIO.ConvertToValidFramesetName(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
141 FrameKeyboardActions keyboardListner = new FrameKeyboardActions();
142 this.getContentPane().addKeyListener(keyboardListner);
143 this.addKeyListener(keyboardListner);
144 }
145
146 private void setupGraphics() {
147 g = (Graphics2D) this.getContentPane().getGraphics();
148 assert g != null;
149 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
150 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
151 g.setFont(g.getFont().deriveFont(40f));
152 FrameGraphics.setDisplayGraphics(g);
153 }
154
155 @Override
156 public void paint(Graphics g) {
157 super.paint(g);
158 FrameGraphics.Repaint();
159 }
160
161 /**
162 * @inheritDoc
163 */
164 public void componentResized(ComponentEvent e) {
165 setSizes(this.getSize());
166 setupGraphics();
167 repaint();
168 }
169
170 /**
171 * @inheritDoc
172 */
173 public void componentMoved(ComponentEvent e) {
174 // FrameGraphics.setMaxSize(this.getSize());
175 }
176
177 /**
178 * @inheritDoc
179 */
180 public void componentShown(ComponentEvent e) {
181 }
182
183 /**
184 * @inheritDoc
185 */
186 public void componentHidden(ComponentEvent e) {
187 }
188
189 public void windowClosing(WindowEvent e) {
190 Misc.Exit();
191 }
192
193 public void windowClosed(WindowEvent e) {
194 }
195
196 public void windowOpened(WindowEvent e) {
197 }
198
199 public void windowIconified(WindowEvent e) {
200 }
201
202 public void windowDeiconified(WindowEvent e) {
203 }
204
205 public void windowActivated(WindowEvent e) {
206 // System.out.println("Activated");
207 }
208
209 public void windowDeactivated(WindowEvent e) {
210 }
211
212 /*
213 * public void windowGainedFocus(WindowEvent e) { FrameGraphics.Repaint(); }
214 *
215 * public void windowLostFocus(WindowEvent e) { //FrameGraphics.Repaint(); }
216 */
217 public void windowStateChanged(WindowEvent e) {
218 //FrameGraphics.Repaint();
219 //System.out.println('C');
220 }
221
222 public int getDrawingAreaX() {
223 // return scrollPane.getLocationOnScreen().x;
224 return this.getLocationOnScreen().x;
225 }
226
227 public int getDrawingAreaY() {
228 // return scrollPane.getLocationOnScreen().y;
229 return this.getLocationOnScreen().y;
230 }
231}
Note: See TracBrowser for help on using the repository browser.