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

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

Displaying better error messages when trying to create a frameset with a name that already exists.

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