KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > InnerPanel


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.common;
25
26 // ToolBox
27
import org.enhydra.tool.common.event.FirstFocusEvent;
28 import org.enhydra.tool.common.event.FirstFocusListener;
29
30 // JDK
31
import java.awt.*;
32 import java.awt.event.FocusAdapter JavaDoc;
33 import java.awt.event.FocusEvent JavaDoc;
34 import java.awt.event.WindowEvent JavaDoc;
35 import java.awt.event.WindowAdapter JavaDoc;
36 import java.beans.*;
37 import java.lang.ref.WeakReference JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Arrays JavaDoc;
40 import javax.swing.*;
41
42 //
43
public class InnerPanel extends JPanel implements FirstFocusListener {
44     private boolean init = true;
45     private boolean saveOnClose = false;
46     private String JavaDoc progressTitle = new String JavaDoc();
47     private FirstFocusListener[] firstListeners = new FirstFocusListener[0];
48     private LocalFocusListener focusListener = null;
49     private LocalWindowListener windowListener = null;
50
51     public static InnerPanel findAncestor(Component comp) {
52         Container ancestor = null;
53         InnerPanel found = null;
54
55         ancestor = comp.getParent();
56         while (ancestor != null) {
57             ancestor = ancestor.getParent();
58             if (ancestor instanceof InnerPanel) {
59                 found = (InnerPanel) ancestor;
60                 break;
61             }
62         }
63         return found;
64     }
65
66     public InnerPanel() {
67         super();
68     }
69
70     public Window getWindow() {
71         Window window = null;
72
73         if (getTopLevelAncestor() instanceof Window) {
74             window = (Window) getTopLevelAncestor();
75         }
76         return window;
77     }
78
79     public Dialog getDialog() {
80         Dialog dialog = null;
81
82         if (getWindow() instanceof Dialog) {
83             dialog = (Dialog) getWindow();
84         }
85         return dialog;
86     }
87
88     public void setSaveOnClose(boolean b) {
89         saveOnClose = b;
90         if (saveOnClose) {
91            preShow();
92         }
93     }
94
95     public boolean isSaveOnClose() {
96         return saveOnClose;
97     }
98
99     /**
100      * Remove all object references.
101      */

102     public void clearAll() {
103         clearWindowListener();
104         clearFocusListener();
105         removeAll();
106         firstListeners = new FirstFocusListener[0];
107     }
108
109     public String JavaDoc getProgressTitle() {
110         return progressTitle;
111     }
112
113     public void setProgressTitle(String JavaDoc t) {
114         progressTitle = t;
115     }
116
117     public void initPreferredSize() {
118         Dimension d = new Dimension();
119
120         d.height = 342;
121         d.width = 542;
122         setPreferredSize(d);
123     }
124
125     public void save() {
126         System.err.println("InnerPanel.save() not implemented");
127     }
128
129     public void addFirstFocusListener(FirstFocusListener listener) {
130         ArrayList JavaDoc list = null;
131
132         list = new ArrayList JavaDoc(Arrays.asList(firstListeners));
133         if (!list.contains(listener)) {
134             list.add(listener);
135             list.trimToSize();
136             firstListeners = new FirstFocusListener[list.size()];
137             firstListeners =
138                 (FirstFocusListener[]) list.toArray(firstListeners);
139         }
140         list.clear();
141     }
142
143     public void removeFirstFocusListener(FirstFocusListener listener) {
144         ArrayList JavaDoc list = null;
145
146         list = new ArrayList JavaDoc(Arrays.asList(firstListeners));
147         if (list.contains(listener)) {
148             list.remove(listener);
149             list.trimToSize();
150             firstListeners = new FirstFocusListener[list.size()];
151             firstListeners =
152                 (FirstFocusListener[]) list.toArray(firstListeners);
153         }
154         list.clear();
155     }
156
157     private void notifyFirstFocusListeners() {
158         FirstFocusEvent event = new FirstFocusEvent(this);
159
160         for (int i = 0; i < firstListeners.length; i++) {
161             firstListeners[i].onFirstFocus(event);
162         }
163         firstListeners = new FirstFocusListener[0];
164     }
165
166     // protected
167
protected Component[] getFirstFocusComponents() {
168         Component[] comps = new Component[1];
169
170         comps[0] = this;
171         System.err.println("InnerPanel.getFirstFocusComponents() not implemented");
172         return comps;
173     }
174
175     public void onFirstFocus(FirstFocusEvent event) {
176         preShow();
177         if (isSaveOnClose()) {
178             initWindowListener();
179         }
180         clearFocusListener();
181     }
182
183     public void preShow() {
184         Component[] comps = getFirstFocusComponents();
185         addFirstFocusListener(this);
186         if (focusListener == null) {
187             focusListener = new LocalFocusListener();
188             for (int i = 0; i < comps.length; i++) {
189                 comps[i].addFocusListener(focusListener);
190             }
191         }
192     }
193
194     private void clearFocusListener() {
195         Component[] comps = getFirstFocusComponents();
196
197         if (focusListener != null) {
198             for (int i = 0; i < comps.length; i++) {
199                 comps[i].removeFocusListener(focusListener);
200             }
201         }
202         focusListener = null;
203     }
204
205     private void initWindowListener() {
206         if (windowListener == null) {
207             if (getWindow() != null) {
208                 windowListener = new LocalWindowListener();
209                 getWindow().addWindowListener(windowListener);
210             }
211         }
212     }
213
214     private void clearWindowListener() {
215         Window win = null;
216
217         if (windowListener != null) {
218             if (getWindow() != null) {
219                 getWindow().removeWindowListener(windowListener);
220             }
221         }
222         windowListener = null;
223     }
224
225     //
226
private class LocalFocusListener extends FocusAdapter JavaDoc {
227         public void focusGained(FocusEvent e) {
228             if (init) {
229                 init = false;
230                 notifyFirstFocusListeners();
231             }
232         }
233
234     }
235     private class LocalWindowListener extends WindowAdapter JavaDoc {
236         private boolean invoked = false;
237
238         public void windowClosed(WindowEvent JavaDoc event) {
239             if (!invoked) {
240                 saveAndClear(event.getWindow());
241             }
242         }
243
244         public void windowClosing(WindowEvent JavaDoc event) {
245             invoked = true;
246             saveAndClear(event.getWindow());
247         }
248
249         private void saveAndClear(Window win) {
250             save();
251             clearAll();
252             win.dispose();
253         }
254
255     }
256 }
257
Popular Tags