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: /**
019: * @author Vadim L. Bogdanov
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Component;
025: import java.awt.Container;
026: import java.awt.Dimension;
027: import java.awt.IllegalComponentStateException;
028: import java.awt.LayoutManager;
029: import java.awt.LayoutManager2;
030: import java.awt.Rectangle;
031:
032: import java.awt.event.ActionEvent;
033:
034: import java.io.Serializable;
035:
036: import javax.accessibility.Accessible;
037: import javax.accessibility.AccessibleContext;
038: import javax.accessibility.AccessibleRole;
039:
040: import javax.swing.plaf.RootPaneUI;
041:
042: import org.apache.harmony.x.swing.Utilities;
043:
044: public class JRootPane extends JComponent implements Accessible {
045: public static final int NONE = 0;
046: public static final int FRAME = 1;
047: public static final int PLAIN_DIALOG = 2;
048: public static final int INFORMATION_DIALOG = 3;
049: public static final int ERROR_DIALOG = 4;
050: public static final int COLOR_CHOOSER_DIALOG = 5;
051: public static final int FILE_CHOOSER_DIALOG = 6;
052: public static final int QUESTION_DIALOG = 7;
053: public static final int WARNING_DIALOG = 8;
054:
055: protected JMenuBar menuBar;
056: protected Container contentPane;
057: protected JLayeredPane layeredPane;
058: protected Component glassPane;
059: protected JButton defaultButton;
060:
061: /**
062: * @deprecated
063: */
064: protected DefaultAction defaultPressAction;
065:
066: /**
067: * @deprecated
068: */
069: protected DefaultAction defaultReleaseAction;
070:
071: private int windowDecorationStyle = NONE;
072: private JButton savedDefaultButton;
073:
074: public JRootPane() {
075: setGlassPane(createGlassPane());
076: setLayeredPane(createLayeredPane());
077: setContentPane(createContentPane());
078: setLayout(createRootLayout());
079: setDoubleBuffered(true);
080:
081: updateUI();
082: }
083:
084: /**
085: * This class implements accessibility support for <code>JRootPane</code>.
086: */
087: protected class AccessibleJRootPane extends AccessibleJComponent {
088: protected AccessibleJRootPane() {
089: }
090:
091: public AccessibleRole getAccessibleRole() {
092: return AccessibleRole.ROOT_PANE;
093: }
094:
095: public int getAccessibleChildrenCount() {
096: // not sure
097: return 1;
098: }
099:
100: public Accessible getAccessibleChild(final int i) {
101: // not sure
102: return (Accessible) contentPane;
103: }
104: }
105:
106: protected class RootLayout implements LayoutManager2, Serializable {
107: protected RootLayout() {
108: }
109:
110: public Dimension preferredLayoutSize(final Container parent) {
111: return Utilities.getRootPaneLayoutSize(getContentPane()
112: .getPreferredSize(),
113: getJMenuBar() != null ? getJMenuBar()
114: .getPreferredSize() : null, null,
115: getInsets());
116: }
117:
118: public Dimension minimumLayoutSize(final Container parent) {
119: return Utilities
120: .getRootPaneLayoutSize(getContentPane()
121: .getMinimumSize(),
122: getJMenuBar() != null ? getJMenuBar()
123: .getMinimumSize() : null, null,
124: getInsets());
125: }
126:
127: public Dimension maximumLayoutSize(final Container target) {
128: return Utilities
129: .getRootPaneLayoutSize(getContentPane()
130: .getMaximumSize(),
131: getJMenuBar() != null ? getJMenuBar()
132: .getMaximumSize() : null, null,
133: getInsets());
134: }
135:
136: public void layoutContainer(final Container parent) {
137: //The glassPane fills the entire viewable area of the JRootPane (bounds - insets).
138: //The layeredPane fills the entire viewable area of the JRootPane. (bounds - insets)
139: //The menuBar is positioned at the upper edge of the layeredPane.
140: //The contentPane fills the entire viewable area, minus the menuBar, if present.
141: JRootPane root = (JRootPane) parent;
142: Rectangle r = SwingUtilities.calculateInnerArea(root, null);
143:
144: root.getGlassPane().setBounds(r);
145: root.getLayeredPane().setBounds(r);
146:
147: // menuBar, contentPane lay in layeredPane
148: int top = 0;
149: int height = r.height;
150: if (root.getJMenuBar() != null) {
151: int menuHeight = root.getJMenuBar().getPreferredSize().height;
152: // menuBar lays in layeredPane
153: root.getJMenuBar().setBounds(0, 0, r.width, menuHeight);
154: top = menuHeight;
155: height -= menuHeight;
156: }
157: // contentPane lays in layeredPane
158: root.getContentPane().setBounds(0, top, r.width, height);
159: }
160:
161: public void addLayoutComponent(final String name,
162: final Component comp) {
163: // this method is not used
164: }
165:
166: public void removeLayoutComponent(final Component comp) {
167: // this method is not used
168: }
169:
170: public void addLayoutComponent(final Component comp,
171: final Object constraints) {
172: // this method is not used
173: }
174:
175: public float getLayoutAlignmentX(final Container target) {
176: return 0;
177: }
178:
179: public float getLayoutAlignmentY(final Container target) {
180: return 0;
181: }
182:
183: public void invalidateLayout(final Container target) {
184: // this method is not used
185: }
186: }
187:
188: /*
189: * This is deprecate functionality and the class is private.
190: * No implmenentation is required.
191: */
192: private class DefaultAction extends AbstractAction {
193: private DefaultAction() {
194: }
195:
196: public void actionPerformed(final ActionEvent e) {
197: }
198: }
199:
200: public void setUI(final RootPaneUI newUI) {
201: super .setUI(newUI);
202: }
203:
204: public RootPaneUI getUI() {
205: return (RootPaneUI) ui;
206: }
207:
208: public void updateUI() {
209: setUI((RootPaneUI) UIManager.getUI(this ));
210: }
211:
212: public String getUIClassID() {
213: return "RootPaneUI";
214: }
215:
216: public int getWindowDecorationStyle() {
217: return windowDecorationStyle;
218: }
219:
220: public void setWindowDecorationStyle(
221: final int newWindowDecorationStyle) {
222: switch (newWindowDecorationStyle) {
223: case NONE:
224: case FRAME:
225: case PLAIN_DIALOG:
226: case INFORMATION_DIALOG:
227: case ERROR_DIALOG:
228: case COLOR_CHOOSER_DIALOG:
229: case FILE_CHOOSER_DIALOG:
230: case QUESTION_DIALOG:
231: case WARNING_DIALOG:
232: int oldWindowDecorationStyle = windowDecorationStyle;
233: windowDecorationStyle = newWindowDecorationStyle;
234: firePropertyChange("windowDecorationStyle",
235: oldWindowDecorationStyle, newWindowDecorationStyle);
236: return;
237: }
238: throw new IllegalArgumentException();
239: }
240:
241: /**
242: * @deprecated
243: */
244: public void setMenuBar(final JMenuBar menu) {
245: setJMenuBar(menu);
246: }
247:
248: public void setJMenuBar(final JMenuBar menu) {
249: if (getJMenuBar() != null) {
250: layeredPane.remove(getJMenuBar());
251: }
252: if (menu != null) {
253: layeredPane.add(menu, JLayeredPane.FRAME_CONTENT_LAYER);
254: }
255: menuBar = menu;
256: }
257:
258: /**
259: * @deprecated
260: */
261: public JMenuBar getMenuBar() {
262: return getJMenuBar();
263: }
264:
265: public JMenuBar getJMenuBar() {
266: return menuBar;
267: }
268:
269: protected JLayeredPane createLayeredPane() {
270: JLayeredPane panel = new JLayeredPane();
271: panel.setName("layeredPane");
272: return panel;
273: }
274:
275: protected Container createContentPane() {
276: //return super.createContentPane();
277: JPanel panel = new JPanel(new BorderLayout());
278: panel.setName("contentPane");
279: panel.setOpaque(true);
280: return panel;
281: }
282:
283: protected Component createGlassPane() {
284: JPanel panel = new JPanel(false); // double buffering is set to false
285: panel.setName("glassPane");
286: panel.setVisible(false);
287: panel.setOpaque(false);
288: return panel;
289: }
290:
291: public Container getContentPane() {
292: return contentPane;
293: }
294:
295: public void setContentPane(final Container content) {
296: if (content == null) {
297: throw new IllegalComponentStateException();
298: }
299: if (getContentPane() != null) {
300: layeredPane.remove(getContentPane());
301: }
302: layeredPane.add(content, JLayeredPane.FRAME_CONTENT_LAYER);
303: contentPane = content;
304: }
305:
306: public void setLayeredPane(final JLayeredPane layered) {
307: if (layered == null) {
308: throw new IllegalComponentStateException();
309: }
310: if (getLayeredPane() != null) {
311: remove(getLayeredPane());
312: }
313: add(layered);
314: layeredPane = layered;
315: }
316:
317: public JLayeredPane getLayeredPane() {
318: return layeredPane;
319: }
320:
321: public void setGlassPane(final Component glass) {
322: if (glass == null) {
323: throw new NullPointerException();
324: }
325: if (getGlassPane() != null) {
326: remove(getGlassPane());
327: }
328: glassPane = glass;
329: add(glass);
330: }
331:
332: public Component getGlassPane() {
333: return glassPane;
334: }
335:
336: protected void addImpl(final Component comp,
337: final Object constraints, final int index) {
338: if (comp == getGlassPane()) {
339: // glassPane should always has a position 0
340: super .addImpl(comp, constraints, 0);
341: } else if (index == 0) {
342: // not a glass pane, index cannot be 0
343: super .addImpl(comp, constraints, 1);
344: } else {
345: super .addImpl(comp, constraints, index);
346: }
347: }
348:
349: public boolean isValidateRoot() {
350: return true;
351: }
352:
353: public boolean isOptimizedDrawingEnabled() {
354: return !getGlassPane().isVisible();
355: }
356:
357: protected String paramString() {
358: return super .paramString();
359: }
360:
361: protected LayoutManager createRootLayout() {
362: return new RootLayout();
363: }
364:
365: public void setDefaultButton(final JButton button) {
366: JButton oldButton = defaultButton;
367: defaultButton = button;
368: savedDefaultButton = button;
369: firePropertyChange("defaultButton", oldButton, button);
370: }
371:
372: public JButton getDefaultButton() {
373: return defaultButton;
374: }
375:
376: /**
377: * Returns the accessible context for the root pane.
378: *
379: * @return the accessible context for the root pane
380: */
381: public AccessibleContext getAccessibleContext() {
382: if (accessibleContext == null) {
383: accessibleContext = new AccessibleJRootPane();
384: }
385:
386: return accessibleContext;
387: }
388:
389: public void removeNotify() {
390: JButton lastDefaultButton = getDefaultButton();
391: super .removeNotify();
392: savedDefaultButton = lastDefaultButton;
393: }
394:
395: public void addNotify() {
396: super.addNotify();
397: if (getDefaultButton() == null && savedDefaultButton != null
398: && savedDefaultButton.getRootPane() == this) {
399: setDefaultButton(savedDefaultButton);
400: }
401: }
402: }
|