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.MouseEvent;
048: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerAction;
049: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerActionFactory;
050: import org.netbeans.modules.mobility.svgcore.composer.ActionMouseCursor;
051: import org.netbeans.modules.mobility.svgcore.composer.ComposerAction;
052: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
053: import org.netbeans.modules.mobility.svgcore.composer.SVGObjectOutline;
054: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
055:
056: /**
057: *
058: * @author Pavel Benes
059: */
060: public final class SkewActionFactory extends
061: AbstractComposerActionFactory {
062: private static final ActionMouseCursor SKEW_MOUSE_CURSOR = new ActionMouseCursor(
063: Toolkit
064: .getDefaultToolkit()
065: .createCustomCursor(
066: org.openide.util.Utilities
067: .loadImage("org/netbeans/modules/mobility/svgcore/resources/skew_cursor.png"), // NOI18N
068: new Point(8, 8), "skewCursor"), 3); //NOI18N
069:
070: private final class SkewAction extends AbstractComposerAction {
071: private final SVGObject m_skewed;
072: private final int m_initialX;
073: private final int m_initialY;
074:
075: public SkewAction(SVGObject skewed, MouseEvent me) {
076: super (SkewActionFactory.this );
077: m_skewed = skewed;
078: m_initialX = me.getX();
079: m_initialY = me.getY();
080: }
081:
082: public boolean consumeEvent(AWTEvent evt, boolean isOutsideEvent) {
083: if (!isOutsideEvent
084: && evt.getID() == MouseEvent.MOUSE_DRAGGED) {
085: MouseEvent me = (MouseEvent) evt;
086:
087: //calculate area to repaint
088: Rectangle bBox = m_skewed.getScreenBBox();
089: int diff;
090: float skewX = 0;
091: float skewY = 0;
092:
093: if (bBox.height > 0
094: && (diff = me.getX() - m_initialX) != 0) {
095: double a = Math.atan((double) diff / bBox.height);
096: skewX = (float) java.lang.Math.toDegrees(a);
097: }
098: if (bBox.width > 0
099: && (diff = m_initialY - me.getY()) != 0) {
100: double a = Math.atan((double) diff / bBox.width);
101: skewY = (float) java.lang.Math.toDegrees(a);
102: }
103:
104: m_skewed.skew(skewX, skewY);
105: bBox.add(m_skewed.getScreenBBox());
106:
107: m_factory.getSceneManager().getScreenManager().repaint(
108: bBox, SVGObjectOutline.SELECTOR_OVERLAP);
109: } else {
110: actionCompleted();
111: m_skewed.commitChanges();
112: }
113: return false;
114: }
115:
116: public ActionMouseCursor getMouseCursor(boolean isOutsideEvent) {
117: return isOutsideEvent ? null : SKEW_MOUSE_CURSOR;
118: }
119:
120: public void actionCompleted() {
121: m_skewed.repaint(SVGObjectOutline.SELECTOR_OVERLAP);
122: m_skewed.applyTextChanges();
123: m_skewed.commitChanges();
124: super .actionCompleted();
125: }
126: }
127:
128: public SkewActionFactory(SceneManager sceneMgr) {
129: super (sceneMgr);
130: }
131:
132: public synchronized ComposerAction startAction(AWTEvent e,
133: boolean isOutsideEvent) {
134: if (!isOutsideEvent && !m_sceneMgr.isReadOnly()
135: && e.getID() == MouseEvent.MOUSE_PRESSED) {
136: MouseEvent me = (MouseEvent) e;
137: SVGObject selObj = getObjectToSkewAt(me);
138: if (selObj != null) {
139: return new SkewAction(selObj, me);
140: }
141: }
142: return null;
143: }
144:
145: public ActionMouseCursor getMouseCursor(MouseEvent evt,
146: boolean isOutsideEvent) {
147: if (!isOutsideEvent && getObjectToSkewAt(evt) != null) {
148: return SKEW_MOUSE_CURSOR;
149: }
150: return null;
151: }
152:
153: private SVGObject getObjectToSkewAt(MouseEvent me) {
154: SVGObject[] selectedObjects = m_sceneMgr.getSelected();
155: if (selectedObjects != null && selectedObjects.length > 0) {
156: SVGObject selObj = selectedObjects[0];
157: if (selObj.getOutline().isAtSkewHandlePoint(
158: (float) me.getX(), (float) me.getY())) {
159: return selObj;
160: }
161: }
162: return null;
163: }
164: }
|