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

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

Added Interactive Widget Items

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