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