001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-04-23 16:55:56 +0000 (Thu, 23 Apr 2015) $' 
007 * '$Revision: 33365 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.modulemanager.gui;
031
032import java.io.BufferedReader;
033import java.io.InputStreamReader;
034import java.net.URL;
035import java.util.ArrayList;
036import java.util.List;
037import java.util.concurrent.ExecutionException;
038
039import javax.swing.DefaultListModel;
040import javax.swing.JList;
041import javax.swing.SwingWorker;
042
043import org.kepler.modulemanager.RepositoryLocations;
044
045
046
047/**
048 * Created by David Welker.
049 * Date: Sep 18, 2009
050 * Time: 10:38:53 AM
051 */
052public class ModulesList extends JList
053{
054    //private List<String> testReleases = new ArrayList<String>();
055
056    public ModulesList()
057    {
058        super();
059        //Use an empty default list model, since the permanent model will be calculated in the background.
060        DefaultListModel model = new DefaultListModel();
061        model.addElement("Calculating...");
062        setModel( model );
063        populate();
064    }
065
066    /* NOTE: this code to show test releases does not work since it relies on
067     * the module/suite containing the file module-info/test-release to denote
068     * it as a test release. this may have been the original design, but test
069     * releases are now a separate branch on the svn repository.
070     
071    public void setShowTestReleases(boolean showTestReleases)
072    {
073        if( showTestReleases )
074            showTestReleases();
075        else
076            hideTestReleases();
077    }
078
079    private void showTestReleases()
080    {
081        DefaultListModel model = (DefaultListModel)getModel();
082        for( String testRelease : testReleases )
083            model.addElement(testRelease);
084    }
085
086    private void hideTestReleases()
087    {
088        DefaultListModel model = (DefaultListModel)getModel();
089        for( String testRelease : testReleases )
090          model.removeElement(testRelease);
091    }
092    */
093    
094    public void populate()
095    {
096        SwingWorker<DefaultListModel, Void> worker = new SwingWorker<DefaultListModel, Void>()
097        {
098
099            @Override
100            public DefaultListModel doInBackground() throws Exception
101            {
102                DefaultListModel listModel = new DefaultListModel();
103                List<String> lines = new ArrayList<String>();
104                List<String> files = new ArrayList<String>();
105                
106                try
107                {
108                  URL url = new URL(RepositoryLocations.getReleaseLocation());
109                  
110                  BufferedReader br = null;
111                  InputStreamReader stream = null;
112                        try {
113                                stream = new InputStreamReader(url.openStream());
114                                br = new BufferedReader(stream);
115                                String line = null;
116                                while ((line = br.readLine()) != null)
117                                {
118                                        lines.add(line);
119                                }
120                        } finally {
121                                if(br != null) {
122                                        br.close();
123                                }
124                                if(stream != null) {
125                                        stream.close();
126                                }
127                        }
128                  
129                  for (String l : lines)
130                  {
131                    if (l.trim().startsWith("<dir name=")
132                        || l.trim().startsWith("<file name="))
133                    {
134                      files.add(l.split("\"")[1]);
135                    }
136                  }
137                }
138                catch(Exception e)
139                {
140                  System.out.println("Could not get suite list from SVN: " + e.getMessage());
141                  e.printStackTrace();
142                }
143
144    
145                
146                for(String module : files)
147                {
148                    listModel.addElement(module);
149                }
150
151                return listModel;                                          
152            }
153
154            @Override
155            public void done()
156            {
157                try
158                {
159                    setModel(get());
160                }
161                catch (InterruptedException e)
162                {
163                    e.printStackTrace();
164                }
165                catch (ExecutionException e)
166                {
167                    e.printStackTrace();
168                }
169            }
170        };
171        worker.execute();
172    }
173
174
175}