KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > paint > SaveCanvasAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.paint;
21
22 import java.awt.Toolkit JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.NbBundle;
29 import org.openide.util.WeakListeners;
30 import org.openide.util.actions.CallableSystemAction;
31 import org.openide.windows.TopComponent;
32
33 public final class SaveCanvasAction extends CallableSystemAction implements PropertyChangeListener JavaDoc{
34
35     public SaveCanvasAction() {
36         TopComponent.getRegistry().addPropertyChangeListener(
37                 WeakListeners.propertyChange(this,
38                 TopComponent.getRegistry()));
39         
40         updateEnablement();
41     }
42     
43     public void performAction() {
44         TopComponent tc = TopComponent.getRegistry().getActivated();
45         if (tc instanceof PaintTopComponent) {
46             try {
47                 ((PaintTopComponent) tc).saveAs();
48             } catch (IOException JavaDoc ioe) {
49                 ErrorManager.getDefault().notify(ioe);
50             }
51         } else {
52             //Theoretically the active component could have changed
53
//between the time the menu item or toolbar button was
54
//pressed and when the action was invoked. Not likely,
55
//but theoretically possible
56
Toolkit.getDefaultToolkit().beep();
57         }
58     }
59     
60     public String JavaDoc getName() {
61         return NbBundle.getMessage(SaveCanvasAction.class, "CTL_SaveCanvasAction");
62     }
63     
64     protected String JavaDoc iconResource() {
65         return "org/netbeans/paint/save.PNG";
66     }
67     
68     public HelpCtx getHelpCtx() {
69         return HelpCtx.DEFAULT_HELP;
70     }
71     
72     protected boolean asynchronous() {
73         return false;
74     }
75     
76     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
77         if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())){
78             updateEnablement();
79         }
80     }
81     
82     private void updateEnablement() {
83         setEnabled(TopComponent.getRegistry().getActivated() instanceof PaintTopComponent);
84     }
85     
86 }
87
Popular Tags