001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:44:14 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33630 $'
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 */
029package org.kepler.gui.kar;
030
031import java.awt.Color;
032import java.awt.event.ActionEvent;
033import java.io.File;
034
035import javax.swing.Action;
036import javax.swing.ImageIcon;
037import javax.swing.JFileChooser;
038import javax.swing.JOptionPane;
039import javax.swing.KeyStroke;
040
041import org.apache.commons.logging.Log;
042import org.apache.commons.logging.LogFactory;
043import org.kepler.objectmanager.cache.LocalRepositoryManager;
044import org.kepler.objectmanager.library.LibraryManager;
045
046import diva.gui.GUIUtilities;
047import ptolemy.actor.gui.TableauFrame;
048import ptolemy.gui.ExtensionFilenameFilter;
049import ptolemy.gui.JFileChooserBugFix;
050import ptolemy.gui.PtFileChooser;
051import ptolemy.vergil.toolbox.FigureAction;
052
053/**
054 * This action deletes a kar file from the system.
055 */
056public class DeleteArchiveAction extends FigureAction {
057
058        private static String DISPLAY_NAME = "Delete";
059        private static String TOOLTIP = "Delete a KAR file.";
060        private static ImageIcon LARGE_ICON = null;
061        private static KeyStroke ACCELERATOR_KEY = null;
062
063        // //////////////////////////////////////////////////////////////////////////////
064
065        private TableauFrame parent;
066
067        private final static Log log = LogFactory.getLog(DeleteArchiveAction.class);
068        private static final boolean isDebugging = log.isDebugEnabled();
069
070        private File archiveFile = null;
071
072        /**
073         * Constructor
074         * 
075         *@param parent
076         *            the "frame" (derived from ptolemy.gui.Top) where the menu is
077         *            being added.
078         */
079        public DeleteArchiveAction(TableauFrame parent) {
080                super("Open Archive (KAR)");
081                if (parent == null) {
082                        IllegalArgumentException iae = new IllegalArgumentException(
083                                        "OpenArchiveAction constructor received NULL argument for TableauFrame");
084                        iae.fillInStackTrace();
085                        throw iae;
086                }
087                this.parent = parent;
088
089                this.putValue(Action.NAME, DISPLAY_NAME);
090                this.putValue(GUIUtilities.LARGE_ICON, LARGE_ICON);
091                this.putValue("tooltip", TOOLTIP);
092                this.putValue(GUIUtilities.ACCELERATOR_KEY, ACCELERATOR_KEY);
093        }
094
095        /**
096         * Explicitly set the Archive file that the action will open. If not file is
097         * set a File chooser dialog is displayed to the user.
098         * 
099         * @param archiveFile
100         */
101        public void setArchiveFileToDelete(File archiveFile) {
102                this.archiveFile = archiveFile;
103        }
104
105        /**
106         * Invoked when an action occurs.
107         * 
108         *@param e
109         *            ActionEvent
110         */
111        public void actionPerformed(ActionEvent e) {
112                super.actionPerformed(e);
113
114                File karFile = null;
115                if (archiveFile != null) {
116
117                        karFile = archiveFile;
118
119                } else {
120                        // ask the user what file to delete
121                        // Create a file filter that accepts .kar files.
122                        ExtensionFilenameFilter filter = new ExtensionFilenameFilter(".kar", "kar");
123
124                        // Avoid white boxes in file chooser, see
125                        // http://bugzilla.ecoinformatics.org/show_bug.cgi?id=3801
126                        JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix();
127                        Color background = null;
128                        PtFileChooser chooser = null;
129
130                        try {
131                            background = jFileChooserBugFix.saveBackground();
132                            chooser = new PtFileChooser(parent, "Choose archive", JFileChooser.OPEN_DIALOG);
133                            chooser.setCurrentDirectory(LocalRepositoryManager.getInstance()
134                                        .getSaveRepository());
135                            chooser.addChoosableFileFilter(filter);
136
137                            int returnVal = chooser.showDialog(parent, "Delete");
138                            if (returnVal == JFileChooser.APPROVE_OPTION) {
139                                // process the given kar file
140                                karFile = chooser.getSelectedFile();
141                            }
142                        } finally {
143                            jFileChooserBugFix.restoreBackground(background);
144                        }
145                }
146                if (karFile != null) {
147                        int choice = JOptionPane.showConfirmDialog(parent,
148                                        "Delete this kar from the library and from your disk.  Continue?", "",
149                                        JOptionPane.YES_NO_OPTION);
150                        if (choice == JOptionPane.YES_OPTION) {
151                                try {
152                                        deleteKAR(karFile);
153                                } catch (Exception exc) {
154                                        exc.printStackTrace();
155                                }
156                        }
157                }
158        }
159
160        /**
161         * process the new kar file into the actor library
162         * 
163         *@param karFile
164         *            the file to process
165         */
166        private void deleteKAR(File karFile) throws Exception {
167                if (isDebugging)
168                        log.debug("deleteKAR(" + karFile.toString() + ")");
169
170                // Remove the KAR file from the library
171                LibraryManager.getInstance().deleteKAR(karFile);
172
173        }
174}