001    /*
002     * This file is part of McIDAS-V
003     *
004     * Copyright 2007-2013
005     * Space Science and Engineering Center (SSEC)
006     * University of Wisconsin - Madison
007     * 1225 W. Dayton Street, Madison, WI 53706, USA
008     * https://www.ssec.wisc.edu/mcidas
009     * 
010     * All Rights Reserved
011     * 
012     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013     * some McIDAS-V source code is based on IDV and VisAD source code.  
014     * 
015     * McIDAS-V is free software; you can redistribute it and/or modify
016     * it under the terms of the GNU Lesser Public License as published by
017     * the Free Software Foundation; either version 3 of the License, or
018     * (at your option) any later version.
019     * 
020     * McIDAS-V is distributed in the hope that it will be useful,
021     * but WITHOUT ANY WARRANTY; without even the implied warranty of
022     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023     * GNU Lesser Public License for more details.
024     * 
025     * You should have received a copy of the GNU Lesser Public License
026     * along with this program.  If not, see http://www.gnu.org/licenses.
027     */
028    
029    package edu.wisc.ssec.mcidasv.chooser;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    import javax.swing.JComponent;
035    
036    import org.w3c.dom.Element;
037    
038    import edu.wisc.ssec.mcidasv.ui.McIDASVXmlUi;
039    
040    import ucar.unidata.idv.IdvResourceManager;
041    import ucar.unidata.idv.IntegratedDataViewer;
042    import ucar.unidata.idv.chooser.IdvChooser;
043    import ucar.unidata.idv.chooser.IdvChooserManager;
044    import ucar.unidata.idv.chooser.adde.AddeServer;
045    import ucar.unidata.ui.TreePanel;
046    import ucar.unidata.ui.XmlUi;
047    import ucar.unidata.util.LogUtil;
048    import ucar.unidata.xml.XmlResourceCollection;
049    
050    /**
051     * This creates and manages the set of choosers.
052     * It makes the chooser GUI from an xml specification
053     * e.g.: /ucar/unidata/idv/resources/choosers.xml
054     * It uses the {@link ucar.unidata.ui.XmlUi} to process
055     * the xml.
056     * <p>
057     * This class also processes the end-user created choosers.
058     * This piece has always been a bit flaky
059     *
060     * @author IDV development team
061     * @version $Revision$Date: 2011/03/24 16:06:31 $
062     */
063    
064    public class McIdasChooserManager extends IdvChooserManager {
065    
066    
067    
068        /** All of the adde servers */
069        private List addeServers = new ArrayList();
070    
071        private static boolean myServers = true;
072    
073        
074        /**
075         *  Create a new IdvChooserManager.
076         *
077         *  @param idv The singleton IDV
078         */
079        public McIdasChooserManager(IntegratedDataViewer idv) {
080            super(idv);
081            addeServers = initializeAddeServers(idv);       
082        }
083    
084        /**
085         * Create the Choosers component from the choosers.xml resources
086         *
087         * @param inTabs  Do we use the buttontabbedpane or the treepanel
088         *
089         * @return choosers gui
090         */
091        @Override
092        public JComponent createChoosers(boolean inTabs) {
093            return createChoosers(inTabs, new ArrayList(), null);
094        }
095    
096        /**
097         * Initialize addeServers list
098         */
099        public List initializeAddeServers(IntegratedDataViewer idv) {
100            List servers = initializeAddeServers(idv, true);
101            return servers;
102        }
103    
104        /**
105         * Creates a new {@link McIDASVXmlUi} that can create the UI described in
106         * {@code root}.
107         * 
108         * @param root XML description of a GUI component.
109         * 
110         * @return A new {@code McIDASVXmlUi} to use for creating {@code root}.
111         */
112        @Override protected XmlUi createXmlUi(final Element root) {
113            return new McIDASVXmlUi(getIdv(), root);
114        }
115    
116        /**
117         * Initialize addeServers list
118         *
119         */
120        public List initializeAddeServers(IntegratedDataViewer idv, boolean allServers) {
121            addeServers = new ArrayList();
122    
123            XmlResourceCollection addeServerResources =
124                idv.getResourceManager().getXmlResources(
125                    IdvResourceManager.RSC_ADDESERVER);
126            try {
127                for (int resourceIdx = 0;
128                        resourceIdx < addeServerResources.size(); resourceIdx++) {
129                    if (!allServers)
130                       if (!addeServerResources.isWritableResource(resourceIdx)) continue;
131                    Element root = addeServerResources.getRoot(resourceIdx);
132                    if (root == null) {
133                        continue;
134                    }
135                    List servers = AddeServer.processXml(root);
136                    for (int serverIdx = 0; serverIdx < servers.size();
137                            serverIdx++) {
138                        AddeServer addeServer =
139                            (AddeServer) servers.get(serverIdx);
140                        addeServer.setIsLocal(true);
141                        List groups = addeServer.getGroups();
142                        for (int groupIdx = 0; groupIdx < groups.size();
143                                groupIdx++) {
144                            AddeServer.Group group =
145                                (AddeServer.Group) groups.get(groupIdx);
146                            group.setIsLocal(true);
147                        }
148                    }
149                    addeServers.addAll(servers);
150    //                if (!allServers) break;
151                }
152            } catch (Exception exc) {
153                LogUtil.logException("Error processing adde server descriptions",
154                                     exc);
155            }
156            addeServers = AddeServer.coalesce(addeServers);
157    
158            Object oldServers =
159                getIdv().getStore().get(IdvChooser.PREF_ADDESERVERS);
160            if ((oldServers != null) && (oldServers instanceof List)) {
161                List prefs = (List) oldServers;
162                for (int i = 0; i < prefs.size(); i++) {
163                    String server = (String) prefs.get(i);
164                    addAddeServer(server);
165                }
166                getIdv().getStore().remove(IdvChooser.PREF_ADDESERVERS);
167                getIdv().getStore().saveIfNeeded();
168                writeAddeServers();
169            }
170            return addeServers;
171        }
172    
173        /**
174         * Get AddeServers to use
175         *
176         * @param groupType If null return all, else return the servers that have groups of the given type
177         *
178         * @return List of AddeServers
179         */
180        public List getAddeServers(String groupType) {
181            return getAddeServers(groupType, true);
182        }
183    
184    
185        /**
186         * Get AddeServers to use
187         *
188         * @param groupType If null return all, else return the servers that have groups of the given type
189         * @param onlyActive If true then only fetch the active servers
190         *
191         * @return List of AddeServers
192         */
193        public List getAddeServers(String groupType, boolean onlyActive) {
194            List servers;
195            if (groupType == null) {
196                servers = new ArrayList(addeServers);
197            } else {
198                servers = AddeServer.getServersWithType(groupType, addeServers);
199            }
200            if ( !onlyActive) {
201                return servers;
202            }
203    
204            List       activeServers = new ArrayList();
205            AddeServer addeServer;
206            for (int i = 0; i < addeServers.size(); i++) {
207                addeServer = (AddeServer) addeServers.get(i);
208                if (addeServer.getActive()) {
209                    activeServers.add(addeServer);
210                }
211            }
212            return activeServers;
213        }
214    }