Coverage Report - org.argouml.uml.ui.ActionSetSourcePath
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionSetSourcePath
0%
0/51
0%
0/32
7
 
 1  
 /* $Id: ActionSetSourcePath.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-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  
 
 47  
 import org.argouml.i18n.Translator;
 48  
 import org.argouml.model.Model;
 49  
 import org.argouml.ui.targetmanager.TargetManager;
 50  
 import org.argouml.uml.reveng.ImportInterface;
 51  
 import org.argouml.util.ArgoFrame;
 52  
 import org.argouml.ui.UndoableAction;
 53  
 
 54  
 
 55  
 /**
 56  
  * Action to choose and set source path for model elements.
 57  
  */
 58  
 public class ActionSetSourcePath extends UndoableAction {
 59  
 
 60  
     /**
 61  
      * The constructor.
 62  
      */
 63  
     public ActionSetSourcePath() {
 64  0
         super(Translator.localize("action.set-source-path"), null);
 65  
         // Set the tooltip string:
 66  0
         putValue(Action.SHORT_DESCRIPTION, 
 67  
                 Translator.localize("action.set-source-path"));
 68  0
     }
 69  
 
 70  
 
 71  
     /*
 72  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 73  
      */
 74  
     @Override
 75  
     public void actionPerformed(ActionEvent e) {
 76  0
             super.actionPerformed(e);
 77  0
         File f = getNewDirectory();
 78  0
         if (f != null) {
 79  0
             Object obj = TargetManager.getInstance().getTarget();
 80  0
             if (Model.getFacade().isAModelElement(obj)) {
 81  0
                 Object tv =
 82  
                         Model.getFacade().getTaggedValue(
 83  
                                 obj, ImportInterface.SOURCE_PATH_TAG);
 84  0
                 if (tv == null) {
 85  0
                     Model.getExtensionMechanismsHelper().addTaggedValue(
 86  
                             obj,
 87  
                             Model.getExtensionMechanismsFactory()
 88  
                                     .buildTaggedValue(
 89  
                                             ImportInterface.SOURCE_PATH_TAG,
 90  
                                             f.getPath()));
 91  
                 } else {
 92  0
                     Model.getExtensionMechanismsHelper().setValueOfTag(
 93  
                             tv, f.getPath());
 94  
                 }
 95  
             }
 96  
         }
 97  0
     }
 98  
 
 99  
     /**
 100  
      * @return the new source path directory
 101  
      */
 102  
     protected File getNewDirectory() {
 103  0
         Object obj = TargetManager.getInstance().getTarget();
 104  0
         String name = null;
 105  0
         String type = null;
 106  0
         String path = null;
 107  0
         if (Model.getFacade().isAModelElement(obj)) {
 108  0
             name = Model.getFacade().getName(obj);
 109  0
             Object tv = Model.getFacade().getTaggedValue(obj,
 110  
                     ImportInterface.SOURCE_PATH_TAG);
 111  0
             if (tv != null) {
 112  0
                 path = Model.getFacade().getValueOfTag(tv);
 113  
             }
 114  0
             if (Model.getFacade().isAPackage(obj)) {
 115  0
                 type = "Package";
 116  0
             } else if (Model.getFacade().isAClass(obj)) {
 117  0
                 type = "Class";
 118  
             }
 119  0
             if (Model.getFacade().isAInterface(obj)) {
 120  0
                 type = "Interface";
 121  
             }
 122  0
         } else {
 123  0
             return null;
 124  
         }
 125  
 
 126  0
         JFileChooser chooser = null;
 127  0
         File f = null;
 128  0
         if (path != null) {
 129  0
             f = new File(path);
 130  
         }
 131  0
         if ((f != null) && (f.getPath().length() > 0)) {
 132  0
             chooser = new JFileChooser(f.getPath());
 133  
         }
 134  0
         if (chooser == null) {
 135  0
             chooser = new JFileChooser();
 136  
         }
 137  0
         if (f != null) {
 138  0
             chooser.setSelectedFile(f);
 139  
         }
 140  
 
 141  0
         String sChooserTitle =
 142  
             Translator.localize("action.set-source-path");
 143  0
         if (type != null) {
 144  0
             sChooserTitle += ' ' + type;
 145  
         }
 146  0
         if (name != null) {
 147  0
             sChooserTitle += ' ' + name;
 148  
         }
 149  0
         chooser.setDialogTitle(sChooserTitle);
 150  0
         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 151  
 
 152  0
         int retval =
 153  
             chooser.showDialog(ArgoFrame.getFrame(),
 154  
                     Translator.localize("dialog.button.ok"));
 155  0
         if (retval == JFileChooser.APPROVE_OPTION) {
 156  0
             return chooser.getSelectedFile();
 157  
         } else {
 158  0
             return null;
 159  
         }
 160  
     }
 161  
 
 162  
     /**
 163  
      * The UID.
 164  
      */
 165  
     private static final long serialVersionUID = -6455209886706784094L;
 166  
 }