001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2014-08-22 18:21:57 +0000 (Fri, 22 Aug 2014) $' 
007 * '$Revision: 32916 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui.kar;
031
032import java.awt.Toolkit;
033import java.awt.event.ActionEvent;
034
035import javax.swing.KeyStroke;
036
037import org.kepler.gui.ModelToFrameManager;
038import org.kepler.kar.KARFile;
039import org.kepler.kar.KARManager;
040
041import ptolemy.actor.gui.PtolemyFrame;
042import ptolemy.actor.gui.TableauFrame;
043import ptolemy.kernel.util.NamedObj;
044import ptolemy.vergil.toolbox.FigureAction;
045
046/**
047 * This action attempts to save a KAR in place without user interaction using
048 * ExportArchiveAction. If this cannot be done, a Save As... is attempted using
049 * ExportArchiveAction.
050 */
051public class SaveArchiveAction extends FigureAction {
052
053        protected TableauFrame _parent;
054        private static KeyStroke ACCELERATOR_KEYSTROKE = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, 
055                        Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
056
057        /**
058         * Constructor
059         * 
060         * @param parent
061         *            the "frame" (derived from ptolemy.gui.Top) where the menu is
062         *            being added.
063         */
064        public SaveArchiveAction(TableauFrame parent) {
065                super("");
066                
067                putValue(ACCELERATOR_KEY, ACCELERATOR_KEYSTROKE);
068                
069                _parent = parent;
070
071                if (parent == null) {
072                        IllegalArgumentException iae = new IllegalArgumentException(
073                                        "SaveArchiveAction constructor received NULL argument for TableauFrame");
074                        iae.fillInStackTrace();
075                        throw iae;
076                }
077
078        }
079
080        @Override
081    public void actionPerformed(ActionEvent e) {
082                super.actionPerformed(e);
083
084                TableauFrame _topFrame = _parent;
085                
086                // get the frame for the top level workflow
087                if(_parent instanceof PtolemyFrame) {
088                NamedObj workflow = ((PtolemyFrame)_parent).getModel();
089                NamedObj topWorkflow = workflow.toplevel();
090                if(workflow != topWorkflow) {
091                    _topFrame = ModelToFrameManager.getInstance().getFrame(topWorkflow);
092                }
093                }
094                
095                KARFile karForTopWorkflowFile = KARManager.getInstance().get(_topFrame);
096
097                ExportArchiveAction exportArchiveAction;
098                
099                // canWrite() checks if exists and writeable. We do in fact want
100                // the existence check to occur, e.g. when we create tmp kar files
101                // we sometimes delete them right after opening, and we want 
102                // the user to get a Save As... prompt when they attempt to Save it.
103                if (karForTopWorkflowFile == null || !karForTopWorkflowFile.getFileLocation().canWrite()) {
104                    // since the KAR does not exist, pass the current frame to ExportArchiveAction
105                    // if the workflow is a sub-workflow, ExportArchiveAction will ask the user
106                    // if they want to save only the sub-workflow.
107                exportArchiveAction = new ExportArchiveAction(_parent);
108                exportArchiveAction.actionPerformed(e);
109                } else {
110                    // a KAR file was found for this workflow, so it has already been saved.
111                    // pass the top level frame to ExportArchiveAction so that the top level
112                    // workflow is saved.
113                exportArchiveAction = new ExportArchiveAction(_topFrame);
114                exportArchiveAction.setRefreshFrameAfterSave(false); 
115                exportArchiveAction.setSaveFile(karForTopWorkflowFile.getFileLocation());
116                exportArchiveAction.actionPerformed(e);
117                }
118
119                if (exportArchiveAction.saveSucceeded()) {
120                        _parent.setModified(false);
121                }
122        }
123}