Coverage Report - org.argouml.uml.ui.ActionOpenProject
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionOpenProject
46%
20/43
23%
6/26
5.667
 
 1  
 /* $Id: ActionOpenProject.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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2007 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  
 package org.argouml.uml.ui;
 40  
 
 41  
 import java.awt.event.ActionEvent;
 42  
 import java.io.File;
 43  
 
 44  
 import javax.swing.Action;
 45  
 import javax.swing.JFileChooser;
 46  
 import javax.swing.filechooser.FileFilter;
 47  
 
 48  
 import org.argouml.application.api.CommandLineInterface;
 49  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 50  
 import org.argouml.configuration.Configuration;
 51  
 import org.argouml.i18n.Translator;
 52  
 import org.argouml.kernel.Project;
 53  
 import org.argouml.kernel.ProjectManager;
 54  
 import org.argouml.persistence.AbstractFilePersister;
 55  
 import org.argouml.persistence.PersistenceManager;
 56  
 import org.argouml.persistence.ProjectFileView;
 57  
 import org.argouml.ui.ProjectBrowser;
 58  
 import org.argouml.ui.UndoableAction;
 59  
 import org.argouml.util.ArgoFrame;
 60  
 
 61  
 /**
 62  
  * Action that loads the project.
 63  
  * This will throw away the project that we were working with up to this
 64  
  * point so some extra caution.
 65  
  *
 66  
  * @see ActionSaveProject
 67  
  */
 68  
 public class ActionOpenProject extends UndoableAction
 69  
     implements CommandLineInterface {
 70  
 
 71  
     ////////////////////////////////////////////////////////////////
 72  
     // constructors
 73  
 
 74  
     /**
 75  
      * Constructor for this action.
 76  
      */
 77  
     public ActionOpenProject() {
 78  2700
         super(Translator.localize("action.open-project"),
 79  
                 ResourceLoaderWrapper.lookupIcon("action.open-project"));
 80  
         // Set the tooltip string:
 81  2700
         putValue(Action.SHORT_DESCRIPTION, 
 82  
                 Translator.localize("action.open-project"));
 83  2700
     }
 84  
 
 85  
     ////////////////////////////////////////////////////////////////
 86  
     // main methods
 87  
 
 88  
 
 89  
     /**
 90  
      * Performs the action of opening a project.
 91  
      *
 92  
      * @param e an event
 93  
      */
 94  
     public void actionPerformed(ActionEvent e) {
 95  42
         super.actionPerformed(e);
 96  42
         Project p = ProjectManager.getManager().getCurrentProject();
 97  42
         PersistenceManager pm = PersistenceManager.getInstance();
 98  
 
 99  42
         if (!ProjectBrowser.getInstance().askConfirmationAndSave()) {
 100  0
             return;
 101  
         }
 102  
 
 103  
         // next line does give user.home back but this is not
 104  
         // compliant with how the project.uri works and therefore
 105  
         // open and save project as give different starting
 106  
         // directories.  String directory =
 107  
         // Globals.getLastDirectory();
 108  37
         JFileChooser chooser = null;
 109  37
         if (p != null && p.getURI() != null) {
 110  0
             File file = new File(p.getURI());
 111  0
             if (file.getParentFile() != null) {
 112  0
                 chooser = new JFileChooser(file.getParent());
 113  
             }
 114  0
         } else {
 115  37
             chooser = new JFileChooser();
 116  
         }
 117  
 
 118  37
         if (chooser == null) {
 119  0
             chooser = new JFileChooser();
 120  
         }
 121  
 
 122  37
         chooser.setDialogTitle(
 123  
                 Translator.localize("filechooser.open-project"));
 124  
 
 125  37
         chooser.setAcceptAllFileFilterUsed(false);
 126  
 
 127  
         // adding project files icon
 128  37
         chooser.setFileView(ProjectFileView.getInstance());
 129  
         
 130  37
         pm.setOpenFileChooserFilter(chooser);
 131  
 
 132  37
         String fn = Configuration.getString(
 133  
                 PersistenceManager.KEY_OPEN_PROJECT_PATH);
 134  37
         if (fn.length() > 0) {
 135  0
             chooser.setSelectedFile(new File(fn));
 136  
         }
 137  
 
 138  37
         int retval = chooser.showOpenDialog(ArgoFrame.getFrame());
 139  20
         if (retval == JFileChooser.APPROVE_OPTION) {
 140  0
             File theFile = chooser.getSelectedFile();
 141  
 
 142  0
             if (!theFile.canRead()) {
 143  
                 /* Try adding the extension from the chosen filter. */
 144  0
                 FileFilter ffilter = chooser.getFileFilter();
 145  0
                 if (ffilter instanceof AbstractFilePersister) {
 146  0
                     AbstractFilePersister afp = 
 147  
                         (AbstractFilePersister) ffilter;
 148  0
                     File m =
 149  
                         new File(theFile.getPath() + "."
 150  
                                 + afp.getExtension());
 151  0
                     if (m.canRead()) {
 152  0
                         theFile = m;
 153  
                     }
 154  
                 }
 155  0
                 if (!theFile.canRead()) {
 156  
                     /* Try adding the default extension. */
 157  0
                     File n =
 158  
                         new File(theFile.getPath() + "."
 159  
                                 + pm.getDefaultExtension());
 160  0
                     if (n.canRead()) {
 161  0
                         theFile = n;
 162  
                     }
 163  
                 }
 164  
             }
 165  0
             if (theFile != null) {
 166  0
                 Configuration.setString(
 167  
                         PersistenceManager.KEY_OPEN_PROJECT_PATH,
 168  
                         theFile.getPath());
 169  
 
 170  0
                 ProjectBrowser.getInstance().loadProjectWithProgressMonitor(
 171  
                                 theFile, true);
 172  
             }
 173  
         }
 174  20
     }
 175  
 
 176  
     /**
 177  
      * Execute this action from the command line.
 178  
      *
 179  
      * @see org.argouml.application.api.CommandLineInterface#doCommand(String)
 180  
      * @param argument is the url of the project we load.
 181  
      * @return true if it is OK.
 182  
      */
 183  
     public boolean doCommand(String argument) {
 184  0
         return ProjectBrowser.getInstance()
 185  
             .loadProject(new File(argument), false, null);
 186  
     }
 187  
 
 188  
 } /* end class ActionOpenProject */