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:56:59 +0000 (Thu, 23 Apr 2015) $' 
007 * '$Revision: 33366 $'
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.Collections;
037import java.util.List;
038import java.util.concurrent.ExecutionException;
039
040import javax.swing.DefaultListModel;
041import javax.swing.JList;
042import javax.swing.SwingWorker;
043
044import org.kepler.build.modules.Module;
045import org.kepler.build.util.HttpsSvnReader;
046import org.kepler.modulemanager.RepositoryLocations;
047
048
049/**
050 * Created by David Welker.
051 * Date: Sep 18, 2009
052 * Time: 10:38:43 AM
053 */
054public class SuitesList extends JList
055{
056    private List<String> fullList = new ArrayList<String>();
057    private List<String> patchableOnlyList = new ArrayList<String>();
058    //private List<String> testReleases = new ArrayList<String>();
059
060    private boolean shouldShowFullList = false;
061
062    public SuitesList()
063    {
064        super();
065        //Use an empty default list model, since the permanent model will be calculated in the background.
066        DefaultListModel model = new DefaultListModel();
067        model.addElement("Calculating...");
068        setModel( model );
069        populate();
070    }
071
072    public void removeElement(String element)
073    {
074        ((DefaultListModel)getModel()).removeElement(element);
075        fullList.remove(element);
076        patchableOnlyList.remove(element);
077    }
078
079    public void addElement(String element)
080    {
081        ((DefaultListModel)getModel()).addElement(element);
082        fullList.add(element);
083        patchableOnlyList.add(element);
084    }
085
086    public void sort()
087    {
088        Collections.sort(fullList);
089        Collections.sort(patchableOnlyList);
090        DefaultListModel model = new DefaultListModel();
091        List<String> listToShow = shouldShowFullList ? fullList : patchableOnlyList;
092        for( String module : listToShow )
093        {
094            model.addElement(module);
095        }
096        setModel(model);
097    }
098
099    /*
100    public void setShowTestReleases(boolean showTestReleases)
101    {
102        if( showTestReleases )
103        {
104            showTestReleases();
105        }
106        else
107        {
108            hideTestReleases();
109        }
110    }
111
112    private void showTestReleases()
113    {
114        DefaultListModel model = (DefaultListModel)getModel();
115        for( String testRelease : testReleases )
116        {
117            model.addElement(testRelease);
118        }
119    }
120
121    private void hideTestReleases()
122    {
123        DefaultListModel model = (DefaultListModel)getModel();
124        for( String testRelease : testReleases )
125        {
126          model.removeElement(testRelease);
127        }
128    }
129    */
130
131    public void setShouldShowFullList(boolean shouldShowFullList)
132    {
133        this.shouldShowFullList = shouldShowFullList;
134        DefaultListModel model = new DefaultListModel();
135        List<String> listToShow = shouldShowFullList ? fullList : patchableOnlyList;
136        for( String module : listToShow )
137        {
138            model.addElement(module);
139        }
140        setModel(model);
141    }
142
143    public void populate()
144    {
145        System.out.println("Release: " + RepositoryLocations.getReleaseLocation());
146        SwingWorker<DefaultListModel, Void> worker = new SwingWorker<DefaultListModel, Void>()
147        {
148            @Override
149            public DefaultListModel doInBackground()
150            {                
151                DefaultListModel listModel = new DefaultListModel();                
152                SuitesList.getList(fullList, patchableOnlyList, listModel);
153                return listModel;
154            }
155
156            @Override
157            public void done()
158            {
159                try
160                {
161                    setModel(get());
162                }
163                catch (InterruptedException e)
164                {
165                    e.printStackTrace();
166                }
167                catch (ExecutionException e)
168                {
169                    e.printStackTrace();
170                }
171            }
172        };
173        worker.execute();
174    }
175    
176    public static void getList(List<String> fullList) {
177        getList(fullList, null, null);
178    }
179    
180    public static void getList(List<String> fullList, List<String> patchableOnlyList, DefaultListModel listModel) {
181        
182        List<String> files = new ArrayList<String>();
183
184        String releaseLocation = RepositoryLocations.getReleaseLocation();
185        try
186        {
187            URL url = new URL(releaseLocation);
188
189            System.out.println("Retrieving available suites from " + url);
190            
191            try(InputStreamReader stream = new InputStreamReader(url.openStream());
192                BufferedReader br = new BufferedReader(stream);) {
193                String line = null;
194                while ((line = br.readLine()) != null)
195                {
196                    String trimmed = line.trim();
197                    if(trimmed.startsWith("<dir name=") ||
198                            trimmed.startsWith("<file name=") )
199                    {
200                        files.add(line.split("\"")[1]);
201                    }
202                }
203            }
204        }
205        catch(Exception e)
206        {
207            System.out.println("Could not get suite list from SVN: " + e.getMessage());
208            e.printStackTrace();
209        }
210        
211        for(String module : files )
212        {
213            //System.out.println(module);
214            List<String> moduleInfo = HttpsSvnReader.readAll(releaseLocation + "/" + module + "/module-info");
215            if(moduleInfo != null && moduleInfo.contains("modules.txt"))
216            {
217                String patchableSuite = module.substring(0,
218                        module.length() - String.valueOf(Module.make(module).getPatch()).length() - 1);
219                if( !fullList.contains(patchableSuite) )
220                {
221                    fullList.add(patchableSuite);
222                }
223                if(patchableOnlyList != null && !patchableOnlyList.contains(patchableSuite) )
224                {
225                    patchableOnlyList.add(patchableSuite);
226                    if(listModel != null) {
227                        listModel.addElement(patchableSuite);
228                    }
229                }
230                fullList.add(module);
231            }
232        }
233    }
234}