comparison src/samer/core_/util/swing/Frame.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Frame.java
3 *
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
5 * All rights reserved.
6 *
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 */
11
12 package samer.core.util.swing;
13 import samer.core.*;
14 import java.awt.*;
15 import java.awt.event.*;
16 import javax.swing.*;
17
18 /**
19 <p>
20 This is a useful kind of framed window that uses
21 the property set to get window size and position
22 and colours. It can also send window closing events
23 to the default command handler (see Agency) as
24 "exit" actions.
25
26 <p>
27 Addition: frame can dispose of itself when last component
28 is removed.
29 */
30
31 public class Frame extends JFrame
32 implements ComponentListener, ActionListener, Shell.Window
33 {
34 Node node;
35
36 public Frame() { this("frame"); }
37 public Frame( String nm) {
38 // remove "." from start of name
39 if (nm.startsWith(".")) nm=nm.substring(1);
40 node=new Node(nm); init();
41 }
42
43 public Container container() { return getContentPane(); }
44 public void dispose() { super.dispose(); }
45
46 public void expose() {
47 // this makes sure we don't show a zero-sized window
48 if (getBounds().isEmpty()) pack();
49 setVisible(true);
50 }
51
52 public Node getNode() { return node; }
53
54 // ..... Event Listener bits .......
55
56 public void componentHidden(ComponentEvent e) {}
57 public void componentShown(ComponentEvent e) {}
58 public void componentMoved(ComponentEvent e) { savepos(); }
59 public void componentResized(ComponentEvent e) { savepos(); }
60 public void actionPerformed(ActionEvent e) {
61 if (e.getActionCommand().equals("pack")) pack();
62 }
63
64 // override pack to stop window being bigger than screen
65 public void pack() {
66 super.pack();
67
68 Dimension d=getToolkit().getScreenSize();
69 Rectangle b=getBounds();
70 boolean f=false;
71 if (b.y+b.height>d.height) {
72 b.height=d.height-b.y;
73 b.width+=16; // this is to make room for vertical scroll bar (hack - sorry)
74 f=true;
75 }
76 if (b.x+b.width>d.width) { b.width=d.width-b.x; f=true; }
77 if (f) setBounds(b);
78 }
79
80 // ........... private bits ...............
81
82 private void savepos() { X.store(node.fullNameFor("bounds"),getBounds()); }
83
84 private void init()
85 {
86 Shell.push(node);
87 try {
88 String tit = Shell.getString("title",null);
89 Color bg = Shell.getColor("background",SystemColor.control);
90 Color fg = Shell.getColor("foreground",SystemColor.controlText);
91 Rectangle pos = X.rect(Shell.datum("bounds"),null);
92 // Font font = X.font(Shell.datum("font"),null);
93
94 if (tit!=null) setTitle(tit);
95 else {
96 // remove "." from start of name
97 String n=node.fullName();
98 if (n.startsWith(".")) n=n.substring(1);
99 // remove ".window" from end of name
100 if (n.endsWith(".window")) n=n.substring(0,n.length()-7);
101 setTitle(n);
102 }
103
104 if (pos!=null) setBounds(pos);
105 // if (font!=null) setFont(font);
106
107 // setBackground(bg);
108 // setForeground(fg);
109 } finally { Shell.pop(); }
110
111 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
112
113 JPopupMenu p=new JPopupMenu( "Frame");
114 p.add("pack").addActionListener(this);
115
116 addComponentListener(this);
117 MenuBuilder.addPopup(p,getContentPane()).setBackstop(true);
118 }
119
120
121 }
122