Coverage Report - org.argouml.uml.ui.ActionSaveAllGraphics
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionSaveAllGraphics
62%
63/101
33%
16/48
4.5
 
 1  
 /* $Id: ActionSaveAllGraphics.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-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.uml.ui;
 40  
 
 41  
 import java.awt.event.ActionEvent;
 42  
 import java.io.File;
 43  
 import java.io.FileNotFoundException;
 44  
 import java.io.FileOutputStream;
 45  
 import java.io.IOException;
 46  
 import java.net.MalformedURLException;
 47  
 
 48  
 import javax.swing.AbstractAction;
 49  
 import javax.swing.Action;
 50  
 import javax.swing.JFileChooser;
 51  
 import javax.swing.JOptionPane;
 52  
 
 53  
 import org.apache.log4j.Logger;
 54  
 import org.argouml.application.api.CommandLineInterface;
 55  
 import org.argouml.application.events.ArgoEventPump;
 56  
 import org.argouml.application.events.ArgoEventTypes;
 57  
 import org.argouml.application.events.ArgoStatusEvent;
 58  
 import org.argouml.configuration.Configuration;
 59  
 import org.argouml.i18n.Translator;
 60  
 import org.argouml.kernel.Project;
 61  
 import org.argouml.kernel.ProjectManager;
 62  
 import org.argouml.ui.targetmanager.TargetManager;
 63  
 import org.argouml.uml.diagram.ArgoDiagram;
 64  
 import org.argouml.uml.diagram.DiagramUtils;
 65  
 import org.argouml.util.ArgoFrame;
 66  
 import org.tigris.gef.base.Diagram;
 67  
 import org.tigris.gef.base.SaveGraphicsAction;
 68  
 import org.tigris.gef.util.Util;
 69  
 
 70  
 /**
 71  
  * Wraps a SaveGraphicsAction allow selection of an output directory to which
 72  
  * all diagrams will be written. Introduced thanks to issue 2126. Saves diagrams
 73  
  * only using the default format.
 74  
  * <p>
 75  
  * 
 76  
  * TODO: Add a user choice for other formats (PNG, SVG,...) <p>
 77  
  * 
 78  
  * @author Leonardo Souza Mario Bueno (lsbueno@tigris.org)
 79  
  */
 80  
 
 81  
 public class ActionSaveAllGraphics extends AbstractAction
 82  
     implements CommandLineInterface {
 83  
 
 84  900
     private static final Logger LOG =
 85  
         Logger.getLogger(ActionSaveAllGraphics.class);
 86  
     
 87  
     private boolean overwrite;
 88  
 
 89  
     /**
 90  
      * The constructor.
 91  
      *
 92  
      */
 93  
     public ActionSaveAllGraphics() {
 94  1800
         super(Translator.localize("action.save-all-graphics"),
 95  
                 null);
 96  
         // Set the tooltip string:
 97  1800
         putValue(Action.SHORT_DESCRIPTION, 
 98  
                 Translator.localize("action.save-all-graphics"));
 99  1800
     }
 100  
 
 101  
     public void actionPerformed( ActionEvent ae ) {
 102  11
         trySave( false );
 103  2
     }
 104  
 
 105  
     /**
 106  
      * @param canOverwrite true if we can overwrite without asking
 107  
      * @return success
 108  
      */
 109  
     public boolean trySave(boolean canOverwrite) {
 110  11
         return trySave(canOverwrite, null);
 111  
     }
 112  
     
 113  
     /**
 114  
      * @param canOverwrite
 115  
      *            true if we can overwrite without asking
 116  
      * @param directory
 117  
      *            directory to save to. If null, user will be prompted to
 118  
      *            choose.
 119  
      * @return success save status
 120  
      */
 121  
     public boolean trySave(boolean canOverwrite, File directory) {
 122  11
         overwrite = canOverwrite;
 123  11
         Project p =  ProjectManager.getManager().getCurrentProject();
 124  11
         TargetManager tm = TargetManager.getInstance();
 125  11
         File saveDir = (directory != null) ? directory : getSaveDir(p);
 126  2
         if (saveDir == null) {
 127  
             /* The user cancelled! */
 128  1
             return false;
 129  
         }
 130  1
         boolean okSoFar = true;
 131  1
         ArgoDiagram activeDiagram = DiagramUtils.getActiveDiagram();
 132  1
         for (ArgoDiagram d : p.getDiagramList()) {
 133  2
             tm.setTarget(d);
 134  2
             okSoFar = trySaveDiagram(d, saveDir);
 135  2
             if (!okSoFar) {
 136  0
                 break;
 137  
             }
 138  
         }
 139  1
         tm.setTarget(activeDiagram);
 140  1
         return okSoFar;
 141  
     }
 142  
 
 143  
     /**
 144  
      * @param target the diagram
 145  
      * @param saveDir the directory to save to
 146  
      * @return continue exporting diagrams if true
 147  
      */
 148  
     protected boolean trySaveDiagram(Object target,
 149  
             File saveDir) {
 150  2
         if ( target instanceof Diagram ) {
 151  2
             String defaultName = ((Diagram) target).getName();
 152  2
             defaultName = Util.stripJunk(defaultName);
 153  
             // TODO: It's probably worthwhile to abstract and factor
 154  
             // this chooser and directory stuff. More file handling is
 155  
             // coming, I'm sure.
 156  
             try {
 157  2
                 File theFile = new File(saveDir, defaultName + "."
 158  
                     + SaveGraphicsManager.getInstance().getDefaultSuffix());
 159  2
                 String name = theFile.getName();
 160  2
                 String path = theFile.getParent();
 161  2
                 SaveGraphicsAction cmd = SaveGraphicsManager.getInstance()
 162  
                     .getSaveActionBySuffix(
 163  
                         SaveGraphicsManager.getInstance().getDefaultSuffix());
 164  2
                 if (cmd == null) {
 165  0
                     showStatus("Unknown graphics file type with extension "
 166  
                             + SaveGraphicsManager.getInstance()
 167  
                                     .getDefaultSuffix());
 168  0
                     return false;
 169  
                 }
 170  2
                 showStatus( "Writing " + path + name + "..." );
 171  2
                 boolean result = saveGraphicsToFile(theFile, cmd);
 172  2
                 showStatus( "Wrote " + path + name );
 173  2
                 return result;
 174  
             }
 175  0
             catch ( FileNotFoundException ignore ) {
 176  0
                 LOG.error("got a FileNotFoundException", ignore);
 177  
             }
 178  0
             catch ( IOException ignore ) {
 179  0
                 LOG.error("got an IOException", ignore);
 180  0
             }
 181  
         }
 182  0
         return false;
 183  
     }
 184  
 
 185  
 
 186  
     /**
 187  
      * @param p the current project
 188  
      * @return returns null if the user did not approve his choice
 189  
      */
 190  
     protected File getSaveDir(Project p) {
 191  11
         JFileChooser chooser = getFileChooser(p);
 192  
 
 193  11
         String fn = Configuration.getString(
 194  
                 SaveGraphicsManager.KEY_SAVEALL_GRAPHICS_PATH);
 195  11
         if (fn.length() > 0) {
 196  0
             chooser.setSelectedFile(new File(fn));
 197  
         }
 198  
 
 199  11
         int retval = chooser.showSaveDialog(ArgoFrame.getFrame());
 200  
 
 201  2
         if ( retval == JFileChooser.APPROVE_OPTION ) {
 202  1
             File theFile = chooser.getSelectedFile();
 203  1
             String path = theFile.getPath();
 204  1
             Configuration.setString(
 205  
                     SaveGraphicsManager.KEY_SAVEALL_GRAPHICS_PATH,
 206  
                     path);
 207  1
             return theFile;
 208  
         }
 209  1
         return null;
 210  
     }
 211  
 
 212  
     /**
 213  
      * @param theFile the file to write
 214  
      * @param cmd the action to execute to save the graphics
 215  
      * @return continue exporting diagrams if true
 216  
      * @throws IOException
 217  
      */
 218  
     private boolean saveGraphicsToFile(File theFile, SaveGraphicsAction cmd) 
 219  
         throws IOException {
 220  2
         if ( theFile.exists() && !overwrite ) {
 221  0
             String message = Translator.messageFormat(
 222  
                     "optionpane.confirm-overwrite",
 223  
                     new Object[] {theFile});
 224  0
             String title = Translator.localize(
 225  
                     "optionpane.confirm-overwrite-title"); 
 226  
             //Custom button text:
 227  0
             Object[] options = 
 228  
             {Translator.localize(
 229  
                     "optionpane.confirm-overwrite.overwrite"), // 0
 230  
              Translator.localize(
 231  
                     "optionpane.confirm-overwrite.overwrite-all"), // 1
 232  
              Translator.localize(
 233  
                     "optionpane.confirm-overwrite.skip-this-one"), // 2
 234  
              Translator.localize(
 235  
                     "optionpane.confirm-overwrite.cancel")}; // 3
 236  
 
 237  0
             int response = 
 238  
                 JOptionPane.showOptionDialog(ArgoFrame.getFrame(),
 239  
                     message,
 240  
                     title,
 241  
                     JOptionPane.YES_NO_CANCEL_OPTION,
 242  
                     JOptionPane.QUESTION_MESSAGE,
 243  
                     null,     //do not use a custom Icon
 244  
                     options,  //the titles of buttons
 245  
                     options[0]); //default button title
 246  
 
 247  0
             if (response == 1) {
 248  0
                 overwrite = true;
 249  
             }
 250  0
             if (response == 2) {
 251  0
                 return true;
 252  
             }
 253  0
             if (response == 3) {
 254  0
                 return false;
 255  
             }
 256  0
             if (response == JOptionPane.CLOSED_OPTION) {
 257  0
                 return false;
 258  
             }
 259  
         }
 260  2
         FileOutputStream fo = null;
 261  
         try {
 262  2
             fo = new FileOutputStream( theFile );
 263  2
             cmd.setStream(fo);
 264  2
             cmd.setScale(Configuration.getInteger(
 265  
                     SaveGraphicsManager.KEY_GRAPHICS_RESOLUTION, 1));
 266  2
             cmd.actionPerformed(null);
 267  
         } finally {
 268  2
             if (fo != null) {
 269  2
                 fo.close();
 270  
             }
 271  
         }
 272  2
         return true;
 273  
     }
 274  
 
 275  
     private JFileChooser getFileChooser(Project p) {
 276  11
         JFileChooser chooser = null;
 277  
         try {
 278  11
             if ( p != null 
 279  
                 && p.getURI() != null
 280  
                 && p.getURI().toURL().getFile().length() > 0 ) {
 281  0
                 chooser = new JFileChooser(p.getURI().toURL().getFile());
 282  
             }
 283  
         }
 284  0
         catch ( MalformedURLException ex ) {
 285  0
             LOG.error("exception in opening JFileChooser", ex);
 286  11
         }
 287  
 
 288  11
         if ( chooser == null ) chooser = new JFileChooser();
 289  11
         chooser.setDialogTitle(
 290  
                 Translator.localize("filechooser.save-all-graphics"));
 291  11
         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
 292  11
         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 293  11
         chooser.setMultiSelectionEnabled(false);
 294  11
         return chooser;
 295  
     }
 296  
 
 297  
     private void showStatus(String text) {
 298  4
         ArgoEventPump.fireEvent(new ArgoStatusEvent(
 299  
                 ArgoEventTypes.STATUS_TEXT, this, text));
 300  4
     }
 301  
 
 302  
     /**
 303  
      * Execute this action from the command line.
 304  
      *
 305  
      * @see org.argouml.application.api.CommandLineInterface#doCommand(String)
 306  
      * @param argument is the directory name that we save to.
 307  
      * @return true if it is OK.
 308  
      */
 309  
     public boolean doCommand(String argument) {
 310  0
         File dir = new File(argument);
 311  0
         if (!dir.exists() || !dir.isDirectory()) {
 312  0
             LOG.error("The argument must be a path to an existing directory.");
 313  0
             return false;
 314  
         }
 315  0
         boolean result = true;
 316  0
         for (Project p : ProjectManager.getManager().getOpenProjects()) {
 317  0
             TargetManager tm = TargetManager.getInstance();
 318  0
             for (ArgoDiagram d : p.getDiagramList()) {
 319  0
                 tm.setTarget(d);
 320  0
                 if (!trySaveDiagram(d, dir)) {
 321  0
                     result = false;
 322  
                 }
 323  
             }
 324  0
         }
 325  0
         return result;
 326  
     }
 327  
 }