KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > image > CustomZoomAction


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
21 package org.netbeans.modules.image;
22
23
24 import java.awt.Dialog JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27
28 import org.openide.DialogDescriptor;
29 import org.openide.NotifyDescriptor;
30 import org.openide.NotifyDescriptor.Message;
31 import org.openide.DialogDisplayer;
32 import org.openide.util.actions.CallableSystemAction;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35 import org.openide.windows.TopComponent;
36
37
38 /** Action that can always be invoked and work procedurally.
39  *
40  * @author Lukas Tadial
41  */

42 public class CustomZoomAction extends CallableSystemAction {
43
44     
45     /** Generated serial version UID. */
46     static final long serialVersionUID = 8247068408606777895L;
47     
48     
49     /** Actually performs action. */
50     public void performAction () {
51         final Dialog JavaDoc[] dialogs = new Dialog JavaDoc[1];
52         final CustomZoomPanel zoomPanel = new CustomZoomPanel();
53
54         zoomPanel.setEnlargeFactor(1);
55         zoomPanel.setDecreaseFactor(1);
56         
57         DialogDescriptor dd = new DialogDescriptor(
58             zoomPanel,
59             NbBundle.getBundle(CustomZoomAction.class).getString("LBL_CustomZoomAction"),
60             true,
61             DialogDescriptor.OK_CANCEL_OPTION,
62             DialogDescriptor.OK_OPTION,
63             new ActionListener JavaDoc() {
64                 public void actionPerformed(ActionEvent JavaDoc ev) {
65                     if (ev.getSource() == DialogDescriptor.OK_OPTION) {
66                         int enlargeFactor = 1, decreaseFactor = 1;
67                         
68                         try {
69                             enlargeFactor = zoomPanel.getEnlargeFactor();
70                             decreaseFactor = zoomPanel.getDecreaseFactor();
71                         } catch (NumberFormatException JavaDoc nfe) {
72                             notifyInvalidInput();
73                             return;
74                         }
75                         
76                         // Invalid values.
77
if(enlargeFactor == 0 || decreaseFactor == 0) {
78                             notifyInvalidInput();
79                             return;
80                         }
81                         
82                         performZoom(enlargeFactor, decreaseFactor);
83                         
84                         dialogs[0].setVisible(false);
85                         dialogs[0].dispose();
86                     } else {
87                         dialogs[0].setVisible(false);
88                         dialogs[0].dispose();
89                     }
90                 }
91                 
92                 private void notifyInvalidInput() {
93                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
94                         NbBundle.getBundle(CustomZoomAction.class).getString("MSG_InvalidValues"),
95                         NotifyDescriptor.ERROR_MESSAGE
96                     ));
97                 }
98                 
99             } // End of annonymnous ActionListener.
100
);
101         dialogs[0] = DialogDisplayer.getDefault().createDialog(dd);
102         dialogs[0].setVisible(true);
103         
104     }
105
106     /** Performs customized zoom. */
107     private void performZoom(int enlargeFactor, int decreaseFactor) {
108         TopComponent currentComponent = TopComponent.getRegistry().getActivated();
109         if(currentComponent instanceof ImageViewer)
110             ((ImageViewer)currentComponent).customZoom(enlargeFactor, decreaseFactor);
111     }
112
113     /** Gets action name. Implements superclass abstract method. */
114     public String JavaDoc getName () {
115         return NbBundle.getBundle(CustomZoomAction.class).getString("LBL_CustomZoom");
116     }
117     
118     /** Gets action help context. Implemenets superclass abstract method.*/
119     public HelpCtx getHelpCtx () {
120         return new HelpCtx(CustomZoomAction.class);
121     }
122     
123     /** Gets icon resource. Overrides superclass method. */
124     protected String JavaDoc iconResource() {
125         return "org/netbeans/modules/image/customZoom.gif"; // NOI18N
126
}
127     
128 }
129
Popular Tags