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.Cursor;
045: import java.awt.event.InputEvent;
046: import java.awt.event.KeyEvent;
047: import java.awt.event.MouseEvent;
048: import org.netbeans.modules.mobility.svgcore.composer.AbstractComposerActionFactory;
049: import org.netbeans.modules.mobility.svgcore.composer.ActionMouseCursor;
050: import org.netbeans.modules.mobility.svgcore.composer.ComposerAction;
051: import org.netbeans.modules.mobility.svgcore.composer.SVGObject;
052: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
053:
054: /**
055: *
056: * @author Pavel Benes
057: */
058: public final class TranslateActionFactory extends
059: AbstractComposerActionFactory {
060: private static final ActionMouseCursor TRANSLATE_MOUSE_CURSOR = new ActionMouseCursor(
061: Cursor.MOVE_CURSOR, 1);
062:
063: private static int[] DIFF_LEFT = new int[] { -1, 0 };
064: private static int[] DIFF_RIGHT = new int[] { 1, 0 };
065: private static int[] DIFF_DOWN = new int[] { 0, 1 };
066: private static int[] DIFF_UP = new int[] { 0, -1 };
067:
068: public TranslateActionFactory(SceneManager sceneMgr) {
069: super (sceneMgr);
070: }
071:
072: public synchronized ComposerAction startAction(AWTEvent e,
073: boolean isOutsideEvent) {
074: if (!isOutsideEvent && !m_sceneMgr.isReadOnly()) {
075: if (e.getID() == MouseEvent.MOUSE_PRESSED) {
076: MouseEvent me = (MouseEvent) e;
077: SVGObject selObj = getSelectedObjectAt(me);
078: if (selObj != null) {
079: return new TranslateAction(this , selObj, me);
080: }
081: } else {
082: if (getCoordDiff(e) != null) {
083: SVGObject selObj = getSelectedObject();
084: if (selObj != null) {
085: return new TranslateAction(this , selObj,
086: (KeyEvent) e);
087: }
088: }
089: }
090: }
091: return null;
092: }
093:
094: /*
095: private void move(float dx, float dy) {
096: SVGObject [] selectedObjects = m_sceneMgr.getSelected();
097: if (selectedObjects != null) {
098: SVGObject selected = selectedObjects[0];
099: assert selected != null : "The selection array may not contain null";
100: selected.repaint(SVGObjectOutline.SELECTOR_OVERLAP);
101: selected.translateIncr(dx, dy);
102: selected.commitChanges();
103: selected.applyTextChanges();
104: selected.repaint(SVGObjectOutline.SELECTOR_OVERLAP);
105: }
106: }
107: */
108: public ActionMouseCursor getMouseCursor(MouseEvent evt,
109: boolean isOutsideEvent) {
110: if (!isOutsideEvent && getSelectedObjectAt(evt) != null) {
111: return TRANSLATE_MOUSE_CURSOR;
112: }
113: return null;
114: }
115:
116: private SVGObject getSelectedObjectAt(MouseEvent me) {
117: SVGObject[] selectedObjects = m_sceneMgr.getSelected();
118: if (selectedObjects != null && selectedObjects.length > 0) {
119: SVGObject selObj = selectedObjects[0];
120: if (selObj.getScreenBBox().contains(me.getPoint())) {
121: return selObj;
122: }
123: }
124: return null;
125: }
126:
127: private SVGObject getSelectedObject() {
128: SVGObject[] selectedObjects = m_sceneMgr.getSelected();
129: if (selectedObjects != null && selectedObjects.length > 0) {
130: return selectedObjects[0];
131: }
132: return null;
133: }
134:
135: static int[] getCoordDiff(AWTEvent e) {
136: if (e.getID() == KeyEvent.KEY_PRESSED) {
137: KeyEvent ke = (KeyEvent) e;
138: if (ke.getModifiers() == 0) {
139: switch (ke.getKeyCode()) {
140: case KeyEvent.VK_DOWN:
141: return DIFF_DOWN;
142: case KeyEvent.VK_LEFT:
143: return DIFF_LEFT;
144: case KeyEvent.VK_RIGHT:
145: return DIFF_RIGHT;
146: case KeyEvent.VK_UP:
147: return DIFF_UP;
148: }
149: }
150:
151: }
152: return null;
153: }
154: }
|