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.Dimension;
034import java.awt.event.ActionEvent;
035import java.io.File;
036
037import javax.swing.Action;
038import javax.swing.ImageIcon;
039import javax.swing.JFileChooser;
040import javax.swing.KeyStroke;
041
042import org.apache.commons.logging.Log;
043import org.apache.commons.logging.LogFactory;
044import org.kepler.kar.KARFile;
045import org.kepler.objectmanager.cache.LocalRepositoryManager;
046
047import diva.gui.GUIUtilities;
048import ptolemy.actor.gui.TableauFrame;
049import ptolemy.gui.ExtensionFilenameFilter;
050import ptolemy.gui.JFileChooserBugFix;
051import ptolemy.gui.PtFileChooser;
052import ptolemy.vergil.toolbox.FigureAction;
053
054/**
055 * This action opens a kar file to the system. It is called from the O
056 */
057public class ViewManifestAction extends FigureAction {
058
059        private static final long serialVersionUID = 4476849345160344931L;
060        private static String DISPLAY_NAME = "View Manifest";
061        private static String TOOLTIP = "View the manifest of this Kepler ARchive.";
062        private static ImageIcon LARGE_ICON = null;
063        private static KeyStroke ACCELERATOR_KEY = null;
064
065        // //////////////////////////////////////////////////////////////////////////////
066
067        private TableauFrame parent;
068
069        private final static Log log = LogFactory.getLog(ViewManifestAction.class);
070        private static final boolean isDebugging = log.isDebugEnabled();
071
072        private File archiveFileToOpen = null;
073
074        /**
075         * Constructor
076         * 
077         *@param parent
078         *            the "frame" (derived from ptolemy.gui.Top) where the menu is
079         *            being added.
080         */
081        public ViewManifestAction(TableauFrame parent) {
082                super(DISPLAY_NAME);
083                if (parent == null) {
084                        IllegalArgumentException iae = new IllegalArgumentException(
085                                        "ViewManifestAction constructor received NULL argument for TableauFrame");
086                        iae.fillInStackTrace();
087                        throw iae;
088                }
089                this.parent = parent;
090
091                this.putValue(Action.NAME, DISPLAY_NAME);
092                this.putValue(GUIUtilities.LARGE_ICON, LARGE_ICON);
093                this.putValue("tooltip", TOOLTIP);
094                this.putValue(GUIUtilities.ACCELERATOR_KEY, ACCELERATOR_KEY);
095        }
096
097        /**
098         * Explicitly set the Archive file that the action will open. If not file is
099         * set a File chooser dialog is displayed to the user.
100         * 
101         * @param archiveFile
102         */
103        public void setArchiveFileToOpen(File archiveFile) {
104                archiveFileToOpen = archiveFile;
105        }
106
107        /**
108         * Invoked when an action occurs.
109         * 
110         *@param e
111         *            ActionEvent
112         */
113        public void actionPerformed(ActionEvent e) {
114                super.actionPerformed(e);
115
116                File karFile = null;
117                if (archiveFileToOpen != null) {
118
119                        karFile = archiveFileToOpen;
120
121                } else {
122                        // ask the user what file to open
123                        // Create a file filter that accepts .kar files.
124                        ExtensionFilenameFilter filter = new ExtensionFilenameFilter(".kar", "kar");
125
126                        // Avoid white boxes in file chooser, see
127                        // http://bugzilla.ecoinformatics.org/show_bug.cgi?id=3801
128                        JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix();
129                        Color background = null;
130                        PtFileChooser chooser = null;
131                        try {
132                            background = jFileChooserBugFix.saveBackground();
133                            chooser = new PtFileChooser(parent, "Choose archive", JFileChooser.OPEN_DIALOG);
134                            chooser.setCurrentDirectory(LocalRepositoryManager.getInstance()
135                                    .getSaveRepository());
136                            chooser.addChoosableFileFilter(filter);
137
138                            int returnVal = chooser.showDialog(parent, "Open");
139                            if (returnVal == JFileChooser.APPROVE_OPTION) {
140                                // process the given kar file
141                                karFile = chooser.getSelectedFile();
142                            }
143                        } finally {
144                            jFileChooserBugFix.restoreBackground(background);
145                        }
146
147                }
148                if (karFile != null) {
149                        try {
150                                KarManifestViewer kmv = new KarManifestViewer();
151                                kmv.initialize(new KARFile(karFile));
152                                kmv.setSize(new Dimension(800, 400));
153                                kmv.setLocation(parent.getLocation());
154                                kmv.setVisible(true);
155                        } catch (Exception exc) {
156                                exc.printStackTrace();
157                        }
158                }
159        }
160}