001/*
002 * Copyright (c) 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 */
029
030package org.kepler.gui.kar;
031
032import java.awt.Color;
033import java.awt.event.ActionEvent;
034import java.io.File;
035
036import javax.swing.Action;
037import javax.swing.ImageIcon;
038import javax.swing.JFileChooser;
039import javax.swing.KeyStroke;
040
041import org.apache.commons.logging.Log;
042import org.apache.commons.logging.LogFactory;
043import org.kepler.objectmanager.cache.LocalRepositoryManager;
044
045import diva.gui.GUIUtilities;
046import ptolemy.actor.gui.TableauFrame;
047import ptolemy.gui.JFileChooserBugFix;
048import ptolemy.gui.PtFileChooser;
049import ptolemy.vergil.toolbox.FigureAction;
050
051public class RefreshFolderAction extends FigureAction {
052
053        private static String DISPLAY_NAME = "Refresh";
054        private static String TOOLTIP = "Refresh the folder contents.";
055        private static ImageIcon LARGE_ICON = null;
056        private static KeyStroke ACCELERATOR_KEY = null;
057
058        // //////////////////////////////////////////////////////////////////////////////
059
060        private TableauFrame parent;
061
062        private final static Log log = LogFactory.getLog(RefreshFolderAction.class);
063        private static final boolean isDebugging = log.isDebugEnabled();
064
065        private File _folder = null;
066
067        /**
068         * Constructor
069         * 
070         *@param parent
071         *            the "frame" (derived from ptolemy.gui.Top) where the menu is
072         *            being added.
073         */
074        public RefreshFolderAction(TableauFrame parent) {
075                super("Refresh");
076                if (parent == null) {
077                        IllegalArgumentException iae = new IllegalArgumentException(
078                                        "RefreshFolderAction constructor received NULL argument for TableauFrame");
079                        iae.fillInStackTrace();
080                        throw iae;
081                }
082                this.parent = parent;
083
084                this.putValue(Action.NAME, DISPLAY_NAME);
085                this.putValue(GUIUtilities.LARGE_ICON, LARGE_ICON);
086                this.putValue("tooltip", TOOLTIP);
087                this.putValue(GUIUtilities.ACCELERATOR_KEY, ACCELERATOR_KEY);
088        }
089
090        /**
091         * Explicitly set the folder that the action will open. If no file is set a
092         * File chooser dialog is displayed to the user.
093         * 
094         * @param archiveFile
095         */
096        public void setFolderToRefresh(File folder) {
097                _folder = folder;
098        }
099
100        /**
101         * Invoked when an action occurs.
102         * 
103         *@param e
104         *            ActionEvent
105         */
106        public void actionPerformed(ActionEvent e) {
107                super.actionPerformed(e);
108
109                File folder = null;
110                if (_folder != null) {
111
112                        folder = _folder;
113
114                } else {
115                        // Avoid white boxes in file chooser, see
116                        // http://bugzilla.ecoinformatics.org/show_bug.cgi?id=3801
117                        JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix();
118                        Color background = null;
119                        PtFileChooser chooser = null;
120
121                        try {
122                            background = jFileChooserBugFix.saveBackground();
123                            // ask the user what file to refresh
124                            chooser = new PtFileChooser(parent, "Choose folder", JFileChooser.OPEN_DIALOG);
125                            chooser.setCurrentDirectory(LocalRepositoryManager.getInstance()
126                                        .getSaveRepository());
127                            int returnVal = chooser.showDialog(parent, "Refresh");
128                            if (returnVal == JFileChooser.APPROVE_OPTION) {
129                                // process the given kar file
130                                folder = chooser.getSelectedFile();
131                            }
132                        } finally {
133                            jFileChooserBugFix.restoreBackground(background);
134                        }
135                }
136                if (folder != null) {
137                        System.out.println("Under Construction: "+folder.toString()+" was not refreshed.");
138                }
139                LocalRepositoryManager lrm = LocalRepositoryManager.getInstance();
140                //lrm.refreshFolderModelForFolder(folder);
141        }
142}