001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Rectangle;
026:
027: /**
028: * A container derived form <code>WingComponent</code> used by
029: * <code>WingFrame</code>, <code>WingDialog</code>, <code>WingWindow</code>,
030: * <code>WingApplet</code>.
031: * This class provide:
032: * <ul>
033: * <li>Highly customizable look of top level containers based on hierarchical skin style sheets.
034: * <li>Interactive keyboard shortcut handling.
035: * <li>An infrastructure for painting with double buffering
036: * <li>Layout for content pane and menu
037: * <li>Layout for lightweight popups
038: * </ul>
039: * <br>WingS components can work without the root pane but is recommended
040: * to use top level containers containing it.<br>
041: *
042: * <br>
043: * <b>This is one of the core WingS classes required by all the components</b><br>
044: * <br>
045: * <b>This class is thread safe.</b>
046: **/
047: public class WingRootPane extends WingComponent {
048: private WingComponent content;
049: private WingComponent menu;
050:
051: /**
052: * Loads skin resources.<br>
053: * <pre>
054: * styles:
055: * [optional styleID.]window.normal
056: * </pre>
057: * @see Style
058: * @see WingSkin
059: */
060: public void loadSkin() {
061: stNormal = WingSkin.getStyle(styleId, "window", NORMAL, null);
062: Container p = getParent();
063: if (p != null)
064: p.setBackground(stNormal.background);
065: }
066:
067: public Style getStyle() {
068: return stNormal;
069: }
070:
071: /**
072: * Sets the content pane - the one and only one container
073: * that holds the components.
074: * <br><br><strong>This method acquire TreeLock</strong>
075: * @param c the <code>WingComponent</code> used as the content pane
076: */
077: public void setContentPane(WingComponent c) {
078: synchronized (getTreeLock()) {
079: if (content != null)
080: removeShortcuts(content);
081: this .removeAll();
082: if (c != null) {
083: this .add(c);
084: addShortcuts(c);
085: }
086: this .content = c;
087:
088: setMenuBar(menu);
089: revalidateAndRepaint();
090: }
091: }
092:
093: /**
094: * Adds a lightweight popup component
095: * <br><br><strong>This method acquire TreeLock</strong>
096: * @param c a popup component
097: */
098: public void addPopup(WingComponent c) {
099: this .add(c, 0);
100: }
101:
102: /**
103: * Removes a lightweight popup component
104: * <br><br><strong>This method acquire TreeLock</strong>
105: * @param c a popup component
106: */
107: public void removePopup(WingComponent c) {
108: Rectangle bounds = c.getBounds();
109: this .remove(c);
110: repaint(bounds.x, bounds.y, bounds.width, bounds.height);
111: }
112:
113: /**
114: * Adds, removes or changes the <code>WingComponent</code>
115: * used as a menu bar.
116: * <br><br><strong>This method acquire TreeLock</strong>
117: * @param menu the <code>WingComponent</code> to add;
118: * use <code>null</code> to remove menu bar
119: */
120: public void setMenuBar(WingComponent menu) {
121: synchronized (getTreeLock()) {
122: if (this .menu != null) {
123: removeShortcuts(this .menu);
124: remove(this .menu);
125: }
126: this .menu = menu;
127: if (menu != null) {
128: addShortcuts(menu);
129: add(menu);
130: }
131: revalidateAndRepaint();
132: }
133: }
134:
135: /**
136: * <br><br><strong>This method acquire TreeLock</strong>
137: */
138: public void doLayout() {
139: WingComponent c = content, m = menu;
140: int menuHeight = 0;
141: Dimension size = getSize();
142: if (m != null) {
143: Dimension ms = m.getPreferredSize();
144: for (int f = 0; f < 2; f++) {
145: m.setBounds(0, 0, size.width, ms.height);
146: m.validate();
147: Dimension ms2 = m.getPreferredSize();
148: if (ms2.equals(ms))
149: break;
150: ms = ms2;
151: }
152: menuHeight = ms.height;
153: }
154: if (c != null) {
155: c.setBounds(0, menuHeight, size.width, size.height
156: - menuHeight);
157: }
158: }
159:
160: /**
161: * <br><br><strong>This method acquire TreeLock</strong>
162: */
163: public Dimension getPreferredSize() {
164: WingComponent c = content, m = menu;
165: Dimension dc = null, dm = null;
166: if (c != null)
167: dc = c.getPreferredSize();
168: if (m != null)
169: dm = m.getPreferredSize();
170: if (dc == null && dm == null)
171: return new Dimension();
172: if (dc == null)
173: return dm;
174: if (dm == null)
175: return dc;
176: return new Dimension((dc.width > dm.width) ? dc.width
177: : dm.width, dm.height + dc.height);
178: }
179: }
|