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: package javax.swing;
018:
019: import java.awt.AWTEvent;
020: import java.awt.Component;
021: import java.awt.Container;
022: import java.awt.Frame;
023: import java.awt.Graphics;
024: import java.awt.GraphicsConfiguration;
025: import java.awt.HeadlessException;
026: import java.awt.Image;
027: import java.awt.KeyboardFocusManager;
028: import java.awt.LayoutManager;
029: import java.awt.event.WindowEvent;
030: import javax.accessibility.Accessible;
031: import javax.accessibility.AccessibleContext;
032: import javax.accessibility.AccessibleStateSet;
033: import org.apache.harmony.x.swing.internal.nls.Messages;
034: import org.apache.harmony.x.swing.StringConstants;
035: import org.apache.harmony.x.swing.Utilities;
036:
037: /**
038: * <p>
039: * <i>JFrame</i>
040: * </p>
041: * <h3>Implementation Notes:</h3>
042: * <ul>
043: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
044: * optimization, not as a guarantee of serialization compatibility.</li>
045: * </ul>
046: */
047: public class JFrame extends Frame implements WindowConstants,
048: Accessible, RootPaneContainer {
049: private static final long serialVersionUID = -1026528232454752719L;
050:
051: public static final int EXIT_ON_CLOSE = 3;
052:
053: private static boolean defaultLookAndFeelDecorated;
054:
055: private static Frame sharedFrame;
056:
057: /**
058: * This class implements accessibility support for <code>JFrame</code>.
059: */
060: protected class AccessibleJFrame extends AccessibleAWTFrame {
061: private static final long serialVersionUID = -6604775962178425920L;
062:
063: protected AccessibleJFrame() {
064: super ();
065: }
066:
067: @Override
068: public String getAccessibleName() {
069: return getTitle();
070: }
071:
072: @Override
073: public AccessibleStateSet getAccessibleStateSet() {
074: return super .getAccessibleStateSet();
075: }
076: }
077:
078: protected JRootPane rootPane;
079:
080: protected boolean rootPaneCheckingEnabled;
081:
082: protected AccessibleContext accessibleContext;
083:
084: private int defaultCloseOperation = HIDE_ON_CLOSE;
085:
086: /**
087: * Constructs a new frame in the specified GraphicsConfiguration and
088: * the specified title.
089: *
090: * @param title - title of the frame; if title is null, an empty title
091: * is assumed
092: * @param gc - the GraphicsConfiguration to construct the frame;
093: * if gc is null, the system default GraphicsConfiguration is assumed
094: */
095: public JFrame(final String title, final GraphicsConfiguration gc) {
096: super (title, gc);
097: frameInit();
098: }
099:
100: /**
101: * Constructs a new frame with the specified title which is initially
102: * invisible.
103: *
104: * @param title - title of the frame; if title is null, an empty title
105: * is assumed
106: *
107: * @throws HeadlessException - when GraphicsEnvironment.isHeadless()
108: * returns true
109: */
110: public JFrame(final String title) throws HeadlessException {
111: super (title);
112: frameInit();
113: }
114:
115: /**
116: * Constructs a new frame in the specified GraphicsConfiguration and
117: * an empty title.
118: *
119: * @param gc - the GraphicsConfiguration to construct the frame;
120: * if gc is null, the system default GraphicsConfiguration is assumed
121: */
122: public JFrame(final GraphicsConfiguration gc) {
123: super (gc);
124: frameInit();
125: }
126:
127: /**
128: * Constructs a new frame with an empty title which is initially invisible.
129: *
130: * @throws HeadlessException - when GraphicsEnvironment.isHeadless()
131: * returns true
132: */
133: public JFrame() throws HeadlessException {
134: frameInit();
135: }
136:
137: @Override
138: protected void addImpl(final Component comp,
139: final Object constraints, final int index) {
140: if (isRootPaneCheckingEnabled()) {
141: getContentPane().add(comp, constraints, index);
142: return;
143: }
144: super .addImpl(comp, constraints, index);
145: }
146:
147: /**
148: * Set rootPane property.
149: *
150: * @param root - new rootPane property value
151: */
152: protected void setRootPane(final JRootPane root) {
153: if (rootPane != null) {
154: remove(rootPane);
155: }
156: rootPane = root;
157: if (root != null) {
158: super .addImpl(root, null, 0);
159: }
160: }
161:
162: /**
163: * Get rootPane property.
164: *
165: * @return rootPane property
166: */
167: public JRootPane getRootPane() {
168: return rootPane;
169: }
170:
171: /**
172: * Called by the constructors to create the default rootPane property.
173: *
174: * @return default JRootPane
175: */
176: protected JRootPane createRootPane() {
177: return new JRootPane();
178: }
179:
180: /**
181: * Sets the menu bar for the frame.
182: *
183: * @param menuBar - menu bar to be placed in the frame
184: */
185: public void setJMenuBar(final JMenuBar menuBar) {
186: getRootPane().setJMenuBar(menuBar);
187: }
188:
189: /**
190: * Returns the menu bar for the frame
191: *
192: * @return the menu bar for the frame
193: */
194: public JMenuBar getJMenuBar() {
195: return getRootPane().getJMenuBar();
196: }
197:
198: /**
199: * Sets layeredPane property.
200: *
201: * @param layeredPane - new layeredPane property value
202: */
203: public void setLayeredPane(final JLayeredPane layeredPane) {
204: getRootPane().setLayeredPane(layeredPane);
205: }
206:
207: /**
208: * Returns layeredPane property.
209: *
210: * @return layeredPane property
211: */
212: public JLayeredPane getLayeredPane() {
213: return getRootPane().getLayeredPane();
214: }
215:
216: /**
217: * Returns the accessible context for the frame.
218: *
219: * @return the accessible context for the frame
220: */
221: @Override
222: public AccessibleContext getAccessibleContext() {
223: if (accessibleContext == null) {
224: accessibleContext = new AccessibleJFrame();
225: }
226: return accessibleContext;
227: }
228:
229: /**
230: * Returns string representation of this frame.
231: *
232: * @return string representation of this frame
233: */
234: @Override
235: protected String paramString() {
236: String result = super .paramString();
237: if (getRootPane() != null) {
238: result += ",rootPane=" + getRootPane().toString();
239: } else {
240: result += ",rootPane=null";
241: }
242: return result;
243: }
244:
245: /**
246: * Implements actions depending on defaultCloseOperation property.
247: *
248: * @param e - window event
249: */
250: @Override
251: protected void processWindowEvent(final WindowEvent e) {
252: super .processWindowEvent(e);
253: if (e.getID() == WindowEvent.WINDOW_CLOSING) {
254: switch (getDefaultCloseOperation()) {
255: case DISPOSE_ON_CLOSE: // dispose
256: dispose();
257: break;
258: case HIDE_ON_CLOSE: // hide
259: setVisible(false);
260: break;
261: case DO_NOTHING_ON_CLOSE: // do nothing
262: break;
263: case EXIT_ON_CLOSE: // exit
264: System.exit(0);
265: break;
266: }
267: }
268: }
269:
270: /**
271: *
272: */
273: @Override
274: public void setLayout(final LayoutManager layout) {
275: if (isRootPaneCheckingEnabled()) {
276: getContentPane().setLayout(layout);
277: } else {
278: super .setLayout(layout);
279: }
280: }
281:
282: /**
283: * Just calls paint(g). This method was overridden to prevent
284: * an unnecessary call to clear the background.
285: *
286: * @param g - the graphics context to paint
287: */
288: @Override
289: public void update(final Graphics g) {
290: paint(g);
291: }
292:
293: /**
294: * Sets contentPane property.
295: *
296: * @param contentPane - new contentPane property value
297: */
298: public void setContentPane(final Container contentPane) {
299: getRootPane().setContentPane(contentPane);
300: }
301:
302: /**
303: * Returns the contentPane property.
304: *
305: * @return the contentPane property
306: */
307: public Container getContentPane() {
308: return getRootPane().getContentPane();
309: }
310:
311: /**
312: * Set glassPane property.
313: *
314: * @param glassPane - new glassPane property value
315: */
316: public void setGlassPane(final Component glassPane) {
317: getRootPane().setGlassPane(glassPane);
318: }
319:
320: /**
321: *
322: */
323: @Override
324: public void remove(final Component comp) {
325: // Note: differs from JInternalFrame's behavior,
326: // how titlePane is removed?
327: if (comp == getRootPane()) {
328: // remove directly from JDialog
329: super .remove(comp);
330: } else {
331: getContentPane().remove(comp);
332: }
333: }
334:
335: /**
336: * Returns glassPane property.
337: *
338: * @return glassPane property
339: */
340: public Component getGlassPane() {
341: return getRootPane().getGlassPane();
342: }
343:
344: /**
345: * Sets rootPaneCheckingEnabled.
346: *
347: * @param enabled - new rootPaneCheckingEnabled value
348: */
349: protected void setRootPaneCheckingEnabled(final boolean enabled) {
350: rootPaneCheckingEnabled = enabled;
351: }
352:
353: /**
354: * Sets defaultCloseOperation property. This is a bound property.
355: *
356: * @param operation - new defaultCloseOperation value
357: */
358: public void setDefaultCloseOperation(final int operation) {
359: int oldOperation = getDefaultCloseOperation();
360: switch (operation) {
361: case DO_NOTHING_ON_CLOSE:
362: case HIDE_ON_CLOSE:
363: case DISPOSE_ON_CLOSE:
364: defaultCloseOperation = operation;
365: break;
366: case EXIT_ON_CLOSE:
367: SecurityManager security = System.getSecurityManager();
368: if (security != null) {
369: security.checkExit(0);
370: }
371: defaultCloseOperation = operation;
372: break;
373: default:
374: throw new IllegalArgumentException(
375: Messages
376: .getString(
377: "swing.B2", "defaultCloseOperation", //$NON-NLS-1$ //$NON-NLS-2$
378: "DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, EXIT_ON_CLOSE")); //$NON-NLS-1$
379: }
380: firePropertyChange("defaultCloseOperation", oldOperation,
381: operation);
382: }
383:
384: /**
385: * Returns rootPaneCheckingEnabled value.
386: *
387: * @return value of rootPaneCheckingEnabled
388: */
389: protected boolean isRootPaneCheckingEnabled() {
390: return rootPaneCheckingEnabled;
391: }
392:
393: /**
394: * Called by the constructors to init JFrame
395: */
396: protected void frameInit() {
397: setRootPaneCheckingEnabled(true);
398: setRootPane(createRootPane());
399: setLocale(JComponent.getDefaultLocale());
400: // check isDefaultLookAndFeelDecorated()
401: if (isDefaultLookAndFeelDecorated()) {
402: setUndecorated(Utilities
403: .lookAndFeelSupportsWindowDecorations());
404: getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
405: }
406: setBackground(getContentPane().getBackground());
407: // enable events
408: enableEvents(AWTEvent.WINDOW_EVENT_MASK);
409: enableEvents(AWTEvent.KEY_EVENT_MASK);
410: setFocusTraversalPolicy(KeyboardFocusManager
411: .getCurrentKeyboardFocusManager()
412: .getDefaultFocusTraversalPolicy());
413: }
414:
415: /**
416: * Returns defaultCloseOperation value.
417: *
418: * @return defaultCloseOperation value
419: */
420: public int getDefaultCloseOperation() {
421: return defaultCloseOperation;
422: }
423:
424: @Override
425: public void setIconImage(final Image image) {
426: Image oldValue = getIconImage();
427: super .setIconImage(image);
428: firePropertyChange(StringConstants.ICON_IMAGE_PROPERTY,
429: oldValue, image);
430: }
431:
432: public static void setDefaultLookAndFeelDecorated(
433: final boolean defaultLookAndFeelDecorated) {
434: JFrame.defaultLookAndFeelDecorated = defaultLookAndFeelDecorated;
435: }
436:
437: public static boolean isDefaultLookAndFeelDecorated() {
438: return defaultLookAndFeelDecorated;
439: }
440:
441: /**
442: * Returns the frame that is used as a default shared owner for
443: * <code>JDialog</code> and <code>JWindow</code>.
444: */
445: static Frame getSharedOwner() {
446: if (sharedFrame == null) {
447: sharedFrame = new Frame();
448: }
449: return sharedFrame;
450: }
451: }
|