001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor 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 MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project.editors;
021    
022    import java.awt.Component;
023    import java.util.Iterator;
024    
025    import javax.swing.JComboBox;
026    
027    import org.dom4j.Element;
028    
029    public class MessageSubscribersComboBox extends JComboBox {
030            public MessageSubscribersComboBox() {
031                    addItem(null);
032                    setAlignmentX(Component.LEFT_ALIGNMENT);
033            }
034            public void setConfig(Element messageReceiverNode) {
035                    removeAllItems();
036                    addItem(null);
037                    Element parent = messageReceiverNode.getParent();
038                    while (parent != null) {
039                            Iterator it = parent.elementIterator();
040                            while (it.hasNext()) {
041                                    Element child = (Element) it.next();
042                                    if (child == messageReceiverNode)
043                                            break;
044                                    if (child.getName().equals("message_subscribe"))
045                                            addItem(child.attributeValue("name"));
046                            }
047                            parent = parent.getParent();
048                    }
049            }
050            public String getSelectedMessageSubscriber() {
051                    Object item = getSelectedItem();
052                    if (item == null)
053                            return "";
054                    return (String) item;
055            }
056            public void setSelectedMessageSubscriber(String subscriber) {
057                    int count = getItemCount();
058                    if (subscriber == null || subscriber.trim().length() == 0) {
059                            setSelectedIndex(0);
060                            return;
061                    }
062                    for (int i = 1; i < count; i++) {
063                            String item = (String) getItemAt(i);
064                            if (item.equals(subscriber)) {
065                                    setSelectedIndex(i);
066                                    return;
067                            }
068                    }
069                    setSelectedIndex(0);
070            }
071    }