1 package org.catacomb.druid.swing;
2
3
4 import org.catacomb.druid.event.ClosureListener;
5 import org.catacomb.report.E;
6
7 import java.awt.Color;
8 import java.awt.Rectangle;
9 import java.awt.Toolkit;
10 import java.awt.event.ComponentEvent;
11 import java.awt.event.ComponentListener;
12 import java.awt.event.WindowEvent;
13 import java.awt.event.WindowListener;
14 import java.lang.reflect.Field;
15 import java.lang.reflect.Method;
16 import java.util.StringTokenizer;
17
18 import javax.swing.JFrame;
19 import javax.swing.WindowConstants;
20
21
22 import java.awt.Dimension;
23
24
25 public class DFrame extends JFrame implements WindowListener, ComponentListener {
26
27 static final long serialVersionUID = 1001;
28
29 String name = "anon";
30
31 int px, py, pw, ph;
32
33 int desx;
34 int desy;
35 int[] pidat;
36
37
38 final static int UNKNOWN = 0;
39 final static int YES = 1;
40 final static int NO = 2;
41
42 static int stateSettable = 0;
43
44 static int FRAME_NORMAL = 0;
45 static Method setStateMethod = null;
46
47 ClosureListener clisten;
48
49
50 public DFrame() {
51 this("anon");
52 }
53
54
55 public DFrame(String s) {
56 super();
57 name = s;
58 setTitle(s);
59
60 addWindowListener(this);
61 addComponentListener(this);
62
63 getContentPane().addComponentListener(this);
64
65 setBg(LAF.getBackgroundColor());
66
67 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
68 Toolkit.getDefaultToolkit().setDynamicLayout(true);
69 }
70
71
72 public void setClosureListener(ClosureListener cl) {
73 clisten = cl;
74 }
75
76
77 public void setBg(Color c) {
78 setBackground(c);
79 }
80
81
82 public int[] getIntArraySize() {
83 int[] wh = new int[2];
84 wh[0] = getWidth();
85 wh[1] = getHeight();
86 return wh;
87 }
88
89
90 public void setName(String s) {
91 name = s;
92 }
93
94
95 public String getName() {
96 return name;
97 }
98
99
100 public void setTitle(String s) {
101 super.setTitle(s);
102 }
103
104
105 public void myprint(String s) {
106 System.out.println(" cframe wcev " + name + " " + s);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 void checkStateSettable() {
129 setStateMethod = null;
130 FRAME_NORMAL = -999;
131 try {
132 Method[] ms = getClass().getMethods();
133 for (int i = 0; i < ms.length; i++) {
134 if (ms[i].getName().equals("setState")) {
135 setStateMethod = ms[i];
136 break;
137 }
138 }
139
140 Field[] fs = getClass().getFields();
141 for (int i = 0; i < fs.length; i++) {
142 if (fs[i].getName().equals("NORMAL")) {
143 FRAME_NORMAL = fs[i].getInt(this);
144 }
145 }
146
147 } catch (Exception e) {
148 System.out.println("couldnt do frame stuf " + e);
149 }
150 stateSettable = ((setStateMethod != null && FRAME_NORMAL != -999) ? YES : NO);
151 }
152
153
154 void applySetState() {
155 try {
156 Integer ii = new Integer(FRAME_NORMAL);
157 Object[] args = { ii };
158 setStateMethod.invoke(this, args);
159 } catch (Exception e) {
160 System.out.println("caught exception when trying to set state " + e);
161 }
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public void componentHidden(ComponentEvent e) {
175 }
176
177
178 public void componentMoved(ComponentEvent e) {
179 dce(e);
180 }
181
182
183 public void componentResized(ComponentEvent e) {
184 dce(e);
185 }
186
187
188 public void componentShown(ComponentEvent e) {
189 };
190
191 private void dce(ComponentEvent e) {
192 if (isVisible()) {
193 Rectangle r = getBounds();
194 if (Math.abs(r.x - px) > 10 || Math.abs(r.y - py) > 10 || Math.abs(r.width - pw) > 10
195 || Math.abs(r.height - ph) > 10) {
196 px = r.x;
197 py = r.y;
198 pw = r.width;
199 ph = r.height;
200
201 }
202 }
203 }
204
205
206 public void windowActivated(WindowEvent e) {
207 }
208
209
210 public void windowDeactivated(WindowEvent e) {
211 }
212
213
214 public void windowClosed(WindowEvent e) {
215 if (clisten != null) {
216 clisten.closed();
217 }
218 }
219
220
221 public void windowClosing(WindowEvent e) {
222 if (clisten != null) {
223 clisten.requestClose();
224 }
225 }
226
227
228
229 public void windowDeiconified(WindowEvent e) {
230 }
231
232
233 public void windowIconified(WindowEvent e) {
234 }
235
236
237 public void windowOpened(WindowEvent e) {
238 }
239
240
241
242 public void newState(String s) {
243 if (s.equals("iconified")) {
244 setVisible(false);
245
246 } else if (s.equals("deiconified")) {
247 setVisible(false);
248
249 } else if (s.equals("closed")) {
250 setVisible(false);
251
252 } else if (s.startsWith("moved")) {
253 StringTokenizer st = new StringTokenizer(s, ",");
254 st.nextToken();
255 int ix = Integer.parseInt(st.nextToken());
256 int iy = Integer.parseInt(st.nextToken());
257 int iw = Integer.parseInt(st.nextToken());
258 int ih = Integer.parseInt(st.nextToken());
259 setBounds(new Rectangle(ix, iy, iw, ih));
260
261 } else {
262 E.error(" - unknown stat change in frame " + s);
263
264 }
265 }
266
267
268 public void setPreferredSize(int w, int h) {
269 setPreferredSize(new Dimension(w, h));
270 }
271
272 }