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.applet.Applet;
021: import java.awt.AWTEvent;
022: import java.awt.BorderLayout;
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Container;
026: import java.awt.Graphics;
027: import java.awt.HeadlessException;
028: import java.awt.KeyboardFocusManager;
029: import java.awt.LayoutManager;
030: import javax.accessibility.Accessible;
031: import javax.accessibility.AccessibleContext;
032:
033: /**
034: * <p>
035: * <i>JApplet</i>
036: * </p>
037: * <h3>Implementation Notes:</h3>
038: * <ul>
039: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
040: * optimization, not as a guarantee of serialization compatibility.</li>
041: * </ul>
042: */
043: public class JApplet extends Applet implements Accessible,
044: RootPaneContainer {
045: private static final long serialVersionUID = -8372957450536936161L;
046:
047: protected JRootPane rootPane;
048:
049: protected boolean rootPaneCheckingEnabled;
050:
051: protected AccessibleContext accessibleContext;
052:
053: public JApplet() throws HeadlessException {
054: setLayout(new BorderLayout());
055: setRootPaneCheckingEnabled(true);
056: setRootPane(createRootPane());
057: setLocale(JComponent.getDefaultLocale());
058: setBackground(Color.white);
059: // enable events
060: enableEvents(AWTEvent.KEY_EVENT_MASK);
061: setFocusTraversalPolicyProvider(true);
062: setFocusTraversalPolicy(KeyboardFocusManager
063: .getCurrentKeyboardFocusManager()
064: .getDefaultFocusTraversalPolicy());
065: }
066:
067: protected class AccessibleJApplet extends AccessibleApplet {
068: private static final long serialVersionUID = -7345678942864978889L;
069:
070: protected AccessibleJApplet() {
071: super ();
072: }
073: }
074:
075: @Override
076: protected void addImpl(Component comp, Object constraints, int index) {
077: if (isRootPaneCheckingEnabled()) {
078: getContentPane().add(comp, constraints, index);
079: return;
080: }
081: super .addImpl(comp, constraints, index);
082: }
083:
084: protected void setRootPane(JRootPane root) {
085: if (rootPane != null) {
086: remove(rootPane);
087: }
088: rootPane = root;
089: if (root != null) {
090: super .addImpl(root, null, 0);
091: }
092: }
093:
094: public JRootPane getRootPane() {
095: return rootPane;
096: }
097:
098: protected JRootPane createRootPane() {
099: return new JRootPane();
100: }
101:
102: public void setJMenuBar(JMenuBar menuBar) {
103: getRootPane().setJMenuBar(menuBar);
104: }
105:
106: public JMenuBar getJMenuBar() {
107: return getRootPane().getJMenuBar();
108: }
109:
110: public void setLayeredPane(JLayeredPane layeredPane) {
111: getRootPane().setLayeredPane(layeredPane);
112: }
113:
114: public JLayeredPane getLayeredPane() {
115: return getRootPane().getLayeredPane();
116: }
117:
118: @Override
119: public AccessibleContext getAccessibleContext() {
120: if (accessibleContext == null) {
121: accessibleContext = new AccessibleJApplet();
122: }
123: return accessibleContext;
124: }
125:
126: @Override
127: public void setLayout(LayoutManager layout) {
128: if (isRootPaneCheckingEnabled()) {
129: getContentPane().setLayout(layout);
130: } else {
131: super .setLayout(layout);
132: }
133: }
134:
135: /**
136: * Just calls paint(g). This method was overridden to prevent
137: * an unnecessary call to clear the background.
138: *
139: * @param g - the graphics context to paint
140: */
141: @Override
142: public void update(Graphics g) {
143: paint(g);
144: }
145:
146: public void setContentPane(Container contentPane) {
147: getRootPane().setContentPane(contentPane);
148: }
149:
150: public Container getContentPane() {
151: return getRootPane().getContentPane();
152: }
153:
154: public void setGlassPane(Component glassPane) {
155: getRootPane().setGlassPane(glassPane);
156: }
157:
158: @Override
159: public void remove(Component comp) {
160: if (comp == getRootPane()) {
161: // remove directly from JApplet
162: super .remove(comp);
163: } else {
164: getContentPane().remove(comp);
165: }
166: }
167:
168: public Component getGlassPane() {
169: return getRootPane().getGlassPane();
170: }
171:
172: protected void setRootPaneCheckingEnabled(boolean enabled) {
173: rootPaneCheckingEnabled = enabled;
174: }
175:
176: protected boolean isRootPaneCheckingEnabled() {
177: return rootPaneCheckingEnabled;
178: }
179: }
|