001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.image;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047:
048: import org.openide.DialogDescriptor;
049: import org.openide.NotifyDescriptor;
050: import org.openide.NotifyDescriptor.Message;
051: import org.openide.DialogDisplayer;
052: import org.openide.util.actions.CallableSystemAction;
053: import org.openide.util.HelpCtx;
054: import org.openide.util.NbBundle;
055: import org.openide.windows.TopComponent;
056:
057: /** Action that can always be invoked and work procedurally.
058: *
059: * @author Lukas Tadial
060: */
061: public class CustomZoomAction extends CallableSystemAction {
062:
063: /** Generated serial version UID. */
064: static final long serialVersionUID = 8247068408606777895L;
065:
066: /** Actually performs action. */
067: public void performAction() {
068: final Dialog[] dialogs = new Dialog[1];
069: final CustomZoomPanel zoomPanel = new CustomZoomPanel();
070:
071: zoomPanel.setEnlargeFactor(1);
072: zoomPanel.setDecreaseFactor(1);
073:
074: DialogDescriptor dd = new DialogDescriptor(zoomPanel, NbBundle
075: .getBundle(CustomZoomAction.class).getString(
076: "LBL_CustomZoomAction"), true,
077: DialogDescriptor.OK_CANCEL_OPTION,
078: DialogDescriptor.OK_OPTION, new ActionListener() {
079: public void actionPerformed(ActionEvent ev) {
080: if (ev.getSource() == DialogDescriptor.OK_OPTION) {
081: int enlargeFactor = 1, decreaseFactor = 1;
082:
083: try {
084: enlargeFactor = zoomPanel
085: .getEnlargeFactor();
086: decreaseFactor = zoomPanel
087: .getDecreaseFactor();
088: } catch (NumberFormatException nfe) {
089: notifyInvalidInput();
090: return;
091: }
092:
093: // Invalid values.
094: if (enlargeFactor == 0
095: || decreaseFactor == 0) {
096: notifyInvalidInput();
097: return;
098: }
099:
100: performZoom(enlargeFactor, decreaseFactor);
101:
102: dialogs[0].setVisible(false);
103: dialogs[0].dispose();
104: } else {
105: dialogs[0].setVisible(false);
106: dialogs[0].dispose();
107: }
108: }
109:
110: private void notifyInvalidInput() {
111: DialogDisplayer
112: .getDefault()
113: .notify(
114: new NotifyDescriptor.Message(
115: NbBundle
116: .getBundle(
117: CustomZoomAction.class)
118: .getString(
119: "MSG_InvalidValues"),
120: NotifyDescriptor.ERROR_MESSAGE));
121: }
122:
123: } // End of annonymnous ActionListener.
124: );
125: dialogs[0] = DialogDisplayer.getDefault().createDialog(dd);
126: dialogs[0].setVisible(true);
127:
128: }
129:
130: /** Performs customized zoom. */
131: private void performZoom(int enlargeFactor, int decreaseFactor) {
132: TopComponent currentComponent = TopComponent.getRegistry()
133: .getActivated();
134: if (currentComponent instanceof ImageViewer)
135: ((ImageViewer) currentComponent).customZoom(enlargeFactor,
136: decreaseFactor);
137: }
138:
139: /** Gets action name. Implements superclass abstract method. */
140: public String getName() {
141: return NbBundle.getBundle(CustomZoomAction.class).getString(
142: "LBL_CustomZoom");
143: }
144:
145: /** Gets action help context. Implemenets superclass abstract method.*/
146: public HelpCtx getHelpCtx() {
147: return new HelpCtx(CustomZoomAction.class);
148: }
149:
150: /** Gets icon resource. Overrides superclass method. */
151: protected String iconResource() {
152: return "org/netbeans/modules/image/customZoom.gif"; // NOI18N
153: }
154:
155: }
|