/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protegex.widget.contains;

import java.util.*;

import javax.swing.*;

/**
 *  Holds all the widget wrappers
 *
 * @author    William Grosso <grosso@smi.stanford.edu>
 */
public class WidgetBox extends JPanel implements WidgetWrapperActionProcessor {
    protected WidgetBoxLayoutManager _layoutManager;
    protected LinkedList _widgetWrappers;
    protected LinkedList _widgetSeparators;
    protected WidgetListener _widgetListener;
    protected WidgetWrapperActionProcessorImpl _widgetActionProcessor;
    protected boolean _useSeparators;
    protected int _spacerSize;

    private class WidgetListener implements WidgetWrapperActionListener {
        public void widgetWrapperHadActivity(WidgetWrapper wrapper) {
            announceWrapperActivity(wrapper);
        }
    }

    public WidgetBox(WidgetBoxLayoutManager layoutManager, int spacerSize, boolean useSeparators) {
        super(layoutManager);
        _layoutManager = (WidgetBoxLayoutManager) getLayout();
        _widgetSeparators = new LinkedList();
        _widgetWrappers = new LinkedList();
        _widgetActionProcessor = new WidgetWrapperActionProcessorImpl();
        _widgetListener = new WidgetListener();
    }

    public void addWidgetSeparator(WidgetSeparator separator) {
        if (!_widgetSeparators.contains(separator)) {
            _widgetSeparators.addFirst(separator);
        }
    }

    public boolean addWidgetWrapper(int index, WidgetWrapper widgetWrapper) {
        if (!_widgetWrappers.contains(widgetWrapper)) {
            _widgetWrappers.add(index, widgetWrapper);
            widgetWrapper.addWidgetWrapperActionListener(_widgetListener);
            return true;
        }
        return false;
    }

    public boolean addWidgetWrapper(WidgetWrapper widgetWrapper) {
        if (!_widgetWrappers.contains(widgetWrapper)) {
            _widgetWrappers.add(widgetWrapper);
            widgetWrapper.addWidgetWrapperActionListener(_widgetListener);
            return true;
        }
        return false;
    }

    public void addWidgetWrapperActionListener(WidgetWrapperActionListener listener) {
        _widgetActionProcessor.addWidgetWrapperActionListener(listener);
    }

    private void announceWrapperActivity(WidgetWrapper activeWrapper) {
        _widgetActionProcessor.announceWrapperActivity(activeWrapper);
    }

    public void clearWidgetSeparators() {
        _widgetSeparators.clear();
    }

    public int getNumberOfWidgetWrappers() {
        return _widgetWrappers.size();
    }

    public int getSpacerSize() {
        return _spacerSize;
    }

    public boolean getUseSeparators() {
        return _useSeparators;
    }

    public LinkedList getWidgetSeparators() {
        return new LinkedList(_widgetSeparators);
    }

    public Collection getWidgetWrappers() {
        return new ArrayList(_widgetWrappers);
    }

    public boolean isUseSeparators() {
        return _useSeparators;
    }

    public boolean removeAllWidgetWrappers() {
        Iterator i = (new ArrayList(_widgetWrappers)).iterator();
        while (i.hasNext()) {
            removeWidgetWrapper((WidgetWrapper) i.next());
        }
        _widgetWrappers.clear();
        removeAll();
        return true;
    }

    public boolean removeWidgetWrapper(WidgetWrapper widgetWrapper) {
        if (_widgetWrappers.contains(widgetWrapper)) {
            _widgetWrappers.remove(widgetWrapper);
            widgetWrapper.removeWidgetWrapperActionListener(_widgetListener);
            widgetWrapper.returnToPool();
            remove(widgetWrapper);
            return true;
        }
        return false;
    }

    public void removeWidgetWrapperActionListener(WidgetWrapperActionListener listener) {
        _widgetActionProcessor.removeWidgetWrapperActionListener(listener);
    }

    public void setSpacerSize(int spacerSize) {
        _spacerSize = spacerSize;
    }

    public void setUseSeparators(boolean useSeparators) {
        _useSeparators = useSeparators;
    }
}
