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: package org.netbeans.modules.mobility.svgcore.composer.actions;
042:
043: import java.awt.AWTEvent;
044: import java.awt.Point;
045: import java.awt.Rectangle;
046: import java.awt.Toolkit;
047: import java.awt.event.InputEvent;
048: import java.awt.event.MouseEvent;
049: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerAction;
050: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerActionFactory;
051: import org.netbeans.modules.mobility.svgcore.composer.ActionMouseCursor;
052: import org.netbeans.modules.mobility.svgcore.composer.ComposerAction;
053: import org.netbeans.modules.mobility.svgcore.composer.ComposerActionFactory;
054: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
055: import org.netbeans.modules.mobility.svgcore.composer.SVGObjectOutline;
056: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
057:
058: /**
059: *
060: * @author Pavel Benes
061: */
062: public final class ScaleActionFactory extends
063: AbstractComposerActionFactory {
064: private static final ActionMouseCursor SCALE_MOUSE_CURSOR = new ActionMouseCursor(
065: Toolkit
066: .getDefaultToolkit()
067: .createCustomCursor(
068: org.openide.util.Utilities
069: .loadImage("org/netbeans/modules/mobility/svgcore/resources/resize_cursor.png"), // NOI18N
070: new Point(8, 8), "rotateCursor"), 2); //NOI18N
071:
072: private static class ScaleAction extends AbstractComposerAction {
073: private final SVGObject m_scaled;
074: private final int m_x;
075: private final int m_y;
076:
077: public ScaleAction(ComposerActionFactory factory,
078: SVGObject selected, MouseEvent me) {
079: super (factory);
080: m_scaled = selected;
081: m_x = me.getX();
082: m_y = me.getY();
083: }
084:
085: public boolean consumeEvent(AWTEvent evt, boolean isOutsideEvent) {
086: if (!isOutsideEvent
087: && evt.getID() == MouseEvent.MOUSE_DRAGGED) {
088: MouseEvent me = (MouseEvent) evt;
089:
090: //calculate area to repaint
091: Rectangle bBox = m_scaled.getScreenBBox();
092: m_scaled.scale(calculateScale(me.getX(), me.getY()));
093: bBox.add(m_scaled.getScreenBBox());
094:
095: m_factory.getSceneManager().getScreenManager().repaint(
096: bBox, SVGObjectOutline.SELECTOR_OVERLAP);
097: } else {
098: actionCompleted();
099: m_scaled.commitChanges();
100: }
101: return false;
102: }
103:
104: public ActionMouseCursor getMouseCursor(boolean isOutsideEvent) {
105: return isOutsideEvent ? null : SCALE_MOUSE_CURSOR;
106: }
107:
108: protected float calculateScale(int x, int y) {
109: float[] pt = m_scaled.getOutline().getScalePivotPoint();
110: float d1, d2;
111:
112: d1 = pt[0] - m_x;
113: d2 = pt[1] - m_y;
114: float dist1 = d1 * d1 + d2 * d2;
115: d1 = pt[0] - x;
116: d2 = pt[1] - y;
117: float dist2 = d1 * d1 + d2 * d2;
118:
119: return dist2 / dist1;
120: }
121: }
122:
123: public ScaleActionFactory(SceneManager sceneMgr) {
124: super (sceneMgr);
125: }
126:
127: public synchronized ComposerAction startAction(AWTEvent e,
128: boolean isOutsideEvent) {
129: if (!isOutsideEvent && !m_sceneMgr.isReadOnly()
130: && e.getID() == MouseEvent.MOUSE_PRESSED) {
131: MouseEvent me = (MouseEvent) e;
132: SVGObject selObj = getObjectToScaleAt(me);
133: if (selObj != null) {
134: return new ScaleAction(this , selObj, me);
135: }
136: }
137: return null;
138: }
139:
140: public ActionMouseCursor getMouseCursor(MouseEvent evt,
141: boolean isOutsideEvent) {
142: if (!isOutsideEvent && getObjectToScaleAt(evt) != null) {
143: return SCALE_MOUSE_CURSOR;
144: }
145: return null;
146: }
147:
148: private SVGObject getObjectToScaleAt(MouseEvent me) {
149: SVGObject[] selectedObjects = m_sceneMgr.getSelected();
150: if (selectedObjects != null && selectedObjects.length > 0) {
151: SVGObject selObj = selectedObjects[0];
152: if (selObj.getOutline().isAtScaleHandlePoint(
153: (float) me.getX(), (float) me.getY())) {
154: return selObj;
155: }
156: }
157: return null;
158: }
159: }
|