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.Color;
045: import java.awt.Graphics;
046: import java.awt.Point;
047: import java.awt.Rectangle;
048: import java.awt.Toolkit;
049: import java.awt.event.InputEvent;
050: import java.awt.event.MouseEvent;
051: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerAction;
052: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerActionFactory;
053: import org.netbeans.modules.mobility.svgcore.composer.ActionMouseCursor;
054: import org.netbeans.modules.mobility.svgcore.composer.ComposerAction;
055: import org.netbeans.modules.mobility.svgcore.composer.ComposerActionFactory;
056: import org.netbeans.modules.mobility.svgcore.composer.GraphicUtils;
057: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
058: import org.netbeans.modules.mobility.svgcore.composer.SVGObjectOutline;
059: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
060:
061: /**
062: *
063: * @author Pavel Benes
064: */
065: public final class RotateActionFactory extends
066: AbstractComposerActionFactory {
067: private static final int ROTATE_PIVOT_SIZE = 4;
068: private static final Color COLOR_ROTATE_PIVOT_BODY = Color.BLACK;
069: private static final Color COLOR_ROTATE_PIVOT_OUTLINE = Color.WHITE;
070: private static final ActionMouseCursor ROTATE_MOUSE_CURSOR = new ActionMouseCursor(
071: Toolkit
072: .getDefaultToolkit()
073: .createCustomCursor(
074: org.openide.util.Utilities
075: .loadImage("org/netbeans/modules/mobility/svgcore/resources/rotate_cursor.png"), // NOI18N
076: new Point(8, 8), "rotateCursor"), 3); //NOI18N
077:
078: private class RotateAction extends AbstractComposerAction {
079: private final SVGObject m_rotated;
080: private final float m_initialAngle;
081:
082: public RotateAction(SVGObject rotated, MouseEvent me) {
083: super (RotateActionFactory.this );
084: m_rotated = rotated;
085: repaintRotatePivot();
086: m_initialAngle = calculateRotate(me.getX(), me.getY());
087: }
088:
089: public boolean consumeEvent(AWTEvent evt, boolean isOutsideEvent) {
090: if (!isOutsideEvent
091: && evt.getID() == MouseEvent.MOUSE_DRAGGED) {
092: MouseEvent me = (MouseEvent) evt;
093:
094: //calculate area to repaint
095: Rectangle bBox = m_rotated.getScreenBBox();
096: float angleDiff = calculateRotate(me.getX(), me.getY())
097: - m_initialAngle;
098: if (angleDiff < 0) {
099: angleDiff += 360.0f;
100: }
101: m_rotated.rotate(angleDiff);
102: bBox.add(m_rotated.getScreenBBox());
103:
104: m_factory.getSceneManager().getScreenManager().repaint(
105: bBox, SVGObjectOutline.SELECTOR_OVERLAP);
106: } else {
107: actionCompleted();
108: m_rotated.commitChanges();
109: }
110: return false;
111: }
112:
113: public void paint(Graphics g, int x, int y, boolean isReadOnly) {
114: if (!isReadOnly) {
115: float[] pt = m_rotated.getOutline()
116: .getRotatePivotPoint();
117: GraphicUtils.drawRoundSelectorCorner(g,
118: COLOR_ROTATE_PIVOT_OUTLINE,
119: COLOR_ROTATE_PIVOT_BODY, (int) (pt[0] + x),
120: (int) (pt[1] + y), ROTATE_PIVOT_SIZE);
121: }
122: }
123:
124: public ActionMouseCursor getMouseCursor(boolean isOutsideEvent) {
125: return isOutsideEvent ? null : ROTATE_MOUSE_CURSOR;
126: }
127:
128: public void actionCompleted() {
129: m_rotated.repaint(SVGObjectOutline.SELECTOR_OVERLAP);
130: repaintRotatePivot();
131: m_rotated.applyTextChanges();
132: m_rotated.commitChanges();
133: super .actionCompleted();
134: }
135:
136: private void repaintRotatePivot() {
137: float[] pt = m_rotated.getOutline().getRotatePivotPoint();
138: m_rotated.repaint((int) pt[0] - ROTATE_PIVOT_SIZE - 1,
139: (int) pt[1] - ROTATE_PIVOT_SIZE - 1,
140: ROTATE_PIVOT_SIZE * 2 + 3,
141: ROTATE_PIVOT_SIZE * 2 + 3);
142: }
143:
144: protected float calculateRotate(int x, int y) {
145: float[] pivot = m_rotated.getOutline()
146: .getRotatePivotPoint();
147: return GraphicUtils.calcAngle(pivot[0], pivot[1], x, y);
148: }
149: }
150:
151: public RotateActionFactory(SceneManager sceneMgr) {
152: super (sceneMgr);
153: }
154:
155: public synchronized ComposerAction startAction(AWTEvent e,
156: boolean isOutsideEvent) {
157: if (!isOutsideEvent && !m_sceneMgr.isReadOnly()
158: && e.getID() == MouseEvent.MOUSE_PRESSED) {
159: MouseEvent me = (MouseEvent) e;
160: SVGObject selObj = getObjectToRotateAt(me);
161: if (selObj != null) {
162: return new RotateAction(selObj, me);
163: }
164: }
165: return null;
166: }
167:
168: public ActionMouseCursor getMouseCursor(MouseEvent evt,
169: boolean isOutsideEvent) {
170: if (!isOutsideEvent && getObjectToRotateAt(evt) != null) {
171: return ROTATE_MOUSE_CURSOR;
172: }
173: return null;
174: }
175:
176: private SVGObject getObjectToRotateAt(MouseEvent me) {
177: SVGObject[] selectedObjects = m_sceneMgr.getSelected();
178: if (selectedObjects != null && selectedObjects.length > 0) {
179: SVGObject selObj = selectedObjects[0];
180: if (selObj.getOutline().isAtRotateHandlePoint(
181: (float) me.getX(), (float) me.getY())) {
182: return selObj;
183: }
184: }
185: return null;
186: }
187: }
|