view dwtx/jface/window/WindowManager.d @ 7:8a302fdb4140

Jface some window and resource classes
author Frank Benoit <benoit@tionex.de>
date Fri, 28 Mar 2008 23:32:40 +0100
parents
children 04b47443bb01
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/
module dwtx.jface.window.WindowManager;

import dwtx.jface.window.Window;

import tango.util.collection.ArraySeq;
import tango.util.collection.model.Seq;

import dwtx.core.runtime.Assert;

import dwt.dwthelper.utils;

/**
 * A manager for a group of windows. Window managers are an optional JFace
 * feature used in applications which create many different windows (dialogs,
 * wizards, etc.) in addition to a main window. A window manager can be used to
 * remember all the windows that an application has created (independent of
 * whether they are presently open or closed). There can be several window
 * managers, and they can be arranged into a tree. This kind of organization
 * makes it simple to close whole subgroupings of windows.
 * <p>
 * Creating a window manager is as simple as creating an instance of
 * <code>WindowManager</code>. Associating a window with a window manager is
 * done with <code>WindowManager.add(Window)</code>. A window is automatically
 * removed from its window manager as a side effect of closing the window.
 * </p>
 *
 * @see Window
 */
public class WindowManager {

    /**
     * List of windows managed by this window manager
     * (element type: <code>Window</code>).
     */
    private ArraySeq!(Window) windows;

    /**
     * List of window managers who have this window manager
     * as their parent (element type: <code>WindowManager</code>).
     */
    private Seq!(WindowManager) subManagers;

    /**
     * Creates an empty window manager without a parent window
     * manager (that is, a root window manager).
     */
    public this() {
        windows = new ArraySeq!(Window);
    }

    /**
     * Creates an empty window manager with the given
     * window manager as parent.
     *
     * @param parent the parent window manager
     */
    public this(WindowManager parent) {
        windows = new ArraySeq!(Window);
        Assert.isNotNull(parent);
        parent.addWindowManager(this);
    }

    /**
     * Adds the given window to the set of windows managed by
     * this window manager. Does nothing is this window is
     * already managed by this window manager.
     *
     * @param window the window
     */
    public void add(Window window) {
        if (!windows.contains(window)) {
            windows.append(window);
            window.setWindowManager(this);
        }
    }

    /**
     * Adds the given window manager to the list of
     * window managers that have this one as a parent.
     * </p>
     * @param wm the child window manager
     */
    private void addWindowManager(WindowManager wm) {
        if (subManagers is null) {
            subManagers = new ArraySeq!(WindowManager);
        }
        if (!subManagers.contains(wm)) {
            subManagers.append(wm);
        }
    }

    /**
     * Attempts to close all windows managed by this window manager,
     * as well as windows managed by any descendent window managers.
     *
     * @return <code>true</code> if all windows were sucessfully closed,
     * and <code>false</code> if any window refused to close
     */
    public bool close() {
        auto t = windows.dup(); // make iteration robust
        foreach( window; t ){
            bool closed = window.close();
            if (!closed) {
                return false;
            }
        }
        if (subManagers !is null) {
            foreach( wm; subManagers ){
                bool closed = wm.close();
                if (!closed) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Returns this window manager's number of windows
     *
     * @return the number of windows
     * @since 3.0
     */
    public int getWindowCount() {
        return windows.size();
    }

    /**
     * Returns this window manager's set of windows.
     *
     * @return a possibly empty list of window
     */
    public Window[] getWindows() {
        return windows.toArray();
    }

    /**
     * Removes the given window from the set of windows managed by
     * this window manager. Does nothing is this window is
     * not managed by this window manager.
     *
     * @param window the window
     */
    public final void remove(Window window) {
        if (windows.contains(window)) {
            windows.remove(window);
            window.setWindowManager(null);
        }
    }
}