view dwtx/jface/preference/PreferenceNode.d @ 104:04b47443bb01

Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections. These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author Frank Benoit <benoit@tionex.de>
date Thu, 07 Aug 2008 15:01:33 +0200
parents b3c8e32d406f
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2007 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.preference.PreferenceNode;

import dwtx.jface.preference.IPreferenceNode;
import dwtx.jface.preference.IPreferencePage;


// import java.util.List;

import dwt.graphics.Image;
import dwtx.core.runtime.Assert;
import dwtx.jface.resource.ImageDescriptor;

import dwt.dwthelper.utils;
import dwtx.dwtxhelper.Collection;

/**
 * A concrete implementation of a node in a preference dialog tree. This class
 * also supports lazy creation of the node's preference page.
 */
public class PreferenceNode : IPreferenceNode {
    /**
     * Preference page, or <code>null</code> if not yet loaded.
     */
    private IPreferencePage page;

    /**
     * The list of subnodes (immediate children) of this node (element type:
     * <code>IPreferenceNode</code>).
     */
    private List subNodes;

    /**
     * Name of a class that implements <code>IPreferencePage</code>, or
     * <code>null</code> if none.
     */
    private String classname;

    /**
     * The id of this node.
     */
    private String id;

    /**
     * Text label for this node. Note that this field is only used prior to the
     * creation of the preference page.
     */
    private String label;

    /**
     * Image descriptor for this node, or <code>null</code> if none.
     */
    private ImageDescriptor imageDescriptor;

    /**
     * Cached image, or <code>null</code> if none.
     */
    private Image image;

    /**
     * Creates a new preference node with the given id. The new node has no
     * subnodes.
     *
     * @param id
     *            the node id
     */
    public this(String id) {
        Assert.isNotNull(id);
        this.id = id;
    }

    /**
     * Creates a preference node with the given id, label, and image, and
     * lazily-loaded preference page. The preference node assumes (sole)
     * responsibility for disposing of the image; this will happen when the node
     * is disposed.
     *
     * @param id
     *            the node id
     * @param label
     *            the label used to display the node in the preference dialog's
     *            tree
     * @param image
     *            the image displayed left of the label in the preference
     *            dialog's tree, or <code>null</code> if none
     * @param className
     *            the class name of the preference page; this class must
     *            implement <code>IPreferencePage</code>
     */
    public this(String id, String label, ImageDescriptor image,
            String className) {
        this(id);
        this.imageDescriptor = image;
        Assert.isNotNull(label);
        this.label = label;
        this.classname = className;
    }

    /**
     * Creates a preference node with the given id and preference page. The
     * title of the preference page is used for the node label. The node will
     * not have an image.
     *
     * @param id
     *            the node id
     * @param preferencePage
     *            the preference page
     */
    public this(String id, IPreferencePage preferencePage) {
        this(id);
        Assert.isNotNull(cast(Object)preferencePage);
        page = preferencePage;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public void add(IPreferenceNode node) {
        if (subNodes is null) {
            subNodes = new ArrayList();
        }
        subNodes.add(cast(Object)node);
    }

    /**
     * Creates a new instance of the given class <code>className</code>.
     *
     * @param className
     * @return new Object or <code>null</code> in case of failures.
     */
    private Object createObject(String className) {
        Assert.isNotNull(className);
//         try {
            ClassInfo cl = ClassInfo.find(className);
            if (cl !is null) {
                return cl.create();
            }
//         } catch (ClassNotFoundException e) {
//             return null;
//         } catch (InstantiationException e) {
//             return null;
//         } catch (IllegalAccessException e) {
//             return null;
//         } catch (NoSuchMethodError e) {
//             return null;
//         }
        return null;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public void createPage() {
        page = cast(IPreferencePage) createObject(classname);
        if (getLabelImage() !is null) {
            page.setImageDescriptor(imageDescriptor);
        }
        page.setTitle(label);
    }

    /**
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public void disposeResources() {
        if (image !is null) {
            image.dispose();
            image = null;
        }
        if (page !is null) {
            page.dispose();
            page = null;
        }
    }

    /*
     * (non-Javadoc) Method declared on IContributionNode.
     */
    public IPreferenceNode findSubNode(String id) {
        Assert.isNotNull(id);
        Assert.isTrue(id.length > 0);
        if (subNodes is null) {
            return null;
        }
        int size = subNodes.size();
        for (int i = 0; i < size; i++) {
            IPreferenceNode node = cast(IPreferenceNode) subNodes.get(i);
            if (id.equals(node.getId())) {
                return node;
            }
        }
        return null;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public String getId() {
        return this.id;
    }

    /**
     * Returns the image descriptor for this node.
     *
     * @return the image descriptor
     */
    protected ImageDescriptor getImageDescriptor() {
        return imageDescriptor;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public Image getLabelImage() {
        if (image is null && imageDescriptor !is null) {
            image = imageDescriptor.createImage();
        }
        return image;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public String getLabelText() {
        if (page !is null) {
            return page.getTitle();
        }
        return label;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public IPreferencePage getPage() {
        return page;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public IPreferenceNode[] getSubNodes() {
        if (subNodes is null) {
            return new IPreferenceNode[0];
        }
        return arraycast!(IPreferenceNode)( subNodes
                .toArray());
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public IPreferenceNode remove(String id) {
        IPreferenceNode node = findSubNode(id);
        if (node !is null) {
            remove(node);
        }
        return node;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceNode.
     */
    public bool remove(IPreferenceNode node) {
        if (subNodes is null) {
            return false;
        }
        return subNodes.remove(cast(Object)node);
    }

    /**
     * Set the current page to be newPage.
     *
     * @param newPage
     */
    public void setPage(IPreferencePage newPage) {
        page = newPage;
    }
}