1 package com.explosion.expf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.BorderLayout;
24 import java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.Graphics;
27 import java.awt.Image;
28 import java.awt.Toolkit;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.WindowEvent;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import javax.swing.BorderFactory;
36 import javax.swing.ImageIcon;
37 import javax.swing.JButton;
38 import javax.swing.JDesktopPane;
39 import javax.swing.JDialog;
40 import javax.swing.JFrame;
41 import javax.swing.JInternalFrame;
42 import javax.swing.JSplitPane;
43
44 import com.explosion.expf.menusandtools.menu.ExpMenuBar;
45 import com.explosion.expf.menusandtools.menu.popup.ExpPopupContainerListener;
46 import com.explosion.expf.menusandtools.menu.popup.ExpPopupIntercepter;
47 import com.explosion.expf.menusandtools.menu.popup.ExpPopupMouseListener;
48 import com.explosion.expf.menusandtools.menu.popup.ExpPopupableMenu;
49 import com.explosion.expf.menusandtools.tool.ExpToolBar;
50 import com.explosion.expf.menusandtools.tool.ExpToolBarSegment;
51 import com.explosion.expf.preferences.SystemPreferences;
52 import com.explosion.expf.preferences.Utils.PreferencePositionRecorder;
53 import com.explosion.expf.preferences.Utils.PreferenceResizeRecorder;
54 import com.explosion.utilities.FileSystemUtils;
55 import com.explosion.utilities.GeneralUtils;
56 import com.explosion.utilities.exception.ExceptionManagerFactory;
57
58 /***
59 * @author Stephen Cowx Date created:@29-Jan-2003
60 */
61 public class ExpFrame extends JFrame implements ExpPopupIntercepter
62 {
63
64
65 private JDesktopPane desktop = new JDesktopPane();
66
67
68 public static Integer BASE_LAYER = new Integer(1);
69 public static Integer DOC_FRAME_LAYER = new Integer(2);
70 public static Integer PALETTE_LAYER = new Integer(3);
71 public static int ADD = 0;
72 public static int REMOVE = 1;
73 public static int ACTIVATE = 2;
74 private Image iconImage;
75 private String iconImageLocation;
76 private ExpMenuBar menuBar = null;
77 private ExpToolBar toolBar;
78 private String frameTitle;
79 public boolean initialised = false;
80 private ExpListener listener;
81 private Map dialogs = new HashMap();
82 private Component bottomComponent;
83 private JSplitPane pane;
84
85 /***
86 * Constructor
87 */
88 public ExpFrame(String iconImageLocation, String title)
89 {
90 try
91 {
92 this.setName(ExpConstants.COMPNAME_EXPFRAME);
93 this.iconImageLocation = iconImageLocation;
94 this.frameTitle = title;
95 } catch (Exception e)
96 {
97 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Experienced and exception while constructing ApplicationFrame ");
98 }
99 }
100
101 public void init() throws Exception
102 {
103 this.setTitle(frameTitle);
104
105 if (iconImageLocation != null)
106 {
107 iconImage = FileSystemUtils.loadImage(iconImageLocation, this);
108 this.setIconImage(iconImage);
109 }
110
111 ExpPopupMouseListener mouseListener = new ExpPopupMouseListener();
112 this.getContentPane().addContainerListener(new ExpPopupContainerListener(mouseListener));
113
114 pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
115 pane.add(desktop, JSplitPane.TOP);
116 pane.setDividerLocation(1.0);
117 this.getContentPane().setLayout(new BorderLayout());
118 this.getContentPane().add(pane, BorderLayout.CENTER);
119
120
121 desktop.setBorder(BorderFactory.createLoweredBevelBorder());
122 desktop.setBackground(ExpConstants.BASE_COLOR);
123 desktop.setForeground(ExpConstants.BASE_COLOR);
124
125
126 listener = new ExpCompoundListener();
127
128
129 toolBar = new ExpToolBar(listener);
130 this.getContentPane().add(toolBar, BorderLayout.NORTH);
131
132
133 ExpToolBarSegment appSegment = toolBar.createNewSegment(ExpToolBarSegment.ALWAYS_LAST_SEGMENT);
134 JButton popertiesButton = toolBar.createExpToolBarItem(ExpConstants.DEFAULT_PROPERTIES_ICON, "Preferences", ExpConstants.MENU_PROPERTIES, 1);
135 JButton helpButton = toolBar.createExpToolBarItem(ExpConstants.DEFAULT_HELP_ICON, "Help", ExpConstants.MENU_HELP_CONTENTS, 1);
136
137
138 appSegment.addElement(popertiesButton);
139 appSegment.addElement(helpButton);
140
141
142 menuBar = new ExpMenuBar(listener);
143 this.setJMenuBar(menuBar);
144 this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
145
146 this.addWindowListener(new java.awt.event.WindowAdapter()
147 {
148
149 public void windowClosing(WindowEvent e)
150 {
151 Application.getInstance().exit();
152 }
153
154 });
155
156
157 Application.getInstance().updateLookAndFeel(this);
158
159
160 boolean startMaximised = ((Boolean) SystemPreferences.getPreference(ExpConstants.STARTMAXIMISED).getValue()).booleanValue();
161 if (startMaximised)
162 {
163 setExtendedState(JFrame.MAXIMIZED_BOTH);
164 }
165
166
167
168
169
170 int heightOfIt = ((Integer) SystemPreferences.getPreference(ExpConstants.HEIGHT).getValue()).intValue() - 15;
171 int widthOfIt = ((Integer) SystemPreferences.getPreference(ExpConstants.WIDTH).getValue()).intValue() - 15;
172 setSize(new Dimension(widthOfIt, heightOfIt));
173
174
175
176
177
178 boolean startCentered = ((Boolean) SystemPreferences.getPreference(ExpConstants.STARTCENTERED).getValue()).booleanValue();
179 if (startCentered || SystemPreferences.getPreference(ExpConstants.HEIGHT).isDefaulted())
180 {
181 GeneralUtils.centreWindowOnScreen(this);
182 }
183 else
184 {
185 int xpos = ((Integer) SystemPreferences.getPreference(ExpConstants.XPOS).getValue()).intValue();
186 int ypos = ((Integer) SystemPreferences.getPreference(ExpConstants.YPOS).getValue()).intValue();
187
188 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
189
190
191 xpos = xpos < 0 ? 0 : xpos;
192 xpos = xpos > (int) screenSize.getWidth() ? (int) screenSize.getWidth()-widthOfIt : xpos;
193
194
195 ypos = ypos < 0 ? 0 : ypos;
196 ypos = ypos > (int)screenSize.getHeight() ? (int)screenSize.getHeight()-heightOfIt : ypos;
197
198 this.setLocation(xpos,ypos);
199 }
200
201
202 this.addComponentListener(new PreferenceResizeRecorder(SystemPreferences.getPreference(ExpConstants.HEIGHT), SystemPreferences.getPreference(ExpConstants.WIDTH), this));
203
204
205 this.addComponentListener(new PreferencePositionRecorder(SystemPreferences.getPreference(ExpConstants.XPOS), SystemPreferences.getPreference(ExpConstants.YPOS), this));
206
207 }
208
209 public void show()
210 {
211 if (!initialised)
212 {
213 menuBar.constructMenus();
214 toolBar.constructToolBar();
215 initialised = true;
216 }
217 super.show();
218 }
219
220 public void paint(Graphics g)
221 {
222 checkEnabled();
223 try
224 {
225 super.paint(g);
226 }
227 catch (Throwable e)
228 {
229 ExceptionManagerFactory.getExceptionManager().manageException(e,"Caught exception during repaint. To recover from this you may need to restart the application.");
230 }
231 }
232
233 public void checkEnabled()
234 {
235 if (menuBar != null)
236 menuBar.getMenuHelper().checkEnabled();
237 if (toolBar != null)
238 toolBar.checkEnabled();
239
240 JInternalFrame[] frames = desktop.getAllFrames();
241 for (int i = 0; i < frames.length; i++)
242 ((ExpInternalFrame) frames[i]).checkEnabled();
243 }
244
245 /***
246 * This method contructs a DSInternalframe and places the given component
247 * smack bang in the middle of it. Document frames get put into the document
248 * layer.
249 */
250 public ExpInternalFrame createDocumentFrame(Component comp, Dimension size, String title, boolean doNothingOnClose) throws Exception
251 {
252 return createInternalFrame(comp, size, title, DOC_FRAME_LAYER, doNothingOnClose);
253 }
254
255 /***
256 * This method contructs a DSInternalframe and places the given component
257 * smack bang in the middle of it. Pallete frames get put into the palette
258 * layer
259 */
260 public ExpInternalFrame createPaletteFrame(Component comp, Dimension size, String title, boolean doNothingOnClose) throws Exception
261 {
262 return createInternalFrame(comp, size, title, PALETTE_LAYER, doNothingOnClose);
263 }
264
265 /***
266 * This method contructs a DSInternalframe and places the given component
267 * smack bang in the middle of it.
268 */
269 private ExpInternalFrame createInternalFrame(Component comp, Dimension size, String title, Integer layer, boolean doNothingOnClose) throws Exception
270 {
271 ExpInternalFrame frame = new ExpInternalFrame(comp, title);
272 if (doNothingOnClose)
273 frame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
274
275 if (layer.equals(PALETTE_LAYER))
276 {
277 frame.setIconifiable(false);
278 frame.setMaximizable(false);
279 frame.pack();
280 }
281
282 if (size != null)
283 {
284 int height = (int) size.getHeight();
285 int width = (int) size.getWidth();
286
287 if (size.getHeight() > desktop.getHeight())
288 height = (int)desktop.getHeight();
289
290 if (size.getWidth() > desktop.getWidth())
291 width = desktop.getWidth();
292
293 frame.setSize(new Dimension(width,height));
294 }
295
296 desktop.add(frame, layer);
297 ImageIcon icon = getFrameIcon();
298 if (icon != null)
299 frame.setFrameIcon(icon);
300
301 frame.setDoubleBuffered(true);
302 frame.setVisible(true);
303
304 return frame;
305 }
306
307 /***
308 * This method contructs a DSInternalframe and places the given component
309 * smack bang in the middle of it.
310 */
311 public void createBottomSplit(Component comp) throws Exception
312 {
313 this.bottomComponent = comp;
314 pane.add(bottomComponent, JSplitPane.BOTTOM);
315 pane.setDividerLocation(0.7);
316 validate();
317 repaint();
318 }
319
320 public void removeBottomSplit()
321 {
322 if (bottomComponent instanceof Closeable)
323 {
324 ActionEvent acev = new ActionEvent(this, 0, ((Closeable) bottomComponent).getCloseCommand());
325 ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
326 }
327 pane.remove(bottomComponent);
328 pane.setDividerLocation(1.0);
329 validate();
330 repaint();
331 }
332
333 public void createModalDialog(Component comp, Dimension size, String title, boolean doNothingOnClose) throws Exception
334 {
335 JDialog dialog = new JDialog(Application.getApplicationFrame(), true);
336 dialogs.put(comp, dialog);
337 dialog.getContentPane().setLayout(new BorderLayout());
338 dialog.getContentPane().add(comp, BorderLayout.CENTER);
339 if (size != null)
340 dialog.setSize(size);
341
342 dialog.setTitle(title);
343
344 if (doNothingOnClose)
345 dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
346
347 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
348 dialog.setLocation((d.width - dialog.getSize().width) / 2, (d.height - dialog.getSize().height) / 2);
349 dialog.setVisible(true);
350 }
351
352 public void closeDialogWithComponent(Component comp)
353 {
354 JDialog dialog = (JDialog) dialogs.get(comp);
355 if (dialog != null)
356 {
357 dialog.setVisible(false);
358 dialog.dispose();
359 }
360 }
361
362 /***
363 * This method looks through all of the frames on the desktop in the
364 * specified layer If it findsa frame whose applet matchesthe given
365 * component then it will call dispose() on that frame.
366 */
367 public void closeFrameWithComponent(Component comp, Integer layer) throws Exception
368 {
369 JInternalFrame[] frames = desktop.getAllFramesInLayer(layer.intValue());
370 for (int i = 0; i < frames.length; i++)
371 {
372 if (((ExpInternalFrame) frames[i]).getApplet() == comp)
373 {
374 frames[i].dispose();
375 Application.cleanUpForReference(comp);
376 break;
377 }
378 }
379
380 frames = desktop.getAllFrames();
381 if (frames.length > 0)
382 {
383 desktop.getDesktopManager().activateFrame(frames[0]);
384 }
385 }
386
387 /***
388 * This method looks through all of the frames on the desktop in the
389 * specified layer. If it findsa frame whose applet matchesthe given
390 * component then it will return that frame.
391 */
392 public JInternalFrame getFrameWithComponent(Component comp, int layer) throws Exception
393 {
394 JInternalFrame[] frames = desktop.getAllFramesInLayer(layer);
395 for (int i = 0; i < frames.length; i++)
396 {
397 if (((ExpInternalFrame) frames[i]).getApplet() == comp) { return frames[i]; }
398 }
399 return null;
400 }
401
402 /***
403 * This mthod will attempt to close all open frames in the DOC_FRAME_LAYER.
404 * It will return a boolean indicating what the result was.
405 */
406 public boolean closeAllFrames()
407 {
408 boolean allClosedSuccessfully = true;
409 JInternalFrame[] frames = desktop.getAllFramesInLayer(DOC_FRAME_LAYER.intValue());
410 for (int i = 0; i < frames.length; i++)
411 {
412 ((ExpInternalFrame) frames[i]).close();
413 if (!frames[i].isClosed())
414 allClosedSuccessfully = false;
415 }
416 return allClosedSuccessfully;
417 }
418
419 /***
420 * Method getFrameIcon. Returns the icon being used orthe application frame.
421 * Null if there isn't on.
422 *
423 * @return ImageIcon
424 */
425 public ImageIcon getFrameIcon()
426 {
427 try
428 {
429 if (iconImage == null)
430 {
431 if (iconImageLocation != null)
432 {
433 iconImage = FileSystemUtils.loadImage(iconImageLocation, this);
434 return new ImageIcon(iconImage);
435 }
436 } else
437 {
438 return new ImageIcon(iconImage);
439 }
440 } catch (Exception e)
441 {}
442
443 return null;
444 }
445
446 /***
447 * Returns the currently active frame
448 */
449 public void activateFrame(JInternalFrame frame)
450 {
451 if (getActiveFrame() == null || getActiveFrame() != frame)
452 {
453 desktop.getDesktopManager().activateFrame(frame);
454 }
455 }
456
457 /***
458 * Returns the currently active frame
459 */
460 public ExpInternalFrame getActiveFrame()
461 {
462 JInternalFrame[] frames = desktop.getAllFramesInLayer(DOC_FRAME_LAYER.intValue());
463 for (int i = 0; i < frames.length; i++)
464 {
465 if (frames[i].isSelected())
466 return (ExpInternalFrame) frames[i];
467 }
468 return null;
469 }
470
471 /***
472 * This method returns all the frames in the layer
473 */
474 public JInternalFrame[] getAllInternalFrames() throws Exception
475 {
476 return desktop.getAllFramesInLayer(DOC_FRAME_LAYER.intValue());
477 }
478
479 /***
480 * This method causes a file to be opened
481 */
482 public JDesktopPane getDesktop()
483 {
484 return desktop;
485 }
486
487 /***
488 * Returns the menubar for this application
489 *
490 * @return
491 */
492 public ExpMenuBar getExpMenuBar()
493 {
494 return this.menuBar;
495 }
496
497 /***
498 * Returns the toolbar for this application
499 *
500 * @return
501 */
502 public ExpToolBar getExpToolBar()
503 {
504 return this.toolBar;
505 }
506
507 /***
508 * Returns the compound listener for this application
509 *
510 * @return listener
511 */
512 public ExpListener getListener()
513 {
514 return listener;
515 }
516
517 /***
518 * Pops up a popup menu
519 * @param event
520 */
521 public void popupEvent(MouseEvent event) {
522 System.out.println("ExpFrame popup");
523 ExpPopupableMenu menu = new ExpPopupableMenu();
524 menu.show(event);
525 }
526
527 }
528