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 */
029
030package org.kepler.gui;
031
032import java.awt.event.ActionEvent;
033import java.awt.event.KeyEvent;
034import java.net.URL;
035
036import javax.swing.AbstractAction;
037import javax.swing.Action;
038import javax.swing.ImageIcon;
039import javax.swing.KeyStroke;
040
041import org.apache.commons.logging.Log;
042import org.apache.commons.logging.LogFactory;
043
044import diva.gui.GUIUtilities;
045import ptolemy.actor.gui.TableauFrame;
046import ptolemy.util.FileUtilities;
047
048/**
049 * This action opens the Kelper Cookbook, which is an html file at the
050 * classpath-relative location defined by COOKBOOK_URL_STR
051 * 
052 *@author Matthew Brooke
053 *@since 27 January 2006
054 */
055public class CookbookAction extends AbstractAction {
056
057        // //////////////////////////////////////////////////////////////////////////////
058        // Note that these are defaults - Instantiating code will
059        // probably override these in a localizable manner
060
061        private final String DISPLAY_NAME = "Cookbook";
062        private final String TOOLTIP = "Open Kepler Cookbook";
063        private final ImageIcon LARGE_ICON = null;
064        private final Integer MNEMONIC_KEY = new Integer(KeyEvent.VK_C);
065        private final KeyStroke ACCELERATOR_KEY = null;
066        // = KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().
067        // getMenuShortcutKeyMask());
068        // //////////////////////////////////////////////////////////////////////////////
069
070        // NOTE - $CLASSPATH part is needed by
071        // ptolemy.util.FileUtilities.nameToURL()
072        private final String COOKBOOK_URL_STR = "$CLASSPATH/ptolemy/configs/kepler/cookbook.html";
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 CookbookAction(TableauFrame parent) {
082                super();
083                if (parent == null) {
084                        IllegalArgumentException iae = new IllegalArgumentException(
085                                        "CookbookAction 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(GUIUtilities.MNEMONIC_KEY, MNEMONIC_KEY);
094                this.putValue("tooltip", TOOLTIP);
095                this.putValue(GUIUtilities.ACCELERATOR_KEY, ACCELERATOR_KEY);
096        }
097
098        /**
099         * Invoked when an action occurs.
100         * 
101         *@param e
102         *            ActionEvent
103         */
104        public void actionPerformed(ActionEvent e) {
105
106                try {
107                        URL cookbookUrl = FileUtilities.nameToURL(COOKBOOK_URL_STR, null,
108                                        getClass().getClassLoader());
109                        parent.getConfiguration().openModel(null, cookbookUrl,
110                                        cookbookUrl.toExternalForm());
111                } catch (Exception ex) {
112                        if (isDebugging) {
113                                log
114                                                .error("exception trying to open the cookbook. \nPlease check "
115                                                                + "the classpath-relative location: "
116                                                                + COOKBOOK_URL_STR);
117                        }
118                }
119        }
120
121        private TableauFrame parent;
122
123        private static final Log log = LogFactory.getLog(
124                        CookbookAction.class.getName());
125
126        private static final boolean isDebugging = log.isDebugEnabled();
127
128}