Coverage Report - org.argouml.ui.explorer.ActionExportProfileXMI
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionExportProfileXMI
0%
0/42
0%
0/18
2.857
ActionExportProfileXMI$1
0%
0/3
0%
0/4
2.857
 
 1  
 /* $Id: ActionExportProfileXMI.java 17843 2010-01-12 19:23:29Z 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) 2007-2008 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.ui.explorer;
 40  
 
 41  
 import java.awt.event.ActionEvent;
 42  
 import java.io.File;
 43  
 import java.io.FileOutputStream;
 44  
 import java.io.IOException;
 45  
 import java.io.OutputStream;
 46  
 import java.util.Collection;
 47  
 
 48  
 import javax.swing.AbstractAction;
 49  
 import javax.swing.JFileChooser;
 50  
 import javax.swing.filechooser.FileFilter;
 51  
 
 52  
 import org.apache.log4j.Logger;
 53  
 import org.argouml.application.helpers.ApplicationVersion;
 54  
 import org.argouml.configuration.Configuration;
 55  
 import org.argouml.i18n.Translator;
 56  
 import org.argouml.model.Model;
 57  
 import org.argouml.model.UmlException;
 58  
 import org.argouml.model.XmiWriter;
 59  
 import org.argouml.persistence.PersistenceManager;
 60  
 import org.argouml.persistence.ProjectFileView;
 61  
 import org.argouml.persistence.UmlFilePersister;
 62  
 import org.argouml.profile.Profile;
 63  
 import org.argouml.profile.ProfileException;
 64  
 import org.argouml.util.ArgoFrame;
 65  
 
 66  
 /**
 67  
  * Exports the model of a selected profile as XMI
 68  
  *
 69  
  * @author Marcos Aur�lio
 70  
  */
 71  0
 public class ActionExportProfileXMI extends AbstractAction {
 72  
 
 73  
     /**
 74  
      * Logger.
 75  
      */
 76  0
     private static final Logger LOG = Logger
 77  
             .getLogger(ActionExportProfileXMI.class);
 78  
 
 79  
     private Profile selectedProfile;
 80  
     
 81  
     /**
 82  
      * Default Constructor
 83  
      * 
 84  
      * @param profile the selected profile
 85  
      */
 86  
     public ActionExportProfileXMI(Profile profile) {
 87  0
         super(Translator.localize("action.export-profile-as-xmi"));
 88  0
         this.selectedProfile = profile;
 89  0
     }
 90  
 
 91  
     
 92  
     public void actionPerformed(ActionEvent arg0) {
 93  
         try {
 94  0
             final Collection profilePackages = 
 95  
                 selectedProfile.getProfilePackages();
 96  0
             final Object model = profilePackages.iterator().next();
 97  
             
 98  0
             if (model != null) {
 99  0
                 File destiny = getTargetFile();
 100  0
                 if (destiny != null) {
 101  0
                     saveModel(destiny, model);
 102  
                 }
 103  
             }
 104  0
         } catch (ProfileException e) {
 105  
             // TODO: We should be giving the user more direct feedback
 106  0
             LOG.error("Exception", e);
 107  0
         } catch (IOException e) {
 108  0
             LOG.error("Exception", e);
 109  0
         } catch (UmlException e) {
 110  0
             LOG.error("Exception", e);
 111  0
         }
 112  0
     }
 113  
 
 114  
 
 115  
     private void saveModel(File destiny, Object model) throws IOException,
 116  
             UmlException {
 117  0
         OutputStream stream = new FileOutputStream(destiny);
 118  0
         XmiWriter xmiWriter = 
 119  
             Model.getXmiWriter(model, stream, 
 120  
                     ApplicationVersion.getVersion() + "("
 121  
                         + UmlFilePersister.PERSISTENCE_VERSION + ")");
 122  0
         xmiWriter.write();
 123  0
     }
 124  
 
 125  
     private File getTargetFile() {
 126  
         // show a chooser dialog for the file name, only xmi is allowed
 127  0
         JFileChooser chooser = new JFileChooser();
 128  0
         chooser.setDialogTitle(Translator.localize(
 129  
                                        "action.export-profile-as-xmi"));
 130  0
         chooser.setFileView(ProjectFileView.getInstance());
 131  0
         chooser.setApproveButtonText(Translator.localize(
 132  
                                              "filechooser.export"));
 133  0
         chooser.setAcceptAllFileFilterUsed(true);
 134  0
         chooser.setFileFilter(new FileFilter() {
 135  
 
 136  
             public boolean accept(File file) {
 137  0
                 return file.isDirectory() || isXmiFile(file);
 138  
             }
 139  
 
 140  
 
 141  
 
 142  
             public String getDescription() {
 143  0
                 return "*.XMI";
 144  
             }
 145  
 
 146  
         });
 147  
 
 148  0
         String fn =
 149  
             Configuration.getString(
 150  
                 PersistenceManager.KEY_PROJECT_NAME_PATH);
 151  0
         if (fn.length() > 0) {
 152  0
             fn = PersistenceManager.getInstance().getBaseName(fn);
 153  0
             chooser.setSelectedFile(new File(fn));
 154  
         }
 155  
 
 156  0
         int result = chooser.showSaveDialog(ArgoFrame.getFrame());
 157  0
         if (result == JFileChooser.APPROVE_OPTION) {
 158  0
             File theFile = chooser.getSelectedFile();
 159  0
             if (theFile != null) {
 160  0
                 if (!theFile.getName().toUpperCase().endsWith(".XMI")) {
 161  0
                     theFile = new File(theFile.getAbsolutePath() + ".XMI");
 162  
                 }
 163  0
                 return theFile;
 164  
             }
 165  
         }
 166  
         
 167  0
         return null;
 168  
     }
 169  
     
 170  
     private static boolean isXmiFile(File file) {
 171  0
         return file.isFile()
 172  
                 && (file.getName().toLowerCase().endsWith(".xml") 
 173  
                         || file.getName().toLowerCase().endsWith(".xmi"));
 174  
     }
 175  
 }