Coverage Report - org.argouml.uml.ui.ActionImportFromSources
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionImportFromSources
32%
13/40
6%
1/16
4.25
 
 1  
 /* $Id: ActionImportFromSources.java 17881 2010-01-12 21:09:28Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    bobtarling
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 
 40  
 package org.argouml.uml.ui;
 41  
 
 42  
 import java.awt.event.ActionEvent;
 43  
 import java.io.File;
 44  
 import java.util.Collection;
 45  
 
 46  
 import javax.swing.Action;
 47  
 import javax.swing.JFrame;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 import org.argouml.application.api.CommandLineInterface;
 51  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 52  
 import org.argouml.i18n.Translator;
 53  
 import org.argouml.ui.ExceptionDialog;
 54  
 import org.argouml.uml.reveng.Import;
 55  
 import org.argouml.uml.reveng.ImportInterface;
 56  
 import org.argouml.uml.reveng.ImporterManager;
 57  
 import org.argouml.uml.reveng.ui.ImportStatusScreen;
 58  
 import org.argouml.util.ArgoFrame;
 59  
 import org.argouml.ui.UndoableAction;
 60  
 
 61  
 
 62  
 /** Action to trigger importing from sources.
 63  
  * @stereotype singleton
 64  
  */
 65  
 public class ActionImportFromSources extends UndoableAction 
 66  
         implements CommandLineInterface {
 67  
 
 68  
     /**
 69  
      * Logger.
 70  
      */
 71  900
     private static final Logger LOG =
 72  
         Logger.getLogger(ActionImportFromSources.class);
 73  
 
 74  
     /**
 75  
      * The singleton.
 76  
      */
 77  900
     private static final ActionImportFromSources SINGLETON =
 78  
         new ActionImportFromSources();
 79  
 
 80  
     /**
 81  
      *  The constructor.
 82  
      */
 83  
     public ActionImportFromSources() {
 84  
         // this is never downlighted...
 85  900
         super(Translator.localize("action.import-sources"),
 86  
                 ResourceLoaderWrapper.lookupIcon("action.import-sources"));
 87  
         // Set the tooltip string:
 88  900
         putValue(Action.SHORT_DESCRIPTION, 
 89  
                 Translator.localize("action.import-sources"));
 90  900
     }
 91  
 
 92  
     /*
 93  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 94  
      */
 95  
     public void actionPerformed(ActionEvent event) {
 96  26
             super.actionPerformed(event);
 97  26
             if (ImporterManager.getInstance().hasImporters()) {
 98  0
             new Import(ArgoFrame.getFrame());
 99  
             } else {
 100  26
                 LOG.info("Import sources dialog not shown: no importers!");
 101  26
             ExceptionDialog ed = new ExceptionDialog(ArgoFrame.getFrame(),
 102  
                 Translator.localize("dialog.title.problem"),
 103  
                 Translator.localize("dialog.import.no-importers.intro"),
 104  
                 Translator.localize("dialog.import.no-importers.message"));
 105  26
             ed.setModal(true);
 106  26
             ed.setVisible(true);
 107  
             }
 108  22
     }
 109  
 
 110  
     /**
 111  
      * @return Returns the SINGLETON.
 112  
      */
 113  
     public static ActionImportFromSources getInstance() {
 114  1800
         return SINGLETON;
 115  
     }
 116  
 
 117  
     /**
 118  
      * Command line command for importing a directory or file.
 119  
      * 
 120  
      * @param argument Formatted string (<importmodule>:<importpath>)
 121  
      * @return true if the command was performed successfully.
 122  
 */
 123  
     public boolean doCommand(String argument) {
 124  0
         if (argument == null) {
 125  0
             LOG.error("An argument has to be provided.");
 126  0
             return false;
 127  
         }
 128  0
         int index = argument.indexOf(':');
 129  0
         if (index == -1 || argument.length() <= index) {
 130  0
             LOG.error("Argument must be <importmodule>:<importpath>");
 131  0
             return false;
 132  
         }
 133  0
         Import imp = new Import(null);
 134  0
         Collection languages = imp.getLanguages();
 135  0
         if (languages == null || languages.isEmpty()) {
 136  0
             LOG.error("No importers available.");
 137  0
             return false;
 138  
         }
 139  0
         String importerName = argument.substring(0, index);
 140  0
         ImportInterface importer = imp.getImporter(importerName);
 141  0
         if (importer == null) {
 142  0
             LOG.error("No import support for language " + importerName);
 143  0
             return false;
 144  
         }
 145  0
         imp.setCurrentModule(importer);
 146  0
         File file = new File(argument.substring(index + 1));
 147  0
         if (!file.exists()) {
 148  0
             LOG.error("The specified file/directory doesn't exist.");
 149  0
             return false;
 150  
         }
 151  0
         imp.setFiles(new File[]{file});
 152  0
         ImportStatusScreen iss =
 153  
             new ImportStatusScreen(new JFrame(), "Importing", "Splash");
 154  0
         imp.doImport(iss);
 155  0
         return true;
 156  
     }
 157  
 }
 158  
 /* end class ActionImportFromSources */