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    import java.util.LinkedList;
025    import java.util.List;
026    
027    import javax.swing.JComboBox;
028    
029    import org.dom4j.Document;
030    import org.dom4j.DocumentException;
031    import org.dom4j.Element;
032    import org.dom4j.io.SAXReader;
033    
034    public class MessageBrokerImplementationsComboBox extends JComboBox {
035            private static List<String> impls;
036            public MessageBrokerImplementationsComboBox() {
037                    addItem(null);
038                    setAlignmentX(Component.LEFT_ALIGNMENT);
039                    if (impls == null) {
040                            impls = getImpls();
041                    }
042                    if (impls != null) {
043                            for (String impl : impls) {
044                                    addItem(impl);
045                            }
046                    }
047            }
048            private List<String> getImpls() {
049                    List<String> l = new LinkedList<String>();
050                    try {
051                            Document doc = new SAXReader().read(Thread.currentThread().getContextClassLoader().getResource("message_broker_impls.xml"));
052                            Iterator typeIt = doc.getRootElement().elementIterator("impl");
053                            while (typeIt.hasNext()) {
054                                    Element e = (Element) typeIt.next();
055                                    l.add(e.attributeValue("class"));
056                            }
057                    } catch (DocumentException de) {
058                            de.printStackTrace();
059                    }
060                    return l;
061            }
062            public String getSelectedImplementation() {
063                    Object item = getSelectedItem();
064                    if (item == null)
065                            return "";
066                    return (String) item;
067            }
068            public void getSelectedImplementation(String impl) {
069                    int count = getItemCount();
070                    if (impl == null || impl.trim().length() == 0) {
071                            setSelectedIndex(0);
072                            return;
073                    }
074                    for (int i = 1; i < count; i++) {
075                            String item = (String) getItemAt(i);
076                            if (item.equals(impl)) {
077                                    setSelectedIndex(i);
078                                    return;
079                            }
080                    }
081                    setSelectedIndex(0);
082            }
083    }