001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * * Neither the name of Sun Microsystems, Inc. nor the names of its contributors
015: * may be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.netbeans.paint;
032:
033: import java.awt.Toolkit;
034: import java.beans.PropertyChangeEvent;
035: import java.beans.PropertyChangeListener;
036: import java.io.IOException;
037: import org.openide.util.Exceptions;
038: import org.openide.util.HelpCtx;
039: import org.openide.util.NbBundle;
040: import org.openide.util.WeakListeners;
041: import org.openide.util.actions.CallableSystemAction;
042: import org.openide.windows.TopComponent;
043:
044: public final class SaveCanvasAction extends CallableSystemAction
045: implements PropertyChangeListener {
046:
047: public SaveCanvasAction() {
048: TopComponent.getRegistry().addPropertyChangeListener(
049: WeakListeners.propertyChange(this , TopComponent
050: .getRegistry()));
051:
052: updateEnablement();
053: }
054:
055: public void performAction() {
056: TopComponent tc = TopComponent.getRegistry().getActivated();
057: if (tc instanceof PaintTopComponent) {
058: try {
059: ((PaintTopComponent) tc).saveAs();
060: } catch (IOException ioe) {
061: Exceptions.printStackTrace(ioe);
062: }
063: } else {
064: //Theoretically the active component could have changed
065: //between the time the menu item or toolbar button was
066: //pressed and when the action was invoked. Not likely,
067: //but theoretically possible
068: Toolkit.getDefaultToolkit().beep();
069: }
070: }
071:
072: public String getName() {
073: return NbBundle.getMessage(SaveCanvasAction.class,
074: "CTL_SaveCanvasAction");
075: }
076:
077: protected String iconResource() {
078: return "org/netbeans/paint/save.PNG";
079: }
080:
081: public HelpCtx getHelpCtx() {
082: return HelpCtx.DEFAULT_HELP;
083: }
084:
085: protected boolean asynchronous() {
086: return false;
087: }
088:
089: public void propertyChange(PropertyChangeEvent evt) {
090: if (TopComponent.Registry.PROP_ACTIVATED.equals(evt
091: .getPropertyName())) {
092: updateEnablement();
093: }
094: }
095:
096: private void updateEnablement() {
097: setEnabled(TopComponent.getRegistry().getActivated() instanceof PaintTopComponent);
098: }
099:
100: }
|