001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program 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
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: DockingUtil.java,v 1.25 2007/01/28 21:25:10 jesper Exp $
023: package net.infonode.docking.util;
024:
025: import net.infonode.docking.*;
026: import net.infonode.docking.internalutil.InternalDockingUtil;
027:
028: /**
029: * Class that contains utility methods for docking windows.
030: *
031: * @author $Author: jesper $
032: * @version $Revision: 1.25 $
033: */
034: public final class DockingUtil {
035: private DockingUtil() {
036: }
037:
038: /**
039: * Creates a root window with support for view serialization and popup menues.
040: * All the views are added to a tab window which is placed in the root window.
041: *
042: * @param views the views that can be shown inside the root window
043: * @param createWindowPopupMenu true if a standard window popup menu should be created
044: * @return the created root window
045: */
046: public static RootWindow createRootWindow(AbstractViewMap views,
047: boolean createWindowPopupMenu) {
048: return createRootWindow(views, views, createWindowPopupMenu);
049: }
050:
051: /**
052: * <p>
053: * Creates a root window with support for view serialization, popup menues and support for heavy
054: * weight components inside the views.
055: * </p>
056: *
057: * <p>
058: * All the views are added to a tab window which is placed in the root window.
059: * </p>
060: *
061: * @param views the views that can be shown inside the root window
062: * @param createWindowPopupMenu true if a standard window popup menu should be created
063: * @return the created root window
064: * @since IDW 1.4.0
065: */
066: public static RootWindow createHeavyweightSupportedRootWindow(
067: AbstractViewMap views, boolean createWindowPopupMenu) {
068: return createRootWindow(true, views, views,
069: createWindowPopupMenu);
070: }
071:
072: /**
073: * Creates a root window with support for view serialization and popup menues.
074: * All the views are added to a tab window which is placed in the root window.
075: *
076: * @param views contains all the static views
077: * @param viewSerializer the view serializer used in the created {@link RootWindow}
078: * @param createWindowPopupMenu true if a standard window popup menu should be created
079: * @return the created root window
080: */
081: public static RootWindow createRootWindow(AbstractViewMap views,
082: ViewSerializer viewSerializer, boolean createWindowPopupMenu) {
083:
084: return createRootWindow(false, views, viewSerializer,
085: createWindowPopupMenu);
086: }
087:
088: /**
089: * <p>
090: * Creates a root window with support for view serialization, popup menues and support for
091: * heavyweight components inside the views.
092: * </p>
093: *
094: * <p>
095: * All the views are added to a tab window which is placed in the root window.
096: * </p>
097: *
098: * @param views contains all the static views
099: * @param viewSerializer the view serializer used in the created {@link RootWindow}
100: * @param createWindowPopupMenu true if a standard window popup menu should be created
101: * @return the created root window
102: * @since IDW 1.4.0
103: */
104: public static RootWindow createHeavyweightSupportedRootWindow(
105: AbstractViewMap views, ViewSerializer viewSerializer,
106: boolean createWindowPopupMenu) {
107:
108: return createRootWindow(true, views, viewSerializer,
109: createWindowPopupMenu);
110: }
111:
112: private static RootWindow createRootWindow(
113: boolean heavyweightSupport, AbstractViewMap views,
114: ViewSerializer viewSerializer, boolean createWindowPopupMenu) {
115: TabWindow tabWindow = new TabWindow();
116:
117: for (int i = 0; i < views.getViewCount(); i++)
118: tabWindow.addTab(views.getViewAtIndex(i));
119: tabWindow.setSelectedTab(0);
120: RootWindow rootWindow = new RootWindow(heavyweightSupport,
121: viewSerializer, tabWindow);
122:
123: if (createWindowPopupMenu)
124: rootWindow.setPopupMenuFactory(WindowMenuUtil
125: .createWindowMenuFactory(views, true));
126:
127: return rootWindow;
128: }
129:
130: /**
131: * Returns true if <tt>ancestor</tt> is an ancestor of <tt>child</tt> or the windows are the same.
132: *
133: * @param ancestor the ancestor window
134: * @param child the child window
135: * @return true if <tt>ancestor</tt> is an ancestor of <tt>child</tt> or the windows are the same
136: */
137: public static boolean isAncestor(DockingWindow ancestor,
138: DockingWindow child) {
139: return child != null
140: && (ancestor == child || isAncestor(ancestor, child
141: .getWindowParent()));
142: }
143:
144: /**
145: * <p>
146: * Adds a window inside a root window. The following methods are tried in order:
147: * </p>
148: * <ol>
149: * <li>If the window already is added inside the root window nothing happens.</li>
150: * <li>The window is restored to it's last location if that location is inside the root window.</li>
151: * <li>The window is added inside the root window.</li>
152: * </ol>
153: *
154: * @param window the window to add
155: * @param rootWindow the root window in which to add it
156: * @since IDW 1.1.0
157: */
158: public static void addWindow(DockingWindow window,
159: RootWindow rootWindow) {
160: if (rootWindow == null || window.getRootWindow() == rootWindow)
161: return;
162:
163: if (window.getRootWindow() == null) {
164: window.restore();
165:
166: if (window.getRootWindow() == rootWindow)
167: return;
168: }
169:
170: InternalDockingUtil.addToRootWindow(window, rootWindow);
171: }
172:
173: /**
174: * Returns the {@link TabWindow} for a window. This is either the window itself or the parent window.
175: *
176: * @param window the window
177: * @return the {@link TabWindow} for the window
178: * @since IDW 1.3.0
179: */
180: public static TabWindow getTabWindowFor(DockingWindow window) {
181: return window instanceof TabWindow ? (TabWindow) window
182: : window.getWindowParent() != null
183: && window.getWindowParent() instanceof TabWindow ? (TabWindow) window
184: .getWindowParent()
185: : null;
186: }
187:
188: /**
189: * Returns the {@link FloatingWindow} for a window if the window is undocked.
190: *
191: * @param window the window
192: * @return the {@link FloatingWindow} for the window or null if the window is not undocked
193: * @since IDW 1.4.0
194: */
195: public static FloatingWindow getFloatingWindowFor(
196: DockingWindow window) {
197: if (window == null)
198: return null;
199:
200: if (!window.isUndocked())
201: return null;
202:
203: while (window != null && !(window instanceof FloatingWindow))
204: window = window.getWindowParent();
205:
206: return (FloatingWindow) window;
207: }
208: }
|