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 javax.swing;
019:
020: import java.awt.AWTEvent;
021: import java.awt.Component;
022: import java.awt.Container;
023: import java.awt.Dialog;
024: import java.awt.Frame;
025: import java.awt.Graphics;
026: import java.awt.GraphicsConfiguration;
027: import java.awt.HeadlessException;
028: import java.awt.KeyboardFocusManager;
029: import java.awt.LayoutManager;
030: import java.awt.event.WindowEvent;
031: import javax.accessibility.Accessible;
032: import javax.accessibility.AccessibleContext;
033: import javax.accessibility.AccessibleStateSet;
034: import org.apache.harmony.x.swing.Utilities;
035:
036: /**
037: * <p>
038: * <i>JDialog</i>
039: * </p>
040: * <h3>Implementation Notes:</h3>
041: * <ul>
042: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
043: * optimization, not as a guarantee of serialization compatibility.</li>
044: * </ul>
045: */
046: public class JDialog extends Dialog implements WindowConstants,
047: Accessible, RootPaneContainer {
048: private static final long serialVersionUID = -864070866424508218L;
049:
050: private static boolean defaultLookAndFeelDecorated;
051:
052: /**
053: * This class implements accessibility support for <code>JDialog</code>.
054: */
055: protected class AccessibleJDialog extends AccessibleAWTDialog {
056: private static final long serialVersionUID = 7312926302382808523L;
057:
058: protected AccessibleJDialog() {
059: }
060:
061: @Override
062: public String getAccessibleName() {
063: return getTitle();
064: }
065:
066: @Override
067: public AccessibleStateSet getAccessibleStateSet() {
068: return super .getAccessibleStateSet();
069: }
070: }
071:
072: protected JRootPane rootPane;
073:
074: protected boolean rootPaneCheckingEnabled;
075:
076: protected AccessibleContext accessibleContext;
077:
078: private int defaultCloseOperation = HIDE_ON_CLOSE;
079:
080: /**
081: * Constructs a modal or non-modal dialog with the specified title and
082: * with the specified owner.
083: * If the owner is null, the default shared owner will be used.
084: *
085: * @param owner - the owner
086: * @param title - the title of the dialog
087: * @param modal - true for a modal dialog, false for a non-modal dialog
088: * @param gc - the GraphicsConfiguration of the target screen device.
089: * If gc is null, GraphicsConfiguration of the owner will be used.
090: */
091: public JDialog(final Frame owner, final String title,
092: final boolean modal, final GraphicsConfiguration gc) {
093: super (owner == null ? JFrame.getSharedOwner() : owner, title,
094: modal, gc);
095: dialogInit();
096: }
097:
098: /**
099: * Constructs a modal or on-modal dialog with the specified title and with
100: * the specified owner.
101: *
102: * @param owner - the non-null owner
103: * @param title - the title of the dialog
104: * @param modal - true for a modal dialog, false for a non-modal dialog
105: * @param gc - the GraphicsConfiguration of the target screen device.
106: * If gc is null, GraphicsConfiguration of the owner will be used.
107: *
108: * @throws HeadlessException - if GraphicsEnvironment.isHeadless() returns
109: * true.
110: */
111: public JDialog(final Dialog owner, final String title,
112: final boolean modal, final GraphicsConfiguration gc)
113: throws HeadlessException {
114: super (owner, title, modal, gc);
115: dialogInit();
116: }
117:
118: /**
119: * Constructs a modal or non-modal dialog with the specified title and with
120: * the specified owner.
121: * If the owner is null, the default shared owner will be used.
122: *
123: * @param owner - the owner
124: * @param title - the title of the dialog
125: * @param modal - true for a modal dialog, false for a non-modal dialog
126: *
127: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
128: * returns true.
129: */
130: public JDialog(final Frame owner, final String title,
131: final boolean modal) throws HeadlessException {
132: this (owner, title, modal, null);
133: }
134:
135: /**
136: * Constructs a non-modal dialog with the specified title and with
137: * the specified owner. If the owner is null, the default shared owner
138: * will be used.
139: *
140: * @param owner - the owner
141: * @param title - the title of the dialog
142: *
143: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
144: * returns true.
145: */
146: public JDialog(final Frame owner, final String title)
147: throws HeadlessException {
148: this (owner, title, false);
149: }
150:
151: /**
152: * Constructs a modal or on-modal dialog with the specified title and with
153: * the specified owner.
154: *
155: * @param owner - the non-null owner
156: * @param title - the title of the dialog
157: * @param modal - true for a modal dialog, false for a non-modal dialog
158: *
159: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
160: * returns true.
161: */
162: public JDialog(final Dialog owner, final String title,
163: final boolean modal) throws HeadlessException {
164: this (owner, title, modal, null);
165: }
166:
167: /**
168: * Constructs a non-modal dialog with the specified title and with
169: * the specified owner.
170: *
171: * @param owner - the non-null owner
172: * @param title - the title of the dialog
173: *
174: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
175: * returns true.
176: */
177: public JDialog(final Dialog owner, final String title)
178: throws HeadlessException {
179: this (owner, title, false);
180: }
181:
182: /**
183: * Constructs a non-modal dialog without a title and with
184: * the specified owner.
185: * If the owner is null, the default shared owner will be used.
186: *
187: * @param owner - the owner
188: * @param modal - true for a modal dialog, false for a non-modal dialog
189: *
190: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
191: * returns true.
192: */
193: public JDialog(final Frame owner, final boolean modal)
194: throws HeadlessException {
195: this (owner, null, modal);
196: }
197:
198: /**
199: * Constructs a non-modal dialog without a title and with
200: * the specified owner.
201: * If the owner is null, the default shared owner will be used.
202: *
203: * @param owner - the owner
204: *
205: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
206: * returns true.
207: */
208: public JDialog(final Frame owner) throws HeadlessException {
209: this (owner, null, false);
210: }
211:
212: /**
213: * Constructs a modal or non-modal dialog without a title and with
214: * the specified owner.
215: *
216: * @param owner - the non-null owner
217: * @param modal - true for a modal dialog, false for a non-modal dialog
218: *
219: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
220: * returns true.
221: */
222: public JDialog(final Dialog owner, final boolean modal)
223: throws HeadlessException {
224: this (owner, null, modal);
225: }
226:
227: /**
228: * Constructs a non-modal dialog without a title and with
229: * the specified owner.
230: *
231: * @param owner - the non-null owner
232: *
233: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
234: * returns true.
235: */
236: public JDialog(final Dialog owner) throws HeadlessException {
237: this (owner, null, false);
238: }
239:
240: /**
241: * Constructs a non-modal dialog without a title and without
242: * a specified owner. The default shared owner will be used.
243: *
244: * @throws HeadlessException - if GraphicsEnvironment.isHeadless()
245: * returns true.
246: */
247: public JDialog() throws HeadlessException {
248: this ((Frame) null, null, false);
249: }
250:
251: /**
252: *
253: */
254: @Override
255: protected void addImpl(final Component comp,
256: final Object constraints, final int index) {
257: if (isRootPaneCheckingEnabled()) {
258: getContentPane().add(comp, constraints, index);
259: return;
260: }
261: super .addImpl(comp, constraints, index);
262: }
263:
264: /**
265: * Set rootPane property.
266: *
267: * @param root - new rootPane property value
268: */
269: protected void setRootPane(final JRootPane root) {
270: if (rootPane != null) {
271: remove(rootPane);
272: }
273: rootPane = root;
274: if (root != null) {
275: super .addImpl(root, null, 0);
276: }
277: }
278:
279: /**
280: * Get rootPane property.
281: *
282: * @return rootPane property
283: */
284: public JRootPane getRootPane() {
285: return rootPane;
286: }
287:
288: /**
289: * Called by the constructors to create the default rootPane property.
290: *
291: * @return default JRootPane
292: */
293: protected JRootPane createRootPane() {
294: return new JRootPane();
295: }
296:
297: /**
298: * Sets the menu bar for the frame
299: *
300: * @param menuBar - menu bar to be placed in the frame
301: */
302: public void setJMenuBar(final JMenuBar menuBar) {
303: getRootPane().setJMenuBar(menuBar);
304: }
305:
306: /**
307: * Returns the menu bar for the frame
308: *
309: * @return the menu bar for the frame
310: */
311: public JMenuBar getJMenuBar() {
312: return getRootPane().getJMenuBar();
313: }
314:
315: /**
316: * Sets layeredPane property.
317: *
318: * @param layeredPane - new layeredPane property value
319: */
320: public void setLayeredPane(final JLayeredPane layeredPane) {
321: getRootPane().setLayeredPane(layeredPane);
322: }
323:
324: /**
325: * Returns layeredPane property.
326: *
327: * @return layeredPane property
328: */
329: public JLayeredPane getLayeredPane() {
330: return getRootPane().getLayeredPane();
331: }
332:
333: /**
334: * Returns the accessible context for the dialog.
335: *
336: * @return the accessible context for the dialog
337: */
338: @Override
339: public AccessibleContext getAccessibleContext() {
340: if (accessibleContext == null) {
341: accessibleContext = new AccessibleJDialog();
342: }
343: return accessibleContext;
344: }
345:
346: /**
347: * Returns string representation of this dialog.
348: *
349: * @return string representation of this dialog
350: */
351: @Override
352: protected String paramString() {
353: String result = super .paramString();
354: if (getRootPane() != null) {
355: result += ",rootPane=" + getRootPane().toString();
356: } else {
357: result += ",rootPane=null";
358: }
359: return result;
360: }
361:
362: /**
363: * Implements actions depending on defaultCloseOperation property.
364: *
365: * @param e - window event
366: */
367: @Override
368: protected void processWindowEvent(final WindowEvent e) {
369: super .processWindowEvent(e);
370: if (e.getID() == WindowEvent.WINDOW_CLOSING) {
371: switch (getDefaultCloseOperation()) {
372: case DISPOSE_ON_CLOSE: // dispose
373: dispose();
374: break;
375: case HIDE_ON_CLOSE: // hide
376: setVisible(false);
377: break;
378: case DO_NOTHING_ON_CLOSE: // do nothing
379: break;
380: }
381: }
382: }
383:
384: /**
385: *
386: */
387: @Override
388: public void setLayout(final LayoutManager layout) {
389: if (isRootPaneCheckingEnabled()) {
390: getContentPane().setLayout(layout);
391: } else {
392: super .setLayout(layout);
393: }
394: }
395:
396: /**
397: * Just calls paint(g). This method was overridden to prevent
398: * an unnecessary call to clear the background.
399: *
400: * @param g - the graphics context to paint
401: */
402: @Override
403: public void update(final Graphics g) {
404: paint(g);
405: }
406:
407: /**
408: * Sets contentPane property.
409: *
410: * @param contentPane - new contentPane property value
411: */
412: public void setContentPane(final Container contentPane) {
413: getRootPane().setContentPane(contentPane);
414: }
415:
416: /**
417: * Returns the contentPane property.
418: *
419: * @return the contentPane property
420: */
421: public Container getContentPane() {
422: return getRootPane().getContentPane();
423: }
424:
425: /**
426: * Set glassPane property.
427: *
428: * @param glassPane - new glassPane property value
429: */
430: public void setGlassPane(final Component glassPane) {
431: getRootPane().setGlassPane(glassPane);
432: }
433:
434: /**
435: *
436: */
437: @Override
438: public void remove(final Component comp) {
439: if (comp == getRootPane()) {
440: // remove directly from JDialog
441: super .remove(comp);
442: } else {
443: getContentPane().remove(comp);
444: }
445: }
446:
447: /**
448: * Returns glassPane property.
449: *
450: * @return glassPane property
451: */
452: public Component getGlassPane() {
453: return getRootPane().getGlassPane();
454: }
455:
456: /**
457: * Sets rootPaneCheckingEnabled.
458: *
459: * @param enabled - new rootPaneCheckingEnabled value
460: */
461: protected void setRootPaneCheckingEnabled(final boolean enabled) {
462: rootPaneCheckingEnabled = enabled;
463: }
464:
465: /**
466: * Sets defaultCloseOperation property.
467: *
468: * @param operation - new defaultCloseOperation value
469: */
470: public void setDefaultCloseOperation(final int operation) {
471: defaultCloseOperation = operation;
472: //super.setDefaultCloseOperation(operation);
473: }
474:
475: /**
476: * Returns rootPaneCheckingEnabled value.
477: *
478: * @return value of rootPaneCheckingEnabled
479: */
480: protected boolean isRootPaneCheckingEnabled() {
481: return rootPaneCheckingEnabled;
482: }
483:
484: /**
485: * Called by the constructors to init JDialog
486: */
487: protected void dialogInit() {
488: setRootPaneCheckingEnabled(true);
489: setRootPane(createRootPane());
490: setLocale(JComponent.getDefaultLocale());
491: // check isDefaultLookAndFeelDecorated()
492: if (isDefaultLookAndFeelDecorated()) {
493: setUndecorated(Utilities
494: .lookAndFeelSupportsWindowDecorations());
495: getRootPane().setWindowDecorationStyle(
496: JRootPane.PLAIN_DIALOG);
497: }
498: /*
499: * Enabling WindowEvents is required for processWindowEvent()
500: * to function.
501: */
502: enableEvents(AWTEvent.WINDOW_EVENT_MASK);
503: /*
504: * Enabling KeyEvents is required for events to be propagated over
505: * components hierarchy.
506: */
507: enableEvents(AWTEvent.KEY_EVENT_MASK);
508: /*
509: * This class is a top level container for all Swing components. So,
510: * it has to define a default focus traversal policy.
511: */
512: setFocusTraversalPolicy(KeyboardFocusManager
513: .getCurrentKeyboardFocusManager()
514: .getDefaultFocusTraversalPolicy());
515: }
516:
517: /**
518: * Returns defaultCloseOperation value.
519: *
520: * @return defaultCloseOperation value
521: */
522: public int getDefaultCloseOperation() {
523: return defaultCloseOperation;
524: }
525:
526: public static void setDefaultLookAndFeelDecorated(
527: final boolean defaultLookAndFeelDecorated) {
528: JDialog.defaultLookAndFeelDecorated = defaultLookAndFeelDecorated;
529: }
530:
531: public static boolean isDefaultLookAndFeelDecorated() {
532: return defaultLookAndFeelDecorated;
533: }
534: }
|