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: */package org.netbeans.modules.mobility.svgcore.composer.actions;
041:
042: import java.awt.AWTEvent;
043: import java.awt.Rectangle;
044: import java.awt.event.KeyEvent;
045: import java.awt.event.MouseEvent;
046: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerAction;
047: import org.netbeans.modules.mobility.svgcore.composer.ComposerActionFactory;
048: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
049: import org.netbeans.modules.mobility.svgcore.composer.SVGObjectOutline;
050:
051: /**
052: *
053: * @author Pavel Benes
054: */
055: public final class TranslateAction extends AbstractComposerAction {
056: private final SVGObject m_translated;
057: private int m_x;
058: private int m_y;
059: private boolean m_changed = false;
060:
061: public TranslateAction(ComposerActionFactory factory,
062: SVGObject translated, MouseEvent me) {
063: super (factory);
064: m_translated = translated;
065: assert m_translated != null : "The translated object cannot be null";
066: m_x = me.getX();
067: m_y = me.getY();
068: //m_selected.repaint(GraphicUtils.SELECTOR_OVERLAP);
069: }
070:
071: public TranslateAction(ComposerActionFactory factory,
072: SVGObject translated, KeyEvent ke) {
073: super (factory);
074: m_translated = translated;
075: assert m_translated != null : "The translated object cannot be null";
076: int[] diffs = TranslateActionFactory.getCoordDiff(ke);
077: m_x = diffs[0];
078: m_y = diffs[1];
079: translate(m_x, m_y, true);
080: }
081:
082: public boolean consumeEvent(AWTEvent evt, boolean isOutsideEvent) {
083: if (!isOutsideEvent) {
084: if (evt.getID() == MouseEvent.MOUSE_DRAGGED) {
085: MouseEvent me = (MouseEvent) evt;
086: int dx = me.getX() - m_x;
087: int dy = me.getY() - m_y;
088: //System.out.println("Dragging " + dx + "," + dy);
089:
090: float zoomRatio = m_translated.getScreenManager()
091: .getZoomRatio();
092: translate(dx / zoomRatio, dy / zoomRatio, false);
093: return false;
094: } else {
095: int[] diffs = TranslateActionFactory.getCoordDiff(evt);
096:
097: if (diffs != null) {
098: translate(m_x, m_y, true);
099: return true;
100: }
101: }
102:
103: actionCompleted();
104: }
105:
106: return false;
107: }
108:
109: private void translate(float dx, float dy, boolean isRelative) {
110: Rectangle bBox = m_translated.getScreenBBox();
111: m_translated.translate(dx, dy, isRelative);
112: bBox.add(m_translated.getScreenBBox());
113: m_factory.getSceneManager().getScreenManager().repaint(bBox,
114: SVGObjectOutline.SELECTOR_OVERLAP);
115: m_changed = true;
116: }
117:
118: public void actionCompleted() {
119: if (m_changed) {
120: m_translated.applyTextChanges();
121: m_translated.commitChanges();
122: }
123: super.actionCompleted();
124: }
125: }
|