001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-10-28 20:58:20 +0000 (Wed, 28 Oct 2015) $' 
007 * '$Revision: 34132 $'
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 */
029package org.kepler.modulemanager.gui;
030
031import java.awt.event.WindowAdapter;
032import java.awt.event.WindowEvent;
033import java.io.File;
034import java.util.ArrayList;
035import java.util.LinkedHashMap;
036import java.util.LinkedList;
037import java.util.List;
038import java.util.Map;
039
040import javax.swing.JFrame;
041import javax.swing.JTabbedPane;
042import javax.swing.SwingUtilities;
043
044import org.apache.tools.ant.Project;
045import org.kepler.build.ChangeTo;
046import org.kepler.build.project.ProjectLocator;
047import org.kepler.build.util.Version;
048import org.kepler.modulemanager.RepositoryLocations;
049import org.kepler.modulemanager.gui.patch.PatchChecker;
050
051/** A class to either display the Module Manger UI or perform module manager
052 *  functions from the command line.
053 * 
054 *  @author David Welker, Daniel Crawl
055 *  @version $Id: ModuleManagerPane.java 34132 2015-10-28 20:58:20Z crawl $
056 */
057public class ModuleManagerPane extends JTabbedPane
058{
059    public ModuleManagerPane()
060    {
061        super();
062        addTab("Current Suite", new CurrentSuitePanel());
063       // addTab("Downloaded Modules", new DownloadedModulesPanel());
064        addTab("Available Suites and Modules", new AvailableModulesPanel());
065    }
066
067    /** Parse command-line arguments and perform the action. */
068    public static void main(String[] args)
069        {
070        
071        // tell the build system the release location read from the
072        // module-manager configuration file.
073        // FIXME rename classes
074        org.kepler.build.project.RepositoryLocations.setReleaseLocation(
075            RepositoryLocations.getReleaseLocation());
076
077        if(_parseArgs(args)) {
078            switch(_action) {
079            case ShowUI:
080                        _showPane();
081                        break;
082            case Update:
083                    PatchChecker.check(false, true, true);
084                    break;
085            case ChangeSuite:
086                _changeSuite();
087                break;
088            case List:
089                _list();
090                break;
091                default:
092                    System.out.println("Unknown action: " + _action);
093                    break;
094                }
095        }
096        }
097    
098    /** Change the active suite. */
099    private static void _changeSuite() {
100        
101        Project project = new Project();
102        File projDir = ProjectLocator.getKeplerModulesDir();
103        project.setBaseDir(projDir);
104
105        ChangeTo changeTo = new ChangeTo();
106        changeTo.setProject(project);
107        changeTo.init();
108        changeTo.setSuite(_suiteName);
109        changeTo.execute();
110    }
111    
112    /** List the available suites. */
113    private static void _list() {
114        
115        List<String> suites = new ArrayList<String>();
116        SuitesList.getList(suites);
117        
118        Map<String,List<Version>> suiteMap = new LinkedHashMap<String,List<Version>>();
119        for(String fullName : suites) {
120            Version version = Version.fromVersionString(fullName);
121            List<Version> versions = suiteMap.get(version.getBasename());
122            if(versions == null) {
123                versions = new LinkedList<Version>();
124                suiteMap.put(version.getBasename(), versions);
125            }
126            versions.add(version);
127        }
128        
129        for(Map.Entry<String, List<Version>> entry : suiteMap.entrySet()) {
130            String name = entry.getKey();
131            List<Version> versions = entry.getValue();
132            System.out.print(name + " ");
133            for(Version version : versions) {
134                System.out.print(version.getVersionString() + " ");
135            }
136            System.out.println();
137        }
138    }
139    
140    /** Parse the command line arguments.
141     *  @return True if program execution should continue. False if error
142     *  parsing arguments or program execution should stop.
143     */
144    private static boolean _parseArgs(String[] args) {
145    
146        boolean retval = true;
147    
148        for(int i = 0; i < args.length; i++) {
149            String arg = args[i];
150            if(arg.equals("-change-to")) {
151                i++;
152                if(i == args.length) {
153                    System.out.println("ERROR: must specify suite name.");
154                    retval = false;
155                    break;
156                }
157                _suiteName = args[i];
158                _action = _Action.ChangeSuite;
159            } else if(arg.equals("-list")) {
160                _action = _Action.List;
161            } else if(arg.equals("-h") || arg.equals("--help")) {
162                _showUsage();
163                retval = false;
164                break;
165            } else if(arg.equals("-update")) {
166                _action = _Action.Update;
167            } else {
168                System.out.println("Unknown argument: " + arg);
169                retval = false;
170                break;
171            }
172        }
173        return retval;
174    }
175    
176    /** Display the Module Manage pane. */
177    private static void _showPane() {
178        
179        SwingUtilities.invokeLater( new Runnable()
180        {
181            @Override
182            public void run()
183            {
184                JFrame frame = new JFrame("Module Manager");
185                //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
186                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
187                frame.add(new ModuleManagerPane());
188                frame.pack();
189                frame.setSize(1000,800);
190                // center the window
191                frame.setLocationRelativeTo(null);
192                frame.setVisible(true);
193
194                // add a window listener to close the window
195                frame.addWindowListener(new WindowAdapter() {
196                        
197                        @Override
198                        public void windowClosing(WindowEvent event) {
199                        // before closing, make sure the suite looks ok
200                                if(CurrentSuitePanel.canExit()) {
201                                        System.exit(0);
202                                }
203                        }
204                });
205            }
206        });
207        }
208    
209    /** Show the command line usage. */
210    private static void _showUsage() {
211        System.out.println("USAGE:");
212        System.out.println();
213        System.out.println("module-manager [-change-to name | -h | -list | -update]");
214        System.out.println("-change-to name         change to the named suite.");
215        System.out.println("-h                      show this help.");
216        System.out.println("-list                   list available suites.");     
217        System.out.println("-update                 download and apply updates.");
218        System.out.println();
219    }
220    
221
222    /** Possible actions. */
223    private enum _Action { ShowUI, Update, ChangeSuite, List };
224    
225    /** Action to perform. */
226    private static _Action _action = _Action.ShowUI;
227    
228    /** Name of suite to change to. */
229    private static String _suiteName;
230    
231}