001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.awt;
019:
020: import java.lang.reflect.InvocationTargetException;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import javax.accessibility.AccessibleContext;
025: import javax.accessibility.AccessibleRole;
026: import javax.accessibility.AccessibleState;
027: import javax.accessibility.AccessibleStateSet;
028:
029: import org.apache.harmony.awt.internal.nls.Messages;
030:
031: public class Dialog extends Window {
032: private static final long serialVersionUID = 5920926903803293709L;
033:
034: private DialogModalContext modalContext;
035:
036: private final class DialogModalContext extends ModalContext {
037: private Window restoreActive;
038:
039: void runModalLoop(Window active) {
040: restoreActive = active;
041: super .runModalLoop();
042: }
043:
044: @Override
045: public void endModalLoop() {
046: if (restoreActive != null) {
047: restoreActive.toFront();
048: restoreActive = null;
049: }
050: super .endModalLoop();
051: }
052: }
053:
054: protected class AccessibleAWTDialog extends AccessibleAWTWindow {
055: private static final long serialVersionUID = 4837230331833941201L;
056:
057: @Override
058: public AccessibleStateSet getAccessibleStateSet() {
059: toolkit.lockAWT();
060: try {
061: AccessibleStateSet set = super .getAccessibleStateSet();
062: if (isModal()) {
063: set.add(AccessibleState.MODAL);
064: }
065: return set;
066: } finally {
067: toolkit.unlockAWT();
068: }
069: }
070:
071: @Override
072: public AccessibleRole getAccessibleRole() {
073: toolkit.lockAWT();
074: try {
075: return AccessibleRole.DIALOG;
076: } finally {
077: toolkit.unlockAWT();
078: }
079: }
080: }
081:
082: public Dialog(Frame owner, String title, boolean modal,
083: GraphicsConfiguration gc) {
084: super (owner, gc);
085: toolkit.lockAWT();
086: try {
087: setTitle(title);
088: setModal(modal);
089: setResizable(true);
090: } finally {
091: toolkit.unlockAWT();
092: }
093: }
094:
095: public Dialog(Dialog owner, String title, boolean modal,
096: GraphicsConfiguration gc) {
097: super (owner, gc);
098: toolkit.lockAWT();
099: try {
100: setTitle(title);
101: setModal(modal);
102: setFocusable(true);
103: setResizable(true);
104: setUndecorated(false);
105: } finally {
106: toolkit.unlockAWT();
107: }
108: }
109:
110: public Dialog(Frame owner, String title, boolean modal) {
111: this (owner, title, modal, null);
112: toolkit.lockAWT();
113: try {
114: } finally {
115: toolkit.unlockAWT();
116: }
117: }
118:
119: public Dialog(Frame owner, String title) {
120: this (owner, title, false, null);
121: toolkit.lockAWT();
122: try {
123: } finally {
124: toolkit.unlockAWT();
125: }
126: }
127:
128: public Dialog(Dialog owner, String title, boolean modal) {
129: this (owner, title, modal, null);
130: toolkit.lockAWT();
131: try {
132: } finally {
133: toolkit.unlockAWT();
134: }
135: }
136:
137: public Dialog(Dialog owner, String title) {
138: this (owner, title, false, null);
139: toolkit.lockAWT();
140: try {
141: } finally {
142: toolkit.unlockAWT();
143: }
144: }
145:
146: public Dialog(Frame owner, boolean modal) {
147: this (owner, "", modal, null); //$NON-NLS-1$
148: toolkit.lockAWT();
149: try {
150: } finally {
151: toolkit.unlockAWT();
152: }
153: }
154:
155: public Dialog(Frame owner) {
156: this (owner, "", false, null); //$NON-NLS-1$
157: toolkit.lockAWT();
158: try {
159: } finally {
160: toolkit.unlockAWT();
161: }
162: }
163:
164: public Dialog(Dialog owner) {
165: this (owner, "", false, null); //$NON-NLS-1$
166: toolkit.lockAWT();
167: try {
168: } finally {
169: toolkit.unlockAWT();
170: }
171: }
172:
173: @Override
174: protected String paramString() {
175: toolkit.lockAWT();
176: try {
177: return super .paramString()
178: + ",title=" + getTitle() //$NON-NLS-1$
179: + (isResizable() ? ",resizable" : "") + (isModal() ? ",modal" : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
180: } finally {
181: toolkit.unlockAWT();
182: }
183: }
184:
185: @Override
186: public void dispose() {
187: toolkit.lockAWT();
188: try {
189: if (modalContext != null
190: && modalContext.isModalLoopRunning()) {
191: modalContext.endModalLoop();
192: }
193: super .dispose();
194: } finally {
195: toolkit.unlockAWT();
196: }
197: }
198:
199: @Override
200: public void addNotify() {
201: toolkit.lockAWT();
202: try {
203: super .addNotify();
204: } finally {
205: toolkit.unlockAWT();
206: }
207: }
208:
209: @Override
210: public AccessibleContext getAccessibleContext() {
211: toolkit.lockAWT();
212: try {
213: return super .getAccessibleContext();
214: } finally {
215: toolkit.unlockAWT();
216: }
217: }
218:
219: @SuppressWarnings("deprecation")
220: @Deprecated
221: @Override
222: public void hide() {
223: toolkit.lockAWT();
224: try {
225: hideImpl();
226: } finally {
227: toolkit.unlockAWT();
228: }
229: }
230:
231: @SuppressWarnings("deprecation")
232: void hideImpl() {
233: if (modalContext != null && modalContext.isModalLoopRunning()) {
234: modalContext.endModalLoop();
235: super .hide();
236: } else {
237: super .hide();
238: }
239: }
240:
241: @SuppressWarnings("deprecation")
242: @Deprecated
243: @Override
244: public void show() {
245: showImpl();
246: }
247:
248: @SuppressWarnings("deprecation")
249: void showImpl() {
250: if (isModal()) {
251: if (EventQueue.isDispatchThread()) {
252: showModal();
253: } else {
254: toolkit.lockAWT();
255: try {
256: toolkit.unsafeInvokeAndWait(new Runnable() {
257: public void run() {
258: showModal();
259: }
260: });
261: } catch (InterruptedException e) {
262: e.printStackTrace();
263: } catch (InvocationTargetException e) {
264: e.printStackTrace();
265: } finally {
266: toolkit.unlockAWT();
267: }
268: }
269: } else {
270: super .show();
271: }
272: }
273:
274: @Override
275: public String getTitle() {
276: toolkit.lockAWT();
277: try {
278: return super .getTitle();
279: } finally {
280: toolkit.unlockAWT();
281: }
282: }
283:
284: public boolean isModal() {
285: toolkit.lockAWT();
286: try {
287: return modalContext != null;
288: } finally {
289: toolkit.unlockAWT();
290: }
291: }
292:
293: @Override
294: public boolean isResizable() {
295: toolkit.lockAWT();
296: try {
297: return super .isResizable();
298: } finally {
299: toolkit.unlockAWT();
300: }
301: }
302:
303: @Override
304: public boolean isUndecorated() {
305: toolkit.lockAWT();
306: try {
307: return super .isUndecorated();
308: } finally {
309: toolkit.unlockAWT();
310: }
311: }
312:
313: public void setModal(boolean modal) {
314: toolkit.lockAWT();
315: try {
316: if (modal == isModal()) {
317: return;
318: }
319: if (isVisible()) {
320: // awt.124=Cannot change the modality while the dialog is visible
321: throw new IllegalComponentStateException(Messages
322: .getString("awt.124")); //$NON-NLS-1$
323: }
324: modalContext = modal ? new DialogModalContext() : null;
325: } finally {
326: toolkit.unlockAWT();
327: }
328: }
329:
330: @Override
331: public void setResizable(boolean resizable) {
332: toolkit.lockAWT();
333: try {
334: super .setResizable(resizable);
335: } finally {
336: toolkit.unlockAWT();
337: }
338: }
339:
340: @Override
341: public void setTitle(String title) {
342: super .setTitle(title);
343: }
344:
345: @Override
346: public void setUndecorated(boolean undecorated) {
347: toolkit.lockAWT();
348: try {
349: super .setUndecorated(undecorated);
350: } finally {
351: toolkit.unlockAWT();
352: }
353: }
354:
355: @SuppressWarnings("deprecation")
356: private void showModal() {
357: Collection<Window> otherWindows;
358: Window active;
359: toolkit.lockAWT();
360: try {
361: active = KeyboardFocusManager
362: .getCurrentKeyboardFocusManager().getActiveWindow();
363: otherWindows = disableOtherWindows();
364: super .show();
365: } finally {
366: toolkit.unlockAWT();
367: }
368: modalContext.runModalLoop(active);
369: enableOtherWindows(otherWindows);
370: }
371:
372: private Collection<Window> disableOtherWindows() {
373: Iterator<?> i = toolkit.windows.iterator();
374: LinkedList<Window> result = new LinkedList<Window>();
375: while (i.hasNext()) {
376: Object obj = i.next();
377: if (obj instanceof Window) {
378: Window w = (Window) obj;
379: if (w.isEnabled() && w != this ) {
380: w.setEnabled(false);
381: result.add(w);
382: }
383: }
384: }
385: return result;
386: }
387:
388: private void enableOtherWindows(Collection<Window> disabledWindows) {
389: Iterator<Window> i = disabledWindows.iterator();
390: while (i.hasNext()) {
391: Window w = i.next();
392: w.setEnabled(true);
393: }
394: }
395:
396: @Override
397: AccessibleContext createAccessibleContext() {
398: return new AccessibleAWTDialog();
399: }
400:
401: @Override
402: String autoName() {
403: int number = toolkit.autoNumber.nextDialog++;
404: return "dialog" + Integer.toString(number); //$NON-NLS-1$
405: }
406:
407: @Override
408: Color getDefaultBackground() {
409: return SystemColor.control;
410: }
411:
412: void runModalLoop() {
413: modalContext.runModalLoop();
414: }
415:
416: void endModalLoop() {
417: modalContext.endModalLoop();
418: }
419: }
|